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

701 lines
24 KiB

3 years ago
  1. unit imjcparam;
  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 imjconfig.inc}
  8. uses
  9. imjmorecfg,
  10. imjinclude,
  11. imjdeferr,
  12. imjerror,
  13. imjcomapi,
  14. imjpeglib;
  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. {$J+}
  197. {LOCAL}
  198. procedure std_huff_tables (cinfo : j_compress_ptr);
  199. { Set up the standard Huffman tables (cf. JPEG standard section K.3) }
  200. { IMPORTANT: these are only valid for 8-bit data precision! }
  201. const bits_dc_luminance : array[0..17-1] of UINT8 =
  202. ({ 0-base } 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
  203. const val_dc_luminance : array[0..11] of UINT8 =
  204. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  205. const bits_dc_chrominance : array[0..17-1] of UINT8 =
  206. ( { 0-base } 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
  207. const val_dc_chrominance : array[0..11] of UINT8 =
  208. ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 );
  209. const bits_ac_luminance : array[0..17-1] of UINT8 =
  210. ( { 0-base } 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, $7d );
  211. const val_ac_luminance : array[0..161] of UINT8 =
  212. ( $01, $02, $03, $00, $04, $11, $05, $12,
  213. $21, $31, $41, $06, $13, $51, $61, $07,
  214. $22, $71, $14, $32, $81, $91, $a1, $08,
  215. $23, $42, $b1, $c1, $15, $52, $d1, $f0,
  216. $24, $33, $62, $72, $82, $09, $0a, $16,
  217. $17, $18, $19, $1a, $25, $26, $27, $28,
  218. $29, $2a, $34, $35, $36, $37, $38, $39,
  219. $3a, $43, $44, $45, $46, $47, $48, $49,
  220. $4a, $53, $54, $55, $56, $57, $58, $59,
  221. $5a, $63, $64, $65, $66, $67, $68, $69,
  222. $6a, $73, $74, $75, $76, $77, $78, $79,
  223. $7a, $83, $84, $85, $86, $87, $88, $89,
  224. $8a, $92, $93, $94, $95, $96, $97, $98,
  225. $99, $9a, $a2, $a3, $a4, $a5, $a6, $a7,
  226. $a8, $a9, $aa, $b2, $b3, $b4, $b5, $b6,
  227. $b7, $b8, $b9, $ba, $c2, $c3, $c4, $c5,
  228. $c6, $c7, $c8, $c9, $ca, $d2, $d3, $d4,
  229. $d5, $d6, $d7, $d8, $d9, $da, $e1, $e2,
  230. $e3, $e4, $e5, $e6, $e7, $e8, $e9, $ea,
  231. $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8,
  232. $f9, $fa );
  233. const bits_ac_chrominance : array[0..17-1] of UINT8 =
  234. ( { 0-base } 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, $77 );
  235. const val_ac_chrominance : array[0..161] of UINT8 =
  236. ( $00, $01, $02, $03, $11, $04, $05, $21,
  237. $31, $06, $12, $41, $51, $07, $61, $71,
  238. $13, $22, $32, $81, $08, $14, $42, $91,
  239. $a1, $b1, $c1, $09, $23, $33, $52, $f0,
  240. $15, $62, $72, $d1, $0a, $16, $24, $34,
  241. $e1, $25, $f1, $17, $18, $19, $1a, $26,
  242. $27, $28, $29, $2a, $35, $36, $37, $38,
  243. $39, $3a, $43, $44, $45, $46, $47, $48,
  244. $49, $4a, $53, $54, $55, $56, $57, $58,
  245. $59, $5a, $63, $64, $65, $66, $67, $68,
  246. $69, $6a, $73, $74, $75, $76, $77, $78,
  247. $79, $7a, $82, $83, $84, $85, $86, $87,
  248. $88, $89, $8a, $92, $93, $94, $95, $96,
  249. $97, $98, $99, $9a, $a2, $a3, $a4, $a5,
  250. $a6, $a7, $a8, $a9, $aa, $b2, $b3, $b4,
  251. $b5, $b6, $b7, $b8, $b9, $ba, $c2, $c3,
  252. $c4, $c5, $c6, $c7, $c8, $c9, $ca, $d2,
  253. $d3, $d4, $d5, $d6, $d7, $d8, $d9, $da,
  254. $e2, $e3, $e4, $e5, $e6, $e7, $e8, $e9,
  255. $ea, $f2, $f3, $f4, $f5, $f6, $f7, $f8,
  256. $f9, $fa );
  257. begin
  258. add_huff_table(cinfo, cinfo^.dc_huff_tbl_ptrs[0],
  259. bits_dc_luminance, val_dc_luminance);
  260. add_huff_table(cinfo, cinfo^.ac_huff_tbl_ptrs[0],
  261. bits_ac_luminance, val_ac_luminance);
  262. add_huff_table(cinfo, cinfo^.dc_huff_tbl_ptrs[1],
  263. bits_dc_chrominance, val_dc_chrominance);
  264. add_huff_table(cinfo, cinfo^.ac_huff_tbl_ptrs[1],
  265. bits_ac_chrominance, val_ac_chrominance);
  266. end;
  267. { Default parameter setup for compression.
  268. Applications that don't choose to use this routine must do their
  269. own setup of all these parameters. Alternately, you can call this
  270. to establish defaults and then alter parameters selectively. This
  271. is the recommended approach since, if we add any new parameters,
  272. your code will still work (they'll be set to reasonable defaults). }
  273. {GLOBAL}
  274. procedure jpeg_set_defaults (cinfo : j_compress_ptr);
  275. var
  276. i : int;
  277. begin
  278. { Safety check to ensure start_compress not called yet. }
  279. if (cinfo^.global_state <> CSTATE_START) then
  280. ERREXIT1(J_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  281. { Allocate comp_info array large enough for maximum component count.
  282. Array is made permanent in case application wants to compress
  283. multiple images at same param settings. }
  284. if (cinfo^.comp_info = NIL) then
  285. cinfo^.comp_info := jpeg_component_info_list_ptr(
  286. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  287. MAX_COMPONENTS * SIZEOF(jpeg_component_info)) );
  288. { Initialize everything not dependent on the color space }
  289. cinfo^.data_precision := BITS_IN_JSAMPLE;
  290. { Set up two quantization tables using default quality of 75 }
  291. jpeg_set_quality(cinfo, 75, TRUE);
  292. { Set up two Huffman tables }
  293. std_huff_tables(cinfo);
  294. { Initialize default arithmetic coding conditioning }
  295. for i := 0 to pred(NUM_ARITH_TBLS) do
  296. begin
  297. cinfo^.arith_dc_L[i] := 0;
  298. cinfo^.arith_dc_U[i] := 1;
  299. cinfo^.arith_ac_K[i] := 5;
  300. end;
  301. { Default is no multiple-scan output }
  302. cinfo^.scan_info := NIL;
  303. cinfo^.num_scans := 0;
  304. { Expect normal source image, not raw downsampled data }
  305. cinfo^.raw_data_in := FALSE;
  306. { Use Huffman coding, not arithmetic coding, by default }
  307. cinfo^.arith_code := FALSE;
  308. { By default, don't do extra passes to optimize entropy coding }
  309. cinfo^.optimize_coding := FALSE;
  310. { The standard Huffman tables are only valid for 8-bit data precision.
  311. If the precision is higher, force optimization on so that usable
  312. tables will be computed. This test can be removed if default tables
  313. are supplied that are valid for the desired precision. }
  314. if (cinfo^.data_precision > 8) then
  315. cinfo^.optimize_coding := TRUE;
  316. { By default, use the simpler non-cosited sampling alignment }
  317. cinfo^.CCIR601_sampling := FALSE;
  318. { No input smoothing }
  319. cinfo^.smoothing_factor := 0;
  320. { DCT algorithm preference }
  321. cinfo^.dct_method := JDCT_DEFAULT;
  322. { No restart markers }
  323. cinfo^.restart_interval := 0;
  324. cinfo^.restart_in_rows := 0;
  325. { Fill in default JFIF marker parameters. Note that whether the marker
  326. will actually be written is determined by jpeg_set_colorspace.
  327. By default, the library emits JFIF version code 1.01.
  328. An application that wants to emit JFIF 1.02 extension markers should set
  329. JFIF_minor_version to 2. We could probably get away with just defaulting
  330. to 1.02, but there may still be some decoders in use that will complain
  331. about that; saying 1.01 should minimize compatibility problems. }
  332. cinfo^.JFIF_major_version := 1; { Default JFIF version = 1.01 }
  333. cinfo^.JFIF_minor_version := 1;
  334. cinfo^.density_unit := 0; { Pixel size is unknown by default }
  335. cinfo^.X_density := 1; { Pixel aspect ratio is square by default }
  336. cinfo^.Y_density := 1;
  337. { Choose JPEG colorspace based on input space, set defaults accordingly }
  338. jpeg_default_colorspace(cinfo);
  339. end;
  340. { Select an appropriate JPEG colorspace for in_color_space. }
  341. {GLOBAL}
  342. procedure jpeg_default_colorspace (cinfo : j_compress_ptr);
  343. begin
  344. case (cinfo^.in_color_space) of
  345. JCS_GRAYSCALE:
  346. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  347. JCS_RGB:
  348. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  349. JCS_YCbCr:
  350. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  351. JCS_CMYK:
  352. jpeg_set_colorspace(cinfo, JCS_CMYK); { By default, no translation }
  353. JCS_YCCK:
  354. jpeg_set_colorspace(cinfo, JCS_YCCK);
  355. JCS_UNKNOWN:
  356. jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  357. else
  358. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  359. end;
  360. end;
  361. { Set the JPEG colorspace, and choose colorspace-dependent default values. }
  362. {GLOBAL}
  363. procedure jpeg_set_colorspace (cinfo : j_compress_ptr;
  364. colorspace : J_COLOR_SPACE);
  365. { macro }
  366. procedure SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl : int);
  367. begin
  368. with cinfo^.comp_info^[index] do
  369. begin
  370. component_id := (id);
  371. h_samp_factor := (hsamp);
  372. v_samp_factor := (vsamp);
  373. quant_tbl_no := (quant);
  374. dc_tbl_no := (dctbl);
  375. ac_tbl_no := (actbl);
  376. end;
  377. end;
  378. var
  379. ci : int;
  380. begin
  381. { Safety check to ensure start_compress not called yet. }
  382. if (cinfo^.global_state <> CSTATE_START) then
  383. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  384. { For all colorspaces, we use Q and Huff tables 0 for luminance components,
  385. tables 1 for chrominance components. }
  386. cinfo^.jpeg_color_space := colorspace;
  387. cinfo^.write_JFIF_header := FALSE; { No marker for non-JFIF colorspaces }
  388. cinfo^.write_Adobe_marker := FALSE; { write no Adobe marker by default }
  389. case (colorspace) of
  390. JCS_GRAYSCALE:
  391. begin
  392. cinfo^.write_JFIF_header := TRUE; { Write a JFIF marker }
  393. cinfo^.num_components := 1;
  394. { JFIF specifies component ID 1 }
  395. SET_COMP(0, 1, 1,1, 0, 0,0);
  396. end;
  397. JCS_RGB:
  398. begin
  399. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag RGB }
  400. cinfo^.num_components := 3;
  401. SET_COMP(0, $52 { 'R' }, 1,1, 0, 0,0);
  402. SET_COMP(1, $47 { 'G' }, 1,1, 0, 0,0);
  403. SET_COMP(2, $42 { 'B' }, 1,1, 0, 0,0);
  404. end;
  405. JCS_YCbCr:
  406. begin
  407. cinfo^.write_JFIF_header := TRUE; { Write a JFIF marker }
  408. cinfo^.num_components := 3;
  409. { JFIF specifies component IDs 1,2,3 }
  410. { We default to 2x2 subsamples of chrominance }
  411. SET_COMP(0, 1, 2,2, 0, 0,0);
  412. SET_COMP(1, 2, 1,1, 1, 1,1);
  413. SET_COMP(2, 3, 1,1, 1, 1,1);
  414. end;
  415. JCS_CMYK:
  416. begin
  417. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag CMYK }
  418. cinfo^.num_components := 4;
  419. SET_COMP(0, $43 { 'C' }, 1,1, 0, 0,0);
  420. SET_COMP(1, $4D { 'M' }, 1,1, 0, 0,0);
  421. SET_COMP(2, $59 { 'Y' }, 1,1, 0, 0,0);
  422. SET_COMP(3, $4B { 'K' }, 1,1, 0, 0,0);
  423. end;
  424. JCS_YCCK:
  425. begin
  426. cinfo^.write_Adobe_marker := TRUE; { write Adobe marker to flag YCCK }
  427. cinfo^.num_components := 4;
  428. SET_COMP(0, 1, 2,2, 0, 0,0);
  429. SET_COMP(1, 2, 1,1, 1, 1,1);
  430. SET_COMP(2, 3, 1,1, 1, 1,1);
  431. SET_COMP(3, 4, 2,2, 0, 0,0);
  432. end;
  433. JCS_UNKNOWN:
  434. begin
  435. cinfo^.num_components := cinfo^.input_components;
  436. if (cinfo^.num_components < 1)
  437. or (cinfo^.num_components > MAX_COMPONENTS) then
  438. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT,
  439. cinfo^.num_components, MAX_COMPONENTS);
  440. for ci := 0 to pred(cinfo^.num_components) do
  441. begin
  442. SET_COMP(ci, ci, 1,1, 0, 0,0);
  443. end;
  444. end;
  445. else
  446. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  447. end;
  448. end;
  449. {$ifdef C_PROGRESSIVE_SUPPORTED}
  450. {LOCAL}
  451. function fill_a_scan (scanptr : jpeg_scan_info_ptr;
  452. ci : int; Ss : int;
  453. Se : int; Ah : int;
  454. Al : int) : jpeg_scan_info_ptr;
  455. { Support routine: generate one scan for specified component }
  456. begin
  457. scanptr^.comps_in_scan := 1;
  458. scanptr^.component_index[0] := ci;
  459. scanptr^.Ss := Ss;
  460. scanptr^.Se := Se;
  461. scanptr^.Ah := Ah;
  462. scanptr^.Al := Al;
  463. Inc(scanptr);
  464. fill_a_scan := scanptr;
  465. end;
  466. {LOCAL}
  467. function fill_scans (scanptr : jpeg_scan_info_ptr;
  468. ncomps : int;
  469. Ss : int; Se : int;
  470. Ah : int; Al : int) : jpeg_scan_info_ptr;
  471. { Support routine: generate one scan for each component }
  472. var
  473. ci : int;
  474. begin
  475. for ci := 0 to pred(ncomps) do
  476. begin
  477. scanptr^.comps_in_scan := 1;
  478. scanptr^.component_index[0] := ci;
  479. scanptr^.Ss := Ss;
  480. scanptr^.Se := Se;
  481. scanptr^.Ah := Ah;
  482. scanptr^.Al := Al;
  483. Inc(scanptr);
  484. end;
  485. fill_scans := scanptr;
  486. end;
  487. {LOCAL}
  488. function fill_dc_scans (scanptr : jpeg_scan_info_ptr;
  489. ncomps : int;
  490. Ah : int; Al : int) : jpeg_scan_info_ptr;
  491. { Support routine: generate interleaved DC scan if possible, else N scans }
  492. var
  493. ci : int;
  494. begin
  495. if (ncomps <= MAX_COMPS_IN_SCAN) then
  496. begin
  497. { Single interleaved DC scan }
  498. scanptr^.comps_in_scan := ncomps;
  499. for ci := 0 to pred(ncomps) do
  500. scanptr^.component_index[ci] := ci;
  501. scanptr^.Ss := 0;
  502. scanptr^.Se := 0;
  503. scanptr^.Ah := Ah;
  504. scanptr^.Al := Al;
  505. Inc(scanptr);
  506. end
  507. else
  508. begin
  509. { Noninterleaved DC scan for each component }
  510. scanptr := fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  511. end;
  512. fill_dc_scans := scanptr;
  513. end;
  514. { Create a recommended progressive-JPEG script.
  515. cinfo^.num_components and cinfo^.jpeg_color_space must be correct. }
  516. {GLOBAL}
  517. procedure jpeg_simple_progression (cinfo : j_compress_ptr);
  518. var
  519. ncomps : int;
  520. nscans : int;
  521. scanptr : jpeg_scan_info_ptr;
  522. begin
  523. ncomps := cinfo^.num_components;
  524. { Safety check to ensure start_compress not called yet. }
  525. if (cinfo^.global_state <> CSTATE_START) then
  526. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  527. { Figure space needed for script. Calculation must match code below! }
  528. if (ncomps = 3) and (cinfo^.jpeg_color_space = JCS_YCbCr) then
  529. begin
  530. { Custom script for YCbCr color images. }
  531. nscans := 10;
  532. end
  533. else
  534. begin
  535. { All-purpose script for other color spaces. }
  536. if (ncomps > MAX_COMPS_IN_SCAN) then
  537. nscans := 6 * ncomps { 2 DC + 4 AC scans per component }
  538. else
  539. nscans := 2 + 4 * ncomps; { 2 DC scans; 4 AC scans per component }
  540. end;
  541. { Allocate space for script.
  542. We need to put it in the permanent pool in case the application performs
  543. multiple compressions without changing the settings. To avoid a memory
  544. leak if jpeg_simple_progression is called repeatedly for the same JPEG
  545. object, we try to re-use previously allocated space, and we allocate
  546. enough space to handle YCbCr even if initially asked for grayscale. }
  547. if (cinfo^.script_space = NIL) or (cinfo^.script_space_size < nscans) then
  548. begin
  549. if nscans > 10 then
  550. cinfo^.script_space_size := nscans
  551. else
  552. cinfo^.script_space_size := 10;
  553. cinfo^.script_space := jpeg_scan_info_ptr(
  554. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  555. cinfo^.script_space_size * SIZEOF(jpeg_scan_info)) );
  556. end;
  557. scanptr := cinfo^.script_space;
  558. cinfo^.scan_info := scanptr;
  559. cinfo^.num_scans := nscans;
  560. if (ncomps = 3) and (cinfo^.jpeg_color_space = JCS_YCbCr) then
  561. begin
  562. { Custom script for YCbCr color images. }
  563. { Initial DC scan }
  564. scanptr := fill_dc_scans(scanptr, ncomps, 0, 1);
  565. { Initial AC scan: get some luma data out in a hurry }
  566. scanptr := fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  567. { Chroma data is too small to be worth expending many scans on }
  568. scanptr := fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  569. scanptr := fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  570. { Complete spectral selection for luma AC }
  571. scanptr := fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  572. { Refine next bit of luma AC }
  573. scanptr := fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  574. { Finish DC successive approximation }
  575. scanptr := fill_dc_scans(scanptr, ncomps, 1, 0);
  576. { Finish AC successive approximation }
  577. scanptr := fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  578. scanptr := fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  579. { Luma bottom bit comes last since it's usually largest scan }
  580. scanptr := fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  581. end
  582. else
  583. begin
  584. { All-purpose script for other color spaces. }
  585. { Successive approximation first pass }
  586. scanptr := fill_dc_scans(scanptr, ncomps, 0, 1);
  587. scanptr := fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  588. scanptr := fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  589. { Successive approximation second pass }
  590. scanptr := fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  591. { Successive approximation final pass }
  592. scanptr := fill_dc_scans(scanptr, ncomps, 1, 0);
  593. scanptr := fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  594. end;
  595. end;
  596. {$endif}
  597. end.