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