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.

237 lines
7.5 KiB

3 years ago
  1. unit imjfdctfst;
  2. { This file contains a fast, not so accurate integer implementation of the
  3. forward DCT (Discrete Cosine Transform).
  4. A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
  5. on each column. Direct algorithms are also available, but they are
  6. much more complex and seem not to be any faster when reduced to code.
  7. This implementation is based on Arai, Agui, and Nakajima's algorithm for
  8. scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  9. Japanese, but the algorithm is described in the Pennebaker & Mitchell
  10. JPEG textbook (see REFERENCES section in file README). The following code
  11. is based directly on figure 4-8 in P&M.
  12. While an 8-point DCT cannot be done in less than 11 multiplies, it is
  13. possible to arrange the computation so that many of the multiplies are
  14. simple scalings of the final outputs. These multiplies can then be
  15. folded into the multiplications or divisions by the JPEG quantization
  16. table entries. The AA&N method leaves only 5 multiplies and 29 adds
  17. to be done in the DCT itself.
  18. The primary disadvantage of this method is that with fixed-point math,
  19. accuracy is lost due to imprecise representation of the scaled
  20. quantization values. The smaller the quantization table entry, the less
  21. precise the scaled value, so this implementation does worse with high-
  22. quality-setting files than with low-quality ones. }
  23. { Original: jfdctfst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  24. interface
  25. {$I imjconfig.inc}
  26. uses
  27. imjmorecfg,
  28. imjinclude,
  29. imjpeglib,
  30. imjdct; { Private declarations for DCT subsystem }
  31. { Perform the forward DCT on one block of samples. }
  32. {GLOBAL}
  33. procedure jpeg_fdct_ifast (var data : array of DCTELEM);
  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. { Scaling decisions are generally the same as in the LL&M algorithm;
  40. see jfdctint.c for more details. However, we choose to descale
  41. (right shift) multiplication products as soon as they are formed,
  42. rather than carrying additional fractional bits into subsequent additions.
  43. This compromises accuracy slightly, but it lets us save a few shifts.
  44. More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  45. everywhere except in the multiplications proper; this saves a good deal
  46. of work on 16-bit-int machines.
  47. Again to save a few shifts, the intermediate results between pass 1 and
  48. pass 2 are not upscaled, but are represented only to integral precision.
  49. A final compromise is to represent the multiplicative constants to only
  50. 8 fractional bits, rather than 13. This saves some shifting work on some
  51. machines, and may also reduce the cost of multiplication (since there
  52. are fewer one-bits in the constants). }
  53. const
  54. CONST_BITS = 8;
  55. const
  56. CONST_SCALE = (INT32(1) shl CONST_BITS);
  57. const
  58. FIX_0_382683433 = INT32(Round(CONST_SCALE * 0.382683433)); {98}
  59. FIX_0_541196100 = INT32(Round(CONST_SCALE * 0.541196100)); {139}
  60. FIX_0_707106781 = INT32(Round(CONST_SCALE * 0.707106781)); {181}
  61. FIX_1_306562965 = INT32(Round(CONST_SCALE * 1.306562965)); {334}
  62. { Descale and correctly round an INT32 value that's scaled by N bits.
  63. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  64. the fudge factor is correct for either sign of X. }
  65. function DESCALE(x : INT32; n : int) : INT32;
  66. var
  67. shift_temp : INT32;
  68. begin
  69. { We can gain a little more speed, with a further compromise in accuracy,
  70. by omitting the addition in a descaling shift. This yields an incorrectly
  71. rounded result half the time... }
  72. {$ifndef USE_ACCURATE_ROUNDING}
  73. shift_temp := x;
  74. {$else}
  75. shift_temp := x + (INT32(1) shl (n-1));
  76. {$endif}
  77. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  78. if shift_temp < 0 then
  79. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  80. else
  81. {$endif}
  82. Descale := (shift_temp shr n);
  83. end;
  84. { Multiply a DCTELEM variable by an INT32 constant, and immediately
  85. descale to yield a DCTELEM result. }
  86. function MULTIPLY(X : DCTELEM; Y: INT32): DCTELEM;
  87. begin
  88. Multiply := DeScale((X) * (Y), CONST_BITS);
  89. end;
  90. { Perform the forward DCT on one block of samples. }
  91. {GLOBAL}
  92. procedure jpeg_fdct_ifast (var data : array of DCTELEM);
  93. type
  94. PWorkspace = ^TWorkspace;
  95. TWorkspace = array [0..DCTSIZE2-1] of DCTELEM;
  96. var
  97. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : DCTELEM;
  98. tmp10, tmp11, tmp12, tmp13 : DCTELEM;
  99. z1, z2, z3, z4, z5, z11, z13 : DCTELEM;
  100. dataptr : PWorkspace;
  101. ctr : int;
  102. {SHIFT_TEMPS}
  103. begin
  104. { Pass 1: process rows. }
  105. dataptr := PWorkspace(@data);
  106. for ctr := DCTSIZE-1 downto 0 do
  107. begin
  108. tmp0 := dataptr^[0] + dataptr^[7];
  109. tmp7 := dataptr^[0] - dataptr^[7];
  110. tmp1 := dataptr^[1] + dataptr^[6];
  111. tmp6 := dataptr^[1] - dataptr^[6];
  112. tmp2 := dataptr^[2] + dataptr^[5];
  113. tmp5 := dataptr^[2] - dataptr^[5];
  114. tmp3 := dataptr^[3] + dataptr^[4];
  115. tmp4 := dataptr^[3] - dataptr^[4];
  116. { Even part }
  117. tmp10 := tmp0 + tmp3; { phase 2 }
  118. tmp13 := tmp0 - tmp3;
  119. tmp11 := tmp1 + tmp2;
  120. tmp12 := tmp1 - tmp2;
  121. dataptr^[0] := tmp10 + tmp11; { phase 3 }
  122. dataptr^[4] := tmp10 - tmp11;
  123. z1 := MULTIPLY(tmp12 + tmp13, FIX_0_707106781); { c4 }
  124. dataptr^[2] := tmp13 + z1; { phase 5 }
  125. dataptr^[6] := tmp13 - z1;
  126. { Odd part }
  127. tmp10 := tmp4 + tmp5; { phase 2 }
  128. tmp11 := tmp5 + tmp6;
  129. tmp12 := tmp6 + tmp7;
  130. { The rotator is modified from fig 4-8 to avoid extra negations. }
  131. z5 := MULTIPLY(tmp10 - tmp12, FIX_0_382683433); { c6 }
  132. z2 := MULTIPLY(tmp10, FIX_0_541196100) + z5; { c2-c6 }
  133. z4 := MULTIPLY(tmp12, FIX_1_306562965) + z5; { c2+c6 }
  134. z3 := MULTIPLY(tmp11, FIX_0_707106781); { c4 }
  135. z11 := tmp7 + z3; { phase 5 }
  136. z13 := tmp7 - z3;
  137. dataptr^[5] := z13 + z2; { phase 6 }
  138. dataptr^[3] := z13 - z2;
  139. dataptr^[1] := z11 + z4;
  140. dataptr^[7] := z11 - z4;
  141. Inc(DCTELEMPTR(dataptr), DCTSIZE); { advance pointer to next row }
  142. end;
  143. { Pass 2: process columns. }
  144. dataptr := PWorkspace(@data);
  145. for ctr := DCTSIZE-1 downto 0 do
  146. begin
  147. tmp0 := dataptr^[DCTSIZE*0] + dataptr^[DCTSIZE*7];
  148. tmp7 := dataptr^[DCTSIZE*0] - dataptr^[DCTSIZE*7];
  149. tmp1 := dataptr^[DCTSIZE*1] + dataptr^[DCTSIZE*6];
  150. tmp6 := dataptr^[DCTSIZE*1] - dataptr^[DCTSIZE*6];
  151. tmp2 := dataptr^[DCTSIZE*2] + dataptr^[DCTSIZE*5];
  152. tmp5 := dataptr^[DCTSIZE*2] - dataptr^[DCTSIZE*5];
  153. tmp3 := dataptr^[DCTSIZE*3] + dataptr^[DCTSIZE*4];
  154. tmp4 := dataptr^[DCTSIZE*3] - dataptr^[DCTSIZE*4];
  155. { Even part }
  156. tmp10 := tmp0 + tmp3; { phase 2 }
  157. tmp13 := tmp0 - tmp3;
  158. tmp11 := tmp1 + tmp2;
  159. tmp12 := tmp1 - tmp2;
  160. dataptr^[DCTSIZE*0] := tmp10 + tmp11; { phase 3 }
  161. dataptr^[DCTSIZE*4] := tmp10 - tmp11;
  162. z1 := MULTIPLY(tmp12 + tmp13, FIX_0_707106781); { c4 }
  163. dataptr^[DCTSIZE*2] := tmp13 + z1; { phase 5 }
  164. dataptr^[DCTSIZE*6] := tmp13 - z1;
  165. { Odd part }
  166. tmp10 := tmp4 + tmp5; { phase 2 }
  167. tmp11 := tmp5 + tmp6;
  168. tmp12 := tmp6 + tmp7;
  169. { The rotator is modified from fig 4-8 to avoid extra negations. }
  170. z5 := MULTIPLY(tmp10 - tmp12, FIX_0_382683433); { c6 }
  171. z2 := MULTIPLY(tmp10, FIX_0_541196100) + z5; { c2-c6 }
  172. z4 := MULTIPLY(tmp12, FIX_1_306562965) + z5; { c2+c6 }
  173. z3 := MULTIPLY(tmp11, FIX_0_707106781); { c4 }
  174. z11 := tmp7 + z3; { phase 5 }
  175. z13 := tmp7 - z3;
  176. dataptr^[DCTSIZE*5] := z13 + z2; { phase 6 }
  177. dataptr^[DCTSIZE*3] := z13 - z2;
  178. dataptr^[DCTSIZE*1] := z11 + z4;
  179. dataptr^[DCTSIZE*7] := z11 - z4;
  180. Inc(DCTELEMPTR(dataptr)); { advance pointer to next column }
  181. end;
  182. end;
  183. end.