jcmaster.pas 23 KB

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