draw-unpack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "draw-imp.h"
  24. #include <string.h>
  25. /* Unpack image samples and optionally pad pixels with opaque alpha */
  26. #define get1(buf,x) ((buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  27. #define get2(buf,x) ((buf[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3 )
  28. #define get4(buf,x) ((buf[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15 )
  29. #define get8(buf,x) (buf[x])
  30. #define get16(buf,x) (buf[x << 1])
  31. #define get24(buf,x) (buf[(x << 1) + x])
  32. #define get32(buf,x) (buf[x << 2])
  33. static unsigned char get1_tab_1[256][8];
  34. static unsigned char get1_tab_1p[256][16];
  35. static unsigned char get1_tab_255[256][8];
  36. static unsigned char get1_tab_255p[256][16];
  37. /*
  38. Bug 697012 shows that the unpacking code can confuse valgrind due
  39. to the use of undefined bits in the padding at the end of lines.
  40. We unpack from bits to bytes by copying from a lookup table.
  41. Valgrind is not capable of understanding that it doesn't matter
  42. what the undefined bits are, as the bytes we copy that correspond
  43. to the defined bits will always agree regardless of these
  44. undefined bits by construction of the table.
  45. We therefore have a VGMASK macro that explicitly masks off these
  46. bits in PACIFY_VALGRIND builds.
  47. */
  48. #ifdef PACIFY_VALGRIND
  49. static const unsigned char mask[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  50. #define VGMASK(v,m) (v & mask[(m)])
  51. #else
  52. #define VGMASK(v,m) (v)
  53. #endif
  54. static void
  55. init_get1_tables(void)
  56. {
  57. static int once = 0;
  58. unsigned char bits[1];
  59. int i, k, x;
  60. /* TODO: mutex lock here */
  61. if (once)
  62. return;
  63. for (i = 0; i < 256; i++)
  64. {
  65. bits[0] = i;
  66. for (k = 0; k < 8; k++)
  67. {
  68. x = get1(bits, k);
  69. get1_tab_1[i][k] = x;
  70. get1_tab_1p[i][k * 2] = x;
  71. get1_tab_1p[i][k * 2 + 1] = 255;
  72. get1_tab_255[i][k] = x * 255;
  73. get1_tab_255p[i][k * 2] = x * 255;
  74. get1_tab_255p[i][k * 2 + 1] = 255;
  75. }
  76. }
  77. once = 1;
  78. }
  79. static void
  80. fz_unpack_mono_line_unscaled(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  81. {
  82. int w3 = w >> 3;
  83. int x;
  84. for (x = 0; x < w3; x++)
  85. {
  86. memcpy(dp, get1_tab_1[*sp++], 8);
  87. dp += 8;
  88. }
  89. x = x << 3;
  90. if (x < w)
  91. memcpy(dp, get1_tab_1[VGMASK(*sp, w - x)], w - x);
  92. }
  93. static void
  94. fz_unpack_mono_line_scaled(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  95. {
  96. int w3 = w >> 3;
  97. int x;
  98. for (x = 0; x < w3; x++)
  99. {
  100. memcpy(dp, get1_tab_255[*sp++], 8);
  101. dp += 8;
  102. }
  103. x = x << 3;
  104. if (x < w)
  105. memcpy(dp, get1_tab_255[VGMASK(*sp, w - x)], w - x);
  106. }
  107. static void
  108. fz_unpack_mono_line_unscaled_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  109. {
  110. int w3 = w >> 3;
  111. int x;
  112. for (x = 0; x < w3; x++)
  113. {
  114. memcpy(dp, get1_tab_1p[*sp++], 16);
  115. dp += 16;
  116. }
  117. x = x << 3;
  118. if (x < w)
  119. memcpy(dp, get1_tab_1p[VGMASK(*sp, w - x)], (w - x) << 1);
  120. }
  121. static void
  122. fz_unpack_mono_line_scaled_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  123. {
  124. int w3 = w >> 3;
  125. int x;
  126. for (x = 0; x < w3; x++)
  127. {
  128. memcpy(dp, get1_tab_255p[*sp++], 16);
  129. dp += 16;
  130. }
  131. x = x << 3;
  132. if (x < w)
  133. memcpy(dp, get1_tab_255p[VGMASK(*sp, w - x)], (w - x) << 1);
  134. }
  135. static void
  136. fz_unpack_line(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  137. {
  138. int len = w * n;
  139. while (len--)
  140. *dp++ = *sp++;
  141. }
  142. static void
  143. fz_unpack_line_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  144. {
  145. int x, k;
  146. for (x = 0; x < w; x++)
  147. {
  148. for (k = 0; k < n; k++)
  149. *dp++ = *sp++;
  150. *dp++ = 255;
  151. }
  152. }
  153. static void
  154. fz_unpack_any_l2depth(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
  155. {
  156. unsigned char *p = dp;
  157. int b = 0;
  158. int x, k;
  159. for (x = 0; x < w; x++)
  160. {
  161. for (k = 0; k < n; k++)
  162. {
  163. switch (depth)
  164. {
  165. case 1: *p++ = get1(sp, b) * scale; break;
  166. case 2: *p++ = get2(sp, b) * scale; break;
  167. case 4: *p++ = get4(sp, b) * scale; break;
  168. case 8: *p++ = get8(sp, b); break;
  169. case 16: *p++ = get16(sp, b); break;
  170. case 24: *p++ = get24(sp, b); break;
  171. case 32: *p++ = get32(sp, b); break;
  172. }
  173. b++;
  174. }
  175. b += skip;
  176. if (pad)
  177. *p++ = 255;
  178. }
  179. }
  180. typedef void (*fz_unpack_line_fn)(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip);
  181. void
  182. fz_unpack_tile(fz_context *ctx, fz_pixmap *dst, unsigned char *src, int n, int depth, size_t stride, int scale)
  183. {
  184. unsigned char *sp = src;
  185. unsigned char *dp = dst->samples;
  186. fz_unpack_line_fn unpack_line = NULL;
  187. int pad, y, skip;
  188. int w = dst->w;
  189. int h = dst->h;
  190. pad = 0;
  191. skip = 0;
  192. if (dst->n > n)
  193. pad = 255;
  194. if (dst->n < n)
  195. {
  196. skip = n - dst->n;
  197. n = dst->n;
  198. }
  199. if (depth == 1)
  200. init_get1_tables();
  201. if (scale == 0)
  202. {
  203. switch (depth)
  204. {
  205. case 1: scale = 255; break;
  206. case 2: scale = 85; break;
  207. case 4: scale = 17; break;
  208. }
  209. }
  210. if (n == 1 && depth == 1 && scale == 1 && !pad && !skip)
  211. unpack_line = fz_unpack_mono_line_unscaled;
  212. else if (n == 1 && depth == 1 && scale == 255 && !pad && !skip)
  213. unpack_line = fz_unpack_mono_line_scaled;
  214. else if (n == 1 && depth == 1 && scale == 1 && pad && !skip)
  215. unpack_line = fz_unpack_mono_line_unscaled_with_padding;
  216. else if (n == 1 && depth == 1 && scale == 255 && pad && !skip)
  217. unpack_line = fz_unpack_mono_line_scaled_with_padding;
  218. else if (depth == 8 && !pad && !skip)
  219. unpack_line = fz_unpack_line;
  220. else if (depth == 8 && pad && !skip)
  221. unpack_line = fz_unpack_line_with_padding;
  222. else if (depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16 || depth == 24 || depth == 32)
  223. unpack_line = fz_unpack_any_l2depth;
  224. if (unpack_line)
  225. {
  226. for (y = 0; y < h; y++, sp += stride, dp += dst->stride)
  227. unpack_line(dp, sp, w, n, depth, scale, pad, skip);
  228. }
  229. else if (depth > 0 && depth <= 8 * (int)sizeof(int))
  230. {
  231. fz_stream *stm;
  232. int x, k;
  233. size_t skipbits = 8 * stride - (size_t)w * n * depth;
  234. if (skipbits > 32)
  235. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Inappropriate stride!");
  236. stm = fz_open_memory(ctx, sp, h * stride);
  237. fz_try(ctx)
  238. {
  239. for (y = 0; y < h; y++)
  240. {
  241. for (x = 0; x < w; x++)
  242. {
  243. for (k = 0; k < n; k++)
  244. {
  245. if (depth <= 8)
  246. *dp++ = fz_read_bits(ctx, stm, depth) << (8 - depth);
  247. else
  248. *dp++ = fz_read_bits(ctx, stm, depth) >> (depth - 8);
  249. }
  250. if (pad)
  251. *dp++ = 255;
  252. }
  253. dp += dst->stride - (size_t)w * (n + (pad > 0));
  254. (void) fz_read_bits(ctx, stm, (int)skipbits);
  255. }
  256. }
  257. fz_always(ctx)
  258. fz_drop_stream(ctx, stm);
  259. fz_catch(ctx)
  260. fz_rethrow(ctx);
  261. }
  262. else
  263. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot unpack tile with %d bits per component", depth);
  264. }
  265. /* Apply decode array */
  266. void
  267. fz_decode_indexed_tile(fz_context *ctx, fz_pixmap *pix, const float *decode, int maxval)
  268. {
  269. int add[FZ_MAX_COLORS];
  270. int mul[FZ_MAX_COLORS];
  271. unsigned char *p = pix->samples;
  272. size_t stride = pix->stride - pix->w * (size_t)pix->n;
  273. int len;
  274. int pn = pix->n;
  275. int n = pn - pix->alpha;
  276. int needed;
  277. int k;
  278. int h;
  279. needed = 0;
  280. for (k = 0; k < n; k++)
  281. {
  282. int min = decode[k * 2] * 256;
  283. int max = decode[k * 2 + 1] * 256;
  284. add[k] = min;
  285. mul[k] = (max - min) / maxval;
  286. needed |= min != 0 || max != maxval * 256;
  287. }
  288. if (!needed)
  289. return;
  290. h = pix->h;
  291. while (h--)
  292. {
  293. len = pix->w;
  294. while (len--)
  295. {
  296. for (k = 0; k < n; k++)
  297. {
  298. int value = (add[k] + (((p[k] << 8) * mul[k]) >> 8)) >> 8;
  299. p[k] = fz_clampi(value, 0, 255);
  300. }
  301. p += pn;
  302. }
  303. p += stride;
  304. }
  305. }
  306. void
  307. fz_decode_tile(fz_context *ctx, fz_pixmap *pix, const float *decode)
  308. {
  309. int add[FZ_MAX_COLORS];
  310. int mul[FZ_MAX_COLORS];
  311. unsigned char *p = pix->samples;
  312. size_t stride = pix->stride - pix->w * (size_t)pix->n;
  313. int len;
  314. int n = fz_maxi(1, pix->n - pix->alpha);
  315. int k;
  316. int h;
  317. for (k = 0; k < n; k++)
  318. {
  319. int min = decode[k * 2] * 255;
  320. int max = decode[k * 2 + 1] * 255;
  321. add[k] = min;
  322. mul[k] = max - min;
  323. }
  324. h = pix->h;
  325. while (h--)
  326. {
  327. len = pix->w;
  328. while (len--)
  329. {
  330. for (k = 0; k < n; k++)
  331. {
  332. int value = add[k] + fz_mul255(p[k], mul[k]);
  333. p[k] = fz_clampi(value, 0, 255);
  334. }
  335. p += pix->n;
  336. }
  337. p += stride;
  338. }
  339. }
  340. typedef struct
  341. {
  342. fz_stream *src;
  343. int depth;
  344. int w;
  345. int h;
  346. int n;
  347. int skip;
  348. int pad;
  349. int scale;
  350. int src_stride;
  351. int dst_stride;
  352. fz_unpack_line_fn unpack;
  353. unsigned char buf[1];
  354. } unpack_state;
  355. static int
  356. unpack_next(fz_context *ctx, fz_stream *stm, size_t max)
  357. {
  358. unpack_state *state = (unpack_state *)stm->state;
  359. size_t n = state->src_stride;
  360. stm->rp = state->buf;
  361. do
  362. {
  363. size_t a = fz_available(ctx, state->src, n);
  364. if (a == 0)
  365. return EOF;
  366. if (a > n)
  367. a = n;
  368. memcpy(stm->rp, state->src->rp, a);
  369. stm->rp += a;
  370. state->src->rp += a;
  371. n -= a;
  372. }
  373. while (n);
  374. state->h--;
  375. stm->pos += state->dst_stride;
  376. stm->wp = stm->rp + state->dst_stride;
  377. state->unpack(stm->rp, state->buf, state->w, state->n, state->depth, state->scale, state->pad, state->skip);
  378. return *stm->rp++;
  379. }
  380. static void
  381. unpack_drop(fz_context *ctx, void *state)
  382. {
  383. fz_free(ctx, state);
  384. }
  385. fz_stream *
  386. fz_unpack_stream(fz_context *ctx, fz_stream *src, int depth, int w, int h, int n, int indexed, int pad, int skip)
  387. {
  388. int src_stride = (w*depth*n+7)>>3;
  389. int dst_stride;
  390. unpack_state *state;
  391. fz_unpack_line_fn unpack_line = NULL;
  392. int scale = 1;
  393. if (depth == 1)
  394. init_get1_tables();
  395. if (!indexed)
  396. switch (depth)
  397. {
  398. case 1: scale = 255; break;
  399. case 2: scale = 85; break;
  400. case 4: scale = 17; break;
  401. }
  402. dst_stride = w * (n + !!pad);
  403. if (n == 1 && depth == 1 && scale == 1 && !pad && !skip)
  404. unpack_line = fz_unpack_mono_line_unscaled;
  405. else if (n == 1 && depth == 1 && scale == 255 && !pad && !skip)
  406. unpack_line = fz_unpack_mono_line_scaled;
  407. else if (n == 1 && depth == 1 && scale == 1 && pad && !skip)
  408. unpack_line = fz_unpack_mono_line_unscaled_with_padding;
  409. else if (n == 1 && depth == 1 && scale == 255 && pad && !skip)
  410. unpack_line = fz_unpack_mono_line_scaled_with_padding;
  411. else if (depth == 8 && !pad && !skip)
  412. unpack_line = fz_unpack_line;
  413. else if (depth == 8 && pad && !skip)
  414. unpack_line = fz_unpack_line_with_padding;
  415. else if (depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16 || depth == 24 || depth == 32)
  416. unpack_line = fz_unpack_any_l2depth;
  417. else
  418. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported combination in fz_unpack_stream");
  419. state = fz_malloc(ctx, sizeof(unpack_state) + dst_stride + src_stride);
  420. state->src = src;
  421. state->depth = depth;
  422. state->w = w;
  423. state->h = h;
  424. state->n = n;
  425. state->skip = skip;
  426. state->pad = pad;
  427. state->scale = scale;
  428. state->unpack = unpack_line;
  429. state->src_stride = src_stride;
  430. state->dst_stride = dst_stride;
  431. return fz_new_stream(ctx, state, unpack_next, unpack_drop);
  432. }