jddctmgr.pas 10 KB

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