jdmaster.cpp 22 KB

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