jdmaster.pas 22 KB

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