jdinput.cpp 15 KB

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