Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

679 lines
22 KiB

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