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.

328 lines
9.5 KiB

3 years ago
  1. unit imjddctmgr;
  2. { Original : jddctmgr.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  3. { This file contains the inverse-DCT management logic.
  4. This code selects a particular IDCT implementation to be used,
  5. and it performs related housekeeping chores. No code in this file
  6. is executed per IDCT step, only during output pass setup.
  7. Note that the IDCT routines are responsible for performing coefficient
  8. dequantization as well as the IDCT proper. This module sets up the
  9. dequantization multiplier table needed by the IDCT routine. }
  10. interface
  11. {$I imjconfig.inc}
  12. uses
  13. imjmorecfg,
  14. imjinclude,
  15. imjdeferr,
  16. imjerror,
  17. imjpeglib,
  18. imjdct, { Private declarations for DCT subsystem }
  19. imjidctfst,
  20. {$IFDEF BASM}
  21. imjidctasm,
  22. {$ELSE}
  23. imjidctint,
  24. {$ENDIF}
  25. imjidctflt,
  26. imjidctred;
  27. { Initialize IDCT manager. }
  28. {GLOBAL}
  29. procedure jinit_inverse_dct (cinfo : j_decompress_ptr);
  30. implementation
  31. { The decompressor input side (jdinput.c) saves away the appropriate
  32. quantization table for each component at the start of the first scan
  33. involving that component. (This is necessary in order to correctly
  34. decode files that reuse Q-table slots.)
  35. When we are ready to make an output pass, the saved Q-table is converted
  36. to a multiplier table that will actually be used by the IDCT routine.
  37. The multiplier table contents are IDCT-method-dependent. To support
  38. application changes in IDCT method between scans, we can remake the
  39. multiplier tables if necessary.
  40. In buffered-image mode, the first output pass may occur before any data
  41. has been seen for some components, and thus before their Q-tables have
  42. been saved away. To handle this case, multiplier tables are preset
  43. to zeroes; the result of the IDCT will be a neutral gray level. }
  44. { Private subobject for this module }
  45. type
  46. my_idct_ptr = ^my_idct_controller;
  47. my_idct_controller = record
  48. pub : jpeg_inverse_dct; { public fields }
  49. { This array contains the IDCT method code that each multiplier table
  50. is currently set up for, or -1 if it's not yet set up.
  51. The actual multiplier tables are pointed to by dct_table in the
  52. per-component comp_info structures. }
  53. cur_method : array[0..MAX_COMPONENTS-1] of int;
  54. end; {my_idct_controller;}
  55. { Allocated multiplier tables: big enough for any supported variant }
  56. type
  57. multiplier_table = record
  58. case byte of
  59. 0:(islow_array : array[0..DCTSIZE2-1] of ISLOW_MULT_TYPE);
  60. {$ifdef DCT_IFAST_SUPPORTED}
  61. 1:(ifast_array : array[0..DCTSIZE2-1] of IFAST_MULT_TYPE);
  62. {$endif}
  63. {$ifdef DCT_FLOAT_SUPPORTED}
  64. 2:(float_array : array[0..DCTSIZE2-1] of FLOAT_MULT_TYPE);
  65. {$endif}
  66. end;
  67. { The current scaled-IDCT routines require ISLOW-style multiplier tables,
  68. so be sure to compile that code if either ISLOW or SCALING is requested. }
  69. {$ifdef DCT_ISLOW_SUPPORTED}
  70. {$define PROVIDE_ISLOW_TABLES}
  71. {$else}
  72. {$ifdef IDCT_SCALING_SUPPORTED}
  73. {$define PROVIDE_ISLOW_TABLES}
  74. {$endif}
  75. {$endif}
  76. { Prepare for an output pass.
  77. Here we select the proper IDCT routine for each component and build
  78. a matching multiplier table. }
  79. {METHODDEF}
  80. procedure start_pass (cinfo : j_decompress_ptr);
  81. var
  82. idct : my_idct_ptr;
  83. ci, i : int;
  84. compptr : jpeg_component_info_ptr;
  85. method : J_DCT_METHOD;
  86. method_ptr : inverse_DCT_method_ptr;
  87. qtbl : JQUANT_TBL_PTR;
  88. {$ifdef PROVIDE_ISLOW_TABLES}
  89. var
  90. ismtbl : ISLOW_MULT_TYPE_FIELD_PTR;
  91. {$endif}
  92. {$ifdef DCT_IFAST_SUPPORTED}
  93. const
  94. CONST_BITS = 14;
  95. const
  96. aanscales : array[0..DCTSIZE2-1] of INT16 =
  97. ({ precomputed values scaled up by 14 bits }
  98. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  99. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  100. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  101. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  102. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  103. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  104. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  105. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247);
  106. var
  107. ifmtbl : IFAST_MULT_TYPE_FIELD_PTR;
  108. {SHIFT_TEMPS}
  109. { Descale and correctly round an INT32 value that's scaled by N bits.
  110. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  111. the fudge factor is correct for either sign of X. }
  112. function DESCALE(x : INT32; n : int) : INT32;
  113. var
  114. shift_temp : INT32;
  115. begin
  116. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  117. shift_temp := x + (INT32(1) shl (n-1));
  118. if shift_temp < 0 then
  119. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  120. else
  121. Descale := (shift_temp shr n);
  122. {$else}
  123. Descale := (x + (INT32(1) shl (n-1)) shr n;
  124. {$endif}
  125. end;
  126. {$endif}
  127. {$ifdef DCT_FLOAT_SUPPORTED}
  128. const
  129. aanscalefactor : array[0..DCTSIZE-1] of double =
  130. (1.0, 1.387039845, 1.306562965, 1.175875602,
  131. 1.0, 0.785694958, 0.541196100, 0.275899379);
  132. var
  133. fmtbl : FLOAT_MULT_TYPE_FIELD_PTR;
  134. row, col : int;
  135. {$endif}
  136. begin
  137. idct := my_idct_ptr (cinfo^.idct);
  138. method := J_DCT_METHOD(0);
  139. method_ptr := NIL;
  140. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  141. for ci := 0 to pred(cinfo^.num_components) do
  142. begin
  143. { Select the proper IDCT routine for this component's scaling }
  144. case (compptr^.DCT_scaled_size) of
  145. {$ifdef IDCT_SCALING_SUPPORTED}
  146. 1:begin
  147. method_ptr := jpeg_idct_1x1;
  148. method := JDCT_ISLOW; { jidctred uses islow-style table }
  149. end;
  150. 2:begin
  151. method_ptr := jpeg_idct_2x2;
  152. method := JDCT_ISLOW; { jidctred uses islow-style table }
  153. end;
  154. 4:begin
  155. method_ptr := jpeg_idct_4x4;
  156. method := JDCT_ISLOW; { jidctred uses islow-style table }
  157. end;
  158. {$endif}
  159. DCTSIZE:
  160. case (cinfo^.dct_method) of
  161. {$ifdef DCT_ISLOW_SUPPORTED}
  162. JDCT_ISLOW:
  163. begin
  164. method_ptr := @jpeg_idct_islow;
  165. method := JDCT_ISLOW;
  166. end;
  167. {$endif}
  168. {$ifdef DCT_IFAST_SUPPORTED}
  169. JDCT_IFAST:
  170. begin
  171. method_ptr := @jpeg_idct_ifast;
  172. method := JDCT_IFAST;
  173. end;
  174. {$endif}
  175. {$ifdef DCT_FLOAT_SUPPORTED}
  176. JDCT_FLOAT:
  177. begin
  178. method_ptr := @jpeg_idct_float;
  179. method := JDCT_FLOAT;
  180. end;
  181. {$endif}
  182. else
  183. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  184. end;
  185. else
  186. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_DCTSIZE, compptr^.DCT_scaled_size);
  187. end;
  188. idct^.pub.inverse_DCT[ci] := method_ptr;
  189. { Create multiplier table from quant table.
  190. However, we can skip this if the component is uninteresting
  191. or if we already built the table. Also, if no quant table
  192. has yet been saved for the component, we leave the
  193. multiplier table all-zero; we'll be reading zeroes from the
  194. coefficient controller's buffer anyway. }
  195. if (not compptr^.component_needed) or (idct^.cur_method[ci] = int(method)) then
  196. continue;
  197. qtbl := compptr^.quant_table;
  198. if (qtbl = NIL) then { happens if no data yet for component }
  199. continue;
  200. idct^.cur_method[ci] := int(method);
  201. case (method) of
  202. {$ifdef PROVIDE_ISLOW_TABLES}
  203. JDCT_ISLOW:
  204. begin
  205. { For LL&M IDCT method, multipliers are equal to raw quantization
  206. coefficients, but are stored as ints to ensure access efficiency. }
  207. ismtbl := ISLOW_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  208. for i := 0 to pred(DCTSIZE2) do
  209. begin
  210. ismtbl^[i] := ISLOW_MULT_TYPE (qtbl^.quantval[i]);
  211. end;
  212. end;
  213. {$endif}
  214. {$ifdef DCT_IFAST_SUPPORTED}
  215. JDCT_IFAST:
  216. begin
  217. { For AA&N IDCT method, multipliers are equal to quantization
  218. coefficients scaled by scalefactor[row]*scalefactor[col], where
  219. scalefactor[0] := 1
  220. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
  221. For integer operation, the multiplier table is to be scaled by
  222. IFAST_SCALE_BITS. }
  223. ifmtbl := IFAST_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  224. for i := 0 to pred(DCTSIZE2) do
  225. begin
  226. ifmtbl^[i] := IFAST_MULT_TYPE(
  227. DESCALE( INT32 (qtbl^.quantval[i]) * INT32 (aanscales[i]),
  228. CONST_BITS-IFAST_SCALE_BITS) );
  229. end;
  230. end;
  231. {$endif}
  232. {$ifdef DCT_FLOAT_SUPPORTED}
  233. JDCT_FLOAT:
  234. begin
  235. { For float AA&N IDCT method, multipliers are equal to quantization
  236. coefficients scaled by scalefactor[row]*scalefactor[col], where
  237. scalefactor[0] := 1
  238. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7 }
  239. fmtbl := FLOAT_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
  240. i := 0;
  241. for row := 0 to pred(DCTSIZE) do
  242. begin
  243. for col := 0 to pred(DCTSIZE) do
  244. begin
  245. fmtbl^[i] := {FLOAT_MULT_TYPE} (
  246. {double} qtbl^.quantval[i] *
  247. aanscalefactor[row] * aanscalefactor[col] );
  248. Inc(i);
  249. end;
  250. end;
  251. end;
  252. {$endif}
  253. else
  254. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  255. break;
  256. end;
  257. Inc(compptr);
  258. end;
  259. end;
  260. { Initialize IDCT manager. }
  261. {GLOBAL}
  262. procedure jinit_inverse_dct (cinfo : j_decompress_ptr);
  263. var
  264. idct : my_idct_ptr;
  265. ci : int;
  266. compptr : jpeg_component_info_ptr;
  267. begin
  268. idct := my_idct_ptr(
  269. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  270. SIZEOF(my_idct_controller)) );
  271. cinfo^.idct := jpeg_inverse_dct_ptr (idct);
  272. idct^.pub.start_pass := start_pass;
  273. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  274. for ci := 0 to pred(cinfo^.num_components) do
  275. begin
  276. { Allocate and pre-zero a multiplier table for each component }
  277. compptr^.dct_table :=
  278. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  279. SIZEOF(multiplier_table));
  280. MEMZERO(compptr^.dct_table, SIZEOF(multiplier_table));
  281. { Mark multiplier table not yet set up for any method }
  282. idct^.cur_method[ci] := -1;
  283. Inc(compptr);
  284. end;
  285. end;
  286. end.