jbig2_page.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <stdlib.h>
  20. #ifdef OUTPUT_PBM
  21. #include <stdio.h>
  22. #endif
  23. #include "jbig2.h"
  24. #include "jbig2_priv.h"
  25. #include "jbig2_image.h"
  26. #include "jbig2_page.h"
  27. #include "jbig2_segment.h"
  28. /* dump the page struct info */
  29. static void
  30. dump_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, Jbig2Page *page)
  31. {
  32. if (page->x_resolution == 0) {
  33. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height);
  34. } else if (page->x_resolution == page->y_resolution) {
  35. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution);
  36. } else {
  37. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  38. "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution);
  39. }
  40. if (page->striped) {
  41. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size);
  42. }
  43. }
  44. /**
  45. * jbig2_page_info: parse page info segment
  46. *
  47. * Parse the page info segment data and fill out a corresponding
  48. * Jbig2Page struct and ready it for subsequent rendered data,
  49. * including allocating an image buffer for the page (or the first stripe)
  50. **/
  51. int
  52. jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
  53. {
  54. Jbig2Page *page, *pages;
  55. /* a new page info segment implies the previous page is finished */
  56. page = &(ctx->pages[ctx->current_page]);
  57. if (page->number != 0 && (page->state == JBIG2_PAGE_NEW || page->state == JBIG2_PAGE_FREE)) {
  58. page->state = JBIG2_PAGE_COMPLETE;
  59. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished");
  60. }
  61. /* find a free page */
  62. {
  63. size_t index, j;
  64. index = ctx->current_page;
  65. while (ctx->pages[index].state != JBIG2_PAGE_FREE) {
  66. index++;
  67. if (index >= ctx->max_page_index) {
  68. /* grow the list */
  69. if (ctx->max_page_index == UINT32_MAX) {
  70. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many pages in jbig2 image");
  71. }
  72. else if (ctx->max_page_index > (UINT32_MAX >> 2)) {
  73. ctx->max_page_index = UINT32_MAX;
  74. }
  75. pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, (ctx->max_page_index <<= 2));
  76. if (pages == NULL) {
  77. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate pages");
  78. }
  79. ctx->pages = pages;
  80. for (j = index; j < ctx->max_page_index; j++) {
  81. ctx->pages[j].state = JBIG2_PAGE_FREE;
  82. ctx->pages[j].number = 0;
  83. ctx->pages[j].image = NULL;
  84. }
  85. }
  86. }
  87. page = &(ctx->pages[index]);
  88. ctx->current_page = index;
  89. page->state = JBIG2_PAGE_NEW;
  90. page->number = segment->page_association;
  91. }
  92. /* FIXME: would be nice if we tried to work around this */
  93. if (segment->data_length < 19) {
  94. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  95. }
  96. /* 7.4.8.x */
  97. page->width = jbig2_get_uint32(segment_data);
  98. page->height = jbig2_get_uint32(segment_data + 4);
  99. page->x_resolution = jbig2_get_uint32(segment_data + 8);
  100. page->y_resolution = jbig2_get_uint32(segment_data + 12);
  101. page->flags = segment_data[16];
  102. /* Check for T.88 amendment 3 */
  103. if (page->flags & 0x80)
  104. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "page segment indicates use of color segments (NYI)");
  105. /* 7.4.8.6 */
  106. {
  107. int16_t striping = jbig2_get_int16(segment_data + 17);
  108. if (striping & 0x8000) {
  109. page->striped = TRUE;
  110. page->stripe_size = striping & 0x7FFF;
  111. } else {
  112. page->striped = FALSE;
  113. page->stripe_size = 0; /* would page->height be better? */
  114. }
  115. }
  116. if (page->height == 0xFFFFFFFF && page->striped == FALSE) {
  117. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not marked as striped, assuming striped with maximum strip size");
  118. page->striped = TRUE;
  119. page->stripe_size = 0x7FFF;
  120. }
  121. page->end_row = 0;
  122. if (segment->data_length > 19) {
  123. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment");
  124. }
  125. dump_page_info(ctx, segment, page);
  126. /* allocate an appropriate page image buffer */
  127. /* 7.4.8.2 */
  128. if (page->height == 0xFFFFFFFF) {
  129. page->image = jbig2_image_new(ctx, page->width, page->stripe_size);
  130. } else {
  131. page->image = jbig2_image_new(ctx, page->width, page->height);
  132. }
  133. if (page->image == NULL) {
  134. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate buffer for page image");
  135. } else {
  136. /* 8.2 (3) fill the page with the default pixel value */
  137. jbig2_image_clear(ctx, page->image, (page->flags & 4));
  138. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  139. "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height);
  140. }
  141. return 0;
  142. }
  143. /**
  144. * jbig2_end_of_stripe: parse and implement an end of stripe segment
  145. **/
  146. int
  147. jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
  148. {
  149. Jbig2Page *page = &ctx->pages[ctx->current_page];
  150. uint32_t end_row;
  151. if (segment->data_length < 4)
  152. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  153. end_row = jbig2_get_uint32(segment_data);
  154. if (end_row < page->end_row) {
  155. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  156. "end of stripe segment with non-positive end row advance (new end row %d vs current end row %d)", end_row, page->end_row);
  157. } else {
  158. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of stripe: advancing end row from %u to %u", page->end_row, end_row);
  159. }
  160. page->end_row = end_row;
  161. return 0;
  162. }
  163. /**
  164. * jbig2_complete_page: complete a page image
  165. *
  166. * called upon seeing an 'end of page' segment, this routine
  167. * marks a page as completed so it can be returned.
  168. * compositing will have already happened in the previous
  169. * segment handlers.
  170. **/
  171. int
  172. jbig2_complete_page(Jbig2Ctx *ctx)
  173. {
  174. int code;
  175. /* check for unfinished segments */
  176. if (ctx->segment_index != ctx->n_segments) {
  177. Jbig2Segment *segment = ctx->segments[ctx->segment_index];
  178. /* Some versions of Xerox Workcentre generate PDF files
  179. with the segment data length field of the last segment
  180. set to -1. Try to cope with this here. */
  181. if ((segment->data_length & 0xffffffff) == 0xffffffff) {
  182. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "file has an invalid segment data length; trying to decode using the available data");
  183. segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
  184. code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  185. ctx->buf_rd_ix += segment->data_length;
  186. ctx->segment_index++;
  187. if (code < 0) {
  188. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to parse segment");
  189. }
  190. }
  191. }
  192. /* ensure image exists before marking page as complete */
  193. if (ctx->pages[ctx->current_page].image == NULL) {
  194. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page has no image, cannot be completed");
  195. }
  196. ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
  197. return 0;
  198. }
  199. /**
  200. * jbig2_end_of_page: parse and implement an end of page segment
  201. **/
  202. int
  203. jbig2_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
  204. {
  205. uint32_t page_number = ctx->pages[ctx->current_page].number;
  206. int code;
  207. if (segment->page_association != page_number) {
  208. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  209. "end of page marker for page %d doesn't match current page number %d", segment->page_association, page_number);
  210. }
  211. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of page %d", page_number);
  212. code = jbig2_complete_page(ctx);
  213. if (code < 0)
  214. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to complete page");
  215. #ifdef OUTPUT_PBM
  216. code = jbig2_image_write_pbm(ctx->pages[ctx->current_page].image, stdout);
  217. if (code < 0)
  218. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write page image");
  219. #endif
  220. return 0;
  221. }
  222. /**
  223. * jbig2_add_page_result: composite a decoding result onto a page
  224. *
  225. * this is called to add the results of segment decode (when it
  226. * is an image) to a page image buffer
  227. **/
  228. int
  229. jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, uint32_t x, uint32_t y, Jbig2ComposeOp op)
  230. {
  231. int code;
  232. if (x > INT32_MAX || y > INT32_MAX)
  233. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unsupported image coordinates");
  234. /* ensure image exists first */
  235. if (page->image == NULL)
  236. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page info possibly missing, no image defined");
  237. /* grow the page to accommodate a new stripe if necessary */
  238. if (page->striped && page->height == 0xFFFFFFFF) {
  239. uint32_t new_height;
  240. if (y > UINT32_MAX - image->height)
  241. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "adding image at coordinate would grow page out of bounds");
  242. new_height = y + image->height;
  243. if (page->image->height < new_height) {
  244. Jbig2Image *resized_image = NULL;
  245. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "growing page buffer to %u rows to accommodate new stripe", new_height);
  246. resized_image = jbig2_image_resize(ctx, page->image, page->image->width, new_height, page->flags & 4);
  247. if (resized_image == NULL) {
  248. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to resize image to accommodate new stripe");
  249. }
  250. page->image = resized_image;
  251. }
  252. }
  253. code = jbig2_image_compose(ctx, page->image, image, x, y, op);
  254. if (code < 0)
  255. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image with page");
  256. return 0;
  257. }
  258. /**
  259. * jbig2_get_page: return the next available page image buffer
  260. *
  261. * the client can call this at any time to check if any pages
  262. * have been decoded. If so, it returns the first available
  263. * one. The client should then call jbig2_release_page() when
  264. * it no longer needs to refer to the image buffer.
  265. *
  266. * since this is a public routine for the library clients, we
  267. * return an image structure pointer, even though the function
  268. * name refers to a page; the page structure is private.
  269. **/
  270. Jbig2Image *
  271. jbig2_page_out(Jbig2Ctx *ctx)
  272. {
  273. uint32_t index;
  274. /* search for a completed page */
  275. for (index = 0; index < ctx->max_page_index; index++) {
  276. if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) {
  277. Jbig2Image *img = ctx->pages[index].image;
  278. uint32_t page_number = ctx->pages[index].number;
  279. if (img == NULL) {
  280. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned with no associated image", page_number);
  281. continue;
  282. }
  283. ctx->pages[index].state = JBIG2_PAGE_RETURNED;
  284. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned to the client", page_number);
  285. return jbig2_image_reference(ctx, img);
  286. }
  287. }
  288. /* no pages available */
  289. return NULL;
  290. }
  291. /**
  292. * jbig2_release_page: tell the library a page can be freed
  293. **/
  294. void
  295. jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image)
  296. {
  297. uint32_t index;
  298. if (image == NULL)
  299. return;
  300. /* find the matching page struct and mark it released */
  301. for (index = 0; index < ctx->max_page_index; index++) {
  302. if (ctx->pages[index].image == image) {
  303. jbig2_image_release(ctx, image);
  304. ctx->pages[index].state = JBIG2_PAGE_RELEASED;
  305. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d released by the client", ctx->pages[index].number);
  306. return;
  307. }
  308. }
  309. /* no matching pages */
  310. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to release unknown page");
  311. }