jdmaster.c 19 KB

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