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.

410 lines
14 KiB

3 years ago
  1. unit imjidctfst;
  2. { This file contains a fast, not so accurate integer implementation of the
  3. inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  4. must also perform dequantization of the input coefficients.
  5. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  6. on each row (or vice versa, but it's more convenient to emit a row at
  7. a time). Direct algorithms are also available, but they are much more
  8. complex and seem not to be any faster when reduced to code.
  9. This implementation is based on Arai, Agui, and Nakajima's algorithm for
  10. scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  11. Japanese, but the algorithm is described in the Pennebaker & Mitchell
  12. JPEG textbook (see REFERENCES section in file README). The following code
  13. is based directly on figure 4-8 in P&M.
  14. While an 8-point DCT cannot be done in less than 11 multiplies, it is
  15. possible to arrange the computation so that many of the multiplies are
  16. simple scalings of the final outputs. These multiplies can then be
  17. folded into the multiplications or divisions by the JPEG quantization
  18. table entries. The AA&N method leaves only 5 multiplies and 29 adds
  19. to be done in the DCT itself.
  20. The primary disadvantage of this method is that with fixed-point math,
  21. accuracy is lost due to imprecise representation of the scaled
  22. quantization values. The smaller the quantization table entry, the less
  23. precise the scaled value, so this implementation does worse with high-
  24. quality-setting files than with low-quality ones. }
  25. { Original : jidctfst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  26. interface
  27. {$I imjconfig.inc}
  28. uses
  29. imjmorecfg,
  30. imjinclude,
  31. imjpeglib,
  32. imjdct; { Private declarations for DCT subsystem }
  33. { Perform dequantization and inverse DCT on one block of coefficients. }
  34. {GLOBAL}
  35. procedure jpeg_idct_ifast (cinfo : j_decompress_ptr;
  36. compptr : jpeg_component_info_ptr;
  37. coef_block : JCOEFPTR;
  38. output_buf : JSAMPARRAY;
  39. output_col : JDIMENSION);
  40. implementation
  41. { This module is specialized to the case DCTSIZE = 8. }
  42. {$ifndef DCTSIZE_IS_8}
  43. Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
  44. {$endif}
  45. { Scaling decisions are generally the same as in the LL&M algorithm;
  46. see jidctint.c for more details. However, we choose to descale
  47. (right shift) multiplication products as soon as they are formed,
  48. rather than carrying additional fractional bits into subsequent additions.
  49. This compromises accuracy slightly, but it lets us save a few shifts.
  50. More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  51. everywhere except in the multiplications proper; this saves a good deal
  52. of work on 16-bit-int machines.
  53. The dequantized coefficients are not integers because the AA&N scaling
  54. factors have been incorporated. We represent them scaled up by PASS1_BITS,
  55. so that the first and second IDCT rounds have the same input scaling.
  56. For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
  57. avoid a descaling shift; this compromises accuracy rather drastically
  58. for small quantization table entries, but it saves a lot of shifts.
  59. For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
  60. so we use a much larger scaling factor to preserve accuracy.
  61. A final compromise is to represent the multiplicative constants to only
  62. 8 fractional bits, rather than 13. This saves some shifting work on some
  63. machines, and may also reduce the cost of multiplication (since there
  64. are fewer one-bits in the constants). }
  65. {$ifdef BITS_IN_JSAMPLE_IS_8}
  66. const
  67. CONST_BITS = 8;
  68. PASS1_BITS = 2;
  69. {$else}
  70. const
  71. CONST_BITS = 8;
  72. PASS1_BITS = 1; { lose a little precision to avoid overflow }
  73. {$endif}
  74. const
  75. FIX_1_082392200 = INT32(Round((INT32(1) shl CONST_BITS)*1.082392200)); {277}
  76. FIX_1_414213562 = INT32(Round((INT32(1) shl CONST_BITS)*1.414213562)); {362}
  77. FIX_1_847759065 = INT32(Round((INT32(1) shl CONST_BITS)*1.847759065)); {473}
  78. FIX_2_613125930 = INT32(Round((INT32(1) shl CONST_BITS)*2.613125930)); {669}
  79. { Descale and correctly round an INT32 value that's scaled by N bits.
  80. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  81. the fudge factor is correct for either sign of X. }
  82. function DESCALE(x : INT32; n : int) : INT32;
  83. var
  84. shift_temp : INT32;
  85. begin
  86. {$ifdef USE_ACCURATE_ROUNDING}
  87. shift_temp := x + (INT32(1) shl (n-1));
  88. {$else}
  89. { We can gain a little more speed, with a further compromise in accuracy,
  90. by omitting the addition in a descaling shift. This yields an incorrectly
  91. rounded result half the time... }
  92. shift_temp := x;
  93. {$endif}
  94. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  95. if shift_temp < 0 then
  96. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  97. else
  98. {$endif}
  99. Descale := (shift_temp shr n);
  100. end;
  101. { Multiply a DCTELEM variable by an INT32 constant, and immediately
  102. descale to yield a DCTELEM result. }
  103. {(DCTELEM( DESCALE((var) * (const), CONST_BITS))}
  104. function Multiply(Avar, Aconst: Integer): DCTELEM;
  105. begin
  106. Multiply := DCTELEM( Avar*INT32(Aconst) div (INT32(1) shl CONST_BITS));
  107. end;
  108. { Dequantize a coefficient by multiplying it by the multiplier-table
  109. entry; produce a DCTELEM result. For 8-bit data a 16x16->16
  110. multiplication will do. For 12-bit data, the multiplier table is
  111. declared INT32, so a 32-bit multiply will be used. }
  112. {$ifdef BITS_IN_JSAMPLE_IS_8}
  113. function DEQUANTIZE(coef,quantval : int) : int;
  114. begin
  115. Dequantize := ( IFAST_MULT_TYPE(coef) * quantval);
  116. end;
  117. {$else}
  118. function DEQUANTIZE(coef,quantval : INT32) : int;
  119. begin
  120. Dequantize := DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS);
  121. end;
  122. {$endif}
  123. { Like DESCALE, but applies to a DCTELEM and produces an int.
  124. We assume that int right shift is unsigned if INT32 right shift is. }
  125. function IDESCALE(x : DCTELEM; n : int) : int;
  126. {$ifdef BITS_IN_JSAMPLE_IS_8}
  127. const
  128. DCTELEMBITS = 16; { DCTELEM may be 16 or 32 bits }
  129. {$else}
  130. const
  131. DCTELEMBITS = 32; { DCTELEM must be 32 bits }
  132. {$endif}
  133. var
  134. ishift_temp : DCTELEM;
  135. begin
  136. {$ifndef USE_ACCURATE_ROUNDING}
  137. ishift_temp := x + (INT32(1) shl (n-1));
  138. {$else}
  139. { We can gain a little more speed, with a further compromise in accuracy,
  140. by omitting the addition in a descaling shift. This yields an incorrectly
  141. rounded result half the time... }
  142. ishift_temp := x;
  143. {$endif}
  144. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  145. if ishift_temp < 0 then
  146. IDescale := (ishift_temp shr n)
  147. or ((not DCTELEM(0)) shl (DCTELEMBITS-n))
  148. else
  149. {$endif}
  150. IDescale := (ishift_temp shr n);
  151. end;
  152. { Perform dequantization and inverse DCT on one block of coefficients. }
  153. {GLOBAL}
  154. procedure jpeg_idct_ifast (cinfo : j_decompress_ptr;
  155. compptr : jpeg_component_info_ptr;
  156. coef_block : JCOEFPTR;
  157. output_buf : JSAMPARRAY;
  158. output_col : JDIMENSION);
  159. type
  160. PWorkspace = ^TWorkspace;
  161. TWorkspace = coef_bits_field; { buffers data between passes }
  162. var
  163. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : DCTELEM;
  164. tmp10, tmp11, tmp12, tmp13 : DCTELEM;
  165. z5, z10, z11, z12, z13 : DCTELEM;
  166. inptr : JCOEFPTR;
  167. quantptr : IFAST_MULT_TYPE_FIELD_PTR;
  168. wsptr : PWorkspace;
  169. outptr : JSAMPROW;
  170. range_limit : JSAMPROW;
  171. ctr : int;
  172. workspace : TWorkspace; { buffers data between passes }
  173. {SHIFT_TEMPS} { for DESCALE }
  174. {ISHIFT_TEMPS} { for IDESCALE }
  175. var
  176. dcval : int;
  177. var
  178. dcval_ : JSAMPLE;
  179. begin
  180. { Each IDCT routine is responsible for range-limiting its results and
  181. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  182. be quite far out of range if the input data is corrupt, so a bulletproof
  183. range-limiting step is required. We use a mask-and-table-lookup method
  184. to do the combined operations quickly. See the comments with
  185. prepare_range_limit_table (in jdmaster.c) for more info. }
  186. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  187. { Pass 1: process columns from input, store into work array. }
  188. inptr := coef_block;
  189. quantptr := IFAST_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
  190. wsptr := @workspace;
  191. for ctr := pred(DCTSIZE) downto 0 do
  192. begin
  193. { Due to quantization, we will usually find that many of the input
  194. coefficients are zero, especially the AC terms. We can exploit this
  195. by short-circuiting the IDCT calculation for any column in which all
  196. the AC terms are zero. In that case each output is equal to the
  197. DC coefficient (with scale factor as needed).
  198. With typical images and quantization tables, half or more of the
  199. column DCT calculations can be simplified this way. }
  200. if (inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and (inptr^[DCTSIZE*3]=0) and
  201. (inptr^[DCTSIZE*4]=0) and (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
  202. (inptr^[DCTSIZE*7]=0) then
  203. begin
  204. { AC terms all zero }
  205. dcval := int(DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]));
  206. wsptr^[DCTSIZE*0] := dcval;
  207. wsptr^[DCTSIZE*1] := dcval;
  208. wsptr^[DCTSIZE*2] := dcval;
  209. wsptr^[DCTSIZE*3] := dcval;
  210. wsptr^[DCTSIZE*4] := dcval;
  211. wsptr^[DCTSIZE*5] := dcval;
  212. wsptr^[DCTSIZE*6] := dcval;
  213. wsptr^[DCTSIZE*7] := dcval;
  214. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  215. Inc(IFAST_MULT_TYPE_PTR(quantptr));
  216. Inc(int_ptr(wsptr));
  217. continue;
  218. end;
  219. { Even part }
  220. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  221. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  222. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  223. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  224. tmp10 := tmp0 + tmp2; { phase 3 }
  225. tmp11 := tmp0 - tmp2;
  226. tmp13 := tmp1 + tmp3; { phases 5-3 }
  227. tmp12 := MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; { 2*c4 }
  228. tmp0 := tmp10 + tmp13; { phase 2 }
  229. tmp3 := tmp10 - tmp13;
  230. tmp1 := tmp11 + tmp12;
  231. tmp2 := tmp11 - tmp12;
  232. { Odd part }
  233. tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  234. tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  235. tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  236. tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  237. z13 := tmp6 + tmp5; { phase 6 }
  238. z10 := tmp6 - tmp5;
  239. z11 := tmp4 + tmp7;
  240. z12 := tmp4 - tmp7;
  241. tmp7 := z11 + z13; { phase 5 }
  242. tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
  243. z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
  244. tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
  245. tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
  246. tmp6 := tmp12 - tmp7; { phase 2 }
  247. tmp5 := tmp11 - tmp6;
  248. tmp4 := tmp10 + tmp5;
  249. wsptr^[DCTSIZE*0] := int (tmp0 + tmp7);
  250. wsptr^[DCTSIZE*7] := int (tmp0 - tmp7);
  251. wsptr^[DCTSIZE*1] := int (tmp1 + tmp6);
  252. wsptr^[DCTSIZE*6] := int (tmp1 - tmp6);
  253. wsptr^[DCTSIZE*2] := int (tmp2 + tmp5);
  254. wsptr^[DCTSIZE*5] := int (tmp2 - tmp5);
  255. wsptr^[DCTSIZE*4] := int (tmp3 + tmp4);
  256. wsptr^[DCTSIZE*3] := int (tmp3 - tmp4);
  257. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  258. Inc(IFAST_MULT_TYPE_PTR(quantptr));
  259. Inc(int_ptr(wsptr));
  260. end;
  261. { Pass 2: process rows from work array, store into output array. }
  262. { Note that we must descale the results by a factor of 8 == 2**3, }
  263. { and also undo the PASS1_BITS scaling. }
  264. wsptr := @workspace;
  265. for ctr := 0 to pred(DCTSIZE) do
  266. begin
  267. outptr := JSAMPROW(@output_buf^[ctr]^[output_col]);
  268. { Rows of zeroes can be exploited in the same way as we did with columns.
  269. However, the column calculation has created many nonzero AC terms, so
  270. the simplification applies less often (typically 5% to 10% of the time).
  271. On machines with very fast multiplication, it's possible that the
  272. test takes more time than it's worth. In that case this section
  273. may be commented out. }
  274. {$ifndef NO_ZERO_ROW_TEST}
  275. if (wsptr^[1]=0) and (wsptr^[2]=0) and (wsptr^[3]=0) and (wsptr^[4]=0) and
  276. (wsptr^[5]=0) and (wsptr^[6]=0) and (wsptr^[7]=0) then
  277. begin
  278. { AC terms all zero }
  279. dcval_ := range_limit^[IDESCALE(wsptr^[0], PASS1_BITS+3)
  280. and RANGE_MASK];
  281. outptr^[0] := dcval_;
  282. outptr^[1] := dcval_;
  283. outptr^[2] := dcval_;
  284. outptr^[3] := dcval_;
  285. outptr^[4] := dcval_;
  286. outptr^[5] := dcval_;
  287. outptr^[6] := dcval_;
  288. outptr^[7] := dcval_;
  289. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  290. continue;
  291. end;
  292. {$endif}
  293. { Even part }
  294. tmp10 := (DCTELEM(wsptr^[0]) + DCTELEM(wsptr^[4]));
  295. tmp11 := (DCTELEM(wsptr^[0]) - DCTELEM(wsptr^[4]));
  296. tmp13 := (DCTELEM(wsptr^[2]) + DCTELEM(wsptr^[6]));
  297. tmp12 := MULTIPLY(DCTELEM(wsptr^[2]) - DCTELEM(wsptr^[6]), FIX_1_414213562)
  298. - tmp13;
  299. tmp0 := tmp10 + tmp13;
  300. tmp3 := tmp10 - tmp13;
  301. tmp1 := tmp11 + tmp12;
  302. tmp2 := tmp11 - tmp12;
  303. { Odd part }
  304. z13 := DCTELEM(wsptr^[5]) + DCTELEM(wsptr^[3]);
  305. z10 := DCTELEM(wsptr^[5]) - DCTELEM(wsptr^[3]);
  306. z11 := DCTELEM(wsptr^[1]) + DCTELEM(wsptr^[7]);
  307. z12 := DCTELEM(wsptr^[1]) - DCTELEM(wsptr^[7]);
  308. tmp7 := z11 + z13; { phase 5 }
  309. tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
  310. z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
  311. tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
  312. tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
  313. tmp6 := tmp12 - tmp7; { phase 2 }
  314. tmp5 := tmp11 - tmp6;
  315. tmp4 := tmp10 + tmp5;
  316. { Final output stage: scale down by a factor of 8 and range-limit }
  317. outptr^[0] := range_limit^[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  318. and RANGE_MASK];
  319. outptr^[7] := range_limit^[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  320. and RANGE_MASK];
  321. outptr^[1] := range_limit^[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  322. and RANGE_MASK];
  323. outptr^[6] := range_limit^[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  324. and RANGE_MASK];
  325. outptr^[2] := range_limit^[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  326. and RANGE_MASK];
  327. outptr^[5] := range_limit^[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  328. and RANGE_MASK];
  329. outptr^[4] := range_limit^[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  330. and RANGE_MASK];
  331. outptr^[3] := range_limit^[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  332. and RANGE_MASK];
  333. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  334. end;
  335. end;
  336. end.