Leaked source code of windows server 2003
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.

432 lines
13 KiB

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