jdapimin.cpp 13 KB

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