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.

285 lines
10 KiB

3 years ago
  1. unit imjidctflt;
  2. { This file contains a floating-point 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. This implementation should be more accurate than either of the integer
  6. IDCT implementations. However, it may not give the same results on all
  7. machines because of differences in roundoff behavior. Speed will depend
  8. on the hardware's floating point capacity.
  9. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  10. on each row (or vice versa, but it's more convenient to emit a row at
  11. a time). Direct algorithms are also available, but they are much more
  12. complex and seem not to be any faster when reduced to code.
  13. This implementation is based on Arai, Agui, and Nakajima's algorithm for
  14. scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  15. Japanese, but the algorithm is described in the Pennebaker & Mitchell
  16. JPEG textbook (see REFERENCES section in file README). The following code
  17. is based directly on figure 4-8 in P&M.
  18. While an 8-point DCT cannot be done in less than 11 multiplies, it is
  19. possible to arrange the computation so that many of the multiplies are
  20. simple scalings of the final outputs. These multiplies can then be
  21. folded into the multiplications or divisions by the JPEG quantization
  22. table entries. The AA&N method leaves only 5 multiplies and 29 adds
  23. to be done in the DCT itself.
  24. The primary disadvantage of this method is that with a fixed-point
  25. implementation, accuracy is lost due to imprecise representation of the
  26. scaled quantization values. However, that problem does not arise if
  27. we use floating point arithmetic. }
  28. { Original: jidctflt.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  29. interface
  30. {$I imjconfig.inc}
  31. uses
  32. imjmorecfg,
  33. imjinclude,
  34. imjpeglib,
  35. imjdct; { Private declarations for DCT subsystem }
  36. { Perform dequantization and inverse DCT on one block of coefficients. }
  37. {GLOBAL}
  38. procedure jpeg_idct_float (cinfo : j_decompress_ptr;
  39. compptr : jpeg_component_info_ptr;
  40. coef_block : JCOEFPTR;
  41. output_buf : JSAMPARRAY;
  42. output_col : JDIMENSION);
  43. implementation
  44. { This module is specialized to the case DCTSIZE = 8. }
  45. {$ifndef DCTSIZE_IS_8}
  46. Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
  47. {$endif}
  48. { Dequantize a coefficient by multiplying it by the multiplier-table
  49. entry; produce a float result. }
  50. function DEQUANTIZE(coef : int; quantval : FAST_FLOAT) : FAST_FLOAT;
  51. begin
  52. Dequantize := ( (coef) * quantval);
  53. end;
  54. { Descale and correctly round an INT32 value that's scaled by N bits.
  55. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  56. the fudge factor is correct for either sign of X. }
  57. function DESCALE(x : INT32; n : int) : INT32;
  58. var
  59. shift_temp : INT32;
  60. begin
  61. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  62. shift_temp := x + (INT32(1) shl (n-1));
  63. if shift_temp < 0 then
  64. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  65. else
  66. Descale := (shift_temp shr n);
  67. {$else}
  68. Descale := (x + (INT32(1) shl (n-1)) shr n;
  69. {$endif}
  70. end;
  71. { Perform dequantization and inverse DCT on one block of coefficients. }
  72. {GLOBAL}
  73. procedure jpeg_idct_float (cinfo : j_decompress_ptr;
  74. compptr : jpeg_component_info_ptr;
  75. coef_block : JCOEFPTR;
  76. output_buf : JSAMPARRAY;
  77. output_col : JDIMENSION);
  78. type
  79. PWorkspace = ^TWorkspace;
  80. TWorkspace = array[0..DCTSIZE2-1] of FAST_FLOAT;
  81. var
  82. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : FAST_FLOAT;
  83. tmp10, tmp11, tmp12, tmp13 : FAST_FLOAT;
  84. z5, z10, z11, z12, z13 : FAST_FLOAT;
  85. inptr : JCOEFPTR;
  86. quantptr : FLOAT_MULT_TYPE_FIELD_PTR;
  87. wsptr : PWorkSpace;
  88. outptr : JSAMPROW;
  89. range_limit : JSAMPROW;
  90. ctr : int;
  91. workspace : TWorkspace; { buffers data between passes }
  92. {SHIFT_TEMPS}
  93. var
  94. dcval : FAST_FLOAT;
  95. begin
  96. { Each IDCT routine is responsible for range-limiting its results and
  97. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  98. be quite far out of range if the input data is corrupt, so a bulletproof
  99. range-limiting step is required. We use a mask-and-table-lookup method
  100. to do the combined operations quickly. See the comments with
  101. prepare_range_limit_table (in jdmaster.c) for more info. }
  102. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  103. { Pass 1: process columns from input, store into work array. }
  104. inptr := coef_block;
  105. quantptr := FLOAT_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  106. wsptr := @workspace;
  107. for ctr := pred(DCTSIZE) downto 0 do
  108. begin
  109. { Due to quantization, we will usually find that many of the input
  110. coefficients are zero, especially the AC terms. We can exploit this
  111. by short-circuiting the IDCT calculation for any column in which all
  112. the AC terms are zero. In that case each output is equal to the
  113. DC coefficient (with scale factor as needed).
  114. With typical images and quantization tables, half or more of the
  115. column DCT calculations can be simplified this way. }
  116. if (inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and
  117. (inptr^[DCTSIZE*3]=0) and (inptr^[DCTSIZE*4]=0) and
  118. (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
  119. (inptr^[DCTSIZE*7]=0) then
  120. begin
  121. { AC terms all zero }
  122. FAST_FLOAT(dcval) := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  123. wsptr^[DCTSIZE*0] := dcval;
  124. wsptr^[DCTSIZE*1] := dcval;
  125. wsptr^[DCTSIZE*2] := dcval;
  126. wsptr^[DCTSIZE*3] := dcval;
  127. wsptr^[DCTSIZE*4] := dcval;
  128. wsptr^[DCTSIZE*5] := dcval;
  129. wsptr^[DCTSIZE*6] := dcval;
  130. wsptr^[DCTSIZE*7] := dcval;
  131. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  132. Inc(FLOAT_MULT_TYPE_PTR(quantptr));
  133. Inc(FAST_FLOAT_PTR(wsptr));
  134. continue;
  135. end;
  136. { Even part }
  137. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  138. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  139. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  140. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  141. tmp10 := tmp0 + tmp2; { phase 3 }
  142. tmp11 := tmp0 - tmp2;
  143. tmp13 := tmp1 + tmp3; { phases 5-3 }
  144. tmp12 := (tmp1 - tmp3) * ({FAST_FLOAT}(1.414213562)) - tmp13; { 2*c4 }
  145. tmp0 := tmp10 + tmp13; { phase 2 }
  146. tmp3 := tmp10 - tmp13;
  147. tmp1 := tmp11 + tmp12;
  148. tmp2 := tmp11 - tmp12;
  149. { Odd part }
  150. tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  151. tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  152. tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  153. tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  154. z13 := tmp6 + tmp5; { phase 6 }
  155. z10 := tmp6 - tmp5;
  156. z11 := tmp4 + tmp7;
  157. z12 := tmp4 - tmp7;
  158. tmp7 := z11 + z13; { phase 5 }
  159. tmp11 := (z11 - z13) * ({FAST_FLOAT}(1.414213562)); { 2*c4 }
  160. z5 := (z10 + z12) * ({FAST_FLOAT}(1.847759065)); { 2*c2 }
  161. tmp10 := ({FAST_FLOAT}(1.082392200)) * z12 - z5; { 2*(c2-c6) }
  162. tmp12 := ({FAST_FLOAT}(-2.613125930)) * z10 + z5; { -2*(c2+c6) }
  163. tmp6 := tmp12 - tmp7; { phase 2 }
  164. tmp5 := tmp11 - tmp6;
  165. tmp4 := tmp10 + tmp5;
  166. wsptr^[DCTSIZE*0] := tmp0 + tmp7;
  167. wsptr^[DCTSIZE*7] := tmp0 - tmp7;
  168. wsptr^[DCTSIZE*1] := tmp1 + tmp6;
  169. wsptr^[DCTSIZE*6] := tmp1 - tmp6;
  170. wsptr^[DCTSIZE*2] := tmp2 + tmp5;
  171. wsptr^[DCTSIZE*5] := tmp2 - tmp5;
  172. wsptr^[DCTSIZE*4] := tmp3 + tmp4;
  173. wsptr^[DCTSIZE*3] := tmp3 - tmp4;
  174. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  175. Inc(FLOAT_MULT_TYPE_PTR(quantptr));
  176. Inc(FAST_FLOAT_PTR(wsptr));
  177. end;
  178. { Pass 2: process rows from work array, store into output array. }
  179. { Note that we must descale the results by a factor of 8 = 2**3. }
  180. wsptr := @workspace;
  181. for ctr := 0 to pred(DCTSIZE) do
  182. begin
  183. outptr := JSAMPROW(@(output_buf^[ctr]^[output_col]));
  184. { Rows of zeroes can be exploited in the same way as we did with columns.
  185. However, the column calculation has created many nonzero AC terms, so
  186. the simplification applies less often (typically 5% to 10% of the time).
  187. And testing floats for zero is relatively expensive, so we don't bother. }
  188. { Even part }
  189. tmp10 := wsptr^[0] + wsptr^[4];
  190. tmp11 := wsptr^[0] - wsptr^[4];
  191. tmp13 := wsptr^[2] + wsptr^[6];
  192. tmp12 := (wsptr^[2] - wsptr^[6]) * ({FAST_FLOAT}(1.414213562)) - tmp13;
  193. tmp0 := tmp10 + tmp13;
  194. tmp3 := tmp10 - tmp13;
  195. tmp1 := tmp11 + tmp12;
  196. tmp2 := tmp11 - tmp12;
  197. { Odd part }
  198. z13 := wsptr^[5] + wsptr^[3];
  199. z10 := wsptr^[5] - wsptr^[3];
  200. z11 := wsptr^[1] + wsptr^[7];
  201. z12 := wsptr^[1] - wsptr^[7];
  202. tmp7 := z11 + z13;
  203. tmp11 := (z11 - z13) * ({FAST_FLOAT}(1.414213562));
  204. z5 := (z10 + z12) * ({FAST_FLOAT}(1.847759065)); { 2*c2 }
  205. tmp10 := ({FAST_FLOAT}(1.082392200)) * z12 - z5; { 2*(c2-c6) }
  206. tmp12 := ({FAST_FLOAT}(-2.613125930)) * z10 + z5; { -2*(c2+c6) }
  207. tmp6 := tmp12 - tmp7;
  208. tmp5 := tmp11 - tmp6;
  209. tmp4 := tmp10 + tmp5;
  210. { Final output stage: scale down by a factor of 8 and range-limit }
  211. outptr^[0] := range_limit^[ int(DESCALE( INT32(Round((tmp0 + tmp7))), 3))
  212. and RANGE_MASK];
  213. outptr^[7] := range_limit^[ int(DESCALE( INT32(Round((tmp0 - tmp7))), 3))
  214. and RANGE_MASK];
  215. outptr^[1] := range_limit^[ int(DESCALE( INT32(Round((tmp1 + tmp6))), 3))
  216. and RANGE_MASK];
  217. outptr^[6] := range_limit^[ int(DESCALE( INT32(Round((tmp1 - tmp6))), 3))
  218. and RANGE_MASK];
  219. outptr^[2] := range_limit^[ int(DESCALE( INT32(Round((tmp2 + tmp5))), 3))
  220. and RANGE_MASK];
  221. outptr^[5] := range_limit^[ int(DESCALE( INT32(Round((tmp2 - tmp5))), 3))
  222. and RANGE_MASK];
  223. outptr^[4] := range_limit^[ int(DESCALE( INT32(Round((tmp3 + tmp4))), 3))
  224. and RANGE_MASK];
  225. outptr^[3] := range_limit^[ int(DESCALE( INT32(Round((tmp3 - tmp4))), 3))
  226. and RANGE_MASK];
  227. Inc(FAST_FLOAT_PTR(wsptr), DCTSIZE); { advance pointer to next row }
  228. end;
  229. end;
  230. end.