jcmaster.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. Unit JcMaster;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4. { This file contains master control logic for the JPEG compressor.
  5. These routines are concerned with parameter validation, initial setup,
  6. and inter-pass control (determining the number of passes and the work
  7. to be done in each pass). }
  8. { Original: jcmaster.c ; Copyright (C) 1991-1997, 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.Jdeferr,
  16. System.Jpeg.Jerror,
  17. System.Jpeg.Jutils,
  18. System.Jpeg.Jpeglib;
  19. {$ELSE FPC_DOTTEDUNITS}
  20. uses
  21. jmorecfg,
  22. jinclude,
  23. jdeferr,
  24. jerror,
  25. jutils,
  26. jpeglib;
  27. {$ENDIF FPC_DOTTEDUNITS}
  28. { Initialize master compression control. }
  29. {GLOBAL}
  30. procedure jinit_c_master_control (cinfo : j_compress_ptr;
  31. transcode_only : boolean);
  32. implementation
  33. { Private state }
  34. type
  35. c_pass_type = (
  36. main_pass, { input data, also do first output step }
  37. huff_opt_pass, { Huffman code optimization pass }
  38. output_pass { data output pass }
  39. );
  40. type
  41. my_master_ptr = ^my_comp_master;
  42. my_comp_master = record
  43. pub : jpeg_comp_master; { public fields }
  44. pass_type : c_pass_type; { the type of the current pass }
  45. pass_number : int; { # of passes completed }
  46. total_passes : int; { total # of passes needed }
  47. scan_number : int; { current index in scan_info[] }
  48. end;
  49. { Support routines that do various essential calculations. }
  50. {LOCAL}
  51. procedure initial_setup (cinfo : j_compress_ptr);
  52. { Do computations that are needed before master selection phase }
  53. var
  54. ci : int;
  55. compptr : jpeg_component_info_ptr;
  56. samplesperrow : long;
  57. jd_samplesperrow : JDIMENSION;
  58. begin
  59. { Sanity check on image dimensions }
  60. if (cinfo^.image_height <= 0) or (cinfo^.image_width <= 0) or
  61. (cinfo^.num_components <= 0) or (cinfo^.input_components <= 0) then
  62. ERREXIT(j_common_ptr(cinfo), JERR_EMPTY_IMAGE);
  63. { Make sure image isn't bigger than I can handle }
  64. if ( long(cinfo^.image_height) > long(JPEG_MAX_DIMENSION)) or
  65. ( long(cinfo^.image_width) > long(JPEG_MAX_DIMENSION)) then
  66. ERREXIT1(j_common_ptr(cinfo), JERR_IMAGE_TOO_BIG,
  67. uInt(JPEG_MAX_DIMENSION));
  68. { Width of an input scanline must be representable as JDIMENSION. }
  69. samplesperrow := long (cinfo^.image_width) * long (cinfo^.input_components);
  70. jd_samplesperrow := JDIMENSION (samplesperrow);
  71. if ( long(jd_samplesperrow) <> samplesperrow) then
  72. ERREXIT(j_common_ptr(cinfo), JERR_WIDTH_OVERFLOW);
  73. { For now, precision must match compiled-in value... }
  74. if (cinfo^.data_precision <> BITS_IN_JSAMPLE) then
  75. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PRECISION, cinfo^.data_precision);
  76. { Check that number of components won't exceed internal array sizes }
  77. if (cinfo^.num_components > MAX_COMPONENTS) then
  78. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT, cinfo^.num_components,
  79. MAX_COMPONENTS);
  80. { Compute maximum sampling factors; check factor validity }
  81. cinfo^.max_h_samp_factor := 1;
  82. cinfo^.max_v_samp_factor := 1;
  83. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  84. for ci := 0 to pred(cinfo^.num_components) do
  85. begin
  86. if (compptr^.h_samp_factor<=0) or (compptr^.h_samp_factor>MAX_SAMP_FACTOR)
  87. or (compptr^.v_samp_factor<=0) or (compptr^.v_samp_factor>MAX_SAMP_FACTOR) then
  88. ERREXIT(j_common_ptr(cinfo), JERR_BAD_SAMPLING);
  89. { MAX }
  90. if cinfo^.max_h_samp_factor > compptr^.h_samp_factor then
  91. cinfo^.max_h_samp_factor := cinfo^.max_h_samp_factor
  92. else
  93. cinfo^.max_h_samp_factor := compptr^.h_samp_factor;
  94. { MAX }
  95. if cinfo^.max_v_samp_factor > compptr^.v_samp_factor then
  96. cinfo^.max_v_samp_factor := cinfo^.max_v_samp_factor
  97. else
  98. cinfo^.max_v_samp_factor := compptr^.v_samp_factor;
  99. Inc(compptr);
  100. end;
  101. { Compute dimensions of components }
  102. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  103. for ci := 0 to pred(cinfo^.num_components) do
  104. begin
  105. { Fill in the correct component_index value; don't rely on application }
  106. compptr^.component_index := ci;
  107. { For compression, we never do DCT scaling. }
  108. compptr^.DCT_scaled_size := DCTSIZE;
  109. { Size in DCT blocks }
  110. compptr^.width_in_blocks := JDIMENSION (
  111. jdiv_round_up(long (cinfo^.image_width) * long (compptr^.h_samp_factor),
  112. long (cinfo^.max_h_samp_factor * DCTSIZE)) );
  113. compptr^.height_in_blocks := JDIMENSION (
  114. jdiv_round_up(long (cinfo^.image_height) * long (compptr^.v_samp_factor),
  115. long (cinfo^.max_v_samp_factor * DCTSIZE)) );
  116. { Size in samples }
  117. compptr^.downsampled_width := JDIMENSION (
  118. jdiv_round_up(long(cinfo^.image_width) * long(compptr^.h_samp_factor),
  119. long(cinfo^.max_h_samp_factor)) );
  120. compptr^.downsampled_height := JDIMENSION (
  121. jdiv_round_up(long (cinfo^.image_height) * long(compptr^.v_samp_factor),
  122. long (cinfo^.max_v_samp_factor)) );
  123. { Mark component needed (this flag isn't actually used for compression) }
  124. compptr^.component_needed := TRUE;
  125. Inc(compptr);
  126. end;
  127. { Compute number of fully interleaved MCU rows (number of times that
  128. main controller will call coefficient controller). }
  129. cinfo^.total_iMCU_rows := JDIMENSION (
  130. jdiv_round_up(long (cinfo^.image_height),
  131. long (cinfo^.max_v_samp_factor*DCTSIZE)) );
  132. end;
  133. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  134. {LOCAL}
  135. procedure validate_script (cinfo : j_compress_ptr);
  136. { Verify that the scan script in cinfo^.scan_info[] is valid; also
  137. determine whether it uses progressive JPEG, and set cinfo^.progressive_mode. }
  138. type
  139. IntRow = array[0..DCTSIZE2-1] of int;
  140. introw_ptr = ^IntRow;
  141. var
  142. {const}scanptr : jpeg_scan_info_ptr;
  143. scanno, ncomps, ci, coefi, thisi : int;
  144. Ss, Se, Ah, Al : int;
  145. component_sent : array[0..MAX_COMPONENTS-1] of boolean;
  146. {$ifdef C_PROGRESSIVE_SUPPORTED}
  147. last_bitpos_int_ptr : int_ptr;
  148. last_bitpos_ptr : introw_ptr;
  149. last_bitpos : array[0..MAX_COMPONENTS-1] of IntRow;
  150. { -1 until that coefficient has been seen; then last Al for it }
  151. { The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  152. seems wrong: the upper bound ought to depend on data precision.
  153. Perhaps they really meant 0..N+1 for N-bit precision.
  154. Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  155. out-of-range reconstructed DC values during the first DC scan,
  156. which might cause problems for some decoders. }
  157. {$ifdef BITS_IN_JSAMPLE_IS_8}
  158. const
  159. MAX_AH_AL = 10;
  160. {$else}
  161. const
  162. MAX_AH_AL = 13;
  163. {$endif}
  164. {$endif}
  165. begin
  166. if (cinfo^.num_scans <= 0) then
  167. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_SCAN_SCRIPT, 0);
  168. { For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  169. for progressive JPEG, no scan can have this. }
  170. scanptr := cinfo^.scan_info;
  171. if (scanptr^.Ss <> 0) or (scanptr^.Se <> DCTSIZE2-1) then
  172. begin
  173. {$ifdef C_PROGRESSIVE_SUPPORTED}
  174. cinfo^.progressive_mode := TRUE;
  175. last_bitpos_int_ptr := @(last_bitpos[0][0]);
  176. for ci := 0 to pred(cinfo^.num_components) do
  177. for coefi := 0 to pred(DCTSIZE2) do
  178. begin
  179. last_bitpos_int_ptr^ := -1;
  180. Inc(last_bitpos_int_ptr);
  181. end;
  182. {$else}
  183. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  184. {$endif}
  185. end
  186. else
  187. begin
  188. cinfo^.progressive_mode := FALSE;
  189. for ci := 0 to pred(cinfo^.num_components) do
  190. component_sent[ci] := FALSE;
  191. end;
  192. for scanno := 1 to cinfo^.num_scans do
  193. begin
  194. { Validate component indexes }
  195. ncomps := scanptr^.comps_in_scan;
  196. if (ncomps <= 0) or (ncomps > MAX_COMPS_IN_SCAN) then
  197. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  198. for ci := 0 to pred(ncomps) do
  199. begin
  200. thisi := scanptr^.component_index[ci];
  201. if (thisi < 0) or (thisi >= cinfo^.num_components) then
  202. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_SCAN_SCRIPT, scanno);
  203. { Components must appear in SOF order within each scan }
  204. if (ci > 0) and (thisi <= scanptr^.component_index[ci-1]) then
  205. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_SCAN_SCRIPT, scanno);
  206. end;
  207. { Validate progression parameters }
  208. Ss := scanptr^.Ss;
  209. Se := scanptr^.Se;
  210. Ah := scanptr^.Ah;
  211. Al := scanptr^.Al;
  212. if (cinfo^.progressive_mode) then
  213. begin
  214. {$ifdef C_PROGRESSIVE_SUPPORTED}
  215. if (Ss < 0) or (Ss >= DCTSIZE2) or (Se < Ss) or (Se >= DCTSIZE2) or
  216. (Ah < 0) or (Ah > MAX_AH_AL) or (Al < 0) or (Al > MAX_AH_AL) then
  217. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  218. if (Ss < 0) or (Ss >= DCTSIZE2) or (Se < Ss) or (Se >= DCTSIZE2)
  219. or (Ah < 0) or (Ah > MAX_AH_AL) or (Al < 0) or (Al > MAX_AH_AL) then
  220. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  221. if (Ss = 0) then
  222. begin
  223. if (Se <> 0) then { DC and AC together not OK }
  224. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  225. end
  226. else
  227. begin
  228. if (ncomps <> 1) then { AC scans must be for only one component }
  229. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  230. end;
  231. for ci := 0 to pred(ncomps) do
  232. begin
  233. last_bitpos_ptr := @( last_bitpos[scanptr^.component_index[ci]]);
  234. if (Ss <> 0) and (last_bitpos_ptr^[0] < 0) then { AC without prior DC scan }
  235. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  236. for coefi := Ss to Se do
  237. begin
  238. if (last_bitpos_ptr^[coefi] < 0) then
  239. begin
  240. { first scan of this coefficient }
  241. if (Ah <> 0) then
  242. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  243. end
  244. else
  245. begin
  246. { not first scan }
  247. if (Ah <> last_bitpos_ptr^[coefi]) or (Al <> Ah-1) then
  248. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  249. end;
  250. last_bitpos_ptr^[coefi] := Al;
  251. end;
  252. end;
  253. {$endif}
  254. end
  255. else
  256. begin
  257. { For sequential JPEG, all progression parameters must be these: }
  258. if (Ss <> 0) or (Se <> DCTSIZE2-1) or (Ah <> 0) or (Al <> 0) then
  259. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PROG_SCRIPT, scanno);
  260. { Make sure components are not sent twice }
  261. for ci := 0 to pred(ncomps) do
  262. begin
  263. thisi := scanptr^.component_index[ci];
  264. if (component_sent[thisi]) then
  265. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_SCAN_SCRIPT, scanno);
  266. component_sent[thisi] := TRUE;
  267. end;
  268. end;
  269. Inc(scanptr);
  270. end;
  271. { Now verify that everything got sent. }
  272. if (cinfo^.progressive_mode) then
  273. begin
  274. {$ifdef C_PROGRESSIVE_SUPPORTED
  275. { For progressive mode, we only check that at least some DC data
  276. got sent for each component; the spec does not require that all bits
  277. of all coefficients be transmitted. Would it be wiser to enforce
  278. transmission of all coefficient bits?? }
  279. for ci := 0 to pred(cinfo^.num_components) do
  280. begin
  281. if (last_bitpos[ci][0] < 0) then
  282. ERREXIT(j_common_ptr(cinfo), JERR_MISSING_DATA);
  283. end;
  284. {$endif}
  285. end
  286. else
  287. begin
  288. for ci := 0 to pred(cinfo^.num_components) do
  289. begin
  290. if (not component_sent[ci]) then
  291. ERREXIT(j_common_ptr(cinfo), JERR_MISSING_DATA);
  292. end;
  293. end;
  294. end;
  295. {$endif} { C_MULTISCAN_FILES_SUPPORTED }
  296. {LOCAL}
  297. procedure select_scan_parameters (cinfo : j_compress_ptr);
  298. { Set up the scan parameters for the current scan }
  299. var
  300. master : my_master_ptr;
  301. {const} scanptr : jpeg_scan_info_ptr;
  302. ci : int;
  303. var
  304. comp_infos : jpeg_component_info_list_ptr;
  305. begin
  306. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  307. if (cinfo^.scan_info <> NIL) then
  308. begin
  309. { Prepare for current scan --- the script is already validated }
  310. master := my_master_ptr (cinfo^.master);
  311. scanptr := cinfo^.scan_info;
  312. Inc(scanptr, master^.scan_number);
  313. cinfo^.comps_in_scan := scanptr^.comps_in_scan;
  314. comp_infos := cinfo^.comp_info;
  315. for ci := 0 to pred(scanptr^.comps_in_scan) do
  316. begin
  317. cinfo^.cur_comp_info[ci] :=
  318. @(comp_infos^[scanptr^.component_index[ci]]);
  319. end;
  320. cinfo^.Ss := scanptr^.Ss;
  321. cinfo^.Se := scanptr^.Se;
  322. cinfo^.Ah := scanptr^.Ah;
  323. cinfo^.Al := scanptr^.Al;
  324. end
  325. else
  326. {$endif}
  327. begin
  328. { Prepare for single sequential-JPEG scan containing all components }
  329. if (cinfo^.num_components > MAX_COMPS_IN_SCAN) then
  330. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT, cinfo^.num_components,
  331. MAX_COMPS_IN_SCAN);
  332. cinfo^.comps_in_scan := cinfo^.num_components;
  333. comp_infos := cinfo^.comp_info;
  334. for ci := 0 to pred(cinfo^.num_components) do
  335. begin
  336. cinfo^.cur_comp_info[ci] := @(comp_infos^[ci]);
  337. end;
  338. cinfo^.Ss := 0;
  339. cinfo^.Se := DCTSIZE2-1;
  340. cinfo^.Ah := 0;
  341. cinfo^.Al := 0;
  342. end;
  343. end;
  344. {LOCAL}
  345. procedure per_scan_setup (cinfo : j_compress_ptr);
  346. { Do computations that are needed before processing a JPEG scan }
  347. { cinfo^.comps_in_scan and cinfo^.cur_comp_info[] are already set }
  348. var
  349. ci, mcublks, tmp : int;
  350. compptr : jpeg_component_info_ptr;
  351. nominal : long;
  352. begin
  353. if (cinfo^.comps_in_scan = 1) then
  354. begin
  355. { Noninterleaved (single-component) scan }
  356. compptr := cinfo^.cur_comp_info[0];
  357. { Overall image size in MCUs }
  358. cinfo^.MCUs_per_row := compptr^.width_in_blocks;
  359. cinfo^.MCU_rows_in_scan := compptr^.height_in_blocks;
  360. { For noninterleaved scan, always one block per MCU }
  361. compptr^.MCU_width := 1;
  362. compptr^.MCU_height := 1;
  363. compptr^.MCU_blocks := 1;
  364. compptr^.MCU_sample_width := DCTSIZE;
  365. compptr^.last_col_width := 1;
  366. { For noninterleaved scans, it is convenient to define last_row_height
  367. as the number of block rows present in the last iMCU row. }
  368. tmp := int (compptr^.height_in_blocks mod compptr^.v_samp_factor);
  369. if (tmp = 0) then
  370. tmp := compptr^.v_samp_factor;
  371. compptr^.last_row_height := tmp;
  372. { Prepare array describing MCU composition }
  373. cinfo^.blocks_in_MCU := 1;
  374. cinfo^.MCU_membership[0] := 0;
  375. end
  376. else
  377. begin
  378. { Interleaved (multi-component) scan }
  379. if (cinfo^.comps_in_scan <= 0) or
  380. (cinfo^.comps_in_scan > MAX_COMPS_IN_SCAN) then
  381. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT,
  382. cinfo^.comps_in_scan, MAX_COMPS_IN_SCAN);
  383. { Overall image size in MCUs }
  384. cinfo^.MCUs_per_row := JDIMENSION (
  385. jdiv_round_up( long (cinfo^.image_width),
  386. long (cinfo^.max_h_samp_factor*DCTSIZE)) );
  387. cinfo^.MCU_rows_in_scan := JDIMENSION (
  388. jdiv_round_up( long (cinfo^.image_height),
  389. long (cinfo^.max_v_samp_factor*DCTSIZE)) );
  390. cinfo^.blocks_in_MCU := 0;
  391. for ci := 0 to pred(cinfo^.comps_in_scan) do
  392. begin
  393. compptr := cinfo^.cur_comp_info[ci];
  394. { Sampling factors give # of blocks of component in each MCU }
  395. compptr^.MCU_width := compptr^.h_samp_factor;
  396. compptr^.MCU_height := compptr^.v_samp_factor;
  397. compptr^.MCU_blocks := compptr^.MCU_width * compptr^.MCU_height;
  398. compptr^.MCU_sample_width := compptr^.MCU_width * DCTSIZE;
  399. { Figure number of non-dummy blocks in last MCU column & row }
  400. tmp := int (compptr^.width_in_blocks mod compptr^.MCU_width);
  401. if (tmp = 0) then
  402. tmp := compptr^.MCU_width;
  403. compptr^.last_col_width := tmp;
  404. tmp := int (compptr^.height_in_blocks mod compptr^.MCU_height);
  405. if (tmp = 0) then
  406. tmp := compptr^.MCU_height;
  407. compptr^.last_row_height := tmp;
  408. { Prepare array describing MCU composition }
  409. mcublks := compptr^.MCU_blocks;
  410. if (cinfo^.blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) then
  411. ERREXIT(j_common_ptr(cinfo), JERR_BAD_MCU_SIZE);
  412. while (mcublks > 0) do
  413. begin
  414. Dec(mcublks);
  415. cinfo^.MCU_membership[cinfo^.blocks_in_MCU] := ci;
  416. Inc(cinfo^.blocks_in_MCU);
  417. end;
  418. end;
  419. end;
  420. { Convert restart specified in rows to actual MCU count. }
  421. { Note that count must fit in 16 bits, so we provide limiting. }
  422. if (cinfo^.restart_in_rows > 0) then
  423. begin
  424. nominal := long(cinfo^.restart_in_rows) * long(cinfo^.MCUs_per_row);
  425. if nominal < long(65535) then
  426. cinfo^.restart_interval := uInt (nominal)
  427. else
  428. cinfo^.restart_interval := long(65535);
  429. end;
  430. end;
  431. { Per-pass setup.
  432. This is called at the beginning of each pass. We determine which modules
  433. will be active during this pass and give them appropriate start_pass calls.
  434. We also set is_last_pass to indicate whether any more passes will be
  435. required. }
  436. {METHODDEF}
  437. procedure prepare_for_pass (cinfo : j_compress_ptr); far;
  438. var
  439. master : my_master_ptr;
  440. var
  441. fallthrough : boolean;
  442. begin
  443. master := my_master_ptr (cinfo^.master);
  444. fallthrough := true;
  445. case (master^.pass_type) of
  446. main_pass:
  447. begin
  448. { Initial pass: will collect input data, and do either Huffman
  449. optimization or data output for the first scan. }
  450. select_scan_parameters(cinfo);
  451. per_scan_setup(cinfo);
  452. if (not cinfo^.raw_data_in) then
  453. begin
  454. cinfo^.cconvert^.start_pass (cinfo);
  455. cinfo^.downsample^.start_pass (cinfo);
  456. cinfo^.prep^.start_pass (cinfo, JBUF_PASS_THRU);
  457. end;
  458. cinfo^.fdct^.start_pass (cinfo);
  459. cinfo^.entropy^.start_pass (cinfo, cinfo^.optimize_coding);
  460. if master^.total_passes > 1 then
  461. cinfo^.coef^.start_pass (cinfo, JBUF_SAVE_AND_PASS)
  462. else
  463. cinfo^.coef^.start_pass (cinfo, JBUF_PASS_THRU);
  464. cinfo^.main^.start_pass (cinfo, JBUF_PASS_THRU);
  465. if (cinfo^.optimize_coding) then
  466. begin
  467. { No immediate data output; postpone writing frame/scan headers }
  468. master^.pub.call_pass_startup := FALSE;
  469. end
  470. else
  471. begin
  472. { Will write frame/scan headers at first jpeg_write_scanlines call }
  473. master^.pub.call_pass_startup := TRUE;
  474. end;
  475. end;
  476. {$ifdef ENTROPY_OPT_SUPPORTED}
  477. huff_opt_pass,
  478. output_pass:
  479. begin
  480. if (master^.pass_type = huff_opt_pass) then
  481. begin
  482. { Do Huffman optimization for a scan after the first one. }
  483. select_scan_parameters(cinfo);
  484. per_scan_setup(cinfo);
  485. if (cinfo^.Ss <> 0) or (cinfo^.Ah = 0) or (cinfo^.arith_code) then
  486. begin
  487. cinfo^.entropy^.start_pass (cinfo, TRUE);
  488. cinfo^.coef^.start_pass (cinfo, JBUF_CRANK_DEST);
  489. master^.pub.call_pass_startup := FALSE;
  490. fallthrough := false;
  491. end;
  492. { Special case: Huffman DC refinement scans need no Huffman table
  493. and therefore we can skip the optimization pass for them. }
  494. if fallthrough then
  495. begin
  496. master^.pass_type := output_pass;
  497. Inc(master^.pass_number);
  498. {FALLTHROUGH}
  499. end;
  500. end;
  501. {$else}
  502. output_pass:
  503. begin
  504. {$endif}
  505. if fallthrough then
  506. begin
  507. { Do a data-output pass. }
  508. { We need not repeat per-scan setup if prior optimization pass did it. }
  509. if (not cinfo^.optimize_coding) then
  510. begin
  511. select_scan_parameters(cinfo);
  512. per_scan_setup(cinfo);
  513. end;
  514. cinfo^.entropy^.start_pass (cinfo, FALSE);
  515. cinfo^.coef^.start_pass (cinfo, JBUF_CRANK_DEST);
  516. { We emit frame/scan headers now }
  517. if (master^.scan_number = 0) then
  518. cinfo^.marker^.write_frame_header (cinfo);
  519. cinfo^.marker^.write_scan_header (cinfo);
  520. master^.pub.call_pass_startup := FALSE;
  521. end;
  522. end;
  523. else
  524. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  525. end;
  526. master^.pub.is_last_pass := (master^.pass_number = master^.total_passes-1);
  527. { Set up progress monitor's pass info if present }
  528. if (cinfo^.progress <> NIL) then
  529. begin
  530. cinfo^.progress^.completed_passes := master^.pass_number;
  531. cinfo^.progress^.total_passes := master^.total_passes;
  532. end;
  533. end;
  534. { Special start-of-pass hook.
  535. This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  536. In single-pass processing, we need this hook because we don't want to
  537. write frame/scan headers during jpeg_start_compress; we want to let the
  538. application write COM markers etc. between jpeg_start_compress and the
  539. jpeg_write_scanlines loop.
  540. In multi-pass processing, this routine is not used. }
  541. {METHODDEF}
  542. procedure pass_startup (cinfo : j_compress_ptr); far;
  543. begin
  544. cinfo^.master^.call_pass_startup := FALSE; { reset flag so call only once }
  545. cinfo^.marker^.write_frame_header (cinfo);
  546. cinfo^.marker^.write_scan_header (cinfo);
  547. end;
  548. { Finish up at end of pass. }
  549. {METHODDEF}
  550. procedure finish_pass_master (cinfo : j_compress_ptr); far;
  551. var
  552. master : my_master_ptr;
  553. begin
  554. master := my_master_ptr (cinfo^.master);
  555. { The entropy coder always needs an end-of-pass call,
  556. either to analyze statistics or to flush its output buffer. }
  557. cinfo^.entropy^.finish_pass (cinfo);
  558. { Update state for next pass }
  559. case (master^.pass_type) of
  560. main_pass:
  561. begin
  562. { next pass is either output of scan 0 (after optimization)
  563. or output of scan 1 (if no optimization). }
  564. master^.pass_type := output_pass;
  565. if (not cinfo^.optimize_coding) then
  566. Inc(master^.scan_number);
  567. end;
  568. huff_opt_pass:
  569. { next pass is always output of current scan }
  570. master^.pass_type := output_pass;
  571. output_pass:
  572. begin
  573. { next pass is either optimization or output of next scan }
  574. if (cinfo^.optimize_coding) then
  575. master^.pass_type := huff_opt_pass;
  576. Inc(master^.scan_number);
  577. end;
  578. end;
  579. Inc(master^.pass_number);
  580. end;
  581. { Initialize master compression control. }
  582. {GLOBAL}
  583. procedure jinit_c_master_control (cinfo : j_compress_ptr;
  584. transcode_only : boolean);
  585. var
  586. master : my_master_ptr;
  587. begin
  588. master := my_master_ptr(
  589. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  590. SIZEOF(my_comp_master)) );
  591. cinfo^.master := jpeg_comp_master_ptr(master);
  592. master^.pub.prepare_for_pass := prepare_for_pass;
  593. master^.pub.pass_startup := pass_startup;
  594. master^.pub.finish_pass := finish_pass_master;
  595. master^.pub.is_last_pass := FALSE;
  596. { Validate parameters, determine derived values }
  597. initial_setup(cinfo);
  598. if (cinfo^.scan_info <> NIL) then
  599. begin
  600. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  601. validate_script(cinfo);
  602. {$else}
  603. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  604. {$endif}
  605. end
  606. else
  607. begin
  608. cinfo^.progressive_mode := FALSE;
  609. cinfo^.num_scans := 1;
  610. end;
  611. if (cinfo^.progressive_mode) then { TEMPORARY HACK ??? }
  612. cinfo^.optimize_coding := TRUE; { assume default tables no good for progressive mode }
  613. { Initialize my private state }
  614. if (transcode_only) then
  615. begin
  616. { no main pass in transcoding }
  617. if (cinfo^.optimize_coding) then
  618. master^.pass_type := huff_opt_pass
  619. else
  620. master^.pass_type := output_pass;
  621. end
  622. else
  623. begin
  624. { for normal compression, first pass is always this type: }
  625. master^.pass_type := main_pass;
  626. end;
  627. master^.scan_number := 0;
  628. master^.pass_number := 0;
  629. if (cinfo^.optimize_coding) then
  630. master^.total_passes := cinfo^.num_scans * 2
  631. else
  632. master^.total_passes := cinfo^.num_scans;
  633. end;
  634. end.