jdmaster.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * jdmaster.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 2002-2009 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2009-2011, 2016, 2019, 2022, D. R. Commander.
  9. * Copyright (C) 2013, Linaro Limited.
  10. * Copyright (C) 2015, Google, Inc.
  11. * For conditions of distribution and use, see the accompanying README.ijg
  12. * file.
  13. *
  14. * This file contains master control logic for the JPEG decompressor.
  15. * These routines are concerned with selecting the modules to be executed
  16. * and with determining the number of passes and the work to be done in each
  17. * pass.
  18. */
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. #include "jpegcomp.h"
  23. #include "jdmaster.h"
  24. /*
  25. * Determine whether merged upsample/color conversion should be used.
  26. * CRUCIAL: this must match the actual capabilities of jdmerge.c!
  27. */
  28. LOCAL(boolean)
  29. use_merged_upsample(j_decompress_ptr cinfo)
  30. {
  31. #ifdef UPSAMPLE_MERGING_SUPPORTED
  32. /* Merging is the equivalent of plain box-filter upsampling */
  33. if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
  34. return FALSE;
  35. /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
  36. if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
  37. (cinfo->out_color_space != JCS_RGB &&
  38. cinfo->out_color_space != JCS_RGB565 &&
  39. cinfo->out_color_space != JCS_EXT_RGB &&
  40. cinfo->out_color_space != JCS_EXT_RGBX &&
  41. cinfo->out_color_space != JCS_EXT_BGR &&
  42. cinfo->out_color_space != JCS_EXT_BGRX &&
  43. cinfo->out_color_space != JCS_EXT_XBGR &&
  44. cinfo->out_color_space != JCS_EXT_XRGB &&
  45. cinfo->out_color_space != JCS_EXT_RGBA &&
  46. cinfo->out_color_space != JCS_EXT_BGRA &&
  47. cinfo->out_color_space != JCS_EXT_ABGR &&
  48. cinfo->out_color_space != JCS_EXT_ARGB))
  49. return FALSE;
  50. if ((cinfo->out_color_space == JCS_RGB565 &&
  51. cinfo->out_color_components != 3) ||
  52. (cinfo->out_color_space != JCS_RGB565 &&
  53. cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
  54. return FALSE;
  55. /* and it only handles 2h1v or 2h2v sampling ratios */
  56. if (cinfo->comp_info[0].h_samp_factor != 2 ||
  57. cinfo->comp_info[1].h_samp_factor != 1 ||
  58. cinfo->comp_info[2].h_samp_factor != 1 ||
  59. cinfo->comp_info[0].v_samp_factor > 2 ||
  60. cinfo->comp_info[1].v_samp_factor != 1 ||
  61. cinfo->comp_info[2].v_samp_factor != 1)
  62. return FALSE;
  63. /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  64. if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
  65. cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
  66. cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
  67. return FALSE;
  68. /* ??? also need to test for upsample-time rescaling, when & if supported */
  69. return TRUE; /* by golly, it'll work... */
  70. #else
  71. return FALSE;
  72. #endif
  73. }
  74. /*
  75. * Compute output image dimensions and related values.
  76. * NOTE: this is exported for possible use by application.
  77. * Hence it mustn't do anything that can't be done twice.
  78. */
  79. #if JPEG_LIB_VERSION >= 80
  80. GLOBAL(void)
  81. #else
  82. LOCAL(void)
  83. #endif
  84. jpeg_core_output_dimensions(j_decompress_ptr cinfo)
  85. /* Do computations that are needed before master selection phase.
  86. * This function is used for transcoding and full decompression.
  87. */
  88. {
  89. #ifdef IDCT_SCALING_SUPPORTED
  90. int ci;
  91. jpeg_component_info *compptr;
  92. /* Compute actual output image dimensions and DCT scaling choices. */
  93. if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
  94. /* Provide 1/block_size scaling */
  95. cinfo->output_width = (JDIMENSION)
  96. jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
  97. cinfo->output_height = (JDIMENSION)
  98. jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
  99. cinfo->_min_DCT_h_scaled_size = 1;
  100. cinfo->_min_DCT_v_scaled_size = 1;
  101. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
  102. /* Provide 2/block_size scaling */
  103. cinfo->output_width = (JDIMENSION)
  104. jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
  105. cinfo->output_height = (JDIMENSION)
  106. jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
  107. cinfo->_min_DCT_h_scaled_size = 2;
  108. cinfo->_min_DCT_v_scaled_size = 2;
  109. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
  110. /* Provide 3/block_size scaling */
  111. cinfo->output_width = (JDIMENSION)
  112. jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
  113. cinfo->output_height = (JDIMENSION)
  114. jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
  115. cinfo->_min_DCT_h_scaled_size = 3;
  116. cinfo->_min_DCT_v_scaled_size = 3;
  117. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
  118. /* Provide 4/block_size scaling */
  119. cinfo->output_width = (JDIMENSION)
  120. jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
  121. cinfo->output_height = (JDIMENSION)
  122. jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
  123. cinfo->_min_DCT_h_scaled_size = 4;
  124. cinfo->_min_DCT_v_scaled_size = 4;
  125. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
  126. /* Provide 5/block_size scaling */
  127. cinfo->output_width = (JDIMENSION)
  128. jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
  129. cinfo->output_height = (JDIMENSION)
  130. jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
  131. cinfo->_min_DCT_h_scaled_size = 5;
  132. cinfo->_min_DCT_v_scaled_size = 5;
  133. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
  134. /* Provide 6/block_size scaling */
  135. cinfo->output_width = (JDIMENSION)
  136. jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
  137. cinfo->output_height = (JDIMENSION)
  138. jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
  139. cinfo->_min_DCT_h_scaled_size = 6;
  140. cinfo->_min_DCT_v_scaled_size = 6;
  141. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
  142. /* Provide 7/block_size scaling */
  143. cinfo->output_width = (JDIMENSION)
  144. jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
  145. cinfo->output_height = (JDIMENSION)
  146. jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
  147. cinfo->_min_DCT_h_scaled_size = 7;
  148. cinfo->_min_DCT_v_scaled_size = 7;
  149. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
  150. /* Provide 8/block_size scaling */
  151. cinfo->output_width = (JDIMENSION)
  152. jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
  153. cinfo->output_height = (JDIMENSION)
  154. jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
  155. cinfo->_min_DCT_h_scaled_size = 8;
  156. cinfo->_min_DCT_v_scaled_size = 8;
  157. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
  158. /* Provide 9/block_size scaling */
  159. cinfo->output_width = (JDIMENSION)
  160. jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
  161. cinfo->output_height = (JDIMENSION)
  162. jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
  163. cinfo->_min_DCT_h_scaled_size = 9;
  164. cinfo->_min_DCT_v_scaled_size = 9;
  165. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
  166. /* Provide 10/block_size scaling */
  167. cinfo->output_width = (JDIMENSION)
  168. jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
  169. cinfo->output_height = (JDIMENSION)
  170. jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
  171. cinfo->_min_DCT_h_scaled_size = 10;
  172. cinfo->_min_DCT_v_scaled_size = 10;
  173. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
  174. /* Provide 11/block_size scaling */
  175. cinfo->output_width = (JDIMENSION)
  176. jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
  177. cinfo->output_height = (JDIMENSION)
  178. jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
  179. cinfo->_min_DCT_h_scaled_size = 11;
  180. cinfo->_min_DCT_v_scaled_size = 11;
  181. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
  182. /* Provide 12/block_size scaling */
  183. cinfo->output_width = (JDIMENSION)
  184. jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
  185. cinfo->output_height = (JDIMENSION)
  186. jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
  187. cinfo->_min_DCT_h_scaled_size = 12;
  188. cinfo->_min_DCT_v_scaled_size = 12;
  189. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
  190. /* Provide 13/block_size scaling */
  191. cinfo->output_width = (JDIMENSION)
  192. jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
  193. cinfo->output_height = (JDIMENSION)
  194. jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
  195. cinfo->_min_DCT_h_scaled_size = 13;
  196. cinfo->_min_DCT_v_scaled_size = 13;
  197. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
  198. /* Provide 14/block_size scaling */
  199. cinfo->output_width = (JDIMENSION)
  200. jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
  201. cinfo->output_height = (JDIMENSION)
  202. jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
  203. cinfo->_min_DCT_h_scaled_size = 14;
  204. cinfo->_min_DCT_v_scaled_size = 14;
  205. } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
  206. /* Provide 15/block_size scaling */
  207. cinfo->output_width = (JDIMENSION)
  208. jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
  209. cinfo->output_height = (JDIMENSION)
  210. jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
  211. cinfo->_min_DCT_h_scaled_size = 15;
  212. cinfo->_min_DCT_v_scaled_size = 15;
  213. } else {
  214. /* Provide 16/block_size scaling */
  215. cinfo->output_width = (JDIMENSION)
  216. jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
  217. cinfo->output_height = (JDIMENSION)
  218. jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
  219. cinfo->_min_DCT_h_scaled_size = 16;
  220. cinfo->_min_DCT_v_scaled_size = 16;
  221. }
  222. /* Recompute dimensions of components */
  223. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  224. ci++, compptr++) {
  225. compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
  226. compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
  227. }
  228. #else /* !IDCT_SCALING_SUPPORTED */
  229. /* Hardwire it to "no scaling" */
  230. cinfo->output_width = cinfo->image_width;
  231. cinfo->output_height = cinfo->image_height;
  232. /* jdinput.c has already initialized DCT_scaled_size,
  233. * and has computed unscaled downsampled_width and downsampled_height.
  234. */
  235. #endif /* IDCT_SCALING_SUPPORTED */
  236. }
  237. /*
  238. * Compute output image dimensions and related values.
  239. * NOTE: this is exported for possible use by application.
  240. * Hence it mustn't do anything that can't be done twice.
  241. * Also note that it may be called before the master module is initialized!
  242. */
  243. GLOBAL(void)
  244. jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
  245. /* Do computations that are needed before master selection phase */
  246. {
  247. #ifdef IDCT_SCALING_SUPPORTED
  248. int ci;
  249. jpeg_component_info *compptr;
  250. #endif
  251. /* Prevent application from calling me at wrong times */
  252. if (cinfo->global_state != DSTATE_READY)
  253. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  254. /* Compute core output image dimensions and DCT scaling choices. */
  255. jpeg_core_output_dimensions(cinfo);
  256. #ifdef IDCT_SCALING_SUPPORTED
  257. /* In selecting the actual DCT scaling for each component, we try to
  258. * scale up the chroma components via IDCT scaling rather than upsampling.
  259. * This saves time if the upsampler gets to use 1:1 scaling.
  260. * Note this code adapts subsampling ratios which are powers of 2.
  261. */
  262. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  263. ci++, compptr++) {
  264. int ssize = cinfo->_min_DCT_scaled_size;
  265. while (ssize < DCTSIZE &&
  266. ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
  267. (compptr->h_samp_factor * ssize * 2) == 0) &&
  268. ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
  269. (compptr->v_samp_factor * ssize * 2) == 0)) {
  270. ssize = ssize * 2;
  271. }
  272. #if JPEG_LIB_VERSION >= 70
  273. compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
  274. #else
  275. compptr->DCT_scaled_size = ssize;
  276. #endif
  277. }
  278. /* Recompute downsampled dimensions of components;
  279. * application needs to know these if using raw downsampled data.
  280. */
  281. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  282. ci++, compptr++) {
  283. /* Size in samples, after IDCT scaling */
  284. compptr->downsampled_width = (JDIMENSION)
  285. jdiv_round_up((long)cinfo->image_width *
  286. (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size),
  287. (long)(cinfo->max_h_samp_factor * DCTSIZE));
  288. compptr->downsampled_height = (JDIMENSION)
  289. jdiv_round_up((long)cinfo->image_height *
  290. (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size),
  291. (long)(cinfo->max_v_samp_factor * DCTSIZE));
  292. }
  293. #else /* !IDCT_SCALING_SUPPORTED */
  294. /* Hardwire it to "no scaling" */
  295. cinfo->output_width = cinfo->image_width;
  296. cinfo->output_height = cinfo->image_height;
  297. /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
  298. * and has computed unscaled downsampled_width and downsampled_height.
  299. */
  300. #endif /* IDCT_SCALING_SUPPORTED */
  301. /* Report number of components in selected colorspace. */
  302. /* Probably this should be in the color conversion module... */
  303. switch (cinfo->out_color_space) {
  304. case JCS_GRAYSCALE:
  305. cinfo->out_color_components = 1;
  306. break;
  307. case JCS_RGB:
  308. case JCS_EXT_RGB:
  309. case JCS_EXT_RGBX:
  310. case JCS_EXT_BGR:
  311. case JCS_EXT_BGRX:
  312. case JCS_EXT_XBGR:
  313. case JCS_EXT_XRGB:
  314. case JCS_EXT_RGBA:
  315. case JCS_EXT_BGRA:
  316. case JCS_EXT_ABGR:
  317. case JCS_EXT_ARGB:
  318. cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
  319. break;
  320. case JCS_YCbCr:
  321. case JCS_RGB565:
  322. cinfo->out_color_components = 3;
  323. break;
  324. case JCS_CMYK:
  325. case JCS_YCCK:
  326. cinfo->out_color_components = 4;
  327. break;
  328. default: /* else must be same colorspace as in file */
  329. cinfo->out_color_components = cinfo->num_components;
  330. break;
  331. }
  332. cinfo->output_components = (cinfo->quantize_colors ? 1 :
  333. cinfo->out_color_components);
  334. /* See if upsampler will want to emit more than one row at a time */
  335. if (use_merged_upsample(cinfo))
  336. cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  337. else
  338. cinfo->rec_outbuf_height = 1;
  339. }
  340. /*
  341. * Several decompression processes need to range-limit values to the range
  342. * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  343. * due to noise introduced by quantization, roundoff error, etc. These
  344. * processes are inner loops and need to be as fast as possible. On most
  345. * machines, particularly CPUs with pipelines or instruction prefetch,
  346. * a (subscript-check-less) C table lookup
  347. * x = sample_range_limit[x];
  348. * is faster than explicit tests
  349. * if (x < 0) x = 0;
  350. * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
  351. * These processes all use a common table prepared by the routine below.
  352. *
  353. * For most steps we can mathematically guarantee that the initial value
  354. * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  355. * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
  356. * limiting step (just after the IDCT), a wildly out-of-range value is
  357. * possible if the input data is corrupt. To avoid any chance of indexing
  358. * off the end of memory and getting a bad-pointer trap, we perform the
  359. * post-IDCT limiting thus:
  360. * x = range_limit[x & MASK];
  361. * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  362. * samples. Under normal circumstances this is more than enough range and
  363. * a correct output will be generated; with bogus input data the mask will
  364. * cause wraparound, and we will safely generate a bogus-but-in-range output.
  365. * For the post-IDCT step, we want to convert the data from signed to unsigned
  366. * representation by adding CENTERJSAMPLE at the same time that we limit it.
  367. * So the post-IDCT limiting table ends up looking like this:
  368. * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  369. * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  370. * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  371. * 0,1,...,CENTERJSAMPLE-1
  372. * Negative inputs select values from the upper half of the table after
  373. * masking.
  374. *
  375. * We can save some space by overlapping the start of the post-IDCT table
  376. * with the simpler range limiting table. The post-IDCT table begins at
  377. * sample_range_limit + CENTERJSAMPLE.
  378. */
  379. LOCAL(void)
  380. prepare_range_limit_table(j_decompress_ptr cinfo)
  381. /* Allocate and fill in the sample_range_limit table */
  382. {
  383. JSAMPLE *table;
  384. int i;
  385. table = (JSAMPLE *)
  386. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  387. (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
  388. table += (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */
  389. cinfo->sample_range_limit = table;
  390. /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  391. memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
  392. /* Main part of "simple" table: limit[x] = x */
  393. for (i = 0; i <= MAXJSAMPLE; i++)
  394. table[i] = (JSAMPLE)i;
  395. table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
  396. /* End of simple table, rest of first half of post-IDCT table */
  397. for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
  398. table[i] = MAXJSAMPLE;
  399. /* Second half of post-IDCT table */
  400. memset(table + (2 * (MAXJSAMPLE + 1)), 0,
  401. (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
  402. memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
  403. cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
  404. }
  405. /*
  406. * Master selection of decompression modules.
  407. * This is done once at jpeg_start_decompress time. We determine
  408. * which modules will be used and give them appropriate initialization calls.
  409. * We also initialize the decompressor input side to begin consuming data.
  410. *
  411. * Since jpeg_read_header has finished, we know what is in the SOF
  412. * and (first) SOS markers. We also have all the application parameter
  413. * settings.
  414. */
  415. LOCAL(void)
  416. master_selection(j_decompress_ptr cinfo)
  417. {
  418. my_master_ptr master = (my_master_ptr)cinfo->master;
  419. boolean use_c_buffer;
  420. long samplesperrow;
  421. JDIMENSION jd_samplesperrow;
  422. /* Initialize dimensions and other stuff */
  423. jpeg_calc_output_dimensions(cinfo);
  424. prepare_range_limit_table(cinfo);
  425. /* Width of an output scanline must be representable as JDIMENSION. */
  426. samplesperrow = (long)cinfo->output_width *
  427. (long)cinfo->out_color_components;
  428. jd_samplesperrow = (JDIMENSION)samplesperrow;
  429. if ((long)jd_samplesperrow != samplesperrow)
  430. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  431. /* Initialize my private state */
  432. master->pass_number = 0;
  433. master->using_merged_upsample = use_merged_upsample(cinfo);
  434. /* Color quantizer selection */
  435. master->quantizer_1pass = NULL;
  436. master->quantizer_2pass = NULL;
  437. /* No mode changes if not using buffered-image mode. */
  438. if (!cinfo->quantize_colors || !cinfo->buffered_image) {
  439. cinfo->enable_1pass_quant = FALSE;
  440. cinfo->enable_external_quant = FALSE;
  441. cinfo->enable_2pass_quant = FALSE;
  442. }
  443. if (cinfo->quantize_colors) {
  444. if (cinfo->raw_data_out)
  445. ERREXIT(cinfo, JERR_NOTIMPL);
  446. /* 2-pass quantizer only works in 3-component color space. */
  447. if (cinfo->out_color_components != 3) {
  448. cinfo->enable_1pass_quant = TRUE;
  449. cinfo->enable_external_quant = FALSE;
  450. cinfo->enable_2pass_quant = FALSE;
  451. cinfo->colormap = NULL;
  452. } else if (cinfo->colormap != NULL) {
  453. cinfo->enable_external_quant = TRUE;
  454. } else if (cinfo->two_pass_quantize) {
  455. cinfo->enable_2pass_quant = TRUE;
  456. } else {
  457. cinfo->enable_1pass_quant = TRUE;
  458. }
  459. if (cinfo->enable_1pass_quant) {
  460. #ifdef QUANT_1PASS_SUPPORTED
  461. jinit_1pass_quantizer(cinfo);
  462. master->quantizer_1pass = cinfo->cquantize;
  463. #else
  464. ERREXIT(cinfo, JERR_NOT_COMPILED);
  465. #endif
  466. }
  467. /* We use the 2-pass code to map to external colormaps. */
  468. if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  469. #ifdef QUANT_2PASS_SUPPORTED
  470. jinit_2pass_quantizer(cinfo);
  471. master->quantizer_2pass = cinfo->cquantize;
  472. #else
  473. ERREXIT(cinfo, JERR_NOT_COMPILED);
  474. #endif
  475. }
  476. /* If both quantizers are initialized, the 2-pass one is left active;
  477. * this is necessary for starting with quantization to an external map.
  478. */
  479. }
  480. /* Post-processing: in particular, color conversion first */
  481. if (!cinfo->raw_data_out) {
  482. if (master->using_merged_upsample) {
  483. #ifdef UPSAMPLE_MERGING_SUPPORTED
  484. jinit_merged_upsampler(cinfo); /* does color conversion too */
  485. #else
  486. ERREXIT(cinfo, JERR_NOT_COMPILED);
  487. #endif
  488. } else {
  489. jinit_color_deconverter(cinfo);
  490. jinit_upsampler(cinfo);
  491. }
  492. jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  493. }
  494. /* Inverse DCT */
  495. jinit_inverse_dct(cinfo);
  496. /* Entropy decoding: either Huffman or arithmetic coding. */
  497. if (cinfo->arith_code) {
  498. #ifdef D_ARITH_CODING_SUPPORTED
  499. jinit_arith_decoder(cinfo);
  500. #else
  501. ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  502. #endif
  503. } else {
  504. if (cinfo->progressive_mode) {
  505. #ifdef D_PROGRESSIVE_SUPPORTED
  506. jinit_phuff_decoder(cinfo);
  507. #else
  508. ERREXIT(cinfo, JERR_NOT_COMPILED);
  509. #endif
  510. } else
  511. jinit_huff_decoder(cinfo);
  512. }
  513. /* Initialize principal buffer controllers. */
  514. use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  515. jinit_d_coef_controller(cinfo, use_c_buffer);
  516. if (!cinfo->raw_data_out)
  517. jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  518. /* We can now tell the memory manager to allocate virtual arrays. */
  519. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
  520. /* Initialize input side of decompressor to consume first scan. */
  521. (*cinfo->inputctl->start_input_pass) (cinfo);
  522. /* Set the first and last iMCU columns to decompress from single-scan images.
  523. * By default, decompress all of the iMCU columns.
  524. */
  525. cinfo->master->first_iMCU_col = 0;
  526. cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
  527. cinfo->master->last_good_iMCU_row = 0;
  528. #ifdef D_MULTISCAN_FILES_SUPPORTED
  529. /* If jpeg_start_decompress will read the whole file, initialize
  530. * progress monitoring appropriately. The input step is counted
  531. * as one pass.
  532. */
  533. if (cinfo->progress != NULL && !cinfo->buffered_image &&
  534. cinfo->inputctl->has_multiple_scans) {
  535. int nscans;
  536. /* Estimate number of scans to set pass_limit. */
  537. if (cinfo->progressive_mode) {
  538. /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  539. nscans = 2 + 3 * cinfo->num_components;
  540. } else {
  541. /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  542. nscans = cinfo->num_components;
  543. }
  544. cinfo->progress->pass_counter = 0L;
  545. cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
  546. cinfo->progress->completed_passes = 0;
  547. cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  548. /* Count the input pass as done */
  549. master->pass_number++;
  550. }
  551. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  552. }
  553. /*
  554. * Per-pass setup.
  555. * This is called at the beginning of each output pass. We determine which
  556. * modules will be active during this pass and give them appropriate
  557. * start_pass calls. We also set is_dummy_pass to indicate whether this
  558. * is a "real" output pass or a dummy pass for color quantization.
  559. * (In the latter case, jdapistd.c will crank the pass to completion.)
  560. */
  561. METHODDEF(void)
  562. prepare_for_output_pass(j_decompress_ptr cinfo)
  563. {
  564. my_master_ptr master = (my_master_ptr)cinfo->master;
  565. if (master->pub.is_dummy_pass) {
  566. #ifdef QUANT_2PASS_SUPPORTED
  567. /* Final pass of 2-pass quantization */
  568. master->pub.is_dummy_pass = FALSE;
  569. (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  570. (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  571. (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  572. #else
  573. ERREXIT(cinfo, JERR_NOT_COMPILED);
  574. #endif /* QUANT_2PASS_SUPPORTED */
  575. } else {
  576. if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  577. /* Select new quantization method */
  578. if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  579. cinfo->cquantize = master->quantizer_2pass;
  580. master->pub.is_dummy_pass = TRUE;
  581. } else if (cinfo->enable_1pass_quant) {
  582. cinfo->cquantize = master->quantizer_1pass;
  583. } else {
  584. ERREXIT(cinfo, JERR_MODE_CHANGE);
  585. }
  586. }
  587. (*cinfo->idct->start_pass) (cinfo);
  588. (*cinfo->coef->start_output_pass) (cinfo);
  589. if (!cinfo->raw_data_out) {
  590. if (!master->using_merged_upsample)
  591. (*cinfo->cconvert->start_pass) (cinfo);
  592. (*cinfo->upsample->start_pass) (cinfo);
  593. if (cinfo->quantize_colors)
  594. (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  595. (*cinfo->post->start_pass) (cinfo,
  596. (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  597. (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  598. }
  599. }
  600. /* Set up progress monitor's pass info if present */
  601. if (cinfo->progress != NULL) {
  602. cinfo->progress->completed_passes = master->pass_number;
  603. cinfo->progress->total_passes = master->pass_number +
  604. (master->pub.is_dummy_pass ? 2 : 1);
  605. /* In buffered-image mode, we assume one more output pass if EOI not
  606. * yet reached, but no more passes if EOI has been reached.
  607. */
  608. if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
  609. cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  610. }
  611. }
  612. }
  613. /*
  614. * Finish up at end of an output pass.
  615. */
  616. METHODDEF(void)
  617. finish_output_pass(j_decompress_ptr cinfo)
  618. {
  619. my_master_ptr master = (my_master_ptr)cinfo->master;
  620. if (cinfo->quantize_colors)
  621. (*cinfo->cquantize->finish_pass) (cinfo);
  622. master->pass_number++;
  623. }
  624. #ifdef D_MULTISCAN_FILES_SUPPORTED
  625. /*
  626. * Switch to a new external colormap between output passes.
  627. */
  628. GLOBAL(void)
  629. jpeg_new_colormap(j_decompress_ptr cinfo)
  630. {
  631. my_master_ptr master = (my_master_ptr)cinfo->master;
  632. /* Prevent application from calling me at wrong times */
  633. if (cinfo->global_state != DSTATE_BUFIMAGE)
  634. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  635. if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  636. cinfo->colormap != NULL) {
  637. /* Select 2-pass quantizer for external colormap use */
  638. cinfo->cquantize = master->quantizer_2pass;
  639. /* Notify quantizer of colormap change */
  640. (*cinfo->cquantize->new_color_map) (cinfo);
  641. master->pub.is_dummy_pass = FALSE; /* just in case */
  642. } else
  643. ERREXIT(cinfo, JERR_MODE_CHANGE);
  644. }
  645. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  646. /*
  647. * Initialize master decompression control and select active modules.
  648. * This is performed at the start of jpeg_start_decompress.
  649. */
  650. GLOBAL(void)
  651. jinit_master_decompress(j_decompress_ptr cinfo)
  652. {
  653. my_master_ptr master = (my_master_ptr)cinfo->master;
  654. master->pub.prepare_for_output_pass = prepare_for_output_pass;
  655. master->pub.finish_output_pass = finish_output_pass;
  656. master->pub.is_dummy_pass = FALSE;
  657. master->pub.jinit_upsampler_no_alloc = FALSE;
  658. master_selection(cinfo);
  659. }