jdmaster.c 19 KB

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