jbig2_image.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* Copyright (C) 2001-2023 Artifex Software, Inc.
  2. All Rights Reserved.
  3. This software is provided AS-IS with no warranty, either express or
  4. implied.
  5. This software is distributed under license and may not be copied,
  6. modified or distributed except as expressly authorized under the terms
  7. of the license contained in the file LICENSE in this distribution.
  8. Refer to licensing information at http://www.artifex.com or contact
  9. Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  10. CA 94129, USA, for further information.
  11. */
  12. /*
  13. jbig2dec
  14. */
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include "os_types.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h> /* memcpy() */
  22. #include "jbig2.h"
  23. #include "jbig2_priv.h"
  24. #include "jbig2_image.h"
  25. /* allocate a Jbig2Image structure and its associated bitmap */
  26. Jbig2Image *
  27. jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height)
  28. {
  29. Jbig2Image *image;
  30. uint32_t stride;
  31. if (width == 0 || height == 0) {
  32. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to create zero sized image");
  33. return NULL;
  34. }
  35. image = jbig2_new(ctx, Jbig2Image, 1);
  36. if (image == NULL) {
  37. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image");
  38. return NULL;
  39. }
  40. stride = ((width - 1) >> 3) + 1; /* generate a byte-aligned stride */
  41. /* check for integer multiplication overflow */
  42. if (height > (INT32_MAX / stride)) {
  43. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow (stride=%u, height=%u)", stride, height);
  44. jbig2_free(ctx->allocator, image);
  45. return NULL;
  46. }
  47. image->data = jbig2_new(ctx, uint8_t, (size_t) height * stride);
  48. if (image->data == NULL) {
  49. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image data buffer (stride=%u, height=%u)", stride, height);
  50. jbig2_free(ctx->allocator, image);
  51. return NULL;
  52. }
  53. image->width = width;
  54. image->height = height;
  55. image->stride = stride;
  56. image->refcount = 1;
  57. return image;
  58. }
  59. /* bump the reference count for an image pointer */
  60. Jbig2Image *
  61. jbig2_image_reference(Jbig2Ctx *ctx, Jbig2Image *image)
  62. {
  63. if (image)
  64. image->refcount++;
  65. return image;
  66. }
  67. /* release an image pointer, freeing it it appropriate */
  68. void
  69. jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image)
  70. {
  71. if (image == NULL)
  72. return;
  73. image->refcount--;
  74. if (image->refcount == 0)
  75. jbig2_image_free(ctx, image);
  76. }
  77. /* free a Jbig2Image structure and its associated memory */
  78. void
  79. jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
  80. {
  81. if (image != NULL) {
  82. jbig2_free(ctx->allocator, image->data);
  83. jbig2_free(ctx->allocator, image);
  84. }
  85. }
  86. /* resize a Jbig2Image */
  87. Jbig2Image *
  88. jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height, int value)
  89. {
  90. if (width == image->width) {
  91. uint8_t *data;
  92. /* check for integer multiplication overflow */
  93. if (image->height > (INT32_MAX / image->stride)) {
  94. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow during resize (stride=%u, height=%u)", image->stride, height);
  95. return NULL;
  96. }
  97. /* use the same stride, just change the length */
  98. data = jbig2_renew(ctx, image->data, uint8_t, (size_t) height * image->stride);
  99. if (data == NULL) {
  100. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to reallocate image");
  101. return NULL;
  102. }
  103. image->data = data;
  104. if (height > image->height) {
  105. const uint8_t fill = value ? 0xFF : 0x00;
  106. memset(image->data + (size_t) image->height * image->stride, fill, ((size_t) height - image->height) * image->stride);
  107. }
  108. image->height = height;
  109. } else {
  110. Jbig2Image *newimage;
  111. int code;
  112. /* Unoptimized implementation, but it works. */
  113. newimage = jbig2_image_new(ctx, width, height);
  114. if (newimage == NULL) {
  115. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate resized image");
  116. return NULL;
  117. }
  118. jbig2_image_clear(ctx, newimage, value);
  119. code = jbig2_image_compose(ctx, newimage, image, 0, 0, JBIG2_COMPOSE_REPLACE);
  120. if (code < 0) {
  121. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image buffers when resizing");
  122. jbig2_image_release(ctx, newimage);
  123. return NULL;
  124. }
  125. /* if refcount > 1 the original image, its pointer must
  126. be kept, so simply replaces its innards, and throw away
  127. the empty new image shell. */
  128. jbig2_free(ctx->allocator, image->data);
  129. image->width = newimage->width;
  130. image->height = newimage->height;
  131. image->stride = newimage->stride;
  132. image->data = newimage->data;
  133. jbig2_free(ctx->allocator, newimage);
  134. }
  135. return image;
  136. }
  137. static inline void
  138. template_image_compose_opt(const uint8_t * JBIG2_RESTRICT ss, uint8_t * JBIG2_RESTRICT dd, int early, int late, uint8_t leftmask, uint8_t rightmask, uint32_t bytewidth_, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride, Jbig2ComposeOp op)
  139. {
  140. int i;
  141. uint32_t j;
  142. int bytewidth = (int)bytewidth_;
  143. if (bytewidth == 1) {
  144. for (j = 0; j < h; j++) {
  145. /* Only 1 byte! */
  146. uint8_t v = (((early ? 0 : ss[0]<<8) | (late ? 0 : ss[1]))>>shift);
  147. if (op == JBIG2_COMPOSE_OR)
  148. *dd |= v & leftmask;
  149. else if (op == JBIG2_COMPOSE_AND)
  150. *dd &= (v & leftmask) | ~leftmask;
  151. else if (op == JBIG2_COMPOSE_XOR)
  152. *dd ^= v & leftmask;
  153. else if (op == JBIG2_COMPOSE_XNOR)
  154. *dd ^= (~v) & leftmask;
  155. else /* Replace */
  156. *dd = (v & leftmask) | (*dd & ~leftmask);
  157. dd += dstride;
  158. ss += sstride;
  159. }
  160. return;
  161. }
  162. bytewidth -= 2;
  163. if (shift == 0) {
  164. ss++;
  165. for (j = 0; j < h; j++) {
  166. /* Left byte */
  167. const uint8_t * JBIG2_RESTRICT s = ss;
  168. uint8_t * JBIG2_RESTRICT d = dd;
  169. if (op == JBIG2_COMPOSE_OR)
  170. *d++ |= *s++ & leftmask;
  171. else if (op == JBIG2_COMPOSE_AND)
  172. *d++ &= (*s++ & leftmask) | ~leftmask;
  173. else if (op == JBIG2_COMPOSE_XOR)
  174. *d++ ^= *s++ & leftmask;
  175. else if (op == JBIG2_COMPOSE_XNOR)
  176. *d++ ^= (~*s++) & leftmask;
  177. else /* Replace */
  178. *d = (*s++ & leftmask) | (*d & ~leftmask), d++;
  179. /* Central run */
  180. for (i = bytewidth; i != 0; i--) {
  181. if (op == JBIG2_COMPOSE_OR)
  182. *d++ |= *s++;
  183. else if (op == JBIG2_COMPOSE_AND)
  184. *d++ &= *s++;
  185. else if (op == JBIG2_COMPOSE_XOR)
  186. *d++ ^= *s++;
  187. else if (op == JBIG2_COMPOSE_XNOR)
  188. *d++ ^= ~*s++;
  189. else /* Replace */
  190. *d++ = *s++;
  191. }
  192. /* Right byte */
  193. if (op == JBIG2_COMPOSE_OR)
  194. *d |= *s & rightmask;
  195. else if (op == JBIG2_COMPOSE_AND)
  196. *d &= (*s & rightmask) | ~rightmask;
  197. else if (op == JBIG2_COMPOSE_XOR)
  198. *d ^= *s & rightmask;
  199. else if (op == JBIG2_COMPOSE_XNOR)
  200. *d ^= (~*s) & rightmask;
  201. else /* Replace */
  202. *d = (*s & rightmask) | (*d & ~rightmask);
  203. dd += dstride;
  204. ss += sstride;
  205. }
  206. } else {
  207. for (j = 0; j < h; j++) {
  208. /* Left byte */
  209. const uint8_t * JBIG2_RESTRICT s = ss;
  210. uint8_t * JBIG2_RESTRICT d = dd;
  211. uint8_t s0, s1, v;
  212. s0 = early ? 0 : *s;
  213. s++;
  214. s1 = *s++;
  215. v = ((s0<<8) | s1)>>shift;
  216. if (op == JBIG2_COMPOSE_OR)
  217. *d++ |= v & leftmask;
  218. else if (op == JBIG2_COMPOSE_AND)
  219. *d++ &= (v & leftmask) | ~leftmask;
  220. else if (op == JBIG2_COMPOSE_XOR)
  221. *d++ ^= v & leftmask;
  222. else if (op == JBIG2_COMPOSE_XNOR)
  223. *d++ ^= (~v) & leftmask;
  224. else /* Replace */
  225. *d = (v & leftmask) | (*d & ~leftmask), d++;
  226. /* Central run */
  227. for (i = bytewidth; i > 0; i--) {
  228. s0 = s1; s1 = *s++;
  229. v = ((s0<<8) | s1)>>shift;
  230. if (op == JBIG2_COMPOSE_OR)
  231. *d++ |= v;
  232. else if (op == JBIG2_COMPOSE_AND)
  233. *d++ &= v;
  234. else if (op == JBIG2_COMPOSE_XOR)
  235. *d++ ^= v;
  236. else if (op == JBIG2_COMPOSE_XNOR)
  237. *d++ ^= ~v;
  238. else /* Replace */
  239. *d++ = v;
  240. }
  241. /* Right byte */
  242. s0 = s1; s1 = (late ? 0 : *s);
  243. v = (((s0<<8) | s1)>>shift);
  244. if (op == JBIG2_COMPOSE_OR)
  245. *d |= v & rightmask;
  246. else if (op == JBIG2_COMPOSE_AND)
  247. *d &= (v & rightmask) | ~rightmask;
  248. else if (op == JBIG2_COMPOSE_XOR)
  249. *d ^= v & rightmask;
  250. else if (op == JBIG2_COMPOSE_XNOR)
  251. *d ^= ~v & rightmask;
  252. else /* Replace */
  253. *d = (v & rightmask) | (*d & ~rightmask);
  254. dd += dstride;
  255. ss += sstride;
  256. }
  257. }
  258. }
  259. static void
  260. jbig2_image_compose_opt_OR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
  261. {
  262. if (early || late)
  263. template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  264. else
  265. template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  266. }
  267. static void
  268. jbig2_image_compose_opt_AND(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
  269. {
  270. if (early || late)
  271. template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  272. else
  273. template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  274. }
  275. static void
  276. jbig2_image_compose_opt_XOR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
  277. {
  278. if (early || late)
  279. template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  280. else
  281. template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  282. }
  283. static void
  284. jbig2_image_compose_opt_XNOR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
  285. {
  286. if (early || late)
  287. template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  288. else
  289. template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  290. }
  291. static void
  292. jbig2_image_compose_opt_REPLACE(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
  293. {
  294. if (early || late)
  295. template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  296. else
  297. template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  298. }
  299. /* composite one jbig2_image onto another */
  300. int
  301. jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op)
  302. {
  303. uint32_t w, h;
  304. uint32_t shift;
  305. uint32_t leftbyte;
  306. uint8_t *ss;
  307. uint8_t *dd;
  308. uint8_t leftmask, rightmask;
  309. int early = x >= 0;
  310. int late;
  311. uint32_t bytewidth;
  312. uint32_t syoffset = 0;
  313. if (src == NULL)
  314. return 0;
  315. if ((UINT32_MAX - src->width < (uint32_t) (x > 0 ? x : -x)) ||
  316. (UINT32_MAX - src->height < (uint32_t) (y > 0 ? y : -y)))
  317. {
  318. #ifdef JBIG2_DEBUG
  319. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "overflow in compose_image");
  320. #endif
  321. return 0;
  322. }
  323. /* This code takes a src image and combines it onto dst at offset (x,y), with operation op. */
  324. /* Data is packed msb first within a byte, so with bits numbered: 01234567.
  325. * Second byte is: 89abcdef. So to combine into a run, we use:
  326. * (s[0]<<8) | s[1] == 0123456789abcdef.
  327. * To read from src into dst at offset 3, we need to read:
  328. * read: 0123456789abcdef...
  329. * write: 0123456798abcdef...
  330. * In general, to read from src and write into dst at offset x, we need to shift
  331. * down by (x&7) bits to allow for bit alignment. So shift = x&7.
  332. * So the 'central' part of our runs will see us doing:
  333. * *d++ op= ((s[0]<<8)|s[1])>>shift;
  334. * with special cases on the left and right edges of the run to mask.
  335. * With the left hand edge, we have to be careful not to 'underread' the start of
  336. * the src image; this is what the early flag is about. Similarly we have to be
  337. * careful not to read off the right hand edge; this is what the late flag is for.
  338. */
  339. /* clip */
  340. w = src->width;
  341. h = src->height;
  342. shift = (x & 7);
  343. ss = src->data - early;
  344. if (x < 0) {
  345. if (w < (uint32_t) -x)
  346. w = 0;
  347. else
  348. w += x;
  349. ss += (-x-1)>>3;
  350. x = 0;
  351. }
  352. if (y < 0) {
  353. if (h < (uint32_t) -y)
  354. h = 0;
  355. else
  356. h += y;
  357. syoffset = -y * src->stride;
  358. y = 0;
  359. }
  360. if ((uint32_t)x + w > dst->width)
  361. {
  362. if (dst->width < (uint32_t)x)
  363. w = 0;
  364. else
  365. w = dst->width - x;
  366. }
  367. if ((uint32_t)y + h > dst->height)
  368. {
  369. if (dst->height < (uint32_t)y)
  370. h = 0;
  371. else
  372. h = dst->height - y;
  373. }
  374. #ifdef JBIG2_DEBUG
  375. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "compositing %dx%d at (%d, %d) after clipping", w, h, x, y);
  376. #endif
  377. /* check for zero clipping region */
  378. if ((w <= 0) || (h <= 0)) {
  379. #ifdef JBIG2_DEBUG
  380. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zero clipping region");
  381. #endif
  382. return 0;
  383. }
  384. leftbyte = (uint32_t) x >> 3;
  385. dd = dst->data + y * dst->stride + leftbyte;
  386. bytewidth = (((uint32_t) x + w - 1) >> 3) - leftbyte + 1;
  387. leftmask = 255>>(x&7);
  388. rightmask = (((x+w)&7) == 0) ? 255 : ~(255>>((x+w)&7));
  389. if (bytewidth == 1)
  390. leftmask &= rightmask;
  391. late = (ss + bytewidth >= src->data + ((src->width+7)>>3));
  392. ss += syoffset;
  393. switch(op)
  394. {
  395. case JBIG2_COMPOSE_OR:
  396. jbig2_image_compose_opt_OR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  397. break;
  398. case JBIG2_COMPOSE_AND:
  399. jbig2_image_compose_opt_AND(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  400. break;
  401. case JBIG2_COMPOSE_XOR:
  402. jbig2_image_compose_opt_XOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  403. break;
  404. case JBIG2_COMPOSE_XNOR:
  405. jbig2_image_compose_opt_XNOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  406. break;
  407. case JBIG2_COMPOSE_REPLACE:
  408. jbig2_image_compose_opt_REPLACE(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  409. break;
  410. }
  411. return 0;
  412. }
  413. /* initialize an image bitmap to a constant value */
  414. void
  415. jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value)
  416. {
  417. const uint8_t fill = value ? 0xFF : 0x00;
  418. memset(image->data, fill, image->stride * image->height);
  419. }
  420. /* look up a pixel value in an image.
  421. returns 0 outside the image frame for the convenience of
  422. the template code
  423. */
  424. int
  425. jbig2_image_get_pixel(Jbig2Image *image, int x, int y)
  426. {
  427. const int w = image->width;
  428. const int h = image->height;
  429. const int byte = (x >> 3) + y * image->stride;
  430. const int bit = 7 - (x & 7);
  431. if ((x < 0) || (x >= w))
  432. return 0;
  433. if ((y < 0) || (y >= h))
  434. return 0;
  435. return ((image->data[byte] >> bit) & 1);
  436. }
  437. /* set an individual pixel value in an image */
  438. void
  439. jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value)
  440. {
  441. const int w = image->width;
  442. const int h = image->height;
  443. int scratch, mask;
  444. int bit, byte;
  445. if ((x < 0) || (x >= w))
  446. return;
  447. if ((y < 0) || (y >= h))
  448. return;
  449. byte = (x >> 3) + y * image->stride;
  450. bit = 7 - (x & 7);
  451. mask = (1 << bit) ^ 0xff;
  452. scratch = image->data[byte] & mask;
  453. image->data[byte] = scratch | (value << bit);
  454. }