jdapimin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * jdapimin.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1998, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2016, 2022, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file contains application interface code for the decompression half
  12. * of the JPEG library. These are the "minimum" API routines that may be
  13. * needed in either the normal full-decompression case or the
  14. * transcoding-only case.
  15. *
  16. * Most of the routines intended to be called directly by an application
  17. * are in this file or in jdapistd.c. But also see jcomapi.c for routines
  18. * shared by compression and decompression, and jdtrans.c for the transcoding
  19. * case.
  20. */
  21. #define JPEG_INTERNALS
  22. #include "jinclude.h"
  23. #include "jpeglib.h"
  24. #include "jdmaster.h"
  25. #include "jconfigint.h"
  26. /*
  27. * Initialization of a JPEG decompression object.
  28. * The error manager must already be set up (in case memory manager fails).
  29. */
  30. GLOBAL(void)
  31. jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
  32. {
  33. int i;
  34. /* Guard against version mismatches between library and caller. */
  35. cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
  36. if (version != JPEG_LIB_VERSION)
  37. ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  38. if (structsize != sizeof(struct jpeg_decompress_struct))
  39. ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
  40. (int)sizeof(struct jpeg_decompress_struct), (int)structsize);
  41. /* For debugging purposes, we zero the whole master structure.
  42. * But the application has already set the err pointer, and may have set
  43. * client_data, so we have to save and restore those fields.
  44. * Note: if application hasn't set client_data, tools like Purify may
  45. * complain here.
  46. */
  47. {
  48. struct jpeg_error_mgr *err = cinfo->err;
  49. void *client_data = cinfo->client_data; /* ignore Purify complaint here */
  50. memset(cinfo, 0, sizeof(struct jpeg_decompress_struct));
  51. cinfo->err = err;
  52. cinfo->client_data = client_data;
  53. }
  54. cinfo->is_decompressor = TRUE;
  55. /* Initialize a memory manager instance for this object */
  56. jinit_memory_mgr((j_common_ptr)cinfo);
  57. /* Zero out pointers to permanent structures. */
  58. cinfo->progress = NULL;
  59. cinfo->src = NULL;
  60. for (i = 0; i < NUM_QUANT_TBLS; i++)
  61. cinfo->quant_tbl_ptrs[i] = NULL;
  62. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  63. cinfo->dc_huff_tbl_ptrs[i] = NULL;
  64. cinfo->ac_huff_tbl_ptrs[i] = NULL;
  65. }
  66. /* Initialize marker processor so application can override methods
  67. * for COM, APPn markers before calling jpeg_read_header.
  68. */
  69. cinfo->marker_list = NULL;
  70. jinit_marker_reader(cinfo);
  71. /* And initialize the overall input controller. */
  72. jinit_input_controller(cinfo);
  73. /* OK, I'm ready */
  74. cinfo->global_state = DSTATE_START;
  75. /* The master struct is used to store extension parameters, so we allocate it
  76. * here.
  77. */
  78. cinfo->master = (struct jpeg_decomp_master *)
  79. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  80. sizeof(my_decomp_master));
  81. memset(cinfo->master, 0, sizeof(my_decomp_master));
  82. }
  83. /*
  84. * Destruction of a JPEG decompression object
  85. */
  86. GLOBAL(void)
  87. jpeg_destroy_decompress(j_decompress_ptr cinfo)
  88. {
  89. jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
  90. }
  91. /*
  92. * Abort processing of a JPEG decompression operation,
  93. * but don't destroy the object itself.
  94. */
  95. GLOBAL(void)
  96. jpeg_abort_decompress(j_decompress_ptr cinfo)
  97. {
  98. jpeg_abort((j_common_ptr)cinfo); /* use common routine */
  99. }
  100. /*
  101. * Set default decompression parameters.
  102. */
  103. LOCAL(void)
  104. default_decompress_parms(j_decompress_ptr cinfo)
  105. {
  106. /* Guess the input colorspace, and set output colorspace accordingly. */
  107. /* (Wish JPEG committee had provided a real way to specify this...) */
  108. /* Note application may override our guesses. */
  109. switch (cinfo->num_components) {
  110. case 1:
  111. cinfo->jpeg_color_space = JCS_GRAYSCALE;
  112. cinfo->out_color_space = JCS_GRAYSCALE;
  113. break;
  114. case 3:
  115. if (cinfo->saw_JFIF_marker) {
  116. cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  117. } else if (cinfo->saw_Adobe_marker) {
  118. switch (cinfo->Adobe_transform) {
  119. case 0:
  120. cinfo->jpeg_color_space = JCS_RGB;
  121. break;
  122. case 1:
  123. cinfo->jpeg_color_space = JCS_YCbCr;
  124. break;
  125. default:
  126. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  127. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  128. break;
  129. }
  130. } else {
  131. /* Saw no special markers, try to guess from the component IDs */
  132. int cid0 = cinfo->comp_info[0].component_id;
  133. int cid1 = cinfo->comp_info[1].component_id;
  134. int cid2 = cinfo->comp_info[2].component_id;
  135. if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  136. cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  137. else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  138. cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  139. else {
  140. TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  141. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  142. }
  143. }
  144. /* Always guess RGB is proper output colorspace. */
  145. cinfo->out_color_space = JCS_RGB;
  146. break;
  147. case 4:
  148. if (cinfo->saw_Adobe_marker) {
  149. switch (cinfo->Adobe_transform) {
  150. case 0:
  151. cinfo->jpeg_color_space = JCS_CMYK;
  152. break;
  153. case 2:
  154. cinfo->jpeg_color_space = JCS_YCCK;
  155. break;
  156. default:
  157. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  158. cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  159. break;
  160. }
  161. } else {
  162. /* No special markers, assume straight CMYK. */
  163. cinfo->jpeg_color_space = JCS_CMYK;
  164. }
  165. cinfo->out_color_space = JCS_CMYK;
  166. break;
  167. default:
  168. cinfo->jpeg_color_space = JCS_UNKNOWN;
  169. cinfo->out_color_space = JCS_UNKNOWN;
  170. break;
  171. }
  172. /* Set defaults for other decompression parameters. */
  173. cinfo->scale_num = 1; /* 1:1 scaling */
  174. cinfo->scale_denom = 1;
  175. cinfo->output_gamma = 1.0;
  176. cinfo->buffered_image = FALSE;
  177. cinfo->raw_data_out = FALSE;
  178. cinfo->dct_method = JDCT_DEFAULT;
  179. cinfo->do_fancy_upsampling = TRUE;
  180. cinfo->do_block_smoothing = TRUE;
  181. cinfo->quantize_colors = FALSE;
  182. /* We set these in case application only sets quantize_colors. */
  183. cinfo->dither_mode = JDITHER_FS;
  184. #ifdef QUANT_2PASS_SUPPORTED
  185. cinfo->two_pass_quantize = TRUE;
  186. #else
  187. cinfo->two_pass_quantize = FALSE;
  188. #endif
  189. cinfo->desired_number_of_colors = 256;
  190. cinfo->colormap = NULL;
  191. /* Initialize for no mode change in buffered-image mode. */
  192. cinfo->enable_1pass_quant = FALSE;
  193. cinfo->enable_external_quant = FALSE;
  194. cinfo->enable_2pass_quant = FALSE;
  195. }
  196. /*
  197. * Decompression startup: read start of JPEG datastream to see what's there.
  198. * Need only initialize JPEG object and supply a data source before calling.
  199. *
  200. * This routine will read as far as the first SOS marker (ie, actual start of
  201. * compressed data), and will save all tables and parameters in the JPEG
  202. * object. It will also initialize the decompression parameters to default
  203. * values, and finally return JPEG_HEADER_OK. On return, the application may
  204. * adjust the decompression parameters and then call jpeg_start_decompress.
  205. * (Or, if the application only wanted to determine the image parameters,
  206. * the data need not be decompressed. In that case, call jpeg_abort or
  207. * jpeg_destroy to release any temporary space.)
  208. * If an abbreviated (tables only) datastream is presented, the routine will
  209. * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  210. * re-use the JPEG object to read the abbreviated image datastream(s).
  211. * It is unnecessary (but OK) to call jpeg_abort in this case.
  212. * The JPEG_SUSPENDED return code only occurs if the data source module
  213. * requests suspension of the decompressor. In this case the application
  214. * should load more source data and then re-call jpeg_read_header to resume
  215. * processing.
  216. * If a non-suspending data source is used and require_image is TRUE, then the
  217. * return code need not be inspected since only JPEG_HEADER_OK is possible.
  218. *
  219. * This routine is now just a front end to jpeg_consume_input, with some
  220. * extra error checking.
  221. */
  222. GLOBAL(int)
  223. jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
  224. {
  225. int retcode;
  226. if (cinfo->global_state != DSTATE_START &&
  227. cinfo->global_state != DSTATE_INHEADER)
  228. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  229. retcode = jpeg_consume_input(cinfo);
  230. switch (retcode) {
  231. case JPEG_REACHED_SOS:
  232. retcode = JPEG_HEADER_OK;
  233. break;
  234. case JPEG_REACHED_EOI:
  235. if (require_image) /* Complain if application wanted an image */
  236. ERREXIT(cinfo, JERR_NO_IMAGE);
  237. /* Reset to start state; it would be safer to require the application to
  238. * call jpeg_abort, but we can't change it now for compatibility reasons.
  239. * A side effect is to free any temporary memory (there shouldn't be any).
  240. */
  241. jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
  242. retcode = JPEG_HEADER_TABLES_ONLY;
  243. break;
  244. case JPEG_SUSPENDED:
  245. /* no work */
  246. break;
  247. }
  248. return retcode;
  249. }
  250. /*
  251. * Consume data in advance of what the decompressor requires.
  252. * This can be called at any time once the decompressor object has
  253. * been created and a data source has been set up.
  254. *
  255. * This routine is essentially a state machine that handles a couple
  256. * of critical state-transition actions, namely initial setup and
  257. * transition from header scanning to ready-for-start_decompress.
  258. * All the actual input is done via the input controller's consume_input
  259. * method.
  260. */
  261. GLOBAL(int)
  262. jpeg_consume_input(j_decompress_ptr cinfo)
  263. {
  264. int retcode = JPEG_SUSPENDED;
  265. /* NB: every possible DSTATE value should be listed in this switch */
  266. switch (cinfo->global_state) {
  267. case DSTATE_START:
  268. /* Start-of-datastream actions: reset appropriate modules */
  269. (*cinfo->inputctl->reset_input_controller) (cinfo);
  270. /* Initialize application's data source module */
  271. (*cinfo->src->init_source) (cinfo);
  272. cinfo->global_state = DSTATE_INHEADER;
  273. FALLTHROUGH /*FALLTHROUGH*/
  274. case DSTATE_INHEADER:
  275. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  276. if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  277. /* Set up default parameters based on header data */
  278. default_decompress_parms(cinfo);
  279. /* Set global state: ready for start_decompress */
  280. cinfo->global_state = DSTATE_READY;
  281. }
  282. break;
  283. case DSTATE_READY:
  284. /* Can't advance past first SOS until start_decompress is called */
  285. retcode = JPEG_REACHED_SOS;
  286. break;
  287. case DSTATE_PRELOAD:
  288. case DSTATE_PRESCAN:
  289. case DSTATE_SCANNING:
  290. case DSTATE_RAW_OK:
  291. case DSTATE_BUFIMAGE:
  292. case DSTATE_BUFPOST:
  293. case DSTATE_STOPPING:
  294. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  295. break;
  296. default:
  297. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  298. }
  299. return retcode;
  300. }
  301. /*
  302. * Have we finished reading the input file?
  303. */
  304. GLOBAL(boolean)
  305. jpeg_input_complete(j_decompress_ptr cinfo)
  306. {
  307. /* Check for valid jpeg object */
  308. if (cinfo->global_state < DSTATE_START ||
  309. cinfo->global_state > DSTATE_STOPPING)
  310. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  311. return cinfo->inputctl->eoi_reached;
  312. }
  313. /*
  314. * Is there more than one scan?
  315. */
  316. GLOBAL(boolean)
  317. jpeg_has_multiple_scans(j_decompress_ptr cinfo)
  318. {
  319. /* Only valid after jpeg_read_header completes */
  320. if (cinfo->global_state < DSTATE_READY ||
  321. cinfo->global_state > DSTATE_STOPPING)
  322. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  323. return cinfo->inputctl->has_multiple_scans;
  324. }
  325. /*
  326. * Finish JPEG decompression.
  327. *
  328. * This will normally just verify the file trailer and release temp storage.
  329. *
  330. * Returns FALSE if suspended. The return value need be inspected only if
  331. * a suspending data source is used.
  332. */
  333. GLOBAL(boolean)
  334. jpeg_finish_decompress(j_decompress_ptr cinfo)
  335. {
  336. if ((cinfo->global_state == DSTATE_SCANNING ||
  337. cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {
  338. /* Terminate final pass of non-buffered mode */
  339. if (cinfo->output_scanline < cinfo->output_height)
  340. ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  341. (*cinfo->master->finish_output_pass) (cinfo);
  342. cinfo->global_state = DSTATE_STOPPING;
  343. } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  344. /* Finishing after a buffered-image operation */
  345. cinfo->global_state = DSTATE_STOPPING;
  346. } else if (cinfo->global_state != DSTATE_STOPPING) {
  347. /* STOPPING = repeat call after a suspension, anything else is error */
  348. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  349. }
  350. /* Read until EOI */
  351. while (!cinfo->inputctl->eoi_reached) {
  352. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  353. return FALSE; /* Suspend, come back later */
  354. }
  355. /* Do final cleanup */
  356. (*cinfo->src->term_source) (cinfo);
  357. /* We can use jpeg_abort to release memory and reset global_state */
  358. jpeg_abort((j_common_ptr)cinfo);
  359. return TRUE;
  360. }