Thu, 20 Feb 2003 12:11:06 +0000
added run length encoding function.
bitblt.c | file | annotate | diff | revisions | |
bitblt.h | file | annotate | diff | revisions |
1.1 diff -r 425e8c46d318 -r b2a2f61135bb bitblt.c 1.2 --- a/bitblt.c Wed Feb 19 10:34:35 2003 +0000 1.3 +++ b/bitblt.c Thu Feb 20 12:11:06 2003 +0000 1.4 @@ -4,7 +4,7 @@ 1.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 1.6 * 1.7 * bitblt routines 1.8 - * $Id: bitblt.c,v 1.11 2003/02/19 02:14:44 eric Exp $ 1.9 + * $Id: bitblt.c,v 1.12 2003/02/20 04:11:06 eric Exp $ 1.10 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 1.11 * 1.12 * This program is free software; you can redistribute it and/or modify 1.13 @@ -726,6 +726,79 @@ 1.14 } 1.15 1.16 1.17 -void bitblt_init (void) 1.18 +int32_t get_row_run_lengths (Bitmap *src, 1.19 + int32_t y, 1.20 + int32_t min_x, int32_t max_x, 1.21 + int32_t max_runs, 1.22 + uint32_t *run_length) 1.23 { 1.24 + uint8_t *byte_ptr; 1.25 + uint8_t byte; 1.26 + int bit_pos; 1.27 + 1.28 + uint32_t byte_cnt; 1.29 + int last_bits; 1.30 + bool last_flag = 0; 1.31 + 1.32 + uint8_t pol = 0x00; /* 0x00 = counting zeros (white), 1.33 + 0xff = counting ones (black) */ 1.34 + 1.35 + uint32_t rl; 1.36 + int32_t i = 0; 1.37 + 1.38 + /* adjust coordinate system */ 1.39 + y -= src->rect.min.y; 1.40 + min_x -= src->rect.min.x; 1.41 + max_x -= src->rect.min.x; 1.42 + 1.43 + byte_ptr = (uint8_t *) (src->bits + y * src->row_words); 1.44 + byte_cnt = (max_x / 8) + 1 - (min_x / 8); 1.45 + last_bits = max_x % 8; 1.46 + 1.47 + rl = 0; 1.48 + pol = 0x00; /* initially count white pixels */ 1.49 + 1.50 + byte_ptr += min_x / 8; 1.51 + bit_pos = min_x % 8; 1.52 + byte = *(byte_ptr++); 1.53 + 1.54 + /* is the first byte also the last? */ 1.55 + if (--byte_cnt == 0) 1.56 + { 1.57 + byte <<= (7 - last_bits); /* last byte may be partial */ 1.58 + bit_pos += (7 - last_bits); 1.59 + last_flag = 1; 1.60 + } 1.61 + 1.62 + for (;;) 1.63 + { 1.64 + int b2 = rle_tab [bit_pos] [pol ^ byte]; 1.65 + rl += b2; 1.66 + bit_pos += b2; 1.67 + if (bit_pos == 8) 1.68 + { 1.69 + if (last_flag) 1.70 + { 1.71 + if (rl) 1.72 + run_length [i++] = rl; 1.73 + return (i); 1.74 + } 1.75 + bit_pos = 0; 1.76 + byte = *(byte_ptr++); 1.77 + if (--byte_cnt == 0) 1.78 + { 1.79 + byte <<= (7 - last_bits); /* last byte may be partial */ 1.80 + bit_pos += (7 - last_bits); 1.81 + last_flag = 1; 1.82 + } 1.83 + } 1.84 + else 1.85 + { 1.86 + run_length [i++] = rl; 1.87 + if (i == max_runs) 1.88 + return (-i); 1.89 + rl = 0; 1.90 + pol ^= 0xff; 1.91 + } 1.92 + } 1.93 }
2.1 diff -r 425e8c46d318 -r b2a2f61135bb bitblt.h 2.2 --- a/bitblt.h Wed Feb 19 10:34:35 2003 +0000 2.3 +++ b/bitblt.h Thu Feb 20 12:11:06 2003 +0000 2.4 @@ -4,7 +4,7 @@ 2.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 2.6 * 2.7 * bitblt routines 2.8 - * $Id: bitblt.h,v 1.10 2003/02/19 02:14:44 eric Exp $ 2.9 + * $Id: bitblt.h,v 1.11 2003/02/20 04:11:06 eric Exp $ 2.10 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 2.11 * 2.12 * This program is free software; you can redistribute it and/or modify 2.13 @@ -98,3 +98,25 @@ 2.14 2.15 2.16 void reverse_bits (uint8_t *p, int byte_count); 2.17 + 2.18 + 2.19 +/* 2.20 + * get_row_run_lengths counts the runs of 0 and 1 bits in row 2.21 + * y of a bitmap, from min_x through max_x inclusive. The run lengths 2.22 + * will be stored in the run_length array. The first entry will be 2.23 + * the length of a zero run (which length may be zero, if the first 2.24 + * bit is a one). The next entry will the be the length of a run of 2.25 + * ones, and they will alternate from there, with even entries representing 2.26 + * runs of zeros, and odd entries representing runs of ones. 2.27 + * 2.28 + * max_runs should be set to the maximum number of run lengths that 2.29 + * can be stored in the run_length array. 2.30 + * 2.31 + * Returns the actual number of runs counted, or -max_runs if there 2.32 + * was not enough room in the array. 2.33 + */ 2.34 +int32_t get_row_run_lengths (Bitmap *src, 2.35 + int32_t y, 2.36 + int32_t min_x, int32_t max_x, 2.37 + int32_t max_runs, 2.38 + uint32_t *run_length);