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.

440 lines
16 KiB

3 years ago
  1. unit imjidctint;
  2. {$Q+}
  3. { This file contains a slow-but-accurate integer implementation of the
  4. inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  5. must also perform dequantization of the input coefficients.
  6. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  7. on each row (or vice versa, but it's more convenient to emit a row at
  8. a time). Direct algorithms are also available, but they are much more
  9. complex and seem not to be any faster when reduced to code.
  10. This implementation is based on an algorithm described in
  11. C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  12. Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  13. Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  14. The primary algorithm described there uses 11 multiplies and 29 adds.
  15. We use their alternate method with 12 multiplies and 32 adds.
  16. The advantage of this method is that no data path contains more than one
  17. multiplication; this allows a very simple and accurate implementation in
  18. scaled fixed-point arithmetic, with a minimal number of shifts. }
  19. { Original : jidctint.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
  20. interface
  21. {$I imjconfig.inc}
  22. uses
  23. imjmorecfg,
  24. imjinclude,
  25. imjpeglib,
  26. imjdct; { Private declarations for DCT subsystem }
  27. { Perform dequantization and inverse DCT on one block of coefficients. }
  28. {GLOBAL}
  29. procedure jpeg_idct_islow (cinfo : j_decompress_ptr;
  30. compptr : jpeg_component_info_ptr;
  31. coef_block : JCOEFPTR;
  32. output_buf : JSAMPARRAY;
  33. output_col : JDIMENSION);
  34. implementation
  35. { This module is specialized to the case DCTSIZE = 8. }
  36. {$ifndef DCTSIZE_IS_8}
  37. Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
  38. {$endif}
  39. { The poop on this scaling stuff is as follows:
  40. Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  41. larger than the true IDCT outputs. The final outputs are therefore
  42. a factor of N larger than desired; since N=8 this can be cured by
  43. a simple right shift at the end of the algorithm. The advantage of
  44. this arrangement is that we save two multiplications per 1-D IDCT,
  45. because the y0 and y4 inputs need not be divided by sqrt(N).
  46. We have to do addition and subtraction of the integer inputs, which
  47. is no problem, and multiplication by fractional constants, which is
  48. a problem to do in integer arithmetic. We multiply all the constants
  49. by CONST_SCALE and convert them to integer constants (thus retaining
  50. CONST_BITS bits of precision in the constants). After doing a
  51. multiplication we have to divide the product by CONST_SCALE, with proper
  52. rounding, to produce the correct output. This division can be done
  53. cheaply as a right shift of CONST_BITS bits. We postpone shifting
  54. as long as possible so that partial sums can be added together with
  55. full fractional precision.
  56. The outputs of the first pass are scaled up by PASS1_BITS bits so that
  57. they are represented to better-than-integral precision. These outputs
  58. require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  59. with the recommended scaling. (To scale up 12-bit sample data further, an
  60. intermediate INT32 array would be needed.)
  61. To avoid overflow of the 32-bit intermediate results in pass 2, we must
  62. have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
  63. shows that the values given below are the most effective. }
  64. {$ifdef BITS_IN_JSAMPLE_IS_8}
  65. const
  66. CONST_BITS = 13;
  67. PASS1_BITS = 2;
  68. {$else}
  69. const
  70. CONST_BITS = 13;
  71. PASS1_BITS = 1; { lose a little precision to avoid overflow }
  72. {$endif}
  73. const
  74. CONST_SCALE = (INT32(1) shl CONST_BITS);
  75. const
  76. FIX_0_298631336 = INT32(Round(CONST_SCALE * 0.298631336)); {2446}
  77. FIX_0_390180644 = INT32(Round(CONST_SCALE * 0.390180644)); {3196}
  78. FIX_0_541196100 = INT32(Round(CONST_SCALE * 0.541196100)); {4433}
  79. FIX_0_765366865 = INT32(Round(CONST_SCALE * 0.765366865)); {6270}
  80. FIX_0_899976223 = INT32(Round(CONST_SCALE * 0.899976223)); {7373}
  81. FIX_1_175875602 = INT32(Round(CONST_SCALE * 1.175875602)); {9633}
  82. FIX_1_501321110 = INT32(Round(CONST_SCALE * 1.501321110)); {12299}
  83. FIX_1_847759065 = INT32(Round(CONST_SCALE * 1.847759065)); {15137}
  84. FIX_1_961570560 = INT32(Round(CONST_SCALE * 1.961570560)); {16069}
  85. FIX_2_053119869 = INT32(Round(CONST_SCALE * 2.053119869)); {16819}
  86. FIX_2_562915447 = INT32(Round(CONST_SCALE * 2.562915447)); {20995}
  87. FIX_3_072711026 = INT32(Round(CONST_SCALE * 3.072711026)); {25172}
  88. { Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  89. For 8-bit samples with the recommended scaling, all the variable
  90. and constant values involved are no more than 16 bits wide, so a
  91. 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  92. For 12-bit samples, a full 32-bit multiplication will be needed. }
  93. {$ifdef BITS_IN_JSAMPLE_IS_8}
  94. {$IFDEF BASM16}
  95. {$IFNDEF WIN32}
  96. {MULTIPLY16C16(var,const)}
  97. function Multiply(X, Y: Integer): integer; assembler;
  98. asm
  99. mov ax, X
  100. imul Y
  101. mov al, ah
  102. mov ah, dl
  103. end;
  104. {$ENDIF}
  105. {$ENDIF}
  106. function Multiply(X, Y: INT32): INT32;
  107. begin
  108. Multiply := INT32(X) * INT32(Y);
  109. end;
  110. {$else}
  111. {#define MULTIPLY(var,const) ((var) * (const))}
  112. function Multiply(X, Y: INT32): INT32;
  113. begin
  114. Multiply := INT32(X) * INT32(Y);
  115. end;
  116. {$endif}
  117. { Dequantize a coefficient by multiplying it by the multiplier-table
  118. entry; produce an int result. In this module, both inputs and result
  119. are 16 bits or less, so either int or short multiply will work. }
  120. function DEQUANTIZE(coef,quantval : int) : int;
  121. begin
  122. Dequantize := ( ISLOW_MULT_TYPE(coef) * quantval);
  123. end;
  124. { Descale and correctly round an INT32 value that's scaled by N bits.
  125. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  126. the fudge factor is correct for either sign of X. }
  127. function DESCALE(x : INT32; n : int) : INT32;
  128. var
  129. shift_temp : INT32;
  130. begin
  131. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  132. shift_temp := x + (INT32(1) shl (n-1));
  133. if shift_temp < 0 then
  134. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  135. else
  136. Descale := (shift_temp shr n);
  137. {$else}
  138. Descale := (x + (INT32(1) shl (n-1)) shr n;
  139. {$endif}
  140. end;
  141. { Perform dequantization and inverse DCT on one block of coefficients. }
  142. {GLOBAL}
  143. procedure jpeg_idct_islow (cinfo : j_decompress_ptr;
  144. compptr : jpeg_component_info_ptr;
  145. coef_block : JCOEFPTR;
  146. output_buf : JSAMPARRAY;
  147. output_col : JDIMENSION);
  148. type
  149. PWorkspace = ^TWorkspace;
  150. TWorkspace = coef_bits_field; { buffers data between passes }
  151. var
  152. tmp0, tmp1, tmp2, tmp3 : INT32;
  153. tmp10, tmp11, tmp12, tmp13 : INT32;
  154. z1, z2, z3, z4, z5 : INT32;
  155. inptr : JCOEFPTR;
  156. quantptr : ISLOW_MULT_TYPE_FIELD_PTR;
  157. wsptr : PWorkspace;
  158. outptr : JSAMPROW;
  159. range_limit : JSAMPROW;
  160. ctr : int;
  161. workspace : TWorkspace;
  162. {SHIFT_TEMPS}
  163. var
  164. dcval : int;
  165. var
  166. dcval_ : JSAMPLE;
  167. begin
  168. { Each IDCT routine is responsible for range-limiting its results and
  169. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  170. be quite far out of range if the input data is corrupt, so a bulletproof
  171. range-limiting step is required. We use a mask-and-table-lookup method
  172. to do the combined operations quickly. See the comments with
  173. prepare_range_limit_table (in jdmaster.c) for more info. }
  174. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  175. { Pass 1: process columns from input, store into work array. }
  176. { Note results are scaled up by sqrt(8) compared to a true IDCT; }
  177. { furthermore, we scale the results by 2**PASS1_BITS. }
  178. inptr := coef_block;
  179. quantptr := ISLOW_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  180. wsptr := PWorkspace(@workspace);
  181. for ctr := pred(DCTSIZE) downto 0 do
  182. begin
  183. { Due to quantization, we will usually find that many of the input
  184. coefficients are zero, especially the AC terms. We can exploit this
  185. by short-circuiting the IDCT calculation for any column in which all
  186. the AC terms are zero. In that case each output is equal to the
  187. DC coefficient (with scale factor as needed).
  188. With typical images and quantization tables, half or more of the
  189. column DCT calculations can be simplified this way. }
  190. if ((inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and
  191. (inptr^[DCTSIZE*3]=0) and (inptr^[DCTSIZE*4]=0) and
  192. (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
  193. (inptr^[DCTSIZE*7]=0)) then
  194. begin
  195. { AC terms all zero }
  196. dcval := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]) shl PASS1_BITS;
  197. wsptr^[DCTSIZE*0] := dcval;
  198. wsptr^[DCTSIZE*1] := dcval;
  199. wsptr^[DCTSIZE*2] := dcval;
  200. wsptr^[DCTSIZE*3] := dcval;
  201. wsptr^[DCTSIZE*4] := dcval;
  202. wsptr^[DCTSIZE*5] := dcval;
  203. wsptr^[DCTSIZE*6] := dcval;
  204. wsptr^[DCTSIZE*7] := dcval;
  205. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  206. Inc(ISLOW_MULT_TYPE_PTR(quantptr));
  207. Inc(int_ptr(wsptr));
  208. continue;
  209. end;
  210. { Even part: reverse the even part of the forward DCT. }
  211. { The rotator is sqrt(2)*c(-6). }
  212. z2 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  213. z3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  214. z1 := MULTIPLY(z2 + z3, FIX_0_541196100);
  215. tmp2 := z1 + MULTIPLY(z3, - FIX_1_847759065);
  216. tmp3 := z1 + MULTIPLY(z2, FIX_0_765366865);
  217. z2 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  218. z3 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  219. tmp0 := (z2 + z3) shl CONST_BITS;
  220. tmp1 := (z2 - z3) shl CONST_BITS;
  221. tmp10 := tmp0 + tmp3;
  222. tmp13 := tmp0 - tmp3;
  223. tmp11 := tmp1 + tmp2;
  224. tmp12 := tmp1 - tmp2;
  225. { Odd part per figure 8; the matrix is unitary and hence its
  226. transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
  227. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  228. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  229. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  230. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  231. z1 := tmp0 + tmp3;
  232. z2 := tmp1 + tmp2;
  233. z3 := tmp0 + tmp2;
  234. z4 := tmp1 + tmp3;
  235. z5 := MULTIPLY(z3 + z4, FIX_1_175875602); { sqrt(2) * c3 }
  236. tmp0 := MULTIPLY(tmp0, FIX_0_298631336); { sqrt(2) * (-c1+c3+c5-c7) }
  237. tmp1 := MULTIPLY(tmp1, FIX_2_053119869); { sqrt(2) * ( c1+c3-c5+c7) }
  238. tmp2 := MULTIPLY(tmp2, FIX_3_072711026); { sqrt(2) * ( c1+c3+c5-c7) }
  239. tmp3 := MULTIPLY(tmp3, FIX_1_501321110); { sqrt(2) * ( c1+c3-c5-c7) }
  240. z1 := MULTIPLY(z1, - FIX_0_899976223); { sqrt(2) * (c7-c3) }
  241. z2 := MULTIPLY(z2, - FIX_2_562915447); { sqrt(2) * (-c1-c3) }
  242. z3 := MULTIPLY(z3, - FIX_1_961570560); { sqrt(2) * (-c3-c5) }
  243. z4 := MULTIPLY(z4, - FIX_0_390180644); { sqrt(2) * (c5-c3) }
  244. Inc(z3, z5);
  245. Inc(z4, z5);
  246. Inc(tmp0, z1 + z3);
  247. Inc(tmp1, z2 + z4);
  248. Inc(tmp2, z2 + z3);
  249. Inc(tmp3, z1 + z4);
  250. { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }
  251. wsptr^[DCTSIZE*0] := int (DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS));
  252. wsptr^[DCTSIZE*7] := int (DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS));
  253. wsptr^[DCTSIZE*1] := int (DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS));
  254. wsptr^[DCTSIZE*6] := int (DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS));
  255. wsptr^[DCTSIZE*2] := int (DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS));
  256. wsptr^[DCTSIZE*5] := int (DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS));
  257. wsptr^[DCTSIZE*3] := int (DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS));
  258. wsptr^[DCTSIZE*4] := int (DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS));
  259. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  260. Inc(ISLOW_MULT_TYPE_PTR(quantptr));
  261. Inc(int_ptr(wsptr));
  262. end;
  263. { Pass 2: process rows from work array, store into output array. }
  264. { Note that we must descale the results by a factor of 8 == 2**3, }
  265. { and also undo the PASS1_BITS scaling. }
  266. wsptr := @workspace;
  267. for ctr := 0 to pred(DCTSIZE) do
  268. begin
  269. outptr := output_buf^[ctr];
  270. Inc(JSAMPLE_PTR(outptr), output_col);
  271. { Rows of zeroes can be exploited in the same way as we did with columns.
  272. However, the column calculation has created many nonzero AC terms, so
  273. the simplification applies less often (typically 5% to 10% of the time).
  274. On machines with very fast multiplication, it's possible that the
  275. test takes more time than it's worth. In that case this section
  276. may be commented out. }
  277. {$ifndef NO_ZERO_ROW_TEST}
  278. if ((wsptr^[1]=0) and (wsptr^[2]=0) and (wsptr^[3]=0) and (wsptr^[4]=0)
  279. and (wsptr^[5]=0) and (wsptr^[6]=0) and (wsptr^[7]=0)) then
  280. begin
  281. { AC terms all zero }
  282. JSAMPLE(dcval_) := range_limit^[int(DESCALE(INT32(wsptr^[0]),
  283. PASS1_BITS+3)) and RANGE_MASK];
  284. outptr^[0] := dcval_;
  285. outptr^[1] := dcval_;
  286. outptr^[2] := dcval_;
  287. outptr^[3] := dcval_;
  288. outptr^[4] := dcval_;
  289. outptr^[5] := dcval_;
  290. outptr^[6] := dcval_;
  291. outptr^[7] := dcval_;
  292. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  293. continue;
  294. end;
  295. {$endif}
  296. { Even part: reverse the even part of the forward DCT. }
  297. { The rotator is sqrt(2)*c(-6). }
  298. z2 := INT32 (wsptr^[2]);
  299. z3 := INT32 (wsptr^[6]);
  300. z1 := MULTIPLY(z2 + z3, FIX_0_541196100);
  301. tmp2 := z1 + MULTIPLY(z3, - FIX_1_847759065);
  302. tmp3 := z1 + MULTIPLY(z2, FIX_0_765366865);
  303. tmp0 := (INT32(wsptr^[0]) + INT32(wsptr^[4])) shl CONST_BITS;
  304. tmp1 := (INT32(wsptr^[0]) - INT32(wsptr^[4])) shl CONST_BITS;
  305. tmp10 := tmp0 + tmp3;
  306. tmp13 := tmp0 - tmp3;
  307. tmp11 := tmp1 + tmp2;
  308. tmp12 := tmp1 - tmp2;
  309. { Odd part per figure 8; the matrix is unitary and hence its
  310. transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
  311. tmp0 := INT32(wsptr^[7]);
  312. tmp1 := INT32(wsptr^[5]);
  313. tmp2 := INT32(wsptr^[3]);
  314. tmp3 := INT32(wsptr^[1]);
  315. z1 := tmp0 + tmp3;
  316. z2 := tmp1 + tmp2;
  317. z3 := tmp0 + tmp2;
  318. z4 := tmp1 + tmp3;
  319. z5 := MULTIPLY(z3 + z4, FIX_1_175875602); { sqrt(2) * c3 }
  320. tmp0 := MULTIPLY(tmp0, FIX_0_298631336); { sqrt(2) * (-c1+c3+c5-c7) }
  321. tmp1 := MULTIPLY(tmp1, FIX_2_053119869); { sqrt(2) * ( c1+c3-c5+c7) }
  322. tmp2 := MULTIPLY(tmp2, FIX_3_072711026); { sqrt(2) * ( c1+c3+c5-c7) }
  323. tmp3 := MULTIPLY(tmp3, FIX_1_501321110); { sqrt(2) * ( c1+c3-c5-c7) }
  324. z1 := MULTIPLY(z1, - FIX_0_899976223); { sqrt(2) * (c7-c3) }
  325. z2 := MULTIPLY(z2, - FIX_2_562915447); { sqrt(2) * (-c1-c3) }
  326. z3 := MULTIPLY(z3, - FIX_1_961570560); { sqrt(2) * (-c3-c5) }
  327. z4 := MULTIPLY(z4, - FIX_0_390180644); { sqrt(2) * (c5-c3) }
  328. Inc(z3, z5);
  329. Inc(z4, z5);
  330. Inc(tmp0, z1 + z3);
  331. Inc(tmp1, z2 + z4);
  332. Inc(tmp2, z2 + z3);
  333. Inc(tmp3, z1 + z4);
  334. { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }
  335. outptr^[0] := range_limit^[ int(DESCALE(tmp10 + tmp3,
  336. CONST_BITS+PASS1_BITS+3))
  337. and RANGE_MASK];
  338. outptr^[7] := range_limit^[ int(DESCALE(tmp10 - tmp3,
  339. CONST_BITS+PASS1_BITS+3))
  340. and RANGE_MASK];
  341. outptr^[1] := range_limit^[ int(DESCALE(tmp11 + tmp2,
  342. CONST_BITS+PASS1_BITS+3))
  343. and RANGE_MASK];
  344. outptr^[6] := range_limit^[ int(DESCALE(tmp11 - tmp2,
  345. CONST_BITS+PASS1_BITS+3))
  346. and RANGE_MASK];
  347. outptr^[2] := range_limit^[ int(DESCALE(tmp12 + tmp1,
  348. CONST_BITS+PASS1_BITS+3))
  349. and RANGE_MASK];
  350. outptr^[5] := range_limit^[ int(DESCALE(tmp12 - tmp1,
  351. CONST_BITS+PASS1_BITS+3))
  352. and RANGE_MASK];
  353. outptr^[3] := range_limit^[ int(DESCALE(tmp13 + tmp0,
  354. CONST_BITS+PASS1_BITS+3))
  355. and RANGE_MASK];
  356. outptr^[4] := range_limit^[ int(DESCALE(tmp13 - tmp0,
  357. CONST_BITS+PASS1_BITS+3))
  358. and RANGE_MASK];
  359. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  360. end;
  361. end;
  362. end.