jdapimin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * jdapimin.c
  3. *
  4. * Copyright (C) 1994-1998, Thomas G. Lane.
  5. * Modified 2009-2013 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;
  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. /* First try to guess from the component IDs */
  108. if (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03)
  109. cinfo->jpeg_color_space = JCS_YCbCr;
  110. else if (cid0 == 0x01 && cid1 == 0x22 && cid2 == 0x23)
  111. cinfo->jpeg_color_space = JCS_BG_YCC;
  112. else if (cid0 == 0x52 && cid1 == 0x47 && cid2 == 0x42)
  113. cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  114. else if (cid0 == 0x72 && cid1 == 0x67 && cid2 == 0x62)
  115. cinfo->jpeg_color_space = JCS_BG_RGB; /* ASCII 'r', 'g', 'b' */
  116. else if (cinfo->saw_JFIF_marker)
  117. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  118. else if (cinfo->saw_Adobe_marker) {
  119. switch (cinfo->Adobe_transform) {
  120. case 0:
  121. cinfo->jpeg_color_space = JCS_RGB;
  122. break;
  123. case 1:
  124. cinfo->jpeg_color_space = JCS_YCbCr;
  125. break;
  126. default:
  127. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  128. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  129. break;
  130. }
  131. } else {
  132. TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  133. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  134. }
  135. /* Always guess RGB is proper output colorspace. */
  136. cinfo->out_color_space = JCS_RGB;
  137. break;
  138. case 4:
  139. if (cinfo->saw_Adobe_marker) {
  140. switch (cinfo->Adobe_transform) {
  141. case 0:
  142. cinfo->jpeg_color_space = JCS_CMYK;
  143. break;
  144. case 2:
  145. cinfo->jpeg_color_space = JCS_YCCK;
  146. break;
  147. default:
  148. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  149. cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  150. break;
  151. }
  152. } else {
  153. /* No special markers, assume straight CMYK. */
  154. cinfo->jpeg_color_space = JCS_CMYK;
  155. }
  156. cinfo->out_color_space = JCS_CMYK;
  157. break;
  158. default:
  159. cinfo->jpeg_color_space = JCS_UNKNOWN;
  160. cinfo->out_color_space = JCS_UNKNOWN;
  161. break;
  162. }
  163. /* Set defaults for other decompression parameters. */
  164. cinfo->scale_num = cinfo->block_size; /* 1:1 scaling */
  165. cinfo->scale_denom = cinfo->block_size;
  166. cinfo->output_gamma = 1.0;
  167. cinfo->buffered_image = FALSE;
  168. cinfo->raw_data_out = FALSE;
  169. cinfo->dct_method = JDCT_DEFAULT;
  170. cinfo->do_fancy_upsampling = TRUE;
  171. cinfo->do_block_smoothing = TRUE;
  172. cinfo->quantize_colors = FALSE;
  173. /* We set these in case application only sets quantize_colors. */
  174. cinfo->dither_mode = JDITHER_FS;
  175. #ifdef QUANT_2PASS_SUPPORTED
  176. cinfo->two_pass_quantize = TRUE;
  177. #else
  178. cinfo->two_pass_quantize = FALSE;
  179. #endif
  180. cinfo->desired_number_of_colors = 256;
  181. cinfo->colormap = NULL;
  182. /* Initialize for no mode change in buffered-image mode. */
  183. cinfo->enable_1pass_quant = FALSE;
  184. cinfo->enable_external_quant = FALSE;
  185. cinfo->enable_2pass_quant = FALSE;
  186. }
  187. /*
  188. * Decompression startup: read start of JPEG datastream to see what's there.
  189. * Need only initialize JPEG object and supply a data source before calling.
  190. *
  191. * This routine will read as far as the first SOS marker (ie, actual start of
  192. * compressed data), and will save all tables and parameters in the JPEG
  193. * object. It will also initialize the decompression parameters to default
  194. * values, and finally return JPEG_HEADER_OK. On return, the application may
  195. * adjust the decompression parameters and then call jpeg_start_decompress.
  196. * (Or, if the application only wanted to determine the image parameters,
  197. * the data need not be decompressed. In that case, call jpeg_abort or
  198. * jpeg_destroy to release any temporary space.)
  199. * If an abbreviated (tables only) datastream is presented, the routine will
  200. * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  201. * re-use the JPEG object to read the abbreviated image datastream(s).
  202. * It is unnecessary (but OK) to call jpeg_abort in this case.
  203. * The JPEG_SUSPENDED return code only occurs if the data source module
  204. * requests suspension of the decompressor. In this case the application
  205. * should load more source data and then re-call jpeg_read_header to resume
  206. * processing.
  207. * If a non-suspending data source is used and require_image is TRUE, then the
  208. * return code need not be inspected since only JPEG_HEADER_OK is possible.
  209. *
  210. * This routine is now just a front end to jpeg_consume_input, with some
  211. * extra error checking.
  212. */
  213. GLOBAL(int)
  214. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  215. {
  216. int retcode;
  217. if (cinfo->global_state != DSTATE_START &&
  218. cinfo->global_state != DSTATE_INHEADER)
  219. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  220. retcode = jpeg_consume_input(cinfo);
  221. switch (retcode) {
  222. case JPEG_REACHED_SOS:
  223. retcode = JPEG_HEADER_OK;
  224. break;
  225. case JPEG_REACHED_EOI:
  226. if (require_image) /* Complain if application wanted an image */
  227. ERREXIT(cinfo, JERR_NO_IMAGE);
  228. /* Reset to start state; it would be safer to require the application to
  229. * call jpeg_abort, but we can't change it now for compatibility reasons.
  230. * A side effect is to free any temporary memory (there shouldn't be any).
  231. */
  232. jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  233. retcode = JPEG_HEADER_TABLES_ONLY;
  234. break;
  235. case JPEG_SUSPENDED:
  236. /* no work */
  237. break;
  238. }
  239. return retcode;
  240. }
  241. /*
  242. * Consume data in advance of what the decompressor requires.
  243. * This can be called at any time once the decompressor object has
  244. * been created and a data source has been set up.
  245. *
  246. * This routine is essentially a state machine that handles a couple
  247. * of critical state-transition actions, namely initial setup and
  248. * transition from header scanning to ready-for-start_decompress.
  249. * All the actual input is done via the input controller's consume_input
  250. * method.
  251. */
  252. GLOBAL(int)
  253. jpeg_consume_input (j_decompress_ptr cinfo)
  254. {
  255. int retcode = JPEG_SUSPENDED;
  256. /* NB: every possible DSTATE value should be listed in this switch */
  257. switch (cinfo->global_state) {
  258. case DSTATE_START:
  259. /* Start-of-datastream actions: reset appropriate modules */
  260. (*cinfo->inputctl->reset_input_controller) (cinfo);
  261. /* Initialize application's data source module */
  262. (*cinfo->src->init_source) (cinfo);
  263. cinfo->global_state = DSTATE_INHEADER;
  264. /*FALLTHROUGH*/
  265. case DSTATE_INHEADER:
  266. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  267. if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  268. /* Set up default parameters based on header data */
  269. default_decompress_parms(cinfo);
  270. /* Set global state: ready for start_decompress */
  271. cinfo->global_state = DSTATE_READY;
  272. }
  273. break;
  274. case DSTATE_READY:
  275. /* Can't advance past first SOS until start_decompress is called */
  276. retcode = JPEG_REACHED_SOS;
  277. break;
  278. case DSTATE_PRELOAD:
  279. case DSTATE_PRESCAN:
  280. case DSTATE_SCANNING:
  281. case DSTATE_RAW_OK:
  282. case DSTATE_BUFIMAGE:
  283. case DSTATE_BUFPOST:
  284. case DSTATE_STOPPING:
  285. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  286. break;
  287. default:
  288. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  289. }
  290. return retcode;
  291. }
  292. /*
  293. * Have we finished reading the input file?
  294. */
  295. GLOBAL(boolean)
  296. jpeg_input_complete (j_decompress_ptr cinfo)
  297. {
  298. /* Check for valid jpeg object */
  299. if (cinfo->global_state < DSTATE_START ||
  300. cinfo->global_state > DSTATE_STOPPING)
  301. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  302. return cinfo->inputctl->eoi_reached;
  303. }
  304. /*
  305. * Is there more than one scan?
  306. */
  307. GLOBAL(boolean)
  308. jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  309. {
  310. /* Only valid after jpeg_read_header completes */
  311. if (cinfo->global_state < DSTATE_READY ||
  312. cinfo->global_state > DSTATE_STOPPING)
  313. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  314. return cinfo->inputctl->has_multiple_scans;
  315. }
  316. /*
  317. * Finish JPEG decompression.
  318. *
  319. * This will normally just verify the file trailer and release temp storage.
  320. *
  321. * Returns FALSE if suspended. The return value need be inspected only if
  322. * a suspending data source is used.
  323. */
  324. GLOBAL(boolean)
  325. jpeg_finish_decompress (j_decompress_ptr cinfo)
  326. {
  327. if ((cinfo->global_state == DSTATE_SCANNING ||
  328. cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  329. /* Terminate final pass of non-buffered mode */
  330. if (cinfo->output_scanline < cinfo->output_height)
  331. ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  332. (*cinfo->master->finish_output_pass) (cinfo);
  333. cinfo->global_state = DSTATE_STOPPING;
  334. } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  335. /* Finishing after a buffered-image operation */
  336. cinfo->global_state = DSTATE_STOPPING;
  337. } else if (cinfo->global_state != DSTATE_STOPPING) {
  338. /* STOPPING = repeat call after a suspension, anything else is error */
  339. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  340. }
  341. /* Read until EOI */
  342. while (! cinfo->inputctl->eoi_reached) {
  343. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  344. return FALSE; /* Suspend, come back later */
  345. }
  346. /* Do final cleanup */
  347. (*cinfo->src->term_source) (cinfo);
  348. /* We can use jpeg_abort to release memory and reset global_state */
  349. jpeg_abort((j_common_ptr) cinfo);
  350. return TRUE;
  351. }