filter-predict.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <string.h>
  24. #include <limits.h>
  25. /* TODO: check if this works with 16bpp images */
  26. typedef struct
  27. {
  28. fz_stream *chain;
  29. int predictor;
  30. int columns;
  31. int colors;
  32. int bpc;
  33. int stride;
  34. int bpp;
  35. unsigned char *in;
  36. unsigned char *out;
  37. unsigned char *ref;
  38. unsigned char *rp, *wp;
  39. unsigned char buffer[4096];
  40. } fz_predict;
  41. static inline int getcomponent(unsigned char *line, int x, int bpc)
  42. {
  43. switch (bpc)
  44. {
  45. case 1: return (line[x >> 3] >> ( 7 - (x & 7) ) ) & 1;
  46. case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3;
  47. case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15;
  48. case 8: return line[x];
  49. case 16: return (line[x<<1]<<8)+line[(x<<1)+1];
  50. }
  51. return 0;
  52. }
  53. static inline void putcomponent(unsigned char *buf, int x, int bpc, int value)
  54. {
  55. switch (bpc)
  56. {
  57. case 1: buf[x >> 3] |= value << (7 - (x & 7)); break;
  58. case 2: buf[x >> 2] |= value << ((3 - (x & 3)) << 1); break;
  59. case 4: buf[x >> 1] |= value << ((1 - (x & 1)) << 2); break;
  60. case 8: buf[x] = value; break;
  61. case 16: buf[x<<1] = value>>8; buf[(x<<1)+1] = value; break;
  62. }
  63. }
  64. static inline int paeth(int a, int b, int c)
  65. {
  66. /* The definitions of ac and bc are correct, not a typo. */
  67. int ac = b - c, bc = a - c, abcc = ac + bc;
  68. int pa = fz_absi(ac);
  69. int pb = fz_absi(bc);
  70. int pc = fz_absi(abcc);
  71. return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
  72. }
  73. static void
  74. fz_predict_tiff(fz_predict *state, unsigned char *out, unsigned char *in)
  75. {
  76. int left[FZ_MAX_COLORS];
  77. int i, k;
  78. const int mask = (1 << state->bpc)-1;
  79. for (k = 0; k < state->colors; k++)
  80. left[k] = 0;
  81. /* special fast case */
  82. if (state->bpc == 8)
  83. {
  84. for (i = 0; i < state->columns; i++)
  85. for (k = 0; k < state->colors; k++)
  86. *out++ = left[k] = (*in++ + left[k]) & 0xFF;
  87. return;
  88. }
  89. /* putcomponent assumes zeroed memory for bpc < 8 */
  90. if (state->bpc < 8)
  91. memset(out, 0, state->stride);
  92. for (i = 0; i < state->columns; i++)
  93. {
  94. for (k = 0; k < state->colors; k++)
  95. {
  96. int a = getcomponent(in, i * state->colors + k, state->bpc);
  97. int b = a + left[k];
  98. int c = b & mask;
  99. putcomponent(out, i * state->colors + k, state->bpc, c);
  100. left[k] = c;
  101. }
  102. }
  103. }
  104. static void
  105. fz_predict_png(fz_context *ctx, fz_predict *state, unsigned char *out, unsigned char *in, size_t len, int predictor)
  106. {
  107. int bpp = state->bpp;
  108. size_t i;
  109. unsigned char *ref = state->ref;
  110. if ((size_t)bpp > len)
  111. bpp = (int)len;
  112. switch (predictor)
  113. {
  114. default:
  115. fz_warn(ctx, "unknown png predictor %d, treating as none", predictor);
  116. /* fallthrough */
  117. case 0:
  118. memcpy(out, in, len);
  119. break;
  120. case 1:
  121. for (i = bpp; i > 0; i--)
  122. {
  123. *out++ = *in++;
  124. }
  125. for (i = len - bpp; i > 0; i--)
  126. {
  127. *out = *in++ + out[-bpp];
  128. out++;
  129. }
  130. break;
  131. case 2:
  132. for (i = bpp; i > 0; i--)
  133. {
  134. *out++ = *in++ + *ref++;
  135. }
  136. for (i = len - bpp; i > 0; i--)
  137. {
  138. *out++ = *in++ + *ref++;
  139. }
  140. break;
  141. case 3:
  142. for (i = bpp; i > 0; i--)
  143. {
  144. *out++ = *in++ + (*ref++) / 2;
  145. }
  146. for (i = len - bpp; i > 0; i--)
  147. {
  148. *out = *in++ + (out[-bpp] + *ref++) / 2;
  149. out++;
  150. }
  151. break;
  152. case 4:
  153. for (i = bpp; i > 0; i--)
  154. {
  155. *out++ = *in++ + paeth(0, *ref++, 0);
  156. }
  157. for (i = len - bpp; i > 0; i --)
  158. {
  159. *out = *in++ + paeth(out[-bpp], *ref, ref[-bpp]);
  160. ref++;
  161. out++;
  162. }
  163. break;
  164. }
  165. }
  166. static int
  167. next_predict(fz_context *ctx, fz_stream *stm, size_t len)
  168. {
  169. fz_predict *state = stm->state;
  170. unsigned char *buf = state->buffer;
  171. unsigned char *p = buf;
  172. unsigned char *ep;
  173. int ispng = state->predictor >= 10;
  174. size_t n;
  175. if (len >= sizeof(state->buffer))
  176. len = sizeof(state->buffer);
  177. ep = buf + len;
  178. while (state->rp < state->wp && p < ep)
  179. *p++ = *state->rp++;
  180. while (p < ep)
  181. {
  182. n = fz_read(ctx, state->chain, state->in, state->stride + ispng);
  183. if (n == 0)
  184. break;
  185. if (state->predictor == 1)
  186. memcpy(state->out, state->in, n);
  187. else if (state->predictor == 2)
  188. fz_predict_tiff(state, state->out, state->in);
  189. else
  190. {
  191. fz_predict_png(ctx, state, state->out, state->in + 1, n - 1, state->in[0]);
  192. memcpy(state->ref, state->out, state->stride);
  193. }
  194. state->rp = state->out;
  195. state->wp = state->out + n - ispng;
  196. while (state->rp < state->wp && p < ep)
  197. *p++ = *state->rp++;
  198. }
  199. stm->rp = buf;
  200. stm->wp = p;
  201. if (stm->rp == stm->wp)
  202. return EOF;
  203. stm->pos += p - buf;
  204. return *stm->rp++;
  205. }
  206. static void
  207. close_predict(fz_context *ctx, void *state_)
  208. {
  209. fz_predict *state = (fz_predict *)state_;
  210. fz_drop_stream(ctx, state->chain);
  211. fz_free(ctx, state->in);
  212. fz_free(ctx, state->out);
  213. fz_free(ctx, state->ref);
  214. fz_free(ctx, state);
  215. }
  216. fz_stream *
  217. fz_open_predict(fz_context *ctx, fz_stream *chain, int predictor, int columns, int colors, int bpc)
  218. {
  219. fz_predict *state;
  220. if (predictor < 1)
  221. predictor = 1;
  222. if (columns < 1)
  223. columns = 1;
  224. if (colors < 1)
  225. colors = 1;
  226. if (bpc < 1)
  227. bpc = 8;
  228. if (bpc != 1 && bpc != 2 && bpc != 4 && bpc != 8 && bpc != 16)
  229. fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid number of bits per component: %d", bpc);
  230. if (colors > FZ_MAX_COLORS)
  231. fz_throw(ctx, FZ_ERROR_ARGUMENT, "too many color components (%d > %d)", colors, FZ_MAX_COLORS);
  232. if (columns >= INT_MAX / (bpc * colors))
  233. fz_throw(ctx, FZ_ERROR_LIMIT, "too many columns lead to an integer overflow (%d)", columns);
  234. if (predictor != 1 && predictor != 2 &&
  235. predictor != 10 && predictor != 11 &&
  236. predictor != 12 && predictor != 13 &&
  237. predictor != 14 && predictor != 15)
  238. {
  239. fz_warn(ctx, "invalid predictor: %d", predictor);
  240. predictor = 1;
  241. }
  242. state = fz_malloc_struct(ctx, fz_predict);
  243. fz_try(ctx)
  244. {
  245. state->predictor = predictor;
  246. state->columns = columns;
  247. state->colors = colors;
  248. state->bpc = bpc;
  249. state->stride = (state->bpc * state->colors * state->columns + 7) / 8;
  250. state->bpp = (state->bpc * state->colors + 7) / 8;
  251. state->in = Memento_label(fz_malloc(ctx, state->stride + 1), "predict_in");
  252. state->out = Memento_label(fz_malloc(ctx, state->stride), "predict_out");
  253. state->ref = Memento_label(fz_malloc(ctx, state->stride), "predict_ref");
  254. state->rp = state->out;
  255. state->wp = state->out;
  256. memset(state->ref, 0, state->stride);
  257. state->chain = fz_keep_stream(ctx, chain);
  258. }
  259. fz_catch(ctx)
  260. {
  261. fz_free(ctx, state->in);
  262. fz_free(ctx, state->out);
  263. fz_free(ctx, state);
  264. fz_rethrow(ctx);
  265. }
  266. return fz_new_stream(ctx, state, next_predict, close_predict);
  267. }