jdapimin.c 13 KB

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