jdapimin.c 13 KB

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