jdmaster.c 27 KB

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