Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
14 KiB

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