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.

513 lines
15 KiB

3 years ago
  1. unit imjcdctmgr;
  2. { Original : jcdctmgr.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  3. { This file is part of the Independent JPEG Group's software.
  4. For conditions of distribution and use, see the accompanying README file.
  5. This file contains the forward-DCT management logic.
  6. This code selects a particular DCT implementation to be used,
  7. and it performs related housekeeping chores including coefficient
  8. quantization. }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjinclude,
  14. imjdeferr,
  15. imjerror,
  16. imjpeglib,
  17. imjdct, { Private declarations for DCT subsystem }
  18. imjfdctint, imjfdctfst, imjfdctflt;
  19. { Initialize FDCT manager. }
  20. {GLOBAL}
  21. procedure jinit_forward_dct (cinfo : j_compress_ptr);
  22. implementation
  23. { Private subobject for this module }
  24. type
  25. my_fdct_ptr = ^my_fdct_controller;
  26. my_fdct_controller = record
  27. pub : jpeg_forward_dct; { public fields }
  28. { Pointer to the DCT routine actually in use }
  29. do_dct : forward_DCT_method_ptr;
  30. { The actual post-DCT divisors --- not identical to the quant table
  31. entries, because of scaling (especially for an unnormalized DCT).
  32. Each table is given in normal array order. }
  33. divisors : array[0..NUM_QUANT_TBLS-1] of DCTELEM_FIELD_PTR;
  34. {$ifdef DCT_FLOAT_SUPPORTED}
  35. { Same as above for the floating-point case. }
  36. do_float_dct : float_DCT_method_ptr;
  37. float_divisors : array[0..NUM_QUANT_TBLS-1] of FAST_FLOAT_FIELD_PTR;
  38. {$endif}
  39. end;
  40. { Initialize for a processing pass.
  41. Verify that all referenced Q-tables are present, and set up
  42. the divisor table for each one.
  43. In the current implementation, DCT of all components is done during
  44. the first pass, even if only some components will be output in the
  45. first scan. Hence all components should be examined here. }
  46. {METHODDEF}
  47. procedure start_pass_fdctmgr (cinfo : j_compress_ptr);
  48. var
  49. fdct : my_fdct_ptr;
  50. ci, qtblno, i : int;
  51. compptr : jpeg_component_info_ptr;
  52. qtbl : JQUANT_TBL_PTR;
  53. dtbl : DCTELEM_FIELD_PTR;
  54. {$ifdef DCT_IFAST_SUPPORTED}
  55. const
  56. CONST_BITS = 14;
  57. aanscales : array[0..DCTSIZE2-1] of INT16 =
  58. ({ precomputed values scaled up by 14 bits }
  59. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  60. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  61. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  62. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  63. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  64. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  65. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  66. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247);
  67. {SHIFT_TEMPS}
  68. { Descale and correctly round an INT32 value that's scaled by N bits.
  69. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  70. the fudge factor is correct for either sign of X. }
  71. function DESCALE(x : INT32; n : int) : INT32;
  72. var
  73. shift_temp : INT32;
  74. begin
  75. shift_temp := x + (INT32(1) shl (n-1));
  76. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  77. if shift_temp < 0 then
  78. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  79. else
  80. {$endif}
  81. Descale := (shift_temp shr n);
  82. end;
  83. {$endif}
  84. {$ifdef DCT_FLOAT_SUPPORTED}
  85. var
  86. fdtbl : FAST_FLOAT_FIELD_PTR;
  87. row, col : int;
  88. const
  89. aanscalefactor : array[0..DCTSIZE-1] of double =
  90. (1.0, 1.387039845, 1.306562965, 1.175875602,
  91. 1.0, 0.785694958, 0.541196100, 0.275899379);
  92. {$endif}
  93. begin
  94. fdct := my_fdct_ptr (cinfo^.fdct);
  95. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  96. for ci := 0 to pred(cinfo^.num_components) do
  97. begin
  98. qtblno := compptr^.quant_tbl_no;
  99. { Make sure specified quantization table is present }
  100. if (qtblno < 0) or (qtblno >= NUM_QUANT_TBLS) or
  101. (cinfo^.quant_tbl_ptrs[qtblno] = NIL) then
  102. ERREXIT1(j_common_ptr(cinfo), JERR_NO_QUANT_TABLE, qtblno);
  103. qtbl := cinfo^.quant_tbl_ptrs[qtblno];
  104. { Compute divisors for this quant table }
  105. { We may do this more than once for same table, but it's not a big deal }
  106. case (cinfo^.dct_method) of
  107. {$ifdef DCT_ISLOW_SUPPORTED}
  108. JDCT_ISLOW:
  109. begin
  110. { For LL&M IDCT method, divisors are equal to raw quantization
  111. coefficients multiplied by 8 (to counteract scaling). }
  112. if (fdct^.divisors[qtblno] = NIL) then
  113. begin
  114. fdct^.divisors[qtblno] := DCTELEM_FIELD_PTR(
  115. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  116. DCTSIZE2 * SIZEOF(DCTELEM)) );
  117. end;
  118. dtbl := fdct^.divisors[qtblno];
  119. for i := 0 to pred(DCTSIZE2) do
  120. begin
  121. dtbl^[i] := (DCTELEM(qtbl^.quantval[i])) shl 3;
  122. end;
  123. end;
  124. {$endif}
  125. {$ifdef DCT_IFAST_SUPPORTED}
  126. JDCT_IFAST:
  127. begin
  128. { For AA&N IDCT method, divisors are equal to quantization
  129. coefficients scaled by scalefactor[row]*scalefactor[col], where
  130. scalefactor[0] := 1
  131. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
  132. We apply a further scale factor of 8. }
  133. if (fdct^.divisors[qtblno] = NIL) then
  134. begin
  135. fdct^.divisors[qtblno] := DCTELEM_FIELD_PTR(
  136. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  137. DCTSIZE2 * SIZEOF(DCTELEM)) );
  138. end;
  139. dtbl := fdct^.divisors[qtblno];
  140. for i := 0 to pred(DCTSIZE2) do
  141. begin
  142. dtbl^[i] := DCTELEM(
  143. {MULTIPLY16V16}
  144. DESCALE( INT32(qtbl^.quantval[i]) * INT32 (aanscales[i]),
  145. CONST_BITS-3) );
  146. end;
  147. end;
  148. {$endif}
  149. {$ifdef DCT_FLOAT_SUPPORTED}
  150. JDCT_FLOAT:
  151. begin
  152. { For float AA&N IDCT method, divisors are equal to quantization
  153. coefficients scaled by scalefactor[row]*scalefactor[col], where
  154. scalefactor[0] := 1
  155. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
  156. We apply a further scale factor of 8.
  157. What's actually stored is 1/divisor so that the inner loop can
  158. use a multiplication rather than a division. }
  159. if (fdct^.float_divisors[qtblno] = NIL) then
  160. begin
  161. fdct^.float_divisors[qtblno] := FAST_FLOAT_FIELD_PTR(
  162. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  163. DCTSIZE2 * SIZEOF(FAST_FLOAT)) );
  164. end;
  165. fdtbl := fdct^.float_divisors[qtblno];
  166. i := 0;
  167. for row := 0 to pred(DCTSIZE) do
  168. begin
  169. for col := 0 to pred(DCTSIZE) do
  170. begin
  171. fdtbl^[i] := {FAST_FLOAT}
  172. (1.0 / (( {double}(qtbl^.quantval[i]) *
  173. aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  174. Inc(i);
  175. end;
  176. end;
  177. end;
  178. {$endif}
  179. else
  180. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  181. end;
  182. Inc(compptr);
  183. end;
  184. end;
  185. { Perform forward DCT on one or more blocks of a component.
  186. The input samples are taken from the sample_data[] array starting at
  187. position start_row/start_col, and moving to the right for any additional
  188. blocks. The quantized coefficients are returned in coef_blocks[]. }
  189. {METHODDEF}
  190. procedure forward_DCT (cinfo : j_compress_ptr;
  191. compptr : jpeg_component_info_ptr;
  192. sample_data : JSAMPARRAY;
  193. coef_blocks : JBLOCKROW;
  194. start_row : JDIMENSION;
  195. start_col : JDIMENSION;
  196. num_blocks : JDIMENSION);
  197. { This version is used for integer DCT implementations. }
  198. var
  199. { This routine is heavily used, so it's worth coding it tightly. }
  200. fdct : my_fdct_ptr;
  201. do_dct : forward_DCT_method_ptr;
  202. divisors : DCTELEM_FIELD_PTR;
  203. workspace : array[0..DCTSIZE2-1] of DCTELEM; { work area for FDCT subroutine }
  204. bi : JDIMENSION;
  205. var
  206. {register} workspaceptr : DCTELEMPTR;
  207. {register} elemptr : JSAMPLE_PTR;
  208. {register} elemr : int;
  209. {$ifndef DCTSIZE_IS_8}
  210. var
  211. {register} elemc : int;
  212. {$endif}
  213. var
  214. {register} temp, qval : DCTELEM;
  215. {register} i : int;
  216. {register} output_ptr : JCOEFPTR;
  217. begin
  218. fdct := my_fdct_ptr (cinfo^.fdct);
  219. do_dct := fdct^.do_dct;
  220. divisors := fdct^.divisors[compptr^.quant_tbl_no];
  221. Inc(JSAMPROW_PTR(sample_data), start_row); { fold in the vertical offset once }
  222. for bi := 0 to pred(num_blocks) do
  223. begin
  224. { Load data into workspace, applying unsigned->signed conversion }
  225. workspaceptr := @workspace[0];
  226. for elemr := 0 to pred(DCTSIZE) do
  227. begin
  228. elemptr := @sample_data^[elemr]^[start_col];
  229. {$ifdef DCTSIZE_IS_8} { unroll the inner loop }
  230. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  231. Inc(workspaceptr);
  232. Inc(elemptr);
  233. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  234. Inc(workspaceptr);
  235. Inc(elemptr);
  236. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  237. Inc(workspaceptr);
  238. Inc(elemptr);
  239. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  240. Inc(workspaceptr);
  241. Inc(elemptr);
  242. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  243. Inc(workspaceptr);
  244. Inc(elemptr);
  245. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  246. Inc(workspaceptr);
  247. Inc(elemptr);
  248. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  249. Inc(workspaceptr);
  250. Inc(elemptr);
  251. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  252. Inc(workspaceptr);
  253. {Inc(elemptr); - Value never used }
  254. {$else}
  255. for elemc := pred(DCTSIZE) downto 0 do
  256. begin
  257. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  258. Inc(workspaceptr);
  259. Inc(elemptr);
  260. end;
  261. {$endif}
  262. end;
  263. { Perform the DCT }
  264. do_dct (workspace);
  265. { Quantize/descale the coefficients, and store into coef_blocks[] }
  266. output_ptr := JCOEFPTR(@coef_blocks^[bi]);
  267. for i := 0 to pred(DCTSIZE2) do
  268. begin
  269. qval := divisors^[i];
  270. temp := workspace[i];
  271. { Divide the coefficient value by qval, ensuring proper rounding.
  272. Since C does not specify the direction of rounding for negative
  273. quotients, we have to force the dividend positive for portability.
  274. In most files, at least half of the output values will be zero
  275. (at default quantization settings, more like three-quarters...)
  276. so we should ensure that this case is fast. On many machines,
  277. a comparison is enough cheaper than a divide to make a special test
  278. a win. Since both inputs will be nonnegative, we need only test
  279. for a < b to discover whether a/b is 0.
  280. If your machine's division is fast enough, define FAST_DIVIDE. }
  281. if (temp < 0) then
  282. begin
  283. temp := -temp;
  284. Inc(temp, qval shr 1); { for rounding }
  285. {DIVIDE_BY(temp, qval);}
  286. {$ifdef FAST_DIVIDE}
  287. temp := temp div qval;
  288. {$else}
  289. if (temp >= qval) then
  290. temp := temp div qval
  291. else
  292. temp := 0;
  293. {$endif}
  294. temp := -temp;
  295. end
  296. else
  297. begin
  298. Inc(temp, qval shr 1); { for rounding }
  299. {DIVIDE_BY(temp, qval);}
  300. {$ifdef FAST_DIVIDE}
  301. temp := temp div qval;
  302. {$else}
  303. if (temp >= qval) then
  304. temp := temp div qval
  305. else
  306. temp := 0;
  307. {$endif}
  308. end;
  309. output_ptr^[i] := JCOEF (temp);
  310. end;
  311. Inc(start_col, DCTSIZE);
  312. end;
  313. end;
  314. {$ifdef DCT_FLOAT_SUPPORTED}
  315. {METHODDEF}
  316. procedure forward_DCT_float (cinfo : j_compress_ptr;
  317. compptr : jpeg_component_info_ptr;
  318. sample_data : JSAMPARRAY;
  319. coef_blocks : JBLOCKROW;
  320. start_row : JDIMENSION;
  321. start_col : JDIMENSION;
  322. num_blocks : JDIMENSION);
  323. { This version is used for floating-point DCT implementations. }
  324. var
  325. { This routine is heavily used, so it's worth coding it tightly. }
  326. fdct : my_fdct_ptr;
  327. do_dct : float_DCT_method_ptr;
  328. divisors : FAST_FLOAT_FIELD_PTR;
  329. workspace : array[0..DCTSIZE2-1] of FAST_FLOAT; { work area for FDCT subroutine }
  330. bi : JDIMENSION;
  331. var
  332. {register} workspaceptr : FAST_FLOAT_PTR;
  333. {register} elemptr : JSAMPLE_PTR;
  334. {register} elemr : int;
  335. {$ifndef DCTSIZE_IS_8}
  336. var
  337. {register} elemc : int;
  338. {$endif}
  339. var
  340. {register} temp : FAST_FLOAT;
  341. {register} i : int;
  342. {register} output_ptr : JCOEFPTR;
  343. begin
  344. fdct := my_fdct_ptr (cinfo^.fdct);
  345. do_dct := fdct^.do_float_dct;
  346. divisors := fdct^.float_divisors[compptr^.quant_tbl_no];
  347. Inc(JSAMPROW_PTR(sample_data), start_row); { fold in the vertical offset once }
  348. for bi := 0 to pred(num_blocks) do
  349. begin
  350. { Load data into workspace, applying unsigned->signed conversion }
  351. workspaceptr := @workspace[0];
  352. for elemr := 0 to pred(DCTSIZE) do
  353. begin
  354. elemptr := @(sample_data^[elemr]^[start_col]);
  355. {$ifdef DCTSIZE_IS_8} { unroll the inner loop }
  356. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  357. Inc(workspaceptr);
  358. Inc(elemptr);
  359. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  360. Inc(workspaceptr);
  361. Inc(elemptr);
  362. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  363. Inc(workspaceptr);
  364. Inc(elemptr);
  365. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  366. Inc(workspaceptr);
  367. Inc(elemptr);
  368. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  369. Inc(workspaceptr);
  370. Inc(elemptr);
  371. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  372. Inc(workspaceptr);
  373. Inc(elemptr);
  374. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  375. Inc(workspaceptr);
  376. Inc(elemptr);
  377. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  378. Inc(workspaceptr);
  379. {Inc(elemptr); - value never used }
  380. {$else}
  381. for elemc := pred(DCTSIZE) downto 0 do
  382. begin
  383. workspaceptr^ := {FAST_FLOAT}(
  384. (GETJSAMPLE(elemptr^) - CENTERJSAMPLE) );
  385. Inc(workspaceptr);
  386. Inc(elemptr);
  387. end;
  388. {$endif}
  389. end;
  390. { Perform the DCT }
  391. do_dct (workspace);
  392. { Quantize/descale the coefficients, and store into coef_blocks[] }
  393. output_ptr := JCOEFPTR(@(coef_blocks^[bi]));
  394. for i := 0 to pred(DCTSIZE2) do
  395. begin
  396. { Apply the quantization and scaling factor }
  397. temp := workspace[i] * divisors^[i];
  398. { Round to nearest integer.
  399. Since C does not specify the direction of rounding for negative
  400. quotients, we have to force the dividend positive for portability.
  401. The maximum coefficient size is +-16K (for 12-bit data), so this
  402. code should work for either 16-bit or 32-bit ints. }
  403. output_ptr^[i] := JCOEF ( int(Trunc (temp + {FAST_FLOAT}(16384.5))) - 16384);
  404. end;
  405. Inc(start_col, DCTSIZE);
  406. end;
  407. end;
  408. {$endif} { DCT_FLOAT_SUPPORTED }
  409. { Initialize FDCT manager. }
  410. {GLOBAL}
  411. procedure jinit_forward_dct (cinfo : j_compress_ptr);
  412. var
  413. fdct : my_fdct_ptr;
  414. i : int;
  415. begin
  416. fdct := my_fdct_ptr(
  417. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  418. SIZEOF(my_fdct_controller)) );
  419. cinfo^.fdct := jpeg_forward_dct_ptr (fdct);
  420. fdct^.pub.start_pass := start_pass_fdctmgr;
  421. case (cinfo^.dct_method) of
  422. {$ifdef DCT_ISLOW_SUPPORTED}
  423. JDCT_ISLOW:
  424. begin
  425. fdct^.pub.forward_DCT := forward_DCT;
  426. fdct^.do_dct := jpeg_fdct_islow;
  427. end;
  428. {$endif}
  429. {$ifdef DCT_IFAST_SUPPORTED}
  430. JDCT_IFAST:
  431. begin
  432. fdct^.pub.forward_DCT := forward_DCT;
  433. fdct^.do_dct := jpeg_fdct_ifast;
  434. end;
  435. {$endif}
  436. {$ifdef DCT_FLOAT_SUPPORTED}
  437. JDCT_FLOAT:
  438. begin
  439. fdct^.pub.forward_DCT := forward_DCT_float;
  440. fdct^.do_float_dct := jpeg_fdct_float;
  441. end;
  442. {$endif}
  443. else
  444. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  445. end;
  446. { Mark divisor tables unallocated }
  447. for i := 0 to pred(NUM_QUANT_TBLS) do
  448. begin
  449. fdct^.divisors[i] := NIL;
  450. {$ifdef DCT_FLOAT_SUPPORTED}
  451. fdct^.float_divisors[i] := NIL;
  452. {$endif}
  453. end;
  454. end;
  455. end.