slzwe.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (C) 1993 Aladdin Enterprises. All rights reserved.
  2. This file is part of Aladdin Ghostscript.
  3. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
  4. or distributor accepts any responsibility for the consequences of using it,
  5. or for whether it serves any particular purpose or works at all, unless he
  6. or she says so in writing. Refer to the Aladdin Ghostscript Free Public
  7. License (the "License") for full details.
  8. Every copy of Aladdin Ghostscript must include a copy of the License,
  9. normally in a plain ASCII text file named PUBLIC. The License grants you
  10. the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. under certain conditions described in the License. Among other things, the
  12. License requires that the copyright notice and this notice be preserved on
  13. all copies.
  14. */
  15. /* $Id: slzwe.c,v 1.4 2005/04/04 23:00:24 igor Exp $ */
  16. /* LZW encoding filter */
  17. #include "stdio_.h" /* includes std.h */
  18. #include "gdebug.h"
  19. #include "strimpl.h"
  20. #include "slzwx.h"
  21. /********************************************************/
  22. /* LZW routines are based on: */
  23. /* Dr. Dobbs Journal --- Oct. 1989. */
  24. /* Article on LZW Data Compression by Mark R. Nelson */
  25. /********************************************************/
  26. /* Define the special codes */
  27. #define code_reset 256
  28. #define code_eod 257
  29. #define code_0 258 /* first assignable code */
  30. /* Import the stream closing procedure */
  31. extern stream_proc_release(s_LZW_release);
  32. typedef struct lzw_encode_s {
  33. byte datum; /* last byte of this code */
  34. ushort prefix; /* code for prefix of this code */
  35. } lzw_encode;
  36. #define encode_max 4095 /* max # of codes, must be */
  37. /* > code_0 and <= 4095 */
  38. #define hash_size (encode_max + encode_max / 4)
  39. struct lzw_encode_table_s {
  40. lzw_encode encode[encode_max];
  41. ushort hashed[hash_size];
  42. };
  43. gs_private_st_simple(st_lzwe_table, lzw_encode_table, "lzw_encode_table");
  44. /* Hashing function */
  45. #define encode_hash(code, chr)\
  46. ((uint)((code) * 59 + (chr) * ((hash_size / 256) | 1)) % hash_size)
  47. /* Internal routine to put a code into the output buffer. */
  48. /* Let S = ss->code_size, M = ss->next_code, N = 1 << M. */
  49. /* Relevant invariants: 9 <= S <= 12; N / 2 <= M < N; 0 <= code < N; */
  50. /* 1 <= ss->bits_left <= 8; only the rightmost (8 - ss->bits_left) */
  51. /* bits of ss->bits contain valid data. */
  52. private byte *
  53. lzw_put_code(register stream_LZW_state *ss, byte *q, uint code)
  54. { uint size = ss->code_size;
  55. byte cb = (ss->bits << ss->bits_left) +
  56. (code >> (size - ss->bits_left));
  57. if_debug2('W', "[w]writing 0x%x,%d\n", code, ss->code_size);
  58. *++q = cb;
  59. if ( (ss->bits_left += 8 - size) <= 0 )
  60. { *++q = code >> -ss->bits_left;
  61. ss->bits_left += 8;
  62. }
  63. ss->bits = code;
  64. return q;
  65. }
  66. /* Internal routine to reset the encoding table */
  67. private void
  68. lzw_reset_encode(stream_LZW_state *ss)
  69. { register int c;
  70. lzw_encode_table *table = ss->table.encode;
  71. ss->next_code = code_0;
  72. ss->code_size = 9;
  73. ss->prev_code = code_eod;
  74. for ( c = 0; c < hash_size; c++ )
  75. table->hashed[c] = code_eod;
  76. for ( c = 0; c < 256; c++ )
  77. { lzw_encode *ec = &table->encode[c];
  78. register ushort *tc = &table->hashed[encode_hash(code_eod, c)];
  79. while ( *tc != code_eod )
  80. if ( ++tc == &table->hashed[hash_size] )
  81. tc = &table->hashed[0];
  82. *tc = c;
  83. ec->datum = c, ec->prefix = code_eod;
  84. }
  85. table->encode[code_eod].prefix = code_reset; /* guarantee no match */
  86. }
  87. #define ss ((stream_LZW_state *)st)
  88. /* Initialize LZWEncode filter */
  89. private int
  90. s_LZWE_init(stream_state *st)
  91. { ss->bits_left = 8;
  92. ss->table.encode = gs_alloc_struct(st->memory,
  93. lzw_encode_table, &st_lzwe_table, "LZWEncode init");
  94. if ( ss->table.encode == 0 )
  95. return ERRC; /****** WRONG ******/
  96. ss->first = true;
  97. lzw_reset_encode(ss);
  98. return 0;
  99. }
  100. /* Process a buffer */
  101. private int
  102. s_LZWE_process(stream_state *st, stream_cursor_read *pr,
  103. stream_cursor_write *pw, bool last)
  104. { register const byte *p = pr->ptr;
  105. const byte *rlimit = pr->limit;
  106. register byte *q = pw->ptr;
  107. byte *wlimit = pw->limit;
  108. int code = ss->prev_code;
  109. lzw_encode_table *table = ss->table.encode;
  110. ushort *table_end = &table->hashed[hash_size];
  111. int status = 0;
  112. int limit_code;
  113. #define set_limit_code()\
  114. limit_code = (1 << ss->code_size) - ss->EarlyChange;\
  115. if ( limit_code > encode_max ) limit_code = encode_max
  116. set_limit_code();
  117. if ( ss->first )
  118. { /* Emit the initial reset code. */
  119. if ( wlimit - q < 2 )
  120. return 1;
  121. q = lzw_put_code(ss, q, code_reset);
  122. ss->first = false;
  123. }
  124. while ( p < rlimit )
  125. { byte c = p[1];
  126. ushort *tp;
  127. for ( tp = &table->hashed[encode_hash(code, c)]; ; )
  128. { lzw_encode *ep = &table->encode[*tp];
  129. if ( ep->prefix == code && ep->datum == c )
  130. { code = *tp;
  131. p++;
  132. break;
  133. }
  134. else if ( *tp != code_eod )
  135. { if ( ++tp == table_end )
  136. tp = &table->hashed[0]; /* wrap around */
  137. }
  138. else
  139. { /* end of recognized sequence */
  140. if ( wlimit - q <= 4 )
  141. { status = 1;
  142. goto out;
  143. }
  144. q = lzw_put_code(ss, q, code);
  145. if ( ss->next_code == limit_code )
  146. { /* Reached power of 2 or limit. */
  147. /* Determine which. */
  148. if ( ss->next_code == encode_max )
  149. { q = lzw_put_code(ss, q, code_reset);
  150. lzw_reset_encode(ss);
  151. set_limit_code();
  152. goto cx;
  153. }
  154. ss->code_size++;
  155. set_limit_code();
  156. }
  157. if_debug3('W', "[W]encoding 0x%x=0x%x+%c\n",
  158. ss->next_code, code, c);
  159. *tp = ss->next_code++;
  160. ep = &table->encode[*tp];
  161. ep->datum = c;
  162. ep->prefix = code;
  163. cx: code = code_eod;
  164. break;
  165. }
  166. }
  167. }
  168. if ( last && status == 0 )
  169. { if ( wlimit - q < 4 )
  170. status = 1;
  171. else
  172. { if ( code != code_eod )
  173. { q = lzw_put_code(ss, q, code); /* put out final code */
  174. }
  175. q = lzw_put_code(ss, q, code_eod);
  176. if ( ss->bits_left < 8 )
  177. *++q = ss->bits << ss->bits_left; /* final byte */
  178. }
  179. }
  180. out: ss->prev_code = code;
  181. pr->ptr = p;
  182. pw->ptr = q;
  183. return status;
  184. }
  185. #undef ss
  186. /* Stream template */
  187. const stream_template s_LZWE_template =
  188. { &st_LZW_state, s_LZWE_init, s_LZWE_process, 1, 4, s_LZW_release,
  189. s_LZW_set_defaults
  190. };