jcparam.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. Unit JcParam;
  2. { This file contains optional default-setting code for the JPEG compressor.
  3. Applications do not have to use this file, but those that don't use it
  4. must know a lot more about the innards of the JPEG code. }
  5. { Original: jcparam.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
  6. interface
  7. {$I jconfig.inc}
  8. uses
  9. jmorecfg,
  10. jinclude,
  11. jdeferr,
  12. jerror,
  13. jcomapi,
  14. jpeglib;
  15. { Quantization table setup routines }
  16. {GLOBAL}
  17. procedure jpeg_add_quant_table (cinfo : j_compress_ptr;
  18. which_tbl : int;
  19. const basic_table : array of uInt;
  20. scale_factor : int;
  21. force_baseline : boolean);
  22. {GLOBAL}
  23. procedure jpeg_set_linear_quality (cinfo : j_compress_ptr;
  24. scale_factor : int;
  25. force_baseline : boolean);
  26. { Set or change the 'quality' (quantization) setting, using default tables
  27. and a straight percentage-scaling quality scale. In most cases it's better
  28. to use jpeg_set_quality (below); this entry point is provided for
  29. applications that insist on a linear percentage scaling. }
  30. {GLOBAL}
  31. function jpeg_quality_scaling (quality : int) : int;
  32. { Convert a user-specified quality rating to a percentage scaling factor
  33. for an underlying quantization table, using our recommended scaling curve.
  34. The input 'quality' factor should be 0 (terrible) to 100 (very good). }
  35. {GLOBAL}
  36. procedure jpeg_set_quality (cinfo : j_compress_ptr;
  37. quality : int;
  38. force_baseline : boolean);
  39. { Set or change the 'quality' (quantization) setting, using default tables.
  40. This is the standard quality-adjusting entry point for typical user
  41. interfaces; only those who want detailed control over quantization tables
  42. would use the preceding three routines directly. }
  43. {GLOBAL}
  44. procedure jpeg_set_defaults (cinfo : j_compress_ptr);
  45. { Create a recommended progressive-JPEG script.
  46. cinfo^.num_components and cinfo^.jpeg_color_space must be correct. }
  47. { Set the JPEG colorspace, and choose colorspace-dependent default values. }
  48. {GLOBAL}
  49. procedure jpeg_set_colorspace (cinfo : j_compress_ptr;
  50. colorspace : J_COLOR_SPACE);
  51. { Select an appropriate JPEG colorspace for in_color_space. }
  52. {GLOBAL}
  53. procedure jpeg_default_colorspace (cinfo : j_compress_ptr);
  54. {GLOBAL}
  55. procedure jpeg_simple_progression (cinfo : j_compress_ptr);
  56. implementation
  57. { Quantization table setup routines }
  58. {GLOBAL}
  59. procedure jpeg_add_quant_table (cinfo : j_compress_ptr;
  60. which_tbl : int;
  61. const basic_table : array of uInt;
  62. scale_factor : int;
  63. force_baseline : boolean);
  64. { Define a quantization table equal to the basic_table times
  65. a scale factor (given as a percentage).
  66. If force_baseline is TRUE, the computed quantization table entries
  67. are limited to 1..255 for JPEG baseline compatibility. }
  68. var
  69. qtblptr :^JQUANT_TBL_PTR;
  70. i : int;
  71. temp : long;
  72. begin
  73. { Safety check to ensure start_compress not called yet. }
  74. if (cinfo^.global_state <> CSTATE_START) then
  75. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  76. if (which_tbl < 0) or (which_tbl >= NUM_QUANT_TBLS) then
  77. ERREXIT1(j_common_ptr(cinfo), JERR_DQT_INDEX, which_tbl);
  78. qtblptr := @(cinfo^.quant_tbl_ptrs[which_tbl]);
  79. if (qtblptr^ = NIL) then
  80. qtblptr^ := jpeg_alloc_quant_table(j_common_ptr(cinfo));
  81. for i := 0 to pred(DCTSIZE2) do
  82. begin
  83. temp := (long(basic_table[i] * scale_factor) + long(50)) div long(100);
  84. { limit the values to the valid range }
  85. if (temp <= long(0)) then
  86. temp := long(1);
  87. if (temp > long(32767)) then
  88. temp := long(32767); { max quantizer needed for 12 bits }
  89. if (force_baseline) and (temp > long(255)) then
  90. temp := long(255); { limit to baseline range if requested }
  91. (qtblptr^)^.quantval[i] := UINT16 (temp);
  92. end;
  93. { Initialize sent_table FALSE so table will be written to JPEG file. }
  94. (qtblptr^)^.sent_table := FALSE;
  95. end;
  96. {GLOBAL}
  97. procedure jpeg_set_linear_quality (cinfo : j_compress_ptr;
  98. scale_factor : int;
  99. force_baseline : boolean);
  100. { Set or change the 'quality' (quantization) setting, using default tables
  101. and a straight percentage-scaling quality scale. In most cases it's better
  102. to use jpeg_set_quality (below); this entry point is provided for
  103. applications that insist on a linear percentage scaling. }
  104. { These are the sample quantization tables given in JPEG spec section K.1.
  105. The spec says that the values given produce "good" quality, and
  106. when divided by 2, "very good" quality. }
  107. const
  108. std_luminance_quant_tbl : array[0..DCTSIZE2-1] of uInt =
  109. (16, 11, 10, 16, 24, 40, 51, 61,
  110. 12, 12, 14, 19, 26, 58, 60, 55,
  111. 14, 13, 16, 24, 40, 57, 69, 56,
  112. 14, 17, 22, 29, 51, 87, 80, 62,
  113. 18, 22, 37, 56, 68, 109, 103, 77,
  114. 24, 35, 55, 64, 81, 104, 113, 92,
  115. 49, 64, 78, 87, 103, 121, 120, 101,
  116. 72, 92, 95, 98, 112, 100, 103, 99);
  117. const
  118. std_chrominance_quant_tbl : array[0..DCTSIZE2-1] of uInt =
  119. (17, 18, 24, 47, 99, 99, 99, 99,
  120. 18, 21, 26, 66, 99, 99, 99, 99,
  121. 24, 26, 56, 99, 99, 99, 99, 99,
  122. 47, 66, 99, 99, 99, 99, 99, 99,
  123. 99, 99, 99, 99, 99, 99, 99, 99,
  124. 99, 99, 99, 99, 99, 99, 99, 99,
  125. 99, 99, 99, 99, 99, 99, 99, 99,
  126. 99, 99, 99, 99, 99, 99, 99, 99);
  127. begin
  128. { Set up two quantization tables using the specified scaling }
  129. jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  130. scale_factor, force_baseline);
  131. jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  132. scale_factor, force_baseline);
  133. end;
  134. {GLOBAL}
  135. function jpeg_quality_scaling (quality : int) : int;
  136. { Convert a user-specified quality rating to a percentage scaling factor
  137. for an underlying quantization table, using our recommended scaling curve.
  138. The input 'quality' factor should be 0 (terrible) to 100 (very good). }
  139. begin
  140. { Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. }
  141. if (quality <= 0) then
  142. quality := 1;
  143. if (quality > 100) then
  144. quality := 100;
  145. { The basic table is used as-is (scaling 100) for a quality of 50.
  146. Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  147. note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  148. to make all the table entries 1 (hence, minimum quantization loss).
  149. Qualities 1..50 are converted to scaling percentage 5000/Q. }
  150. if (quality < 50) then
  151. quality := 5000 div quality
  152. else
  153. quality := 200 - quality*2;
  154. jpeg_quality_scaling := quality;
  155. end;
  156. {GLOBAL}
  157. procedure jpeg_set_quality (cinfo : j_compress_ptr;
  158. quality : int;
  159. force_baseline : boolean);
  160. { Set or change the 'quality' (quantization) setting, using default tables.
  161. This is the standard quality-adjusting entry point for typical user
  162. interfaces; only those who want detailed control over quantization tables
  163. would use the preceding three routines directly. }
  164. begin
  165. { Convert user 0-100 rating to percentage scaling }
  166. quality := jpeg_quality_scaling(quality);
  167. { Set up standard quality tables }
  168. jpeg_set_linear_quality(cinfo, quality, force_baseline);
  169. end;
  170. { Huffman table setup routines }
  171. {LOCAL}
  172. procedure add_huff_table (cinfo : j_compress_ptr;
  173. var htblptr : JHUFF_TBL_PTR;
  174. var bits : array of UINT8;
  175. var val : array of UINT8);
  176. { Define a Huffman table }
  177. var
  178. nsymbols, len : int;
  179. begin
  180. if (htblptr = NIL) then
  181. htblptr := jpeg_alloc_huff_table(j_common_ptr(cinfo));
  182. { Copy the number-of-symbols-of-each-code-length counts }
  183. MEMCOPY(@htblptr^.bits, @bits, SIZEOF(htblptr^.bits));
  184. { Validate the counts. We do this here mainly so we can copy the right
  185. number of symbols from the val[] array, without risking marching off
  186. the end of memory. jchuff.c will do a more thorough test later. }
  187. nsymbols := 0;
  188. for len := 1 to 16 do
  189. Inc(nsymbols, bits[len]);
  190. if (nsymbols < 1) or (nsymbols > 256) then
  191. ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
  192. MEMCOPY(@htblptr^.huffval, @val, nsymbols * SIZEOF(UINT8));
  193. { Initialize sent_table FALSE so table will be written to JPEG file. }
  194. (htblptr)^.sent_table := FALSE;
  195. end;
  196. {LOCAL}
  197. procedure std_huff_tables (cinfo : j_compress_ptr);
  198. { Set up the standard Huffman tables (cf. JPEG standard section K.3) }
  199. { IMPORTANT: these are only valid for 8-bit data precision! }
  200. const bits_dc_luminance : array[0..17-1] of UINT8 =
  201. ({ 0-base } 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
  202. const val_dc_luminance : array[0..11] of UINT8 =
  203. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  204. const bits_dc_chrominance : array[0..17-1] of UINT8 =
  205. ( { 0-base } 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
  206. const val_dc_chrominance : array[0..11] of UINT8 =
  207. ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 );
  208. const bits_ac_luminance : array[0..17-1] of UINT8 =
  209. ( { 0-base } 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, $7d );
  210. const val_ac_luminance : array[0..161] of UINT8 =
  211. ( $01, $02, $03, $00, $04, $11, $05, $12,
  212. $21, $31, $41, $06, $13, $51, $61, $07,
  213. $22, $71, $14, $32, $81, $91, $a1, $08,
  214. $23, $42, $b1, $c1, $15, $52, $d1, $f0,
  215. $24, $33, $62, $72, $82, $09, $0a, $16,
  216. $17, $18, $19, $1a, $25, $26, $27, $28,
  217. $29, $2a, $34, $35, $36, $37, $38, $39,
  218. $3a, $43, $44, $45, $46, $47, $48, $49,
  219. $4a, $53, $54, $55, $56, $57, $58, $59,
  220. $5a, $63, $64, $65, $66, $67, $68, $69,
  221. $6a, $73, $74, $75, $76, $77, $78, $79,
  222. $7a, $83, $84, $85, $86, $87, $88, $89,
  223. $8a, $92, $93, $94, $95, $96, $97, $98,
  224. $99, $9a, $a2, $a3, $a4, $a5, $a6, $a7,
  225. $a8, $a9, $aa, $b2, $b3, $b4, $b5, $b6,
  226. $b7, $b8, $b9, $ba, $c2, $c3, $c4, $c5,
  227. $c6, $c7, $c8, $c9, $ca, $d2, $d3, $d4,
  228. $d5, $d6, $d7, $d8, $d9, $da, $e1, $e2,
  229. $e3, $e4, $e5, $e6, $e7, $e8, $e9, $ea,
  230. $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8,
  231. $f9, $fa );
  232. const bits_ac_chrominance : array[0..17-1] of UINT8 =
  233. ( { 0-base } 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, $77 );
  234. const val_ac_chrominance : array[0..161] of UINT8 =
  235. ( $00, $01, $02, $03, $11, $04, $05, $21,
  236. $31, $06, $12, $41, $51, $07, $61, $71,
  237. $13, $22, $32, $81, $08, $14, $42, $91,
  238. $a1, $b1, $c1, $09, $23, $33, $52, $f0,
  239. $15, $62, $72, $d1, $0a, $16, $24, $34,
  240. $e1, $25, $f1, $17, $18, $19, $1a, $26,
  241. $27, $28, $29, $2a, $35, $36, $37, $38,
  242. $39, $3a, $43, $44, $45, $46, $47, $48,
  243. $49, $4a, $53, $54, $55, $56, $57, $58,
  244. $59, $5a, $63, $64, $65, $66, $67, $68,
  245. $69, $6a, $73, $74, $75, $76, $77, $78,
  246. $79, $7a, $82, $83, $84, $85, $86, $87,
  247. $88, $89, $8a, $92, $93, $94, $95, $96,
  248. $97, $98, $99, $9a, $a2, $a3, $a4, $a5,
  249. $a6, $a7, $a8, $a9, $aa, $b2, $b3, $b4,
  250. $b5, $b6, $b7, $b8, $b9, $ba, $c2, $c3,
  251. $c4, $c5, $c6, $c7, $c8, $c9, $ca, $d2,
  252. $d3, $d4, $d5, $d6, $d7, $d8, $d9, $da,
  253. $e2, $e3, $e4, $e5, $e6, $e7, $e8, $e9,
  254. $ea, $f2, $f3, $f4, $f5, $f6, $f7, $f8,
  255. $f9, $fa );
  256. begin
  257. add_huff_table(cinfo, cinfo^.dc_huff_tbl_ptrs[0],
  258. bits_dc_luminance, val_dc_luminance);
  259. add_huff_table(cinfo, cinfo^.ac_huff_tbl_ptrs[0],
  260. bits_ac_luminance, val_ac_luminance);
  261. add_huff_table(cinfo, cinfo^.dc_huff_tbl_ptrs[1],
  262. bits_dc_chrominance, val_dc_chrominance);
  263. add_huff_table(cinfo, cinfo^.ac_huff_tbl_ptrs[1],
  264. bits_ac_chrominance, val_ac_chrominance);
  265. end;
  266. { Default parameter setup for compression.
  267. Applications that don't choose to use this routine must do their
  268. own setup of all these parameters. Alternately, you can call this
  269. to establish defaults and then alter parameters selectively. This
  270. is the recommended approach since, if we add any new parameters,
  271. your code will still work (they'll be set to reasonable defaults). }
  272. {GLOBAL}
  273. procedure jpeg_set_defaults (cinfo : j_compress_ptr);
  274. var
  275. i : int;
  276. begin
  277. { Safety check to ensure start_compress not called yet. }
  278. if (cinfo^.global_state <> CSTATE_START) then
  279. ERREXIT1(J_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  280. { Allocate comp_info array large enough for maximum component count.
  281. Array is made permanent in case application wants to compress
  282. multiple images at same param settings. }
  283. if (cinfo^.comp_info = NIL) then
  284. cinfo^.comp_info := jpeg_component_info_list_ptr(
  285. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  286. MAX_COMPONENTS * SIZEOF(jpeg_component_info)) );
  287. { Initialize everything not dependent on the color space }
  288. cinfo^.data_precision := BITS_IN_JSAMPLE;
  289. { Set up two quantization tables using default quality of 75 }
  290. jpeg_set_quality(cinfo, 75, TRUE);
  291. { Set up two Huffman tables }
  292. std_huff_tables(cinfo);
  293. { Initialize default arithmetic coding conditioning }
  294. for i := 0 to pred(NUM_ARITH_TBLS) do
  295. begin
  296. cinfo^.arith_dc_L[i] := 0;
  297. cinfo^.arith_dc_U[i] := 1;
  298. cinfo^.arith_ac_K[i] := 5;
  299. end;
  300. { Default is no multiple-scan output }
  301. cinfo^.scan_info := NIL;
  302. cinfo^.num_scans := 0;
  303. { Expect normal source image, not raw downsampled data }
  304. cinfo^.raw_data_in := FALSE;
  305. { Use Huffman coding, not arithmetic coding, by default }
  306. cinfo^.arith_code := FALSE;
  307. { By default, don't do extra passes to optimize entropy coding }
  308. cinfo^.optimize_coding := FALSE;
  309. { The standard Huffman tables are only valid for 8-bit data precision.
  310. If the precision is higher, force optimization on so that usable
  311. tables will be computed. This test can be removed if default tables
  312. are supplied that are valid for the desired precision. }
  313. if (cinfo^.data_precision > 8) then
  314. cinfo^.optimize_coding := TRUE;
  315. { By default, use the simpler non-cosited sampling alignment }
  316. cinfo^.CCIR601_sampling := FALSE;
  317. { No input smoothing }
  318. cinfo^.smoothing_factor := 0;
  319. { DCT algorithm preference }
  320. cinfo^.dct_method := JDCT_DEFAULT;
  321. { No restart markers }
  322. cinfo^.restart_interval := 0;
  323. cinfo^.restart_in_rows := 0;
  324. { Fill in default JFIF marker parameters. Note that whether the marker
  325. will actually be written is determined by jpeg_set_colorspace.
  326. By default, the library emits JFIF version code 1.01.
  327. An application that wants to emit JFIF 1.02 extension markers should set
  328. JFIF_minor_version to 2. We could probably get away with just defaulting
  329. to 1.02, but there may still be some decoders in use that will complain
  330. about that; saying 1.01 should minimize compatibility problems. }
  331. cinfo^.JFIF_major_version := 1; { Default JFIF version = 1.01 }
  332. cinfo^.JFIF_minor_version := 1;
  333. cinfo^.density_unit := 0; { Pixel size is unknown by default }
  334. cinfo^.X_density := 1; { Pixel aspect ratio is square by default }
  335. cinfo^.Y_density := 1;
  336. { Choose JPEG colorspace based on input space, set defaults accordingly }
  337. jpeg_default_colorspace(cinfo);
  338. end;
  339. { Select an appropriate JPEG colorspace for in_color_space. }
  340. {GLOBAL}
  341. procedure jpeg_default_colorspace (cinfo : j_compress_ptr);
  342. begin
  343. case (cinfo^.in_color_space) of
  344. JCS_GRAYSCALE:
  345. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  346. JCS_RGB:
  347. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  348. JCS_YCbCr:
  349. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  350. JCS_CMYK:
  351. jpeg_set_colorspace(cinfo, JCS_CMYK); { By default, no translation }
  352. JCS_YCCK:
  353. jpeg_set_colorspace(cinfo, JCS_YCCK);
  354. JCS_UNKNOWN:
  355. jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  356. else
  357. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  358. end;
  359. end;
  360. { Set the JPEG colorspace, and choose colorspace-dependent default values. }
  361. {GLOBAL}
  362. procedure jpeg_set_colorspace (cinfo : j_compress_ptr;
  363. colorspace : J_COLOR_SPACE);
  364. { macro }
  365. procedure SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl : int);
  366. begin
  367. with cinfo^.comp_info^[index] do
  368. begin
  369. component_id := (id);
  370. h_samp_factor := (hsamp);
  371. v_samp_factor := (vsamp);
  372. quant_tbl_no := (quant);
  373. dc_tbl_no := (dctbl);
  374. ac_tbl_no := (actbl);
  375. end;
  376. end;
  377. var
  378. ci : int;
  379. begin
  380. { Safety check to ensure start_compress not called yet. }
  381. if (cinfo^.global_state <> CSTATE_START) then
  382. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  383. { For all colorspaces, we use Q and Huff tables 0 for luminance components,
  384. tables 1 for chrominance components. }
  385. cinfo^.jpeg_color_space := colorspace;
  386. cinfo^.write_JFIF_header := FALSE; { No marker for non-JFIF colorspaces }
  387. cinfo^.write_Adobe_marker := FALSE; { write no Adobe marker by default }
  388. case (colorspace) of
  389. JCS_GRAYSCALE:
  390. begin
  391. cinfo^.write_JFIF_header := TRUE; { Write a JFIF marker }
  392. cinfo^.num_components := 1;
  393. { JFIF specifies component ID 1 }
  394. SET_COMP(0, 1, 1,1, 0, 0,0);
  395. end;
  396. JCS_RGB:
  397. begin
  398. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag RGB }
  399. cinfo^.num_components := 3;
  400. SET_COMP(0, $52 { 'R' }, 1,1, 0, 0,0);
  401. SET_COMP(1, $47 { 'G' }, 1,1, 0, 0,0);
  402. SET_COMP(2, $42 { 'B' }, 1,1, 0, 0,0);
  403. end;
  404. JCS_YCbCr:
  405. begin
  406. cinfo^.write_JFIF_header := TRUE; { Write a JFIF marker }
  407. cinfo^.num_components := 3;
  408. { JFIF specifies component IDs 1,2,3 }
  409. { We default to 2x2 subsamples of chrominance }
  410. SET_COMP(0, 1, 2,2, 0, 0,0);
  411. SET_COMP(1, 2, 1,1, 1, 1,1);
  412. SET_COMP(2, 3, 1,1, 1, 1,1);
  413. end;
  414. JCS_CMYK:
  415. begin
  416. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag CMYK }
  417. cinfo^.num_components := 4;
  418. SET_COMP(0, $43 { 'C' }, 1,1, 0, 0,0);
  419. SET_COMP(1, $4D { 'M' }, 1,1, 0, 0,0);
  420. SET_COMP(2, $59 { 'Y' }, 1,1, 0, 0,0);
  421. SET_COMP(3, $4B { 'K' }, 1,1, 0, 0,0);
  422. end;
  423. JCS_YCCK:
  424. begin
  425. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag YCCK }
  426. cinfo^.num_components := 4;
  427. SET_COMP(0, 1, 2,2, 0, 0,0);
  428. SET_COMP(1, 2, 1,1, 1, 1,1);
  429. SET_COMP(2, 3, 1,1, 1, 1,1);
  430. SET_COMP(3, 4, 2,2, 0, 0,0);
  431. end;
  432. JCS_UNKNOWN:
  433. begin
  434. cinfo^.num_components := cinfo^.input_components;
  435. if (cinfo^.num_components < 1)
  436. or (cinfo^.num_components > MAX_COMPONENTS) then
  437. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT,
  438. cinfo^.num_components, MAX_COMPONENTS);
  439. for ci := 0 to pred(cinfo^.num_components) do
  440. begin
  441. SET_COMP(ci, ci, 1,1, 0, 0,0);
  442. end;
  443. end;
  444. else
  445. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  446. end;
  447. end;
  448. {$ifdef C_PROGRESSIVE_SUPPORTED}
  449. {LOCAL}
  450. function fill_a_scan (scanptr : jpeg_scan_info_ptr;
  451. ci : int; Ss : int;
  452. Se : int; Ah : int;
  453. Al : int) : jpeg_scan_info_ptr;
  454. { Support routine: generate one scan for specified component }
  455. begin
  456. scanptr^.comps_in_scan := 1;
  457. scanptr^.component_index[0] := ci;
  458. scanptr^.Ss := Ss;
  459. scanptr^.Se := Se;
  460. scanptr^.Ah := Ah;
  461. scanptr^.Al := Al;
  462. Inc(scanptr);
  463. fill_a_scan := scanptr;
  464. end;
  465. {LOCAL}
  466. function fill_scans (scanptr : jpeg_scan_info_ptr;
  467. ncomps : int;
  468. Ss : int; Se : int;
  469. Ah : int; Al : int) : jpeg_scan_info_ptr;
  470. { Support routine: generate one scan for each component }
  471. var
  472. ci : int;
  473. begin
  474. for ci := 0 to pred(ncomps) do
  475. begin
  476. scanptr^.comps_in_scan := 1;
  477. scanptr^.component_index[0] := ci;
  478. scanptr^.Ss := Ss;
  479. scanptr^.Se := Se;
  480. scanptr^.Ah := Ah;
  481. scanptr^.Al := Al;
  482. Inc(scanptr);
  483. end;
  484. fill_scans := scanptr;
  485. end;
  486. {LOCAL}
  487. function fill_dc_scans (scanptr : jpeg_scan_info_ptr;
  488. ncomps : int;
  489. Ah : int; Al : int) : jpeg_scan_info_ptr;
  490. { Support routine: generate interleaved DC scan if possible, else N scans }
  491. var
  492. ci : int;
  493. begin
  494. if (ncomps <= MAX_COMPS_IN_SCAN) then
  495. begin
  496. { Single interleaved DC scan }
  497. scanptr^.comps_in_scan := ncomps;
  498. for ci := 0 to pred(ncomps) do
  499. scanptr^.component_index[ci] := ci;
  500. scanptr^.Ss := 0;
  501. scanptr^.Se := 0;
  502. scanptr^.Ah := Ah;
  503. scanptr^.Al := Al;
  504. Inc(scanptr);
  505. end
  506. else
  507. begin
  508. { Noninterleaved DC scan for each component }
  509. scanptr := fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  510. end;
  511. fill_dc_scans := scanptr;
  512. end;
  513. { Create a recommended progressive-JPEG script.
  514. cinfo^.num_components and cinfo^.jpeg_color_space must be correct. }
  515. {GLOBAL}
  516. procedure jpeg_simple_progression (cinfo : j_compress_ptr);
  517. var
  518. ncomps : int;
  519. nscans : int;
  520. scanptr : jpeg_scan_info_ptr;
  521. begin
  522. ncomps := cinfo^.num_components;
  523. { Safety check to ensure start_compress not called yet. }
  524. if (cinfo^.global_state <> CSTATE_START) then
  525. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  526. { Figure space needed for script. Calculation must match code below! }
  527. if (ncomps = 3) and (cinfo^.jpeg_color_space = JCS_YCbCr) then
  528. begin
  529. { Custom script for YCbCr color images. }
  530. nscans := 10;
  531. end
  532. else
  533. begin
  534. { All-purpose script for other color spaces. }
  535. if (ncomps > MAX_COMPS_IN_SCAN) then
  536. nscans := 6 * ncomps { 2 DC + 4 AC scans per component }
  537. else
  538. nscans := 2 + 4 * ncomps; { 2 DC scans; 4 AC scans per component }
  539. end;
  540. { Allocate space for script.
  541. We need to put it in the permanent pool in case the application performs
  542. multiple compressions without changing the settings. To avoid a memory
  543. leak if jpeg_simple_progression is called repeatedly for the same JPEG
  544. object, we try to re-use previously allocated space, and we allocate
  545. enough space to handle YCbCr even if initially asked for grayscale. }
  546. if (cinfo^.script_space = NIL) or (cinfo^.script_space_size < nscans) then
  547. begin
  548. if nscans > 10 then
  549. cinfo^.script_space_size := nscans
  550. else
  551. cinfo^.script_space_size := 10;
  552. cinfo^.script_space := jpeg_scan_info_ptr(
  553. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  554. cinfo^.script_space_size * SIZEOF(jpeg_scan_info)) );
  555. end;
  556. scanptr := cinfo^.script_space;
  557. cinfo^.scan_info := scanptr;
  558. cinfo^.num_scans := nscans;
  559. if (ncomps = 3) and (cinfo^.jpeg_color_space = JCS_YCbCr) then
  560. begin
  561. { Custom script for YCbCr color images. }
  562. { Initial DC scan }
  563. scanptr := fill_dc_scans(scanptr, ncomps, 0, 1);
  564. { Initial AC scan: get some luma data out in a hurry }
  565. scanptr := fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  566. { Chroma data is too small to be worth expending many scans on }
  567. scanptr := fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  568. scanptr := fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  569. { Complete spectral selection for luma AC }
  570. scanptr := fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  571. { Refine next bit of luma AC }
  572. scanptr := fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  573. { Finish DC successive approximation }
  574. scanptr := fill_dc_scans(scanptr, ncomps, 1, 0);
  575. { Finish AC successive approximation }
  576. scanptr := fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  577. scanptr := fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  578. { Luma bottom bit comes last since it's usually largest scan }
  579. scanptr := fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  580. end
  581. else
  582. begin
  583. { All-purpose script for other color spaces. }
  584. { Successive approximation first pass }
  585. scanptr := fill_dc_scans(scanptr, ncomps, 0, 1);
  586. scanptr := fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  587. scanptr := fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  588. { Successive approximation second pass }
  589. scanptr := fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  590. { Successive approximation final pass }
  591. scanptr := fill_dc_scans(scanptr, ncomps, 1, 0);
  592. scanptr := fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  593. end;
  594. end;
  595. {$endif}
  596. end.