jdapimin.c 12 KB

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