diff --git a/bitblt.c b/bitblt.c index 8c3339a..a12a9e1 100644 --- a/bitblt.c +++ b/bitblt.c @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * bitblt routines - * $Id: bitblt.c,v 1.11 2003/02/19 02:14:44 eric Exp $ + * $Id: bitblt.c,v 1.12 2003/02/20 04:11:06 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -726,6 +726,79 @@ void rot_270 (Bitmap *src) /* transpose + flip_v */ } -void bitblt_init (void) +int32_t get_row_run_lengths (Bitmap *src, + int32_t y, + int32_t min_x, int32_t max_x, + int32_t max_runs, + uint32_t *run_length) { + uint8_t *byte_ptr; + uint8_t byte; + int bit_pos; + + uint32_t byte_cnt; + int last_bits; + bool last_flag = 0; + + uint8_t pol = 0x00; /* 0x00 = counting zeros (white), + 0xff = counting ones (black) */ + + uint32_t rl; + int32_t i = 0; + + /* adjust coordinate system */ + y -= src->rect.min.y; + min_x -= src->rect.min.x; + max_x -= src->rect.min.x; + + byte_ptr = (uint8_t *) (src->bits + y * src->row_words); + byte_cnt = (max_x / 8) + 1 - (min_x / 8); + last_bits = max_x % 8; + + rl = 0; + pol = 0x00; /* initially count white pixels */ + + byte_ptr += min_x / 8; + bit_pos = min_x % 8; + byte = *(byte_ptr++); + + /* is the first byte also the last? */ + if (--byte_cnt == 0) + { + byte <<= (7 - last_bits); /* last byte may be partial */ + bit_pos += (7 - last_bits); + last_flag = 1; + } + + for (;;) + { + int b2 = rle_tab [bit_pos] [pol ^ byte]; + rl += b2; + bit_pos += b2; + if (bit_pos == 8) + { + if (last_flag) + { + if (rl) + run_length [i++] = rl; + return (i); + } + bit_pos = 0; + byte = *(byte_ptr++); + if (--byte_cnt == 0) + { + byte <<= (7 - last_bits); /* last byte may be partial */ + bit_pos += (7 - last_bits); + last_flag = 1; + } + } + else + { + run_length [i++] = rl; + if (i == max_runs) + return (-i); + rl = 0; + pol ^= 0xff; + } + } } diff --git a/bitblt.h b/bitblt.h index 8c39ab3..4e629e5 100644 --- a/bitblt.h +++ b/bitblt.h @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * bitblt routines - * $Id: bitblt.h,v 1.10 2003/02/19 02:14:44 eric Exp $ + * $Id: bitblt.h,v 1.11 2003/02/20 04:11:06 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -98,3 +98,25 @@ void rot_270 (Bitmap *src); /* transpose + flip_v */ void reverse_bits (uint8_t *p, int byte_count); + + +/* + * get_row_run_lengths counts the runs of 0 and 1 bits in row + * y of a bitmap, from min_x through max_x inclusive. The run lengths + * will be stored in the run_length array. The first entry will be + * the length of a zero run (which length may be zero, if the first + * bit is a one). The next entry will the be the length of a run of + * ones, and they will alternate from there, with even entries representing + * runs of zeros, and odd entries representing runs of ones. + * + * max_runs should be set to the maximum number of run lengths that + * can be stored in the run_length array. + * + * Returns the actual number of runs counted, or -max_runs if there + * was not enough room in the array. + */ +int32_t get_row_run_lengths (Bitmap *src, + int32_t y, + int32_t min_x, int32_t max_x, + int32_t max_runs, + uint32_t *run_length);