1 /* Copyright (C) 1992, 1995, 1997, 1998, 1999 artofcode LLC. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify it 4 under the terms of the GNU General Public License as published by the 5 Free Software Foundation; either version 2 of the License, or (at your 6 option) any later version. 7 8 This program is distributed in the hope that it will be useful, but 9 WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along 14 with this program; if not, write to the Free Software Foundation, Inc., 15 59 Temple Place, Suite 330, Boston, MA, 02111-1307. 16 17 */ 18 19 /*$Id: scf.h,v 1.2.6.1.2.1 2003/01/17 00:49:05 giles Exp $ */ 20 /* Common definitions for CCITTFax encoding and decoding filters */ 21 22 #ifndef scf_INCLUDED 23 # define scf_INCLUDED 24 25 #include "shc.h" 26 27 /* 28 * The CCITT Group 3 (T.4) and Group 4 (T.6) fax specifications map 29 * run lengths to Huffman codes. White and black have different mappings. 30 * If the run length is 64 or greater, two or more codes are needed: 31 * - One or more 'make-up' codes for 2560 pixels; 32 * - A 'make-up' code that encodes the multiple of 64; 33 * - A 'termination' code for the remainder. 34 * For runs of 63 or less, only the 'termination' code is needed. 35 */ 36 37 /* ------ Encoding tables ------ */ 38 39 /* 40 * The maximum possible length of a scan line is determined by the 41 * requirement that 3 runs have to fit into the stream buffer. 42 * A run of length N requires approximately ceil(N / 2560) makeup codes, 43 * hence 1.5 * ceil(N / 2560) bytes. Taking the largest safe stream 44 * buffer size as 32K, we arrive at the following maximum width: 45 */ 46 #if arch_sizeof_int > 2 47 # define cfe_max_width (2560 * 32000 * 2 / 3) 48 #else 49 # define cfe_max_width (max_int - 40) /* avoid overflows */ 50 #endif 51 /* The +5 in cfe_max_code_bytes is a little conservative. */ 52 #define cfe_max_code_bytes(width) ((width) / 2560 * 3 / 2 + 5) 53 54 typedef hce_code cfe_run; 55 56 /* Codes common to 1-D and 2-D encoding. */ 57 /* The decoding algorithms know that EOL is 0....01. */ 58 #define run_eol_code_length 12 59 #define run_eol_code_value 1 60 extern const cfe_run cf_run_eol; 61 typedef struct cf_runs_s { 62 cfe_run termination[64]; 63 cfe_run make_up[41]; 64 } cf_runs; 65 extern const cf_runs 66 cf_white_runs, cf_black_runs; 67 extern const cfe_run cf_uncompressed[6]; 68 extern const cfe_run cf_uncompressed_exit[10]; /* indexed by 2 x length of */ 69 70 /* white run + (1 if next run black, 0 if white) */ 71 /* 1-D encoding. */ 72 extern const cfe_run cf1_run_uncompressed; 73 74 /* 2-D encoding. */ 75 extern const cfe_run cf2_run_pass; 76 77 #define cf2_run_pass_length 4 78 #define cf2_run_pass_value 0x1 79 #define cf2_run_vertical_offset 3 80 extern const cfe_run cf2_run_vertical[7]; /* indexed by b1 - a1 + offset */ 81 extern const cfe_run cf2_run_horizontal; 82 83 #define cf2_run_horizontal_value 1 84 #define cf2_run_horizontal_length 3 85 extern const cfe_run cf2_run_uncompressed; 86 87 /* 2-D Group 3 encoding. */ 88 extern const cfe_run cf2_run_eol_1d; 89 extern const cfe_run cf2_run_eol_2d; 90 91 /* ------ Decoding tables ------ */ 92 93 typedef hcd_code cfd_node; 94 95 #define run_length value 96 97 /* 98 * The value in the decoding tables is either a white or black run length, 99 * or a (negative) exceptional value. 100 */ 101 #define run_error (-1) 102 #define run_zeros (-2) /* EOL follows, possibly with more padding first */ 103 #define run_uncompressed (-3) 104 /* 2-D codes */ 105 #define run2_pass (-4) 106 #define run2_horizontal (-5) 107 108 #define cfd_white_initial_bits 8 109 #define cfd_white_min_bits 4 /* shortest white run */ 110 extern const cfd_node cf_white_decode[]; 111 112 #define cfd_black_initial_bits 7 113 #define cfd_black_min_bits 2 /* shortest black run */ 114 extern const cfd_node cf_black_decode[]; 115 116 #define cfd_2d_initial_bits 7 117 #define cfd_2d_min_bits 4 /* shortest non-H/V 2-D run */ 118 extern const cfd_node cf_2d_decode[]; 119 120 #define cfd_uncompressed_initial_bits 6 /* must be 6 */ 121 extern const cfd_node cf_uncompressed_decode[]; 122 123 /* ------ Run detection macros ------ */ 124 125 /* 126 * For the run detection macros: 127 * white_byte is 0 or 0xff for BlackIs1 or !BlackIs1 respectively; 128 * data holds p[-1], inverted if !BlackIs1; 129 * count is the number of valid bits remaining in the scan line. 130 */ 131 132 /* Aliases for bit processing tables. */ 133 #define cf_byte_run_length byte_bit_run_length_neg 134 #define cf_byte_run_length_0 byte_bit_run_length_0 135 136 /* Skip over white pixels to find the next black pixel in the input. */ 137 /* Store the run length in rlen, and update data, p, and count. */ 138 /* There are many more white pixels in typical input than black pixels, */ 139 /* and the runs of white pixels tend to be much longer, so we use */ 140 /* substantially different loops for the two cases. */ 141 142 #define skip_white_pixels(data, p, count, white_byte, rlen)\ 143 BEGIN\ 144 rlen = cf_byte_run_length[count & 7][data ^ 0xff];\ 145 if ( rlen >= 8 ) { /* run extends past byte boundary */\ 146 if ( white_byte == 0 ) {\ 147 if ( p[0] ) { data = p[0]; p += 1; rlen -= 8; }\ 148 else if ( p[1] ) { data = p[1]; p += 2; }\ 149 else {\ 150 while ( !(p[2] | p[3] | p[4] | p[5]) )\ 151 p += 4, rlen += 32;\ 152 if ( p[2] ) {\ 153 data = p[2]; p += 3; rlen += 8;\ 154 } else if ( p[3] ) {\ 155 data = p[3]; p += 4; rlen += 16;\ 156 } else if ( p[4] ) {\ 157 data = p[4]; p += 5; rlen += 24;\ 158 } else /* p[5] */ {\ 159 data = p[5]; p += 6; rlen += 32;\ 160 }\ 161 }\ 162 } else {\ 163 if ( p[0] != 0xff ) { data = (byte)~p[0]; p += 1; rlen -= 8; }\ 164 else if ( p[1] != 0xff ) { data = (byte)~p[1]; p += 2; }\ 165 else {\ 166 while ( (p[2] & p[3] & p[4] & p[5]) == 0xff )\ 167 p += 4, rlen += 32;\ 168 if ( p[2] != 0xff ) {\ 169 data = (byte)~p[2]; p += 3; rlen += 8;\ 170 } else if ( p[3] != 0xff ) {\ 171 data = (byte)~p[3]; p += 4; rlen += 16;\ 172 } else if ( p[4] != 0xff ) {\ 173 data = (byte)~p[4]; p += 5; rlen += 24;\ 174 } else /* p[5] != 0xff */ {\ 175 data = (byte)~p[5]; p += 6; rlen += 32;\ 176 }\ 177 }\ 178 }\ 179 rlen += cf_byte_run_length_0[data ^ 0xff];\ 180 }\ 181 count -= rlen;\ 182 END 183 184 /* Skip over black pixels to find the next white pixel in the input. */ 185 /* Store the run length in rlen, and update data, p, and count. */ 186 187 #define skip_black_pixels(data, p, count, white_byte, rlen)\ 188 BEGIN\ 189 rlen = cf_byte_run_length[count & 7][data];\ 190 if ( rlen >= 8 ) {\ 191 if ( white_byte == 0 )\ 192 for ( ; ; p += 4, rlen += 32 ) {\ 193 if ( p[0] != 0xff ) { data = p[0]; p += 1; rlen -= 8; break; }\ 194 if ( p[1] != 0xff ) { data = p[1]; p += 2; break; }\ 195 if ( p[2] != 0xff ) { data = p[2]; p += 3; rlen += 8; break; }\ 196 if ( p[3] != 0xff ) { data = p[3]; p += 4; rlen += 16; break; }\ 197 }\ 198 else\ 199 for ( ; ; p += 4, rlen += 32 ) {\ 200 if ( p[0] ) { data = (byte)~p[0]; p += 1; rlen -= 8; break; }\ 201 if ( p[1] ) { data = (byte)~p[1]; p += 2; break; }\ 202 if ( p[2] ) { data = (byte)~p[2]; p += 3; rlen += 8; break; }\ 203 if ( p[3] ) { data = (byte)~p[3]; p += 4; rlen += 16; break; }\ 204 }\ 205 rlen += cf_byte_run_length_0[data];\ 206 }\ 207 count -= rlen;\ 208 END 209 210 #endif /* scf_INCLUDED */ 211