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.

895 lines
28 KiB

3 years ago
  1. unit imjdcoefct;
  2. { This file contains the coefficient buffer controller for decompression.
  3. This controller is the top level of the JPEG decompressor proper.
  4. The coefficient buffer lies between entropy decoding and inverse-DCT steps.
  5. In buffered-image mode, this controller is the interface between
  6. input-oriented processing and output-oriented processing.
  7. Also, the input side (only) is used when reading a file for transcoding. }
  8. { Original: jdcoefct.c ; Copyright (C) 1994-1997, Thomas G. Lane. }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjinclude,
  14. imjdeferr,
  15. imjerror,
  16. imjutils,
  17. imjpeglib;
  18. {GLOBAL}
  19. procedure jinit_d_coef_controller (cinfo : j_decompress_ptr;
  20. need_full_buffer : boolean);
  21. implementation
  22. { Block smoothing is only applicable for progressive JPEG, so: }
  23. {$ifndef D_PROGRESSIVE_SUPPORTED}
  24. {$undef BLOCK_SMOOTHING_SUPPORTED}
  25. {$endif}
  26. { Private buffer controller object }
  27. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  28. const
  29. SAVED_COEFS = 6; { we save coef_bits[0..5] }
  30. type
  31. Latch = array[0..SAVED_COEFS-1] of int;
  32. Latch_ptr = ^Latch;
  33. {$endif}
  34. type
  35. my_coef_ptr = ^my_coef_controller;
  36. my_coef_controller = record
  37. pub : jpeg_d_coef_controller; { public fields }
  38. { These variables keep track of the current location of the input side. }
  39. { cinfo^.input_iMCU_row is also used for this. }
  40. MCU_ctr : JDIMENSION; { counts MCUs processed in current row }
  41. MCU_vert_offset : int; { counts MCU rows within iMCU row }
  42. MCU_rows_per_iMCU_row : int; { number of such rows needed }
  43. { The output side's location is represented by cinfo^.output_iMCU_row. }
  44. { In single-pass modes, it's sufficient to buffer just one MCU.
  45. We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
  46. and let the entropy decoder write into that workspace each time.
  47. (On 80x86, the workspace is FAR even though it's not really very big;
  48. this is to keep the module interfaces unchanged when a large coefficient
  49. buffer is necessary.)
  50. In multi-pass modes, this array points to the current MCU's blocks
  51. within the virtual arrays; it is used only by the input side. }
  52. MCU_buffer : array[0..D_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;
  53. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  54. { In multi-pass modes, we need a virtual block array for each component. }
  55. whole_image : jvirt_barray_tbl;
  56. {$endif}
  57. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  58. { When doing block smoothing, we latch coefficient Al values here }
  59. coef_bits_latch : Latch_Ptr;
  60. {$endif}
  61. end;
  62. { Forward declarations }
  63. {METHODDEF}
  64. function decompress_onepass (cinfo : j_decompress_ptr;
  65. output_buf : JSAMPIMAGE) : int; forward;
  66. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  67. {METHODDEF}
  68. function decompress_data (cinfo : j_decompress_ptr;
  69. output_buf : JSAMPIMAGE) : int; forward;
  70. {$endif}
  71. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  72. {LOCAL}
  73. function smoothing_ok (cinfo : j_decompress_ptr) : boolean; forward;
  74. {METHODDEF}
  75. function decompress_smooth_data (cinfo : j_decompress_ptr;
  76. output_buf : JSAMPIMAGE) : int; forward;
  77. {$endif}
  78. {LOCAL}
  79. procedure start_iMCU_row (cinfo : j_decompress_ptr);
  80. { Reset within-iMCU-row counters for a new row (input side) }
  81. var
  82. coef : my_coef_ptr;
  83. begin
  84. coef := my_coef_ptr (cinfo^.coef);
  85. { In an interleaved scan, an MCU row is the same as an iMCU row.
  86. In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  87. But at the bottom of the image, process only what's left. }
  88. if (cinfo^.comps_in_scan > 1) then
  89. begin
  90. coef^.MCU_rows_per_iMCU_row := 1;
  91. end
  92. else
  93. begin
  94. if (cinfo^.input_iMCU_row < (cinfo^.total_iMCU_rows-1)) then
  95. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.v_samp_factor
  96. else
  97. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.last_row_height;
  98. end;
  99. coef^.MCU_ctr := 0;
  100. coef^.MCU_vert_offset := 0;
  101. end;
  102. { Initialize for an input processing pass. }
  103. {METHODDEF}
  104. procedure start_input_pass (cinfo : j_decompress_ptr);
  105. begin
  106. cinfo^.input_iMCU_row := 0;
  107. start_iMCU_row(cinfo);
  108. end;
  109. { Initialize for an output processing pass. }
  110. {METHODDEF}
  111. procedure start_output_pass (cinfo : j_decompress_ptr);
  112. var
  113. coef : my_coef_ptr;
  114. begin
  115. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  116. coef := my_coef_ptr (cinfo^.coef);
  117. { If multipass, check to see whether to use block smoothing on this pass }
  118. if (coef^.pub.coef_arrays <> NIL) then
  119. begin
  120. if (cinfo^.do_block_smoothing) and smoothing_ok(cinfo) then
  121. coef^.pub.decompress_data := decompress_smooth_data
  122. else
  123. coef^.pub.decompress_data := decompress_data;
  124. end;
  125. {$endif}
  126. cinfo^.output_iMCU_row := 0;
  127. end;
  128. { Decompress and return some data in the single-pass case.
  129. Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  130. Input and output must run in lockstep since we have only a one-MCU buffer.
  131. Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  132. NB: output_buf contains a plane for each component in image,
  133. which we index according to the component's SOF position.}
  134. {METHODDEF}
  135. function decompress_onepass (cinfo : j_decompress_ptr;
  136. output_buf : JSAMPIMAGE) : int;
  137. var
  138. coef : my_coef_ptr;
  139. MCU_col_num : JDIMENSION; { index of current MCU within row }
  140. last_MCU_col : JDIMENSION;
  141. last_iMCU_row : JDIMENSION;
  142. blkn, ci, xindex, yindex, yoffset, useful_width : int;
  143. output_ptr : JSAMPARRAY;
  144. start_col, output_col : JDIMENSION;
  145. compptr : jpeg_component_info_ptr;
  146. inverse_DCT : inverse_DCT_method_ptr;
  147. begin
  148. coef := my_coef_ptr (cinfo^.coef);
  149. last_MCU_col := cinfo^.MCUs_per_row - 1;
  150. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  151. { Loop to process as much as one whole iMCU row }
  152. for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  153. begin
  154. for MCU_col_num := coef^.MCU_ctr to last_MCU_col do
  155. begin
  156. { Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. }
  157. jzero_far( coef^.MCU_buffer[0],
  158. size_t (cinfo^.blocks_in_MCU * SIZEOF(JBLOCK)));
  159. if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
  160. begin
  161. { Suspension forced; update state counters and exit }
  162. coef^.MCU_vert_offset := yoffset;
  163. coef^.MCU_ctr := MCU_col_num;
  164. decompress_onepass := JPEG_SUSPENDED;
  165. exit;
  166. end;
  167. { Determine where data should go in output_buf and do the IDCT thing.
  168. We skip dummy blocks at the right and bottom edges (but blkn gets
  169. incremented past them!). Note the inner loop relies on having
  170. allocated the MCU_buffer[] blocks sequentially. }
  171. blkn := 0; { index of current DCT block within MCU }
  172. for ci := 0 to pred(cinfo^.comps_in_scan) do
  173. begin
  174. compptr := cinfo^.cur_comp_info[ci];
  175. { Don't bother to IDCT an uninteresting component. }
  176. if (not compptr^.component_needed) then
  177. begin
  178. Inc(blkn, compptr^.MCU_blocks);
  179. continue;
  180. end;
  181. inverse_DCT := cinfo^.idct^.inverse_DCT[compptr^.component_index];
  182. if (MCU_col_num < last_MCU_col) then
  183. useful_width := compptr^.MCU_width
  184. else
  185. useful_width := compptr^.last_col_width;
  186. output_ptr := JSAMPARRAY(@ output_buf^[compptr^.component_index]^
  187. [yoffset * compptr^.DCT_scaled_size]);
  188. start_col := LongInt(MCU_col_num) * compptr^.MCU_sample_width;
  189. for yindex := 0 to pred(compptr^.MCU_height) do
  190. begin
  191. if (cinfo^.input_iMCU_row < last_iMCU_row) or
  192. (yoffset+yindex < compptr^.last_row_height) then
  193. begin
  194. output_col := start_col;
  195. for xindex := 0 to pred(useful_width) do
  196. begin
  197. inverse_DCT (cinfo, compptr,
  198. JCOEFPTR(coef^.MCU_buffer[blkn+xindex]),
  199. output_ptr, output_col);
  200. Inc(output_col, compptr^.DCT_scaled_size);
  201. end;
  202. end;
  203. Inc(blkn, compptr^.MCU_width);
  204. Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
  205. end;
  206. end;
  207. end;
  208. { Completed an MCU row, but perhaps not an iMCU row }
  209. coef^.MCU_ctr := 0;
  210. end;
  211. { Completed the iMCU row, advance counters for next one }
  212. Inc(cinfo^.output_iMCU_row);
  213. Inc(cinfo^.input_iMCU_row);
  214. if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
  215. begin
  216. start_iMCU_row(cinfo);
  217. decompress_onepass := JPEG_ROW_COMPLETED;
  218. exit;
  219. end;
  220. { Completed the scan }
  221. cinfo^.inputctl^.finish_input_pass (cinfo);
  222. decompress_onepass := JPEG_SCAN_COMPLETED;
  223. end;
  224. { Dummy consume-input routine for single-pass operation. }
  225. {METHODDEF}
  226. function dummy_consume_data (cinfo : j_decompress_ptr) : int;
  227. begin
  228. dummy_consume_data := JPEG_SUSPENDED; { Always indicate nothing was done }
  229. end;
  230. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  231. { Consume input data and store it in the full-image coefficient buffer.
  232. We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  233. ie, v_samp_factor block rows for each component in the scan.
  234. Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.}
  235. {METHODDEF}
  236. function consume_data (cinfo : j_decompress_ptr) : int;
  237. var
  238. coef : my_coef_ptr;
  239. MCU_col_num : JDIMENSION; { index of current MCU within row }
  240. blkn, ci, xindex, yindex, yoffset : int;
  241. start_col : JDIMENSION;
  242. buffer : array[0..MAX_COMPS_IN_SCAN-1] of JBLOCKARRAY;
  243. buffer_ptr : JBLOCKROW;
  244. compptr : jpeg_component_info_ptr;
  245. begin
  246. coef := my_coef_ptr (cinfo^.coef);
  247. { Align the virtual buffers for the components used in this scan. }
  248. for ci := 0 to pred(cinfo^.comps_in_scan) do
  249. begin
  250. compptr := cinfo^.cur_comp_info[ci];
  251. buffer[ci] := cinfo^.mem^.access_virt_barray
  252. (j_common_ptr (cinfo), coef^.whole_image[compptr^.component_index],
  253. LongInt(cinfo^.input_iMCU_row) * compptr^.v_samp_factor,
  254. JDIMENSION (compptr^.v_samp_factor), TRUE);
  255. { Note: entropy decoder expects buffer to be zeroed,
  256. but this is handled automatically by the memory manager
  257. because we requested a pre-zeroed array. }
  258. end;
  259. { Loop to process one whole iMCU row }
  260. for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  261. begin
  262. for MCU_col_num := coef^.MCU_ctr to pred(cinfo^.MCUs_per_row) do
  263. begin
  264. { Construct list of pointers to DCT blocks belonging to this MCU }
  265. blkn := 0; { index of current DCT block within MCU }
  266. for ci := 0 to pred(cinfo^.comps_in_scan) do
  267. begin
  268. compptr := cinfo^.cur_comp_info[ci];
  269. start_col := LongInt(MCU_col_num) * compptr^.MCU_width;
  270. for yindex := 0 to pred(compptr^.MCU_height) do
  271. begin
  272. buffer_ptr := JBLOCKROW(@ buffer[ci]^[yindex+yoffset]^[start_col]);
  273. for xindex := 0 to pred(compptr^.MCU_width) do
  274. begin
  275. coef^.MCU_buffer[blkn] := buffer_ptr;
  276. Inc(blkn);
  277. Inc(JBLOCK_PTR(buffer_ptr));
  278. end;
  279. end;
  280. end;
  281. { Try to fetch the MCU. }
  282. if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
  283. begin
  284. { Suspension forced; update state counters and exit }
  285. coef^.MCU_vert_offset := yoffset;
  286. coef^.MCU_ctr := MCU_col_num;
  287. consume_data := JPEG_SUSPENDED;
  288. exit;
  289. end;
  290. end;
  291. { Completed an MCU row, but perhaps not an iMCU row }
  292. coef^.MCU_ctr := 0;
  293. end;
  294. { Completed the iMCU row, advance counters for next one }
  295. Inc(cinfo^.input_iMCU_row);
  296. if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
  297. begin
  298. start_iMCU_row(cinfo);
  299. consume_data := JPEG_ROW_COMPLETED;
  300. exit;
  301. end;
  302. { Completed the scan }
  303. cinfo^.inputctl^.finish_input_pass (cinfo);
  304. consume_data := JPEG_SCAN_COMPLETED;
  305. end;
  306. { Decompress and return some data in the multi-pass case.
  307. Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  308. Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  309. NB: output_buf contains a plane for each component in image. }
  310. {METHODDEF}
  311. function decompress_data (cinfo : j_decompress_ptr;
  312. output_buf : JSAMPIMAGE) : int;
  313. var
  314. coef : my_coef_ptr;
  315. last_iMCU_row : JDIMENSION;
  316. block_num : JDIMENSION;
  317. ci, block_row, block_rows : int;
  318. buffer : JBLOCKARRAY;
  319. buffer_ptr : JBLOCKROW;
  320. output_ptr : JSAMPARRAY;
  321. output_col : JDIMENSION;
  322. compptr : jpeg_component_info_ptr;
  323. inverse_DCT : inverse_DCT_method_ptr;
  324. begin
  325. coef := my_coef_ptr (cinfo^.coef);
  326. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  327. { Force some input to be done if we are getting ahead of the input. }
  328. while (cinfo^.input_scan_number < cinfo^.output_scan_number) or
  329. ((cinfo^.input_scan_number = cinfo^.output_scan_number) and
  330. (LongInt(cinfo^.input_iMCU_row) <= cinfo^.output_iMCU_row)) do
  331. begin
  332. if (cinfo^.inputctl^.consume_input(cinfo) = JPEG_SUSPENDED) then
  333. begin
  334. decompress_data := JPEG_SUSPENDED;
  335. exit;
  336. end;
  337. end;
  338. { OK, output from the virtual arrays. }
  339. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  340. for ci := 0 to pred(cinfo^.num_components) do
  341. begin
  342. { Don't bother to IDCT an uninteresting component. }
  343. if (not compptr^.component_needed) then
  344. continue;
  345. { Align the virtual buffer for this component. }
  346. buffer := cinfo^.mem^.access_virt_barray
  347. (j_common_ptr (cinfo), coef^.whole_image[ci],
  348. cinfo^.output_iMCU_row * compptr^.v_samp_factor,
  349. JDIMENSION (compptr^.v_samp_factor), FALSE);
  350. { Count non-dummy DCT block rows in this iMCU row. }
  351. if (cinfo^.output_iMCU_row < LongInt(last_iMCU_row)) then
  352. block_rows := compptr^.v_samp_factor
  353. else
  354. begin
  355. { NB: can't use last_row_height here; it is input-side-dependent! }
  356. block_rows := int(LongInt(compptr^.height_in_blocks) mod compptr^.v_samp_factor);
  357. if (block_rows = 0) then
  358. block_rows := compptr^.v_samp_factor;
  359. end;
  360. inverse_DCT := cinfo^.idct^.inverse_DCT[ci];
  361. output_ptr := output_buf^[ci];
  362. { Loop over all DCT blocks to be processed. }
  363. for block_row := 0 to pred(block_rows) do
  364. begin
  365. buffer_ptr := buffer^[block_row];
  366. output_col := 0;
  367. for block_num := 0 to pred(compptr^.width_in_blocks) do
  368. begin
  369. inverse_DCT (cinfo, compptr, JCOEFPTR (buffer_ptr),
  370. output_ptr, output_col);
  371. Inc(JBLOCK_PTR(buffer_ptr));
  372. Inc(output_col, compptr^.DCT_scaled_size);
  373. end;
  374. Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
  375. end;
  376. Inc(compptr);
  377. end;
  378. Inc(cinfo^.output_iMCU_row);
  379. if (cinfo^.output_iMCU_row < LongInt(cinfo^.total_iMCU_rows)) then
  380. begin
  381. decompress_data := JPEG_ROW_COMPLETED;
  382. exit;
  383. end;
  384. decompress_data := JPEG_SCAN_COMPLETED;
  385. end;
  386. {$endif} { D_MULTISCAN_FILES_SUPPORTED }
  387. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  388. { This code applies interblock smoothing as described by section K.8
  389. of the JPEG standard: the first 5 AC coefficients are estimated from
  390. the DC values of a DCT block and its 8 neighboring blocks.
  391. We apply smoothing only for progressive JPEG decoding, and only if
  392. the coefficients it can estimate are not yet known to full precision. }
  393. { Natural-order array positions of the first 5 zigzag-order coefficients }
  394. const
  395. Q01_POS = 1;
  396. Q10_POS = 8;
  397. Q20_POS = 16;
  398. Q11_POS = 9;
  399. Q02_POS = 2;
  400. { Determine whether block smoothing is applicable and safe.
  401. We also latch the current states of the coef_bits[] entries for the
  402. AC coefficients; otherwise, if the input side of the decompressor
  403. advances into a new scan, we might think the coefficients are known
  404. more accurately than they really are. }
  405. {LOCAL}
  406. function smoothing_ok (cinfo : j_decompress_ptr) : boolean;
  407. var
  408. coef : my_coef_ptr;
  409. smoothing_useful : boolean;
  410. ci, coefi : int;
  411. compptr : jpeg_component_info_ptr;
  412. qtable : JQUANT_TBL_PTR;
  413. coef_bits : coef_bits_ptr;
  414. coef_bits_latch : Latch_Ptr;
  415. begin
  416. coef := my_coef_ptr (cinfo^.coef);
  417. smoothing_useful := FALSE;
  418. if (not cinfo^.progressive_mode) or (cinfo^.coef_bits = NIL) then
  419. begin
  420. smoothing_ok := FALSE;
  421. exit;
  422. end;
  423. { Allocate latch area if not already done }
  424. if (coef^.coef_bits_latch = NIL) then
  425. coef^.coef_bits_latch := Latch_Ptr(
  426. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  427. cinfo^.num_components *
  428. (SAVED_COEFS * SIZEOF(int))) );
  429. coef_bits_latch := (coef^.coef_bits_latch);
  430. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  431. for ci := 0 to pred(cinfo^.num_components) do
  432. begin
  433. { All components' quantization values must already be latched. }
  434. qtable := compptr^.quant_table;
  435. if (qtable = NIL) then
  436. begin
  437. smoothing_ok := FALSE;
  438. exit;
  439. end;
  440. { Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. }
  441. if (qtable^.quantval[0] = 0) or
  442. (qtable^.quantval[Q01_POS] = 0) or
  443. (qtable^.quantval[Q10_POS] = 0) or
  444. (qtable^.quantval[Q20_POS] = 0) or
  445. (qtable^.quantval[Q11_POS] = 0) or
  446. (qtable^.quantval[Q02_POS] = 0) then
  447. begin
  448. smoothing_ok := FALSE;
  449. exit;
  450. end;
  451. { DC values must be at least partly known for all components. }
  452. coef_bits := @cinfo^.coef_bits^[ci]; { Nomssi }
  453. if (coef_bits^[0] < 0) then
  454. begin
  455. smoothing_ok := FALSE;
  456. exit;
  457. end;
  458. { Block smoothing is helpful if some AC coefficients remain inaccurate. }
  459. for coefi := 1 to 5 do
  460. begin
  461. coef_bits_latch^[coefi] := coef_bits^[coefi];
  462. if (coef_bits^[coefi] <> 0) then
  463. smoothing_useful := TRUE;
  464. end;
  465. Inc(coef_bits_latch {SAVED_COEFS});
  466. Inc(compptr);
  467. end;
  468. smoothing_ok := smoothing_useful;
  469. end;
  470. { Variant of decompress_data for use when doing block smoothing. }
  471. {METHODDEF}
  472. function decompress_smooth_data (cinfo : j_decompress_ptr;
  473. output_buf : JSAMPIMAGE) : int;
  474. var
  475. coef : my_coef_ptr;
  476. last_iMCU_row : JDIMENSION;
  477. block_num, last_block_column : JDIMENSION;
  478. ci, block_row, block_rows, access_rows : int;
  479. buffer : JBLOCKARRAY;
  480. buffer_ptr, prev_block_row, next_block_row : JBLOCKROW;
  481. output_ptr : JSAMPARRAY;
  482. output_col : JDIMENSION;
  483. compptr : jpeg_component_info_ptr;
  484. inverse_DCT : inverse_DCT_method_ptr;
  485. first_row, last_row : boolean;
  486. workspace : JBLOCK;
  487. coef_bits : Latch_Ptr; { coef_bits_ptr; }
  488. quanttbl : JQUANT_TBL_PTR;
  489. Q00,Q01,Q02,Q10,Q11,Q20, num : INT32;
  490. DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9 : int;
  491. Al, pred : int;
  492. var
  493. delta : JDIMENSION;
  494. begin
  495. coef := my_coef_ptr (cinfo^.coef);
  496. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  497. { Force some input to be done if we are getting ahead of the input. }
  498. while (cinfo^.input_scan_number <= cinfo^.output_scan_number) and
  499. (not cinfo^.inputctl^.eoi_reached) do
  500. begin
  501. if (cinfo^.input_scan_number = cinfo^.output_scan_number) then
  502. begin
  503. { If input is working on current scan, we ordinarily want it to
  504. have completed the current row. But if input scan is DC,
  505. we want it to keep one row ahead so that next block row's DC
  506. values are up to date. }
  507. if (cinfo^.Ss = 0) then
  508. delta := 1
  509. else
  510. delta := 0;
  511. if (LongInt(cinfo^.input_iMCU_row) > cinfo^.output_iMCU_row+LongInt(delta)) then
  512. break;
  513. end;
  514. if (cinfo^.inputctl^.consume_input(cinfo) = JPEG_SUSPENDED) then
  515. begin
  516. decompress_smooth_data := JPEG_SUSPENDED;
  517. exit;
  518. end;
  519. end;
  520. { OK, output from the virtual arrays. }
  521. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  522. for ci := 0 to (cinfo^.num_components-1) do
  523. begin
  524. { Don't bother to IDCT an uninteresting component. }
  525. if (not compptr^.component_needed) then
  526. continue;
  527. { Count non-dummy DCT block rows in this iMCU row. }
  528. if (cinfo^.output_iMCU_row < LongInt(last_iMCU_row)) then
  529. begin
  530. block_rows := compptr^.v_samp_factor;
  531. access_rows := block_rows * 2; { this and next iMCU row }
  532. last_row := FALSE;
  533. end
  534. else
  535. begin
  536. { NB: can't use last_row_height here; it is input-side-dependent! }
  537. block_rows := int (compptr^.height_in_blocks) mod compptr^.v_samp_factor;
  538. if (block_rows = 0) then
  539. block_rows := compptr^.v_samp_factor;
  540. access_rows := block_rows; { this iMCU row only }
  541. last_row := TRUE;
  542. end;
  543. { Align the virtual buffer for this component. }
  544. if (cinfo^.output_iMCU_row > 0) then
  545. begin
  546. Inc(access_rows, compptr^.v_samp_factor); { prior iMCU row too }
  547. buffer := cinfo^.mem^.access_virt_barray
  548. (j_common_ptr (cinfo), coef^.whole_image[ci],
  549. (cinfo^.output_iMCU_row - 1) * compptr^.v_samp_factor,
  550. JDIMENSION (access_rows), FALSE);
  551. Inc(JBLOCKROW_PTR(buffer), compptr^.v_samp_factor); { point to current iMCU row }
  552. first_row := FALSE;
  553. end
  554. else
  555. begin
  556. buffer := cinfo^.mem^.access_virt_barray
  557. (j_common_ptr (cinfo), coef^.whole_image[ci],
  558. JDIMENSION (0), JDIMENSION (access_rows), FALSE);
  559. first_row := TRUE;
  560. end;
  561. { Fetch component-dependent info }
  562. coef_bits := coef^.coef_bits_latch;
  563. Inc(coef_bits, ci); { ci * SAVED_COEFS}
  564. quanttbl := compptr^.quant_table;
  565. Q00 := quanttbl^.quantval[0];
  566. Q01 := quanttbl^.quantval[Q01_POS];
  567. Q10 := quanttbl^.quantval[Q10_POS];
  568. Q20 := quanttbl^.quantval[Q20_POS];
  569. Q11 := quanttbl^.quantval[Q11_POS];
  570. Q02 := quanttbl^.quantval[Q02_POS];
  571. inverse_DCT := cinfo^.idct^.inverse_DCT[ci];
  572. output_ptr := output_buf^[ci];
  573. { Loop over all DCT blocks to be processed. }
  574. for block_row := 0 to (block_rows-1) do
  575. begin
  576. buffer_ptr := buffer^[block_row];
  577. if (first_row) and (block_row = 0) then
  578. prev_block_row := buffer_ptr
  579. else
  580. prev_block_row := buffer^[block_row-1];
  581. if (last_row) and (block_row = block_rows-1) then
  582. next_block_row := buffer_ptr
  583. else
  584. next_block_row := buffer^[block_row+1];
  585. { We fetch the surrounding DC values using a sliding-register approach.
  586. Initialize all nine here so as to do the right thing on narrow pics.}
  587. DC3 := int(prev_block_row^[0][0]);
  588. DC2 := DC3;
  589. DC1 := DC2;
  590. DC6 := int(buffer_ptr^[0][0]);
  591. DC5 := DC6;
  592. DC4 := DC5;
  593. DC9 := int(next_block_row^[0][0]);
  594. DC8 := DC9;
  595. DC7 := DC8 ;
  596. output_col := 0;
  597. last_block_column := compptr^.width_in_blocks - 1;
  598. for block_num := 0 to last_block_column do
  599. begin
  600. { Fetch current DCT block into workspace so we can modify it. }
  601. jcopy_block_row(buffer_ptr, JBLOCKROW (@workspace), JDIMENSION(1));
  602. { Update DC values }
  603. if (block_num < last_block_column) then
  604. begin
  605. DC3 := int (prev_block_row^[1][0]);
  606. DC6 := int (buffer_ptr^[1][0]);
  607. DC9 := int (next_block_row^[1][0]);
  608. end;
  609. { Compute coefficient estimates per K.8.
  610. An estimate is applied only if coefficient is still zero,
  611. and is not known to be fully accurate. }
  612. { AC01 }
  613. Al := coef_bits^[1];
  614. if (Al <> 0) and (workspace[1] = 0) then
  615. begin
  616. num := 36 * Q00 * (DC4 - DC6);
  617. if (num >= 0) then
  618. begin
  619. pred := int (((Q01 shl 7) + num) div (Q01 shl 8));
  620. if (Al > 0) and (pred >= (1 shl Al)) then
  621. pred := (1 shl Al)-1;
  622. end
  623. else
  624. begin
  625. pred := int (((Q01 shl 7) - num) div (Q01 shl 8));
  626. if (Al > 0) and (pred >= (1 shl Al)) then
  627. pred := (1 shl Al)-1;
  628. pred := -pred;
  629. end;
  630. workspace[1] := JCOEF (pred);
  631. end;
  632. { AC10 }
  633. Al := coef_bits^[2];
  634. if (Al <> 0) and (workspace[8] = 0) then
  635. begin
  636. num := 36 * Q00 * (DC2 - DC8);
  637. if (num >= 0) then
  638. begin
  639. pred := int (((Q10 shl 7) + num) div (Q10 shl 8));
  640. if (Al > 0) and (pred >= (1 shl Al)) then
  641. pred := (1 shl Al)-1;
  642. end
  643. else
  644. begin
  645. pred := int (((Q10 shl 7) - num) div (Q10 shl 8));
  646. if (Al > 0) and (pred >= (1 shl Al)) then
  647. pred := (1 shl Al)-1;
  648. pred := -pred;
  649. end;
  650. workspace[8] := JCOEF (pred);
  651. end;
  652. { AC20 }
  653. Al := coef_bits^[3];
  654. if (Al <> 0) and (workspace[16] = 0) then
  655. begin
  656. num := 9 * Q00 * (DC2 + DC8 - 2*DC5);
  657. if (num >= 0) then
  658. begin
  659. pred := int (((Q20 shl 7) + num) div (Q20 shl 8));
  660. if (Al > 0) and (pred >= (1 shl Al)) then
  661. pred := (1 shl Al)-1;
  662. end
  663. else
  664. begin
  665. pred := int (((Q20 shl 7) - num) div (Q20 shl 8));
  666. if (Al > 0) and (pred >= (1 shl Al)) then
  667. pred := (1 shl Al)-1;
  668. pred := -pred;
  669. end;
  670. workspace[16] := JCOEF (pred);
  671. end;
  672. { AC11 }
  673. Al := coef_bits^[4];
  674. if (Al <> 0) and (workspace[9] = 0) then
  675. begin
  676. num := 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  677. if (num >= 0) then
  678. begin
  679. pred := int (((Q11 shl 7) + num) div (Q11 shl 8));
  680. if (Al > 0) and (pred >= (1 shl Al)) then
  681. pred := (1 shl Al)-1;
  682. end
  683. else
  684. begin
  685. pred := int (((Q11 shl 7) - num) div (Q11 shl 8));
  686. if (Al > 0) and (pred >= (1 shl Al)) then
  687. pred := (1 shl Al)-1;
  688. pred := -pred;
  689. end;
  690. workspace[9] := JCOEF (pred);
  691. end;
  692. { AC02 }
  693. Al := coef_bits^[5];
  694. if (Al <> 0) and (workspace[2] = 0) then
  695. begin
  696. num := 9 * Q00 * (DC4 + DC6 - 2*DC5);
  697. if (num >= 0) then
  698. begin
  699. pred := int (((Q02 shl 7) + num) div (Q02 shl 8));
  700. if (Al > 0) and (pred >= (1 shl Al)) then
  701. pred := (1 shl Al)-1;
  702. end
  703. else
  704. begin
  705. pred := int (((Q02 shl 7) - num) div (Q02 shl 8));
  706. if (Al > 0) and (pred >= (1 shl Al)) then
  707. pred := (1 shl Al)-1;
  708. pred := -pred;
  709. end;
  710. workspace[2] := JCOEF (pred);
  711. end;
  712. { OK, do the IDCT }
  713. inverse_DCT (cinfo, compptr, JCOEFPTR (@workspace),
  714. output_ptr, output_col);
  715. { Advance for next column }
  716. DC1 := DC2; DC2 := DC3;
  717. DC4 := DC5; DC5 := DC6;
  718. DC7 := DC8; DC8 := DC9;
  719. Inc(JBLOCK_PTR(buffer_ptr));
  720. Inc(JBLOCK_PTR(prev_block_row));
  721. Inc(JBLOCK_PTR(next_block_row));
  722. Inc(output_col, compptr^.DCT_scaled_size);
  723. end;
  724. Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
  725. end;
  726. Inc(compptr);
  727. end;
  728. Inc(cinfo^.output_iMCU_row);
  729. if (cinfo^.output_iMCU_row < LongInt(cinfo^.total_iMCU_rows)) then
  730. begin
  731. decompress_smooth_data := JPEG_ROW_COMPLETED;
  732. exit;
  733. end;
  734. decompress_smooth_data := JPEG_SCAN_COMPLETED;
  735. end;
  736. {$endif} { BLOCK_SMOOTHING_SUPPORTED }
  737. { Initialize coefficient buffer controller. }
  738. {GLOBAL}
  739. procedure jinit_d_coef_controller (cinfo : j_decompress_ptr;
  740. need_full_buffer : boolean);
  741. var
  742. coef : my_coef_ptr;
  743. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  744. var
  745. ci, access_rows : int;
  746. compptr : jpeg_component_info_ptr;
  747. {$endif}
  748. var
  749. buffer : JBLOCK_PTR;
  750. i : int;
  751. begin
  752. coef := my_coef_ptr(
  753. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  754. SIZEOF(my_coef_controller)) );
  755. cinfo^.coef := jpeg_d_coef_controller_ptr(coef);
  756. coef^.pub.start_input_pass := start_input_pass;
  757. coef^.pub.start_output_pass := start_output_pass;
  758. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  759. coef^.coef_bits_latch := NIL;
  760. {$endif}
  761. { Create the coefficient buffer. }
  762. if (need_full_buffer) then
  763. begin
  764. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  765. { Allocate a full-image virtual array for each component, }
  766. { padded to a multiple of samp_factor DCT blocks in each direction. }
  767. { Note we ask for a pre-zeroed array. }
  768. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  769. for ci := 0 to pred(cinfo^.num_components) do
  770. begin
  771. access_rows := compptr^.v_samp_factor;
  772. {$ifdef BLOCK_SMOOTHING_SUPPORTED}
  773. { If block smoothing could be used, need a bigger window }
  774. if (cinfo^.progressive_mode) then
  775. access_rows := access_rows * 3;
  776. {$endif}
  777. coef^.whole_image[ci] := cinfo^.mem^.request_virt_barray
  778. (j_common_ptr (cinfo), JPOOL_IMAGE, TRUE,
  779. JDIMENSION (jround_up( long(compptr^.width_in_blocks),
  780. long(compptr^.h_samp_factor) )),
  781. JDIMENSION (jround_up( long(compptr^.height_in_blocks),
  782. long(compptr^.v_samp_factor) )),
  783. JDIMENSION (access_rows));
  784. Inc(compptr);
  785. end;
  786. coef^.pub.consume_data := consume_data;
  787. coef^.pub.decompress_data := decompress_data;
  788. coef^.pub.coef_arrays := @(coef^.whole_image);
  789. { link to virtual arrays }
  790. {$else}
  791. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  792. {$endif}
  793. end
  794. else
  795. begin
  796. { We only need a single-MCU buffer. }
  797. buffer := JBLOCK_PTR (
  798. cinfo^.mem^.alloc_large (j_common_ptr (cinfo), JPOOL_IMAGE,
  799. D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)) );
  800. for i := 0 to pred(D_MAX_BLOCKS_IN_MCU) do
  801. begin
  802. coef^.MCU_buffer[i] := JBLOCKROW(buffer);
  803. Inc(buffer);
  804. end;
  805. coef^.pub.consume_data := dummy_consume_data;
  806. coef^.pub.decompress_data := decompress_onepass;
  807. coef^.pub.coef_arrays := NIL; { flag for no virtual arrays }
  808. end;
  809. end;
  810. end.