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