jdapimin.c 12 KB

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