jdinput.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * jdinput.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2002-2013 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. break;
  309. }
  310. /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
  311. * In the full decompressor,
  312. * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
  313. * but in the transcoder,
  314. * jpeg_calc_output_dimensions is not used, so we must do it here.
  315. */
  316. cinfo->min_DCT_h_scaled_size = cinfo->block_size;
  317. cinfo->min_DCT_v_scaled_size = cinfo->block_size;
  318. /* Compute dimensions of components */
  319. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  320. ci++, compptr++) {
  321. compptr->DCT_h_scaled_size = cinfo->block_size;
  322. compptr->DCT_v_scaled_size = cinfo->block_size;
  323. /* Size in DCT blocks */
  324. compptr->width_in_blocks = (JDIMENSION)
  325. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  326. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  327. compptr->height_in_blocks = (JDIMENSION)
  328. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  329. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  330. /* downsampled_width and downsampled_height will also be overridden by
  331. * jdmaster.c if we are doing full decompression. The transcoder library
  332. * doesn't use these values, but the calling application might.
  333. */
  334. /* Size in samples */
  335. compptr->downsampled_width = (JDIMENSION)
  336. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  337. (long) cinfo->max_h_samp_factor);
  338. compptr->downsampled_height = (JDIMENSION)
  339. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  340. (long) cinfo->max_v_samp_factor);
  341. /* Mark component needed, until color conversion says otherwise */
  342. compptr->component_needed = TRUE;
  343. /* Mark no quantization table yet saved for component */
  344. compptr->quant_table = NULL;
  345. }
  346. /* Compute number of fully interleaved MCU rows. */
  347. cinfo->total_iMCU_rows = (JDIMENSION)
  348. jdiv_round_up((long) cinfo->image_height,
  349. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  350. /* Decide whether file contains multiple scans */
  351. if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  352. cinfo->inputctl->has_multiple_scans = TRUE;
  353. else
  354. cinfo->inputctl->has_multiple_scans = FALSE;
  355. }
  356. LOCAL(void)
  357. per_scan_setup (j_decompress_ptr cinfo)
  358. /* Do computations that are needed before processing a JPEG scan */
  359. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  360. {
  361. int ci, mcublks, tmp;
  362. jpeg_component_info *compptr;
  363. if (cinfo->comps_in_scan == 1) {
  364. /* Noninterleaved (single-component) scan */
  365. compptr = cinfo->cur_comp_info[0];
  366. /* Overall image size in MCUs */
  367. cinfo->MCUs_per_row = compptr->width_in_blocks;
  368. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  369. /* For noninterleaved scan, always one block per MCU */
  370. compptr->MCU_width = 1;
  371. compptr->MCU_height = 1;
  372. compptr->MCU_blocks = 1;
  373. compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
  374. compptr->last_col_width = 1;
  375. /* For noninterleaved scans, it is convenient to define last_row_height
  376. * as the number of block rows present in the last iMCU row.
  377. */
  378. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  379. if (tmp == 0) tmp = compptr->v_samp_factor;
  380. compptr->last_row_height = tmp;
  381. /* Prepare array describing MCU composition */
  382. cinfo->blocks_in_MCU = 1;
  383. cinfo->MCU_membership[0] = 0;
  384. } else {
  385. /* Interleaved (multi-component) scan */
  386. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  387. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  388. MAX_COMPS_IN_SCAN);
  389. /* Overall image size in MCUs */
  390. cinfo->MCUs_per_row = (JDIMENSION)
  391. jdiv_round_up((long) cinfo->image_width,
  392. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  393. cinfo->MCU_rows_in_scan = (JDIMENSION)
  394. jdiv_round_up((long) cinfo->image_height,
  395. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  396. cinfo->blocks_in_MCU = 0;
  397. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  398. compptr = cinfo->cur_comp_info[ci];
  399. /* Sampling factors give # of blocks of component in each MCU */
  400. compptr->MCU_width = compptr->h_samp_factor;
  401. compptr->MCU_height = compptr->v_samp_factor;
  402. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  403. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
  404. /* Figure number of non-dummy blocks in last MCU column & row */
  405. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  406. if (tmp == 0) tmp = compptr->MCU_width;
  407. compptr->last_col_width = tmp;
  408. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  409. if (tmp == 0) tmp = compptr->MCU_height;
  410. compptr->last_row_height = tmp;
  411. /* Prepare array describing MCU composition */
  412. mcublks = compptr->MCU_blocks;
  413. if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  414. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  415. while (mcublks-- > 0) {
  416. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  417. }
  418. }
  419. }
  420. }
  421. /*
  422. * Save away a copy of the Q-table referenced by each component present
  423. * in the current scan, unless already saved during a prior scan.
  424. *
  425. * In a multiple-scan JPEG file, the encoder could assign different components
  426. * the same Q-table slot number, but change table definitions between scans
  427. * so that each component uses a different Q-table. (The IJG encoder is not
  428. * currently capable of doing this, but other encoders might.) Since we want
  429. * to be able to dequantize all the components at the end of the file, this
  430. * means that we have to save away the table actually used for each component.
  431. * We do this by copying the table at the start of the first scan containing
  432. * the component.
  433. * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  434. * slot between scans of a component using that slot. If the encoder does so
  435. * anyway, this decoder will simply use the Q-table values that were current
  436. * at the start of the first scan for the component.
  437. *
  438. * The decompressor output side looks only at the saved quant tables,
  439. * not at the current Q-table slots.
  440. */
  441. LOCAL(void)
  442. latch_quant_tables (j_decompress_ptr cinfo)
  443. {
  444. int ci, qtblno;
  445. jpeg_component_info *compptr;
  446. JQUANT_TBL * qtbl;
  447. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  448. compptr = cinfo->cur_comp_info[ci];
  449. /* No work if we already saved Q-table for this component */
  450. if (compptr->quant_table != NULL)
  451. continue;
  452. /* Make sure specified quantization table is present */
  453. qtblno = compptr->quant_tbl_no;
  454. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  455. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  456. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  457. /* OK, save away the quantization table */
  458. qtbl = (JQUANT_TBL *)
  459. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  460. SIZEOF(JQUANT_TBL));
  461. MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  462. compptr->quant_table = qtbl;
  463. }
  464. }
  465. /*
  466. * Initialize the input modules to read a scan of compressed data.
  467. * The first call to this is done by jdmaster.c after initializing
  468. * the entire decompressor (during jpeg_start_decompress).
  469. * Subsequent calls come from consume_markers, below.
  470. */
  471. METHODDEF(void)
  472. start_input_pass (j_decompress_ptr cinfo)
  473. {
  474. per_scan_setup(cinfo);
  475. latch_quant_tables(cinfo);
  476. (*cinfo->entropy->start_pass) (cinfo);
  477. (*cinfo->coef->start_input_pass) (cinfo);
  478. cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  479. }
  480. /*
  481. * Finish up after inputting a compressed-data scan.
  482. * This is called by the coefficient controller after it's read all
  483. * the expected data of the scan.
  484. */
  485. METHODDEF(void)
  486. finish_input_pass (j_decompress_ptr cinfo)
  487. {
  488. (*cinfo->entropy->finish_pass) (cinfo);
  489. cinfo->inputctl->consume_input = consume_markers;
  490. }
  491. /*
  492. * Read JPEG markers before, between, or after compressed-data scans.
  493. * Change state as necessary when a new scan is reached.
  494. * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  495. *
  496. * The consume_input method pointer points either here or to the
  497. * coefficient controller's consume_data routine, depending on whether
  498. * we are reading a compressed data segment or inter-segment markers.
  499. *
  500. * Note: This function should NOT return a pseudo SOS marker (with zero
  501. * component number) to the caller. A pseudo marker received by
  502. * read_markers is processed and then skipped for other markers.
  503. */
  504. METHODDEF(int)
  505. consume_markers (j_decompress_ptr cinfo)
  506. {
  507. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  508. int val;
  509. if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  510. return JPEG_REACHED_EOI;
  511. for (;;) { /* Loop to pass pseudo SOS marker */
  512. val = (*cinfo->marker->read_markers) (cinfo);
  513. switch (val) {
  514. case JPEG_REACHED_SOS: /* Found SOS */
  515. if (inputctl->inheaders) { /* 1st SOS */
  516. if (inputctl->inheaders == 1)
  517. initial_setup(cinfo);
  518. if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
  519. inputctl->inheaders = 2;
  520. break;
  521. }
  522. inputctl->inheaders = 0;
  523. /* Note: start_input_pass must be called by jdmaster.c
  524. * before any more input can be consumed. jdapimin.c is
  525. * responsible for enforcing this sequencing.
  526. */
  527. } else { /* 2nd or later SOS marker */
  528. if (! inputctl->pub.has_multiple_scans)
  529. ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  530. if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
  531. break;
  532. start_input_pass(cinfo);
  533. }
  534. return val;
  535. case JPEG_REACHED_EOI: /* Found EOI */
  536. inputctl->pub.eoi_reached = TRUE;
  537. if (inputctl->inheaders) { /* Tables-only datastream, apparently */
  538. if (cinfo->marker->saw_SOF)
  539. ERREXIT(cinfo, JERR_SOF_NO_SOS);
  540. } else {
  541. /* Prevent infinite loop in coef ctlr's decompress_data routine
  542. * if user set output_scan_number larger than number of scans.
  543. */
  544. if (cinfo->output_scan_number > cinfo->input_scan_number)
  545. cinfo->output_scan_number = cinfo->input_scan_number;
  546. }
  547. return val;
  548. case JPEG_SUSPENDED:
  549. return val;
  550. default:
  551. return val;
  552. }
  553. }
  554. }
  555. /*
  556. * Reset state to begin a fresh datastream.
  557. */
  558. METHODDEF(void)
  559. reset_input_controller (j_decompress_ptr cinfo)
  560. {
  561. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  562. inputctl->pub.consume_input = consume_markers;
  563. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  564. inputctl->pub.eoi_reached = FALSE;
  565. inputctl->inheaders = 1;
  566. /* Reset other modules */
  567. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  568. (*cinfo->marker->reset_marker_reader) (cinfo);
  569. /* Reset progression state -- would be cleaner if entropy decoder did this */
  570. cinfo->coef_bits = NULL;
  571. }
  572. /*
  573. * Initialize the input controller module.
  574. * This is called only once, when the decompression object is created.
  575. */
  576. GLOBAL(void)
  577. jinit_input_controller (j_decompress_ptr cinfo)
  578. {
  579. my_inputctl_ptr inputctl;
  580. /* Create subobject in permanent pool */
  581. inputctl = (my_inputctl_ptr)
  582. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  583. SIZEOF(my_input_controller));
  584. cinfo->inputctl = &inputctl->pub;
  585. /* Initialize method pointers */
  586. inputctl->pub.consume_input = consume_markers;
  587. inputctl->pub.reset_input_controller = reset_input_controller;
  588. inputctl->pub.start_input_pass = start_input_pass;
  589. inputctl->pub.finish_input_pass = finish_input_pass;
  590. /* Initialize state: can't use reset_input_controller since we don't
  591. * want to try to reset other modules yet.
  592. */
  593. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  594. inputctl->pub.eoi_reached = FALSE;
  595. inputctl->inheaders = 1;
  596. }