jcparam.pas 24 KB

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