jdinput.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * jdinput.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2002-2020 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 input control logic for the JPEG decompressor.
  10. * These routines are concerned with controlling the decompressor's input
  11. * processing (marker reading and coefficient decoding). The actual input
  12. * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Private state */
  18. typedef struct {
  19. struct jpeg_input_controller pub; /* public fields */
  20. int inheaders; /* Nonzero until first SOS is reached */
  21. } my_input_controller;
  22. typedef my_input_controller * my_inputctl_ptr;
  23. /* Forward declarations */
  24. METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
  25. /*
  26. * Routines to calculate various quantities related to the size of the image.
  27. */
  28. /*
  29. * Compute output image dimensions and related values.
  30. * NOTE: this is exported for possible use by application.
  31. * Hence it mustn't do anything that can't be done twice.
  32. */
  33. GLOBAL(void)
  34. jpeg_core_output_dimensions (j_decompress_ptr cinfo)
  35. /* Do computations that are needed before master selection phase.
  36. * This function is used for transcoding and full decompression.
  37. */
  38. {
  39. #ifdef IDCT_SCALING_SUPPORTED
  40. int ci;
  41. jpeg_component_info *compptr;
  42. /* Compute actual output image dimensions and DCT scaling choices. */
  43. if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
  44. /* Provide 1/block_size scaling */
  45. cinfo->output_width = (JDIMENSION)
  46. jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
  47. cinfo->output_height = (JDIMENSION)
  48. jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
  49. cinfo->min_DCT_h_scaled_size = 1;
  50. cinfo->min_DCT_v_scaled_size = 1;
  51. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
  52. /* Provide 2/block_size scaling */
  53. cinfo->output_width = (JDIMENSION)
  54. jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
  55. cinfo->output_height = (JDIMENSION)
  56. jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
  57. cinfo->min_DCT_h_scaled_size = 2;
  58. cinfo->min_DCT_v_scaled_size = 2;
  59. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) {
  60. /* Provide 3/block_size scaling */
  61. cinfo->output_width = (JDIMENSION)
  62. jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);
  63. cinfo->output_height = (JDIMENSION)
  64. jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);
  65. cinfo->min_DCT_h_scaled_size = 3;
  66. cinfo->min_DCT_v_scaled_size = 3;
  67. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
  68. /* Provide 4/block_size scaling */
  69. cinfo->output_width = (JDIMENSION)
  70. jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
  71. cinfo->output_height = (JDIMENSION)
  72. jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
  73. cinfo->min_DCT_h_scaled_size = 4;
  74. cinfo->min_DCT_v_scaled_size = 4;
  75. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) {
  76. /* Provide 5/block_size scaling */
  77. cinfo->output_width = (JDIMENSION)
  78. jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);
  79. cinfo->output_height = (JDIMENSION)
  80. jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);
  81. cinfo->min_DCT_h_scaled_size = 5;
  82. cinfo->min_DCT_v_scaled_size = 5;
  83. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) {
  84. /* Provide 6/block_size scaling */
  85. cinfo->output_width = (JDIMENSION)
  86. jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);
  87. cinfo->output_height = (JDIMENSION)
  88. jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);
  89. cinfo->min_DCT_h_scaled_size = 6;
  90. cinfo->min_DCT_v_scaled_size = 6;
  91. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) {
  92. /* Provide 7/block_size scaling */
  93. cinfo->output_width = (JDIMENSION)
  94. jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);
  95. cinfo->output_height = (JDIMENSION)
  96. jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);
  97. cinfo->min_DCT_h_scaled_size = 7;
  98. cinfo->min_DCT_v_scaled_size = 7;
  99. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
  100. /* Provide 8/block_size scaling */
  101. cinfo->output_width = (JDIMENSION)
  102. jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
  103. cinfo->output_height = (JDIMENSION)
  104. jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
  105. cinfo->min_DCT_h_scaled_size = 8;
  106. cinfo->min_DCT_v_scaled_size = 8;
  107. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) {
  108. /* Provide 9/block_size scaling */
  109. cinfo->output_width = (JDIMENSION)
  110. jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);
  111. cinfo->output_height = (JDIMENSION)
  112. jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);
  113. cinfo->min_DCT_h_scaled_size = 9;
  114. cinfo->min_DCT_v_scaled_size = 9;
  115. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) {
  116. /* Provide 10/block_size scaling */
  117. cinfo->output_width = (JDIMENSION)
  118. jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);
  119. cinfo->output_height = (JDIMENSION)
  120. jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);
  121. cinfo->min_DCT_h_scaled_size = 10;
  122. cinfo->min_DCT_v_scaled_size = 10;
  123. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) {
  124. /* Provide 11/block_size scaling */
  125. cinfo->output_width = (JDIMENSION)
  126. jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);
  127. cinfo->output_height = (JDIMENSION)
  128. jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);
  129. cinfo->min_DCT_h_scaled_size = 11;
  130. cinfo->min_DCT_v_scaled_size = 11;
  131. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) {
  132. /* Provide 12/block_size scaling */
  133. cinfo->output_width = (JDIMENSION)
  134. jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);
  135. cinfo->output_height = (JDIMENSION)
  136. jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);
  137. cinfo->min_DCT_h_scaled_size = 12;
  138. cinfo->min_DCT_v_scaled_size = 12;
  139. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) {
  140. /* Provide 13/block_size scaling */
  141. cinfo->output_width = (JDIMENSION)
  142. jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);
  143. cinfo->output_height = (JDIMENSION)
  144. jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);
  145. cinfo->min_DCT_h_scaled_size = 13;
  146. cinfo->min_DCT_v_scaled_size = 13;
  147. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) {
  148. /* Provide 14/block_size scaling */
  149. cinfo->output_width = (JDIMENSION)
  150. jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);
  151. cinfo->output_height = (JDIMENSION)
  152. jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);
  153. cinfo->min_DCT_h_scaled_size = 14;
  154. cinfo->min_DCT_v_scaled_size = 14;
  155. } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) {
  156. /* Provide 15/block_size scaling */
  157. cinfo->output_width = (JDIMENSION)
  158. jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);
  159. cinfo->output_height = (JDIMENSION)
  160. jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);
  161. cinfo->min_DCT_h_scaled_size = 15;
  162. cinfo->min_DCT_v_scaled_size = 15;
  163. } else {
  164. /* Provide 16/block_size scaling */
  165. cinfo->output_width = (JDIMENSION)
  166. jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);
  167. cinfo->output_height = (JDIMENSION)
  168. jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);
  169. cinfo->min_DCT_h_scaled_size = 16;
  170. cinfo->min_DCT_v_scaled_size = 16;
  171. }
  172. /* Recompute dimensions of components */
  173. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  174. ci++, compptr++) {
  175. compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
  176. compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
  177. }
  178. #else /* !IDCT_SCALING_SUPPORTED */
  179. /* Hardwire it to "no scaling" */
  180. cinfo->output_width = cinfo->image_width;
  181. cinfo->output_height = cinfo->image_height;
  182. /* initial_setup has already initialized DCT_scaled_size,
  183. * and has computed unscaled downsampled_width and downsampled_height.
  184. */
  185. #endif /* IDCT_SCALING_SUPPORTED */
  186. }
  187. LOCAL(void)
  188. initial_setup (j_decompress_ptr cinfo)
  189. /* Called once, when first SOS marker is reached */
  190. {
  191. int ci;
  192. jpeg_component_info *compptr;
  193. /* Make sure image isn't bigger than I can handle */
  194. if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  195. (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  196. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  197. /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
  198. if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
  199. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  200. /* Check that number of components won't exceed internal array sizes */
  201. if (cinfo->num_components > MAX_COMPONENTS)
  202. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  203. MAX_COMPONENTS);
  204. /* Compute maximum sampling factors; check factor validity */
  205. cinfo->max_h_samp_factor = 1;
  206. cinfo->max_v_samp_factor = 1;
  207. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  208. ci++, compptr++) {
  209. if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  210. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  211. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  212. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  213. compptr->h_samp_factor);
  214. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  215. compptr->v_samp_factor);
  216. }
  217. /* Derive block_size, natural_order, and lim_Se */
  218. if (cinfo->is_baseline || (cinfo->progressive_mode &&
  219. cinfo->comps_in_scan)) { /* no pseudo SOS marker */
  220. cinfo->block_size = DCTSIZE;
  221. cinfo->natural_order = jpeg_natural_order;
  222. cinfo->lim_Se = DCTSIZE2-1;
  223. } else
  224. switch (cinfo->Se) {
  225. case (1*1-1):
  226. cinfo->block_size = 1;
  227. cinfo->natural_order = jpeg_natural_order; /* not needed */
  228. cinfo->lim_Se = cinfo->Se;
  229. break;
  230. case (2*2-1):
  231. cinfo->block_size = 2;
  232. cinfo->natural_order = jpeg_natural_order2;
  233. cinfo->lim_Se = cinfo->Se;
  234. break;
  235. case (3*3-1):
  236. cinfo->block_size = 3;
  237. cinfo->natural_order = jpeg_natural_order3;
  238. cinfo->lim_Se = cinfo->Se;
  239. break;
  240. case (4*4-1):
  241. cinfo->block_size = 4;
  242. cinfo->natural_order = jpeg_natural_order4;
  243. cinfo->lim_Se = cinfo->Se;
  244. break;
  245. case (5*5-1):
  246. cinfo->block_size = 5;
  247. cinfo->natural_order = jpeg_natural_order5;
  248. cinfo->lim_Se = cinfo->Se;
  249. break;
  250. case (6*6-1):
  251. cinfo->block_size = 6;
  252. cinfo->natural_order = jpeg_natural_order6;
  253. cinfo->lim_Se = cinfo->Se;
  254. break;
  255. case (7*7-1):
  256. cinfo->block_size = 7;
  257. cinfo->natural_order = jpeg_natural_order7;
  258. cinfo->lim_Se = cinfo->Se;
  259. break;
  260. case (8*8-1):
  261. cinfo->block_size = 8;
  262. cinfo->natural_order = jpeg_natural_order;
  263. cinfo->lim_Se = DCTSIZE2-1;
  264. break;
  265. case (9*9-1):
  266. cinfo->block_size = 9;
  267. cinfo->natural_order = jpeg_natural_order;
  268. cinfo->lim_Se = DCTSIZE2-1;
  269. break;
  270. case (10*10-1):
  271. cinfo->block_size = 10;
  272. cinfo->natural_order = jpeg_natural_order;
  273. cinfo->lim_Se = DCTSIZE2-1;
  274. break;
  275. case (11*11-1):
  276. cinfo->block_size = 11;
  277. cinfo->natural_order = jpeg_natural_order;
  278. cinfo->lim_Se = DCTSIZE2-1;
  279. break;
  280. case (12*12-1):
  281. cinfo->block_size = 12;
  282. cinfo->natural_order = jpeg_natural_order;
  283. cinfo->lim_Se = DCTSIZE2-1;
  284. break;
  285. case (13*13-1):
  286. cinfo->block_size = 13;
  287. cinfo->natural_order = jpeg_natural_order;
  288. cinfo->lim_Se = DCTSIZE2-1;
  289. break;
  290. case (14*14-1):
  291. cinfo->block_size = 14;
  292. cinfo->natural_order = jpeg_natural_order;
  293. cinfo->lim_Se = DCTSIZE2-1;
  294. break;
  295. case (15*15-1):
  296. cinfo->block_size = 15;
  297. cinfo->natural_order = jpeg_natural_order;
  298. cinfo->lim_Se = DCTSIZE2-1;
  299. break;
  300. case (16*16-1):
  301. cinfo->block_size = 16;
  302. cinfo->natural_order = jpeg_natural_order;
  303. cinfo->lim_Se = DCTSIZE2-1;
  304. break;
  305. default:
  306. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  307. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  308. }
  309. /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
  310. * In the full decompressor,
  311. * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
  312. * but in the transcoder,
  313. * jpeg_calc_output_dimensions is not used, so we must do it here.
  314. */
  315. cinfo->min_DCT_h_scaled_size = cinfo->block_size;
  316. cinfo->min_DCT_v_scaled_size = cinfo->block_size;
  317. /* Compute dimensions of components */
  318. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  319. ci++, compptr++) {
  320. compptr->DCT_h_scaled_size = cinfo->block_size;
  321. compptr->DCT_v_scaled_size = cinfo->block_size;
  322. /* Size in DCT blocks */
  323. compptr->width_in_blocks = (JDIMENSION)
  324. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  325. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  326. compptr->height_in_blocks = (JDIMENSION)
  327. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  328. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  329. /* downsampled_width and downsampled_height will also be overridden by
  330. * jdmaster.c if we are doing full decompression. The transcoder library
  331. * doesn't use these values, but the calling application might.
  332. */
  333. /* Size in samples */
  334. compptr->downsampled_width = (JDIMENSION)
  335. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  336. (long) cinfo->max_h_samp_factor);
  337. compptr->downsampled_height = (JDIMENSION)
  338. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  339. (long) cinfo->max_v_samp_factor);
  340. /* Mark component needed, until color conversion says otherwise */
  341. compptr->component_needed = TRUE;
  342. /* Mark no quantization table yet saved for component */
  343. compptr->quant_table = NULL;
  344. }
  345. /* Compute number of fully interleaved MCU rows. */
  346. cinfo->total_iMCU_rows = (JDIMENSION)
  347. jdiv_round_up((long) cinfo->image_height,
  348. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  349. /* Decide whether file contains multiple scans */
  350. if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  351. cinfo->inputctl->has_multiple_scans = TRUE;
  352. else
  353. cinfo->inputctl->has_multiple_scans = FALSE;
  354. }
  355. LOCAL(void)
  356. per_scan_setup (j_decompress_ptr cinfo)
  357. /* Do computations that are needed before processing a JPEG scan */
  358. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  359. {
  360. int ci, mcublks, tmp;
  361. jpeg_component_info *compptr;
  362. if (cinfo->comps_in_scan == 1) {
  363. /* Noninterleaved (single-component) scan */
  364. compptr = cinfo->cur_comp_info[0];
  365. /* Overall image size in MCUs */
  366. cinfo->MCUs_per_row = compptr->width_in_blocks;
  367. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  368. /* For noninterleaved scan, always one block per MCU */
  369. compptr->MCU_width = 1;
  370. compptr->MCU_height = 1;
  371. compptr->MCU_blocks = 1;
  372. compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
  373. compptr->last_col_width = 1;
  374. /* For noninterleaved scans, it is convenient to define last_row_height
  375. * as the number of block rows present in the last iMCU row.
  376. */
  377. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  378. if (tmp == 0) tmp = compptr->v_samp_factor;
  379. compptr->last_row_height = tmp;
  380. /* Prepare array describing MCU composition */
  381. cinfo->blocks_in_MCU = 1;
  382. cinfo->MCU_membership[0] = 0;
  383. } else {
  384. /* Interleaved (multi-component) scan */
  385. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  386. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  387. MAX_COMPS_IN_SCAN);
  388. /* Overall image size in MCUs */
  389. cinfo->MCUs_per_row = (JDIMENSION)
  390. jdiv_round_up((long) cinfo->image_width,
  391. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  392. cinfo->MCU_rows_in_scan = cinfo->total_iMCU_rows;
  393. cinfo->blocks_in_MCU = 0;
  394. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  395. compptr = cinfo->cur_comp_info[ci];
  396. /* Sampling factors give # of blocks of component in each MCU */
  397. compptr->MCU_width = compptr->h_samp_factor;
  398. compptr->MCU_height = compptr->v_samp_factor;
  399. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  400. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
  401. /* Figure number of non-dummy blocks in last MCU column & row */
  402. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  403. if (tmp == 0) tmp = compptr->MCU_width;
  404. compptr->last_col_width = tmp;
  405. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  406. if (tmp == 0) tmp = compptr->MCU_height;
  407. compptr->last_row_height = tmp;
  408. /* Prepare array describing MCU composition */
  409. mcublks = compptr->MCU_blocks;
  410. if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  411. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  412. while (mcublks-- > 0) {
  413. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  414. }
  415. }
  416. }
  417. }
  418. /*
  419. * Save away a copy of the Q-table referenced by each component present
  420. * in the current scan, unless already saved during a prior scan.
  421. *
  422. * In a multiple-scan JPEG file, the encoder could assign different components
  423. * the same Q-table slot number, but change table definitions between scans
  424. * so that each component uses a different Q-table. (The IJG encoder is not
  425. * currently capable of doing this, but other encoders might.) Since we want
  426. * to be able to dequantize all the components at the end of the file, this
  427. * means that we have to save away the table actually used for each component.
  428. * We do this by copying the table at the start of the first scan containing
  429. * the component.
  430. * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  431. * slot between scans of a component using that slot. If the encoder does so
  432. * anyway, this decoder will simply use the Q-table values that were current
  433. * at the start of the first scan for the component.
  434. *
  435. * The decompressor output side looks only at the saved quant tables,
  436. * not at the current Q-table slots.
  437. */
  438. LOCAL(void)
  439. latch_quant_tables (j_decompress_ptr cinfo)
  440. {
  441. int ci, qtblno;
  442. jpeg_component_info *compptr;
  443. JQUANT_TBL * qtbl;
  444. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  445. compptr = cinfo->cur_comp_info[ci];
  446. /* No work if we already saved Q-table for this component */
  447. if (compptr->quant_table != NULL)
  448. continue;
  449. /* Make sure specified quantization table is present */
  450. qtblno = compptr->quant_tbl_no;
  451. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  452. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  453. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  454. /* OK, save away the quantization table */
  455. qtbl = (JQUANT_TBL *) (*cinfo->mem->alloc_small)
  456. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(JQUANT_TBL));
  457. MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  458. compptr->quant_table = qtbl;
  459. }
  460. }
  461. /*
  462. * Initialize the input modules to read a scan of compressed data.
  463. * The first call to this is done by jdmaster.c after initializing
  464. * the entire decompressor (during jpeg_start_decompress).
  465. * Subsequent calls come from consume_markers, below.
  466. */
  467. METHODDEF(void)
  468. start_input_pass (j_decompress_ptr cinfo)
  469. {
  470. per_scan_setup(cinfo);
  471. latch_quant_tables(cinfo);
  472. (*cinfo->entropy->start_pass) (cinfo);
  473. (*cinfo->coef->start_input_pass) (cinfo);
  474. cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  475. }
  476. /*
  477. * Finish up after inputting a compressed-data scan.
  478. * This is called by the coefficient controller after it's read all
  479. * the expected data of the scan.
  480. */
  481. METHODDEF(void)
  482. finish_input_pass (j_decompress_ptr cinfo)
  483. {
  484. (*cinfo->entropy->finish_pass) (cinfo);
  485. cinfo->inputctl->consume_input = consume_markers;
  486. }
  487. /*
  488. * Read JPEG markers before, between, or after compressed-data scans.
  489. * Change state as necessary when a new scan is reached.
  490. * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  491. *
  492. * The consume_input method pointer points either here or to the
  493. * coefficient controller's consume_data routine, depending on whether
  494. * we are reading a compressed data segment or inter-segment markers.
  495. *
  496. * Note: This function should NOT return a pseudo SOS marker (with zero
  497. * component number) to the caller. A pseudo marker received by
  498. * read_markers is processed and then skipped for other markers.
  499. */
  500. METHODDEF(int)
  501. consume_markers (j_decompress_ptr cinfo)
  502. {
  503. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  504. int val;
  505. if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  506. return JPEG_REACHED_EOI;
  507. for (;;) { /* Loop to pass pseudo SOS marker */
  508. val = (*cinfo->marker->read_markers) (cinfo);
  509. switch (val) {
  510. case JPEG_REACHED_SOS: /* Found SOS */
  511. if (inputctl->inheaders) { /* 1st SOS */
  512. if (inputctl->inheaders == 1)
  513. initial_setup(cinfo);
  514. if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
  515. inputctl->inheaders = 2;
  516. break;
  517. }
  518. inputctl->inheaders = 0;
  519. /* Note: start_input_pass must be called by jdmaster.c
  520. * before any more input can be consumed. jdapimin.c is
  521. * responsible for enforcing this sequencing.
  522. */
  523. } else { /* 2nd or later SOS marker */
  524. if (! inputctl->pub.has_multiple_scans)
  525. ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  526. if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
  527. break;
  528. start_input_pass(cinfo);
  529. }
  530. return val;
  531. case JPEG_REACHED_EOI: /* Found EOI */
  532. inputctl->pub.eoi_reached = TRUE;
  533. if (inputctl->inheaders) { /* Tables-only datastream, apparently */
  534. if (cinfo->marker->saw_SOF)
  535. ERREXIT(cinfo, JERR_SOF_NO_SOS);
  536. } else {
  537. /* Prevent infinite loop in coef ctlr's decompress_data routine
  538. * if user set output_scan_number larger than number of scans.
  539. */
  540. if (cinfo->output_scan_number > cinfo->input_scan_number)
  541. cinfo->output_scan_number = cinfo->input_scan_number;
  542. }
  543. return val;
  544. case JPEG_SUSPENDED:
  545. return val;
  546. default:
  547. return val;
  548. }
  549. }
  550. }
  551. /*
  552. * Reset state to begin a fresh datastream.
  553. */
  554. METHODDEF(void)
  555. reset_input_controller (j_decompress_ptr cinfo)
  556. {
  557. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  558. inputctl->pub.consume_input = consume_markers;
  559. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  560. inputctl->pub.eoi_reached = FALSE;
  561. inputctl->inheaders = 1;
  562. /* Reset other modules */
  563. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  564. (*cinfo->marker->reset_marker_reader) (cinfo);
  565. /* Reset progression state -- would be cleaner if entropy decoder did this */
  566. cinfo->coef_bits = NULL;
  567. }
  568. /*
  569. * Initialize the input controller module.
  570. * This is called only once, when the decompression object is created.
  571. */
  572. GLOBAL(void)
  573. jinit_input_controller (j_decompress_ptr cinfo)
  574. {
  575. my_inputctl_ptr inputctl;
  576. /* Create subobject in permanent pool */
  577. inputctl = (my_inputctl_ptr) (*cinfo->mem->alloc_small)
  578. ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_input_controller));
  579. cinfo->inputctl = &inputctl->pub;
  580. /* Initialize method pointers */
  581. inputctl->pub.consume_input = consume_markers;
  582. inputctl->pub.reset_input_controller = reset_input_controller;
  583. inputctl->pub.start_input_pass = start_input_pass;
  584. inputctl->pub.finish_input_pass = finish_input_pass;
  585. /* Initialize state: can't use reset_input_controller since we don't
  586. * want to try to reset other modules yet.
  587. */
  588. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  589. inputctl->pub.eoi_reached = FALSE;
  590. inputctl->inheaders = 1;
  591. }