home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / SCF.H < prev    next >
C/C++ Source or Header  |  1992-08-03  |  5KB  |  143 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* scf.h */
  21. /* Common definitions for CCITTFax encoding and decoding filters */
  22.  
  23. /*
  24.  * The CCITT Group 3 and Group 4 fax specifications map run lengths to
  25.  * Huffman codes.  White and black have different mappings.
  26.  * If the run length is 64 or greater, two codes are needed,
  27.  * a 'make-up' code that encodes the multiple of 64, and a 'termination'
  28.  * code for the remainder; for runs of 63 or less, only the 'termination'
  29.  * code is needed.
  30.  */
  31.  
  32. /* ------ Encoding tables ------ */
  33.  
  34. /* Define the structure for the encoding tables. */
  35. typedef struct cfe_run_s {
  36.     byte code;        /* (low 8 bits of) code: */
  37.                 /* remember to widen this to uint! */
  38.     char code_length;
  39. } cfe_run;
  40. #define cfe_entry(c, len) { c, len }
  41.  
  42. /* Codes common to 1-D and 2-D encoding. */
  43. /* The decoding algorithms know that EOL is 0....01. */
  44. #define run_eol_code_length 12
  45. #define run_eol_code_value 1
  46. extern const cfe_run cf_run_eol;
  47. extern const cfe_run cf_white_termination[64];
  48. extern const cfe_run cf_white_make_up[41];
  49. extern const cfe_run cf_black_termination[64];
  50. extern const cfe_run cf_black_make_up[41];
  51. extern const cfe_run cf_uncompressed[6];
  52. extern const cfe_run cf_uncompressed_exit[10];    /* indexed by 2 x length of */
  53.             /* white run + (1 if next run black, 0 if white) */
  54. /* 1-D encoding. */
  55. extern const cfe_run cf1_run_uncompressed;
  56. /* 2-D encoding. */
  57. extern const cfe_run cf2_run_pass;
  58. extern const cfe_run cf2_run_vertical[7];    /* indexed by b1 - a1 */
  59. extern const cfe_run cf2_run_horizontal;
  60. extern const cfe_run cf2_run_uncompressed;
  61. /* 2-D Group 3 encoding. */
  62. extern const cfe_run cf2_run_eol_1d;
  63. extern const cfe_run cf2_run_eol_2d;
  64.  
  65. /* ------ Decoding tables ------ */
  66.  
  67. /*
  68.  * Define the structure for the decoding tables.
  69.  * First-level nodes are either leaves, which have
  70.  *    run_length = white or black run length
  71.  *    code_length <= initial_bits
  72.  * or non-leaves, which have
  73.  *    run_length = the index of a sub-table
  74.  *    code_length = initial_bits + the number of additional dispatch bits
  75.  * Second-level nodes are always leaves, with
  76.  *    code_length = the actual number of bits in the code - initial_bits.
  77.  */
  78. #define run_error (-1)
  79. #define run_zeros (-2)    /* EOL follows, possibly with more padding first */
  80. #define run_uncompressed (-3)
  81. /* 2-D codes */
  82. #define run2_pass (-4)
  83. #define run2_horizontal (-5)
  84. typedef struct cfd_node_s cfd_node;
  85. struct cfd_node_s {
  86.     short run_length;
  87.     ushort code_length;
  88. };
  89.  
  90. #define cfd_white_initial_bits 8
  91. extern const cfd_node cf_white_decode[];
  92. #define cfd_black_initial_bits 7
  93. extern const cfd_node cf_black_decode[];
  94. #define cfd_2d_initial_bits 7
  95. extern const cfd_node cf_2d_decode[];
  96. #define cfd_uncompressed_initial_bits 6        /* must be 6 */
  97. extern const cfd_node cf_uncompressed_decode[];
  98.  
  99. /* ------ Run detection macros ------ */
  100.  
  101. /* For the run detection macros, white_byte is */
  102. /* 0 or 0xff for BlackIs1 or !BlackIs1 respectively. */
  103. /* data holds p[-1].  count is the number of valid bits remaining */
  104. /* in the scan line. */
  105.  
  106. /* Tables in scftab.c. */
  107. extern const byte cf_left_bits[8];
  108. extern const byte cf_top_bit[256];
  109.       
  110. /* Skip over white pixels to find the next black pixel in the input. */
  111. /* There are a lot more white pixels than black pixels, */
  112. /* so we go to some extra trouble to make this efficient. */
  113.  
  114. #define skip_white_pixels(data, p, count, white_byte, w0_unused)\
  115. { int top = cf_top_bit[data & cf_left_bits[count & 7]];\
  116.   if ( top ) count = ((count - 1) & ~7) + top;\
  117.   else\
  118.    { for ( ; ; )\
  119.       { if ( p[0] != white_byte )\
  120.      { data = p[0] ^ white_byte; p += 1; count -= 9; break; }\
  121.     if ( p[1] != white_byte )\
  122.      { data = p[1] ^ white_byte; p += 2; count -= 17; break; }\
  123.     if ( p[2] != white_byte )\
  124.      { data = p[2] ^ white_byte; p += 3; count -= 25; break; }\
  125.     if ( p[3] != white_byte )\
  126.      { data = p[3] ^ white_byte; p += 4; count -= 33; break; }\
  127.     p += 4; count -= 32;\
  128.       }\
  129.      count = (count & ~7) + cf_top_bit[data];\
  130.    }\
  131. }
  132.  
  133. /* Skip over black pixels to find the next white pixel in the input. */
  134.  
  135. #define skip_black_pixels(data, p, count, white_byte, b0_unused, b1_unused)\
  136. { int top = cf_top_bit[~data & cf_left_bits[count & 7]];\
  137.   if ( top ) count = ((count - 1) & ~7) + top;\
  138.   else\
  139.    { while ( (data = *p++ ^ white_byte) == 0xff ) count -= 8;/* Skip long runs quickly. */\
  140.      count = ((count - 9) & ~7) + cf_top_bit[data ^ 0xff];\
  141.    }\
  142. }
  143.