jdapimin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. i = sizeof(struct jpeg_decompress_struct);
  35. i = SIZEOF(struct jpeg_decompress_struct);
  36. MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  37. cinfo->err = err;
  38. }
  39. cinfo->is_decompressor = TRUE;
  40. /* Initialize a memory manager instance for this object */
  41. jinit_memory_mgr((j_common_ptr) cinfo);
  42. /* Zero out pointers to permanent structures. */
  43. cinfo->progress = NULL;
  44. cinfo->src = NULL;
  45. for (i = 0; i < NUM_QUANT_TBLS; i++)
  46. cinfo->quant_tbl_ptrs[i] = NULL;
  47. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  48. cinfo->dc_huff_tbl_ptrs[i] = NULL;
  49. cinfo->ac_huff_tbl_ptrs[i] = NULL;
  50. }
  51. /* Initialize marker processor so application can override methods
  52. * for COM, APPn markers before calling jpeg_read_header.
  53. */
  54. jinit_marker_reader(cinfo);
  55. /* And initialize the overall input controller. */
  56. jinit_input_controller(cinfo);
  57. /* OK, I'm ready */
  58. cinfo->global_state = DSTATE_START;
  59. }
  60. /*
  61. * Destruction of a JPEG decompression object
  62. */
  63. GLOBAL void
  64. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  65. {
  66. jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  67. }
  68. /*
  69. * Abort processing of a JPEG decompression operation,
  70. * but don't destroy the object itself.
  71. */
  72. GLOBAL void
  73. jpeg_abort_decompress (j_decompress_ptr cinfo)
  74. {
  75. jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  76. }
  77. /*
  78. * Install a special processing method for COM or APPn markers.
  79. */
  80. GLOBAL void
  81. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  82. jpeg_marker_parser_method routine)
  83. {
  84. if (marker_code == JPEG_COM)
  85. cinfo->marker->process_COM = routine;
  86. else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
  87. cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
  88. else
  89. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  90. }
  91. /*
  92. * Set default decompression parameters.
  93. */
  94. LOCAL void
  95. default_decompress_parms (j_decompress_ptr cinfo)
  96. {
  97. /* Guess the input colorspace, and set output colorspace accordingly. */
  98. /* (Wish JPEG committee had provided a real way to specify this...) */
  99. /* Note application may override our guesses. */
  100. switch (cinfo->num_components) {
  101. case 1:
  102. cinfo->jpeg_color_space = JCS_GRAYSCALE;
  103. cinfo->out_color_space = JCS_GRAYSCALE;
  104. break;
  105. case 3:
  106. if (cinfo->saw_JFIF_marker) {
  107. cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  108. } else if (cinfo->saw_Adobe_marker) {
  109. switch (cinfo->Adobe_transform) {
  110. case 0:
  111. cinfo->jpeg_color_space = JCS_RGB;
  112. break;
  113. case 1:
  114. cinfo->jpeg_color_space = JCS_YCbCr;
  115. break;
  116. default:
  117. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  118. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  119. break;
  120. }
  121. } else {
  122. /* Saw no special markers, try to guess from the component IDs */
  123. int cid0 = cinfo->comp_info[0].component_id;
  124. int cid1 = cinfo->comp_info[1].component_id;
  125. int cid2 = cinfo->comp_info[2].component_id;
  126. if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  127. cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  128. else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  129. cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  130. else {
  131. TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  132. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  133. }
  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 = 1; /* 1:1 scaling */
  165. cinfo->scale_denom = 1;
  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. }