home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / faxg3 / g3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-26  |  6.6 KB  |  281 lines

  1. /* #ident "@(#)g3.c    3.1 95/08/30 Copyright (c) Gert Doering" */
  2.  
  3. #include "config.h"
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #ifdef HAVE_UNISTD_H
  8. #include <unistd.h>
  9. #endif
  10. #include <sys/types.h>
  11. #include <string.h>
  12.  
  13. #include "g3.h"
  14.  
  15. struct g3code t_white[66] = {
  16. { 0,   0, 0x0ac,  8 },
  17. { 0,   1, 0x038,  6 },
  18. { 0,   2, 0x00e,  4 },
  19. { 0,   3, 0x001,  4 },
  20. { 0,   4, 0x00d,  4 },
  21. { 0,   5, 0x003,  4 },
  22. { 0,   6, 0x007,  4 },
  23. { 0,   7, 0x00f,  4 },
  24. { 0,   8, 0x019,  5 },
  25. { 0,   9, 0x005,  5 },
  26. { 0,  10, 0x01c,  5 },
  27. { 0,  11, 0x002,  5 },
  28. { 0,  12, 0x004,  6 },
  29. { 0,  13, 0x030,  6 },
  30. { 0,  14, 0x00b,  6 },
  31. { 0,  15, 0x02b,  6 },
  32. { 0,  16, 0x015,  6 },
  33. { 0,  17, 0x035,  6 },
  34. { 0,  18, 0x072,  7 },
  35. { 0,  19, 0x018,  7 },
  36. { 0,  20, 0x008,  7 },
  37. { 0,  21, 0x074,  7 },
  38. { 0,  22, 0x060,  7 },
  39. { 0,  23, 0x010,  7 },
  40. { 0,  24, 0x00a,  7 },
  41. { 0,  25, 0x06a,  7 },
  42. { 0,  26, 0x064,  7 },
  43. { 0,  27, 0x012,  7 },
  44. { 0,  28, 0x00c,  7 },
  45. { 0,  29, 0x040,  8 },
  46. { 0,  30, 0x0c0,  8 },
  47. { 0,  31, 0x058,  8 },
  48. { 0,  32, 0x0d8,  8 },
  49. { 0,  33, 0x048,  8 },
  50. { 0,  34, 0x0c8,  8 },
  51. { 0,  35, 0x028,  8 },
  52. { 0,  36, 0x0a8,  8 },
  53. { 0,  37, 0x068,  8 },
  54. { 0,  38, 0x0e8,  8 },
  55. { 0,  39, 0x014,  8 },
  56. { 0,  40, 0x094,  8 },
  57. { 0,  41, 0x054,  8 },
  58. { 0,  42, 0x0d4,  8 },
  59. { 0,  43, 0x034,  8 },
  60. { 0,  44, 0x0b4,  8 },
  61. { 0,  45, 0x020,  8 },
  62. { 0,  46, 0x0a0,  8 },
  63. { 0,  47, 0x050,  8 },
  64. { 0,  48, 0x0d0,  8 },
  65. { 0,  49, 0x04a,  8 },
  66. { 0,  50, 0x0ca,  8 },
  67. { 0,  51, 0x02a,  8 },
  68. { 0,  52, 0x0aa,  8 },
  69. { 0,  53, 0x024,  8 },
  70. { 0,  54, 0x0a4,  8 },
  71. { 0,  55, 0x01a,  8 },
  72. { 0,  56, 0x09a,  8 },
  73. { 0,  57, 0x05a,  8 },
  74. { 0,  58, 0x0da,  8 },
  75. { 0,  59, 0x052,  8 },
  76. { 0,  60, 0x0d2,  8 },
  77. { 0,  61, 0x04c,  8 },
  78. { 0,  62, 0x0cc,  8 },
  79. { 0,  63, 0x02c,  8 },
  80. { 0, -1, 0, 11 },        /* 11 0-bits == EOL, special handling */
  81. { 0, -1, 0, 0 }};        /* end of table */
  82.  
  83. /* make-up codes white */
  84. struct g3code m_white[28] = {
  85. { 0,  64, 0x01b,  5 },
  86. { 0, 128, 0x009,  5 },
  87. { 0, 192, 0x03a,  6 },
  88. { 0, 256, 0x076,  7 },
  89. { 0, 320, 0x06c,  8 },
  90. { 0, 384, 0x0ec,  8 },
  91. { 0, 448, 0x026,  8 },
  92. { 0, 512, 0x0a6,  8 },
  93. { 0, 576, 0x016,  8 },
  94. { 0, 640, 0x0e6,  8 },
  95. { 0, 704, 0x066,  9 },
  96. { 0, 768, 0x166,  9 },
  97. { 0, 832, 0x096,  9 },
  98. { 0, 896, 0x196,  9 },
  99. { 0, 960, 0x056,  9 },
  100. { 0,1024, 0x156,  9 },
  101. { 0,1088, 0x0d6,  9 },
  102. { 0,1152, 0x1d6,  9 },
  103. { 0,1216, 0x036,  9 },
  104. { 0,1280, 0x136,  9 },
  105. { 0,1344, 0x0b6,  9 },
  106. { 0,1408, 0x1b6,  9 },
  107. { 0,1472, 0x032,  9 },
  108. { 0,1536, 0x132,  9 },
  109. { 0,1600, 0x0b2,  9 },
  110. { 0,1664, 0x006,  6 },
  111. { 0,1728, 0x1b2,  9 },
  112. { 0,  -1, 0, 0} };
  113.  
  114.  
  115. struct g3code t_black[66] = {
  116. { 0,   0, 0x3b0, 10 },
  117. { 0,   1, 0x002,  3 },
  118. { 0,   2, 0x003,  2 },
  119. { 0,   3, 0x001,  2 },
  120. { 0,   4, 0x006,  3 },
  121. { 0,   5, 0x00c,  4 },
  122. { 0,   6, 0x004,  4 },
  123. { 0,   7, 0x018,  5 },
  124. { 0,   8, 0x028,  6 },
  125. { 0,   9, 0x008,  6 },
  126. { 0,  10, 0x010,  7 },
  127. { 0,  11, 0x050,  7 },
  128. { 0,  12, 0x070,  7 },
  129. { 0,  13, 0x020,  8 },
  130. { 0,  14, 0x0e0,  8 },
  131. { 0,  15, 0x030,  9 },
  132. { 0,  16, 0x3a0, 10 },
  133. { 0,  17, 0x060, 10 },
  134. { 0,  18, 0x040, 10 },
  135. { 0,  19, 0x730, 11 },
  136. { 0,  20, 0x0b0, 11 },
  137. { 0,  21, 0x1b0, 11 },
  138. { 0,  22, 0x760, 11 },
  139. { 0,  23, 0x0a0, 11 },
  140. { 0,  24, 0x740, 11 },
  141. { 0,  25, 0x0c0, 11 },
  142. { 0,  26, 0x530, 12 },
  143. { 0,  27, 0xd30, 12 },
  144. { 0,  28, 0x330, 12 },
  145. { 0,  29, 0xb30, 12 },
  146. { 0,  30, 0x160, 12 },
  147. { 0,  31, 0x960, 12 },
  148. { 0,  32, 0x560, 12 },
  149. { 0,  33, 0xd60, 12 },
  150. { 0,  34, 0x4b0, 12 },
  151. { 0,  35, 0xcb0, 12 },
  152. { 0,  36, 0x2b0, 12 },
  153. { 0,  37, 0xab0, 12 },
  154. { 0,  38, 0x6b0, 12 },
  155. { 0,  39, 0xeb0, 12 },
  156. { 0,  40, 0x360, 12 },
  157. { 0,  41, 0xb60, 12 },
  158. { 0,  42, 0x5b0, 12 },
  159. { 0,  43, 0xdb0, 12 },
  160. { 0,  44, 0x2a0, 12 },
  161. { 0,  45, 0xaa0, 12 },
  162. { 0,  46, 0x6a0, 12 },
  163. { 0,  47, 0xea0, 12 },
  164. { 0,  48, 0x260, 12 },
  165. { 0,  49, 0xa60, 12 },
  166. { 0,  50, 0x4a0, 12 },
  167. { 0,  51, 0xca0, 12 },
  168. { 0,  52, 0x240, 12 },
  169. { 0,  53, 0xec0, 12 },
  170. { 0,  54, 0x1c0, 12 },
  171. { 0,  55, 0xe40, 12 },
  172. { 0,  56, 0x140, 12 },
  173. { 0,  57, 0x1a0, 12 },
  174. { 0,  58, 0x9a0, 12 },
  175. { 0,  59, 0xd40, 12 },
  176. { 0,  60, 0x340, 12 },
  177. { 0,  61, 0x5a0, 12 },
  178. { 0,  62, 0x660, 12 },
  179. { 0,  63, 0xe60, 12 },
  180. { 0,  -1, 0x000, 11 },
  181. { 0,  -1, 0, 0 } };
  182.  
  183. struct g3code m_black[28] = {
  184. { 0,  64, 0x3c0, 10 },
  185. { 0, 128, 0x130, 12 },
  186. { 0, 192, 0x930, 12 },
  187. { 0, 256, 0xda0, 12 },
  188. { 0, 320, 0xcc0, 12 },
  189. { 0, 384, 0x2c0, 12 },
  190. { 0, 448, 0xac0, 12 },
  191. { 0, 512, 0x6c0, 13 },
  192. { 0, 576,0x16c0, 13 },
  193. { 0, 640, 0xa40, 13 },
  194. { 0, 704,0x1a40, 13 },
  195. { 0, 768, 0x640, 13 },
  196. { 0, 832,0x1640, 13 },
  197. { 0, 896, 0x9c0, 13 },
  198. { 0, 960,0x19c0, 13 },
  199. { 0,1024, 0x5c0, 13 },
  200. { 0,1088,0x15c0, 13 },
  201. { 0,1152, 0xdc0, 13 },
  202. { 0,1216,0x1dc0, 13 },
  203. { 0,1280, 0x940, 13 },
  204. { 0,1344,0x1940, 13 },
  205. { 0,1408, 0x540, 13 },
  206. { 0,1472,0x1540, 13 },
  207. { 0,1536, 0xb40, 13 },
  208. { 0,1600,0x1b40, 13 },
  209. { 0,1664, 0x4c0, 13 },
  210. { 0,1728,0x14c0, 13 },
  211. { 0,  -1, 0, 0 } };
  212.  
  213. void tree_add_node( struct g3_tree *p, struct g3code * g3c,
  214.                 int bit_code, int bit_length )
  215. {
  216. int i;
  217.  
  218.     if ( bit_length <= FBITS )        /* leaf (multiple bits) */
  219.     {
  220.     g3c->nr_bits = bit_length;    /* leaf tag */
  221.  
  222.     if ( bit_length == FBITS )    /* full width */
  223.     {
  224.         p->nextb[ bit_code ] = (struct g3_tree *) g3c;
  225.     }
  226.     else                /* fill bits */
  227.       for ( i=0; i< ( 1 << (FBITS-bit_length)); i++ )
  228.       {
  229.         p->nextb[ bit_code + ( i << bit_length ) ] = (struct g3_tree *) g3c;
  230.       }
  231.     }
  232.     else                /* node */
  233.     {
  234.     struct g3_tree *p2;
  235.  
  236.     p2 = p->nextb[ bit_code & BITM ];
  237.     if ( p2 == 0 )            /* no sub-node exists */
  238.     {
  239.         p2 = p->nextb[ bit_code & BITM ] =
  240.         ( struct g3_tree * ) calloc( 1, sizeof( struct g3_tree ));
  241.         if ( p2 == NULL ) { perror( "malloc 3" ); exit(11); }
  242.         p2->nr_bits = 0;        /* node tag */
  243.  
  244.     }
  245.     if ( p2->nr_bits != 0 )
  246.     {
  247.         fprintf( stderr, "internal table setup error\n" ); exit(6);
  248.     }
  249.     tree_add_node( p2, g3c, bit_code >> FBITS, bit_length - FBITS );
  250.     }
  251. }
  252.  
  253. void build_tree (struct g3_tree ** p, struct g3code * c )
  254. {
  255.     if ( *p == NULL )
  256.     {
  257.     (*p) = (struct g3_tree *) calloc( 1, sizeof(struct g3_tree) );
  258.     if ( *p == NULL ) { perror( "malloc(1)" ); exit(10); }
  259.  
  260.     (*p)->nr_bits=0;
  261.     }
  262.  
  263.     while ( c->bit_length != 0 )
  264.     {
  265.     tree_add_node( *p, c, c->bit_code, c->bit_length );
  266.     c++;
  267.     }
  268. }
  269.  
  270. void init_byte_tab (int reverse, int byte_tab[] )
  271. {
  272. int i;
  273.     if ( reverse ) for ( i=0; i<256; i++ ) byte_tab[i] = i;
  274.     else
  275.       for ( i=0; i<256; i++ )
  276.          byte_tab[i] = ( ((i & 0x01) << 7) | ((i & 0x02) << 5) |
  277.                  ((i & 0x04) << 3) | ((i & 0x08) << 1) |
  278.                  ((i & 0x10) >> 1) | ((i & 0x20) >> 3) |
  279.                  ((i & 0x40) >> 5) | ((i & 0x80) >> 7) );
  280. }
  281.