jdmaster.c 25 KB

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