jdmaster.pas 23 KB

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