filter-thunder.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. /* 4bit greyscale Thunderscan decoding */
  24. typedef struct
  25. {
  26. fz_stream *chain;
  27. int lastpixel;
  28. int run;
  29. int pixel;
  30. int len;
  31. unsigned char *buffer;
  32. } fz_thunder;
  33. static int
  34. next_thunder(fz_context *ctx, fz_stream *stm, size_t max)
  35. {
  36. fz_thunder *state = stm->state;
  37. unsigned char *p = state->buffer;
  38. unsigned char *ep;
  39. int c, v, i, pixels, index;
  40. if (max > (size_t)state->len)
  41. max = (size_t)state->len;
  42. ep = p + max;
  43. c = 0;
  44. while (p < ep && c >= 0)
  45. {
  46. pixels = 0;
  47. v = 0;
  48. while (pixels < 2)
  49. {
  50. if (state->run > 0)
  51. {
  52. v <<= 4;
  53. v |= state->pixel & 0xf;
  54. state->pixel >>= 4;
  55. state->run--;
  56. pixels++;
  57. if (state->run > 2)
  58. state->pixel |= ((state->pixel >> 4) & 0xf) << 8;
  59. }
  60. else
  61. {
  62. c = fz_read_byte(ctx, state->chain);
  63. if (c < 0)
  64. break;
  65. switch ((c >> 6) & 0x3)
  66. {
  67. case 0x0: /* run of pixels identical to last pixel */
  68. state->run = c;
  69. state->pixel = (state->lastpixel << 8) | (state->lastpixel << 4) | (state->lastpixel << 0);
  70. break;
  71. case 0x1: /* three pixels with 2bit deltas to last pixel */
  72. for (i = 0; i < 3; i++)
  73. {
  74. static const int deltas[] = { 0, 1, 0, -1 };
  75. index = (c >> (4 - i * 2)) & 0x3;
  76. if (index == 2)
  77. continue;
  78. state->lastpixel = (state->lastpixel + deltas[index]) & 0xf;
  79. state->pixel <<= 4;
  80. state->pixel |= state->lastpixel;
  81. state->run++;
  82. }
  83. break;
  84. case 0x2: /* two pixels with 3bit deltas to last pixel */
  85. for (i = 0; i < 2; i++)
  86. {
  87. static const int deltas[] = { 0, 1, 2, 3, 0, -3, -2, -1 };
  88. index = (c >> (3 - i * 3)) & 0x7;
  89. if (index == 4)
  90. continue;
  91. state->lastpixel = (state->lastpixel + deltas[index]) & 0xf;
  92. state->pixel <<= 4;
  93. state->pixel |= state->lastpixel;
  94. state->run++;
  95. }
  96. break;
  97. case 0x3: /* a single raw 4bit pixel */
  98. state->run = 1;
  99. state->pixel = c & 0xf;
  100. state->lastpixel = state->pixel & 0xf;
  101. break;
  102. }
  103. }
  104. }
  105. if (pixels)
  106. *p++ = v;
  107. }
  108. stm->rp = state->buffer;
  109. stm->wp = p;
  110. stm->pos += p - state->buffer;
  111. if (stm->rp != p)
  112. return *stm->rp++;
  113. return EOF;
  114. }
  115. static void
  116. close_thunder(fz_context *ctx, void *state_)
  117. {
  118. fz_thunder *state = (fz_thunder *)state_;
  119. fz_drop_stream(ctx, state->chain);
  120. fz_free(ctx, state->buffer);
  121. fz_free(ctx, state);
  122. }
  123. fz_stream *
  124. fz_open_thunder(fz_context *ctx, fz_stream *chain, int w)
  125. {
  126. fz_thunder *state = fz_malloc_struct(ctx, fz_thunder);
  127. fz_try(ctx)
  128. {
  129. state->run = 0;
  130. state->pixel = 0;
  131. state->lastpixel = 0;
  132. state->len = w / 2;
  133. state->buffer = Memento_label(fz_malloc(ctx, state->len), "thunder_buffer");
  134. state->chain = fz_keep_stream(ctx, chain);
  135. }
  136. fz_catch(ctx)
  137. {
  138. fz_free(ctx, state);
  139. fz_rethrow(ctx);
  140. }
  141. return fz_new_stream(ctx, state, next_thunder, close_thunder);
  142. }