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.

384 lines
13 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jdinput.c
  5. *
  6. * Copyright (C) 1991-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 input control logic for the JPEG decompressor.
  11. * These routines are concerned with controlling the decompressor's input
  12. * processing (marker reading and coefficient decoding). The actual input
  13. * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
  14. */
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. /* Private state */
  19. typedef struct {
  20. struct jpeg_input_controller pub; /* public fields */
  21. boolean inheaders; /* TRUE until first SOS is reached */
  22. } my_input_controller;
  23. typedef my_input_controller * my_inputctl_ptr;
  24. /* Forward declarations */
  25. METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
  26. /*
  27. * Routines to calculate various quantities related to the size of the image.
  28. */
  29. LOCAL(void)
  30. initial_setup (j_decompress_ptr cinfo)
  31. /* Called once, when first SOS marker is reached */
  32. {
  33. int ci;
  34. jpeg_component_info *compptr;
  35. /* Make sure image isn't bigger than I can handle */
  36. if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  37. (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  38. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  39. /* For now, precision must match compiled-in value... */
  40. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  41. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  42. /* Check that number of components won't exceed internal array sizes */
  43. if (cinfo->num_components > MAX_COMPONENTS)
  44. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  45. MAX_COMPONENTS);
  46. /* Compute maximum sampling factors; check factor validity */
  47. cinfo->max_h_samp_factor = 1;
  48. cinfo->max_v_samp_factor = 1;
  49. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  50. ci++, compptr++) {
  51. if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  52. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  53. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  54. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  55. compptr->h_samp_factor);
  56. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  57. compptr->v_samp_factor);
  58. }
  59. /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
  60. * In the full decompressor, this will be overridden by jdmaster.c;
  61. * but in the transcoder, jdmaster.c is not used, so we must do it here.
  62. */
  63. cinfo->min_DCT_scaled_size = DCTSIZE;
  64. /* Compute dimensions of components */
  65. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  66. ci++, compptr++) {
  67. compptr->DCT_scaled_size = DCTSIZE;
  68. /* Size in DCT blocks */
  69. compptr->width_in_blocks = (JDIMENSION)
  70. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  71. (long) (cinfo->max_h_samp_factor * DCTSIZE));
  72. compptr->height_in_blocks = (JDIMENSION)
  73. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  74. (long) (cinfo->max_v_samp_factor * DCTSIZE));
  75. /* downsampled_width and downsampled_height will also be overridden by
  76. * jdmaster.c if we are doing full decompression. The transcoder library
  77. * doesn't use these values, but the calling application might.
  78. */
  79. /* Size in samples */
  80. compptr->downsampled_width = (JDIMENSION)
  81. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  82. (long) cinfo->max_h_samp_factor);
  83. compptr->downsampled_height = (JDIMENSION)
  84. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  85. (long) cinfo->max_v_samp_factor);
  86. /* Mark component needed, until color conversion says otherwise */
  87. compptr->component_needed = TRUE;
  88. /* Mark no quantization table yet saved for component */
  89. compptr->quant_table = NULL;
  90. }
  91. /* Compute number of fully interleaved MCU rows. */
  92. cinfo->total_iMCU_rows = (JDIMENSION)
  93. jdiv_round_up((long) cinfo->image_height,
  94. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  95. /* Decide whether file contains multiple scans */
  96. if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  97. cinfo->inputctl->has_multiple_scans = TRUE;
  98. else
  99. cinfo->inputctl->has_multiple_scans = FALSE;
  100. }
  101. LOCAL(void)
  102. per_scan_setup (j_decompress_ptr cinfo)
  103. /* Do computations that are needed before processing a JPEG scan */
  104. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  105. {
  106. int ci, mcublks, tmp;
  107. jpeg_component_info *compptr;
  108. if (cinfo->comps_in_scan == 1) {
  109. /* Noninterleaved (single-component) scan */
  110. compptr = cinfo->cur_comp_info[0];
  111. /* Overall image size in MCUs */
  112. cinfo->MCUs_per_row = compptr->width_in_blocks;
  113. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  114. /* For noninterleaved scan, always one block per MCU */
  115. compptr->MCU_width = 1;
  116. compptr->MCU_height = 1;
  117. compptr->MCU_blocks = 1;
  118. compptr->MCU_sample_width = compptr->DCT_scaled_size;
  119. compptr->last_col_width = 1;
  120. /* For noninterleaved scans, it is convenient to define last_row_height
  121. * as the number of block rows present in the last iMCU row.
  122. */
  123. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  124. if (tmp == 0) tmp = compptr->v_samp_factor;
  125. compptr->last_row_height = tmp;
  126. /* Prepare array describing MCU composition */
  127. cinfo->blocks_in_MCU = 1;
  128. cinfo->MCU_membership[0] = 0;
  129. } else {
  130. /* Interleaved (multi-component) scan */
  131. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  132. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  133. MAX_COMPS_IN_SCAN);
  134. /* Overall image size in MCUs */
  135. cinfo->MCUs_per_row = (JDIMENSION)
  136. jdiv_round_up((long) cinfo->image_width,
  137. (long) (cinfo->max_h_samp_factor*DCTSIZE));
  138. cinfo->MCU_rows_in_scan = (JDIMENSION)
  139. jdiv_round_up((long) cinfo->image_height,
  140. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  141. cinfo->blocks_in_MCU = 0;
  142. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  143. compptr = cinfo->cur_comp_info[ci];
  144. /* Sampling factors give # of blocks of component in each MCU */
  145. compptr->MCU_width = compptr->h_samp_factor;
  146. compptr->MCU_height = compptr->v_samp_factor;
  147. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  148. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  149. /* Figure number of non-dummy blocks in last MCU column & row */
  150. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  151. if (tmp == 0) tmp = compptr->MCU_width;
  152. compptr->last_col_width = tmp;
  153. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  154. if (tmp == 0) tmp = compptr->MCU_height;
  155. compptr->last_row_height = tmp;
  156. /* Prepare array describing MCU composition */
  157. mcublks = compptr->MCU_blocks;
  158. if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  159. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  160. while (mcublks-- > 0) {
  161. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  162. }
  163. }
  164. }
  165. }
  166. /*
  167. * Save away a copy of the Q-table referenced by each component present
  168. * in the current scan, unless already saved during a prior scan.
  169. *
  170. * In a multiple-scan JPEG file, the encoder could assign different components
  171. * the same Q-table slot number, but change table definitions between scans
  172. * so that each component uses a different Q-table. (The IJG encoder is not
  173. * currently capable of doing this, but other encoders might.) Since we want
  174. * to be able to dequantize all the components at the end of the file, this
  175. * means that we have to save away the table actually used for each component.
  176. * We do this by copying the table at the start of the first scan containing
  177. * the component.
  178. * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  179. * slot between scans of a component using that slot. If the encoder does so
  180. * anyway, this decoder will simply use the Q-table values that were current
  181. * at the start of the first scan for the component.
  182. *
  183. * The decompressor output side looks only at the saved quant tables,
  184. * not at the current Q-table slots.
  185. */
  186. LOCAL(void)
  187. latch_quant_tables (j_decompress_ptr cinfo)
  188. {
  189. int ci, qtblno;
  190. jpeg_component_info *compptr;
  191. JQUANT_TBL * qtbl;
  192. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  193. compptr = cinfo->cur_comp_info[ci];
  194. /* No work if we already saved Q-table for this component */
  195. if (compptr->quant_table != NULL)
  196. continue;
  197. /* Make sure specified quantization table is present */
  198. qtblno = compptr->quant_tbl_no;
  199. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  200. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  201. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  202. /* OK, save away the quantization table */
  203. qtbl = (JQUANT_TBL *)
  204. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  205. SIZEOF(JQUANT_TBL));
  206. MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  207. compptr->quant_table = qtbl;
  208. }
  209. }
  210. /*
  211. * Initialize the input modules to read a scan of compressed data.
  212. * The first call to this is done by jdmaster.c after initializing
  213. * the entire decompressor (during jpeg_start_decompress).
  214. * Subsequent calls come from consume_markers, below.
  215. */
  216. METHODDEF(void)
  217. start_input_pass (j_decompress_ptr cinfo)
  218. {
  219. per_scan_setup(cinfo);
  220. latch_quant_tables(cinfo);
  221. (*cinfo->entropy->start_pass) (cinfo);
  222. (*cinfo->coef->start_input_pass) (cinfo);
  223. cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  224. }
  225. /*
  226. * Finish up after inputting a compressed-data scan.
  227. * This is called by the coefficient controller after it's read all
  228. * the expected data of the scan.
  229. */
  230. METHODDEF(void)
  231. finish_input_pass (j_decompress_ptr cinfo)
  232. {
  233. cinfo->inputctl->consume_input = consume_markers;
  234. }
  235. /*
  236. * Read JPEG markers before, between, or after compressed-data scans.
  237. * Change state as necessary when a new scan is reached.
  238. * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  239. *
  240. * The consume_input method pointer points either here or to the
  241. * coefficient controller's consume_data routine, depending on whether
  242. * we are reading a compressed data segment or inter-segment markers.
  243. */
  244. METHODDEF(int)
  245. consume_markers (j_decompress_ptr cinfo)
  246. {
  247. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  248. int val;
  249. if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  250. return JPEG_REACHED_EOI;
  251. val = (*cinfo->marker->read_markers) (cinfo);
  252. switch (val) {
  253. case JPEG_REACHED_SOS: /* Found SOS */
  254. if (inputctl->inheaders) { /* 1st SOS */
  255. initial_setup(cinfo);
  256. inputctl->inheaders = FALSE;
  257. /* Note: start_input_pass must be called by jdmaster.c
  258. * before any more input can be consumed. jdapi.c is
  259. * responsible for enforcing this sequencing.
  260. */
  261. } else { /* 2nd or later SOS marker */
  262. if (! inputctl->pub.has_multiple_scans)
  263. ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  264. start_input_pass(cinfo);
  265. }
  266. break;
  267. case JPEG_REACHED_EOI: /* Found EOI */
  268. inputctl->pub.eoi_reached = TRUE;
  269. if (inputctl->inheaders) { /* Tables-only datastream, apparently */
  270. if (cinfo->marker->saw_SOF)
  271. ERREXIT(cinfo, JERR_SOF_NO_SOS);
  272. } else {
  273. /* Prevent infinite loop in coef ctlr's decompress_data routine
  274. * if user set output_scan_number larger than number of scans.
  275. */
  276. if (cinfo->output_scan_number > cinfo->input_scan_number)
  277. cinfo->output_scan_number = cinfo->input_scan_number;
  278. }
  279. break;
  280. case JPEG_SUSPENDED:
  281. break;
  282. }
  283. return val;
  284. }
  285. /*
  286. * Reset state to begin a fresh datastream.
  287. */
  288. METHODDEF(void)
  289. reset_input_controller (j_decompress_ptr cinfo)
  290. {
  291. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  292. inputctl->pub.consume_input = consume_markers;
  293. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  294. inputctl->pub.eoi_reached = FALSE;
  295. inputctl->inheaders = TRUE;
  296. /* Reset other modules */
  297. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  298. (*cinfo->marker->reset_marker_reader) (cinfo);
  299. /* Reset progression state -- would be cleaner if entropy decoder did this */
  300. cinfo->coef_bits = NULL;
  301. }
  302. /*
  303. * Initialize the input controller module.
  304. * This is called only once, when the decompression object is created.
  305. */
  306. GLOBAL(void)
  307. jinit_input_controller (j_decompress_ptr cinfo)
  308. {
  309. my_inputctl_ptr inputctl;
  310. /* Create subobject in permanent pool */
  311. inputctl = (my_inputctl_ptr)
  312. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  313. SIZEOF(my_input_controller));
  314. cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  315. /* Initialize method pointers */
  316. inputctl->pub.consume_input = consume_markers;
  317. inputctl->pub.reset_input_controller = reset_input_controller;
  318. inputctl->pub.start_input_pass = start_input_pass;
  319. inputctl->pub.finish_input_pass = finish_input_pass;
  320. /* Initialize state: can't use reset_input_controller since we don't
  321. * want to try to reset other modules yet.
  322. */
  323. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  324. inputctl->pub.eoi_reached = FALSE;
  325. inputctl->inheaders = TRUE;
  326. }