slzwce.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (C) 1994, 1995, 1996, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: slzwce.c,v 1.2 2000/09/19 19:00:50 lpd Exp $ */
  16. /* Simple encoder compatible with LZW decoding filter */
  17. #include "stdio_.h" /* includes std.h */
  18. #include "gdebug.h"
  19. #include "strimpl.h"
  20. #include "slzwx.h"
  21. /* ------ Alternate LZWEncode filter implementation ------ */
  22. /*
  23. The encoded data stream produced by this implementation of the LZWEncode
  24. filter consists of a sequence of 9-bit data elements. These elements are
  25. packed into bytes in big-endian order, e.g. the elements
  26. 100000000 001100001
  27. occurring at the very beginning of the data stream would be packed into
  28. bytes as
  29. 10000000 00011000 01......
  30. The first bit of each data element is a control bit. If the control bit is
  31. 0, the remaining 8 bits of the data element are a data byte. If the control
  32. bit is 1, the remaining 8 bits of the data element define a control
  33. function:
  34. 1 00000000 synchronization mark, see below
  35. 1 00000001 end of data
  36. 1 xxxxxxxx not used (all other values)
  37. The synchronization mark occurs at the beginning of the data stream, and at
  38. least once every 254 data bytes thereafter.
  39. This format is derived from basic principles of data encoding (the use of a
  40. separate flag bit to distinguish out-of-band control information from data
  41. per se, and the use of a periodic synchronization mark to help verify the
  42. validity of a data stream); it has no relationship to data compression. It
  43. is, however, compatible with LZW decompressors. It produces output that is
  44. approximately 9/8 times the size of the input.
  45. */
  46. /* Define the special codes, relative to 1 << InitialCodeLength. */
  47. #define CODE_RESET 0
  48. #define CODE_EOD 1
  49. #define CODE_0 2 /* first assignable code */
  50. /* Internal routine to put a code into the output buffer. */
  51. /* Let S = ss->code_size. */
  52. /* Relevant invariants: 9 <= S <= 15, 0 <= code < 1 << S; */
  53. /* 1 <= ss->bits_left <= 8; only the rightmost (8 - ss->bits_left) */
  54. /* bits of ss->bits contain valid data. */
  55. private byte *
  56. lzw_put_code(register stream_LZW_state * ss, byte * q, uint code)
  57. {
  58. uint size = ss->code_size;
  59. byte cb = (ss->bits << ss->bits_left) +
  60. (code >> (size - ss->bits_left));
  61. if_debug2('W', "[w]writing 0x%x,%d\n", code, ss->code_size);
  62. *++q = cb;
  63. if ((ss->bits_left += 8 - size) <= 0) {
  64. *++q = code >> -ss->bits_left;
  65. ss->bits_left += 8;
  66. }
  67. ss->bits = code;
  68. return q;
  69. }
  70. /* Initialize LZW-compatible encoding filter. */
  71. private int
  72. s_LZWE_reset(stream_state * st)
  73. {
  74. stream_LZW_state *const ss = (stream_LZW_state *) st;
  75. ss->code_size = ss->InitialCodeLength + 1;
  76. ss->bits_left = 8;
  77. /* Force the first code emitted to be a reset. */
  78. ss->next_code = (1 << ss->code_size) - 2;
  79. return 0;
  80. }
  81. private int
  82. s_LZWE_init(stream_state * st)
  83. {
  84. stream_LZW_state *const ss = (stream_LZW_state *) st;
  85. ss->InitialCodeLength = 8;
  86. ss->table.encode = 0;
  87. return s_LZWE_reset(st);
  88. }
  89. /* Process a buffer */
  90. private int
  91. s_LZWE_process(stream_state * st, stream_cursor_read * pr,
  92. stream_cursor_write * pw, bool last)
  93. {
  94. stream_LZW_state *const ss = (stream_LZW_state *) st;
  95. register const byte *p = pr->ptr;
  96. const byte *rlimit = pr->limit;
  97. register byte *q = pw->ptr;
  98. byte *wlimit = pw->limit;
  99. int status = 0;
  100. int signal = 1 << (ss->code_size - 1);
  101. uint limit_code = (1 << ss->code_size) - 2; /* reset 1 early */
  102. uint next_code = ss->next_code;
  103. while (p < rlimit) {
  104. if (next_code == limit_code) { /* Emit a reset code. */
  105. if (wlimit - q < 2) {
  106. status = 1;
  107. break;
  108. }
  109. q = lzw_put_code(ss, q, signal + CODE_RESET);
  110. next_code = signal + CODE_0;
  111. }
  112. if (wlimit - q < 2) {
  113. status = 1;
  114. break;
  115. }
  116. q = lzw_put_code(ss, q, *++p);
  117. next_code++;
  118. }
  119. if (last && status == 0) {
  120. if (wlimit - q < 2)
  121. status = 1;
  122. else {
  123. q = lzw_put_code(ss, q, signal + CODE_EOD);
  124. if (ss->bits_left < 8)
  125. *++q = ss->bits << ss->bits_left; /* final byte */
  126. }
  127. }
  128. ss->next_code = next_code;
  129. pr->ptr = p;
  130. pw->ptr = q;
  131. return status;
  132. }
  133. /* Stream template */
  134. const stream_template s_LZWE_template = {
  135. &st_LZW_state, s_LZWE_init, s_LZWE_process, 1, 2, NULL,
  136. s_LZW_set_defaults, s_LZWE_reset
  137. };