Team Fortress 2 Source Code as on 22/4/2020
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.

396 lines
12 KiB

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