jdmaster.c 27 KB

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