jdapimin.c 13 KB

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