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.

610 lines
22 KiB

3 years ago
  1. unit imjdmainct;
  2. { This file is part of the Independent JPEG Group's software.
  3. For conditions of distribution and use, see the accompanying README file.
  4. This file contains the main buffer controller for decompression.
  5. The main buffer lies between the JPEG decompressor proper and the
  6. post-processor; it holds downsampled data in the JPEG colorspace.
  7. Note that this code is bypassed in raw-data mode, since the application
  8. supplies the equivalent of the main buffer in that case. }
  9. { Original: jdmainct.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  10. { In the current system design, the main buffer need never be a full-image
  11. buffer; any full-height buffers will be found inside the coefficient or
  12. postprocessing controllers. Nonetheless, the main controller is not
  13. trivial. Its responsibility is to provide context rows for upsampling/
  14. rescaling, and doing this in an efficient fashion is a bit tricky.
  15. Postprocessor input data is counted in "row groups". A row group
  16. is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
  17. sample rows of each component. (We require DCT_scaled_size values to be
  18. chosen such that these numbers are integers. In practice DCT_scaled_size
  19. values will likely be powers of two, so we actually have the stronger
  20. condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
  21. Upsampling will typically produce max_v_samp_factor pixel rows from each
  22. row group (times any additional scale factor that the upsampler is
  23. applying).
  24. The coefficient controller will deliver data to us one iMCU row at a time;
  25. each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
  26. exactly min_DCT_scaled_size row groups. (This amount of data corresponds
  27. to one row of MCUs when the image is fully interleaved.) Note that the
  28. number of sample rows varies across components, but the number of row
  29. groups does not. Some garbage sample rows may be included in the last iMCU
  30. row at the bottom of the image.
  31. Depending on the vertical scaling algorithm used, the upsampler may need
  32. access to the sample row(s) above and below its current input row group.
  33. The upsampler is required to set need_context_rows TRUE at global
  34. selection
  35. time if so. When need_context_rows is FALSE, this controller can simply
  36. obtain one iMCU row at a time from the coefficient controller and dole it
  37. out as row groups to the postprocessor.
  38. When need_context_rows is TRUE, this controller guarantees that the buffer
  39. passed to postprocessing contains at least one row group's worth of samples
  40. above and below the row group(s) being processed. Note that the context
  41. rows "above" the first passed row group appear at negative row offsets in
  42. the passed buffer. At the top and bottom of the image, the required
  43. context rows are manufactured by duplicating the first or last real sample
  44. row; this avoids having special cases in the upsampling inner loops.
  45. The amount of context is fixed at one row group just because that's a
  46. convenient number for this controller to work with. The existing
  47. upsamplers really only need one sample row of context. An upsampler
  48. supporting arbitrary output rescaling might wish for more than one row
  49. group of context when shrinking the image; tough, we don't handle that.
  50. (This is justified by the assumption that downsizing will be handled mostly
  51. by adjusting the DCT_scaled_size values, so that the actual scale factor at
  52. the upsample step needn't be much less than one.)
  53. To provide the desired context, we have to retain the last two row groups
  54. of one iMCU row while reading in the next iMCU row. (The last row group
  55. can't be processed until we have another row group for its below-context,
  56. and so we have to save the next-to-last group too for its above-context.)
  57. We could do this most simply by copying data around in our buffer, but
  58. that'd be very slow. We can avoid copying any data by creating a rather
  59. strange pointer structure. Here's how it works. We allocate a workspace
  60. consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
  61. of row groups per iMCU row). We create two sets of redundant pointers to
  62. the workspace. Labeling the physical row groups 0 to M+1, the synthesized
  63. pointer lists look like this:
  64. M+1 M-1
  65. master pointer --> 0 master pointer --> 0
  66. 1 1
  67. ... ...
  68. M-3 M-3
  69. M-2 M
  70. M-1 M+1
  71. M M-2
  72. M+1 M-1
  73. 0 0
  74. We read alternate iMCU rows using each master pointer; thus the last two
  75. row groups of the previous iMCU row remain un-overwritten in the workspace.
  76. The pointer lists are set up so that the required context rows appear to
  77. be adjacent to the proper places when we pass the pointer lists to the
  78. upsampler.
  79. The above pictures describe the normal state of the pointer lists.
  80. At top and bottom of the image, we diddle the pointer lists to duplicate
  81. the first or last sample row as necessary (this is cheaper than copying
  82. sample rows around).
  83. This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
  84. situation each iMCU row provides only one row group so the buffering logic
  85. must be different (eg, we must read two iMCU rows before we can emit the
  86. first row group). For now, we simply do not support providing context
  87. rows when min_DCT_scaled_size is 1. That combination seems unlikely to
  88. be worth providing --- if someone wants a 1/8th-size preview, they probably
  89. want it quick and dirty, so a context-free upsampler is sufficient. }
  90. interface
  91. {$I imjconfig.inc}
  92. uses
  93. imjmorecfg,
  94. imjinclude,
  95. {$ifdef QUANT_2PASS_SUPPORTED}
  96. imjquant2,
  97. {$endif}
  98. imjdeferr,
  99. imjerror,
  100. imjpeglib;
  101. {GLOBAL}
  102. procedure jinit_d_main_controller (cinfo : j_decompress_ptr;
  103. need_full_buffer : boolean);
  104. implementation
  105. { Private buffer controller object }
  106. type
  107. my_main_ptr = ^my_main_controller;
  108. my_main_controller = record
  109. pub : jpeg_d_main_controller; { public fields }
  110. { Pointer to allocated workspace (M or M+2 row groups). }
  111. buffer : array[0..MAX_COMPONENTS-1] of JSAMPARRAY;
  112. buffer_full : boolean; { Have we gotten an iMCU row from decoder? }
  113. rowgroup_ctr : JDIMENSION ; { counts row groups output to postprocessor }
  114. { Remaining fields are only used in the context case. }
  115. { These are the master pointers to the funny-order pointer lists. }
  116. xbuffer : array[0..2-1] of JSAMPIMAGE; { pointers to weird pointer lists }
  117. whichptr : int; { indicates which pointer set is now in use }
  118. context_state : int; { process_data state machine status }
  119. rowgroups_avail : JDIMENSION; { row groups available to postprocessor }
  120. iMCU_row_ctr : JDIMENSION; { counts iMCU rows to detect image top/bot }
  121. end; { my_main_controller; }
  122. { context_state values: }
  123. const
  124. CTX_PREPARE_FOR_IMCU = 0; { need to prepare for MCU row }
  125. CTX_PROCESS_IMCU = 1; { feeding iMCU to postprocessor }
  126. CTX_POSTPONED_ROW = 2; { feeding postponed row group }
  127. { Forward declarations }
  128. {METHODDEF}
  129. procedure process_data_simple_main(cinfo : j_decompress_ptr;
  130. output_buf : JSAMPARRAY;
  131. var out_row_ctr : JDIMENSION;
  132. out_rows_avail : JDIMENSION); forward;
  133. {METHODDEF}
  134. procedure process_data_context_main (cinfo : j_decompress_ptr;
  135. output_buf : JSAMPARRAY;
  136. var out_row_ctr : JDIMENSION;
  137. out_rows_avail : JDIMENSION); forward;
  138. {$ifdef QUANT_2PASS_SUPPORTED}
  139. {METHODDEF}
  140. procedure process_data_crank_post (cinfo : j_decompress_ptr;
  141. output_buf : JSAMPARRAY;
  142. var out_row_ctr : JDIMENSION;
  143. out_rows_avail : JDIMENSION); forward;
  144. {$endif}
  145. {LOCAL}
  146. procedure alloc_funny_pointers (cinfo : j_decompress_ptr);
  147. { Allocate space for the funny pointer lists.
  148. This is done only once, not once per pass. }
  149. var
  150. main : my_main_ptr;
  151. ci, rgroup : int;
  152. M : int;
  153. compptr : jpeg_component_info_ptr;
  154. xbuf : JSAMPARRAY;
  155. begin
  156. main := my_main_ptr (cinfo^.main);
  157. M := cinfo^.min_DCT_scaled_size;
  158. { Get top-level space for component array pointers.
  159. We alloc both arrays with one call to save a few cycles. }
  160. main^.xbuffer[0] := JSAMPIMAGE (
  161. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  162. cinfo^.num_components * 2 * SIZEOF(JSAMPARRAY)) );
  163. main^.xbuffer[1] := JSAMPIMAGE(@( main^.xbuffer[0]^[cinfo^.num_components] ));
  164. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  165. for ci := 0 to pred(cinfo^.num_components) do
  166. begin
  167. rgroup := (compptr^.v_samp_factor * compptr^.DCT_scaled_size) div
  168. cinfo^.min_DCT_scaled_size; { height of a row group of component }
  169. { Get space for pointer lists --- M+4 row groups in each list.
  170. We alloc both pointer lists with one call to save a few cycles. }
  171. xbuf := JSAMPARRAY (
  172. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  173. 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW)) );
  174. Inc(JSAMPROW_PTR(xbuf), rgroup); { want one row group at negative offsets }
  175. main^.xbuffer[0]^[ci] := xbuf;
  176. Inc(JSAMPROW_PTR(xbuf), rgroup * (M + 4));
  177. main^.xbuffer[1]^[ci] := xbuf;
  178. Inc(compptr);
  179. end;
  180. end;
  181. {LOCAL}
  182. procedure make_funny_pointers (cinfo : j_decompress_ptr);
  183. { Create the funny pointer lists discussed in the comments above.
  184. The actual workspace is already allocated (in main^.buffer),
  185. and the space for the pointer lists is allocated too.
  186. This routine just fills in the curiously ordered lists.
  187. This will be repeated at the beginning of each pass. }
  188. var
  189. main : my_main_ptr;
  190. ci, i, rgroup : int;
  191. M : int;
  192. compptr : jpeg_component_info_ptr;
  193. buf, xbuf0, xbuf1 : JSAMPARRAY;
  194. var
  195. help_xbuf0 : JSAMPARRAY; { work around negative offsets }
  196. begin
  197. main := my_main_ptr (cinfo^.main);
  198. M := cinfo^.min_DCT_scaled_size;
  199. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  200. for ci := 0 to pred(cinfo^.num_components) do
  201. begin
  202. rgroup := (compptr^.v_samp_factor * compptr^.DCT_scaled_size) div
  203. cinfo^.min_DCT_scaled_size; { height of a row group of component }
  204. xbuf0 := main^.xbuffer[0]^[ci];
  205. xbuf1 := main^.xbuffer[1]^[ci];
  206. { First copy the workspace pointers as-is }
  207. buf := main^.buffer[ci];
  208. for i := 0 to pred(rgroup * (M + 2)) do
  209. begin
  210. xbuf0^[i] := buf^[i];
  211. xbuf1^[i] := buf^[i];
  212. end;
  213. { In the second list, put the last four row groups in swapped order }
  214. for i := 0 to pred(rgroup * 2) do
  215. begin
  216. xbuf1^[rgroup*(M-2) + i] := buf^[rgroup*M + i];
  217. xbuf1^[rgroup*M + i] := buf^[rgroup*(M-2) + i];
  218. end;
  219. { The wraparound pointers at top and bottom will be filled later
  220. (see set_wraparound_pointers, below). Initially we want the "above"
  221. pointers to duplicate the first actual data line. This only needs
  222. to happen in xbuffer[0]. }
  223. help_xbuf0 := xbuf0;
  224. Dec(JSAMPROW_PTR(help_xbuf0), rgroup);
  225. for i := 0 to pred(rgroup) do
  226. begin
  227. {xbuf0^[i - rgroup] := xbuf0^[0];}
  228. help_xbuf0^[i] := xbuf0^[0];
  229. end;
  230. Inc(compptr);
  231. end;
  232. end;
  233. {LOCAL}
  234. procedure set_wraparound_pointers (cinfo : j_decompress_ptr);
  235. { Set up the "wraparound" pointers at top and bottom of the pointer lists.
  236. This changes the pointer list state from top-of-image to the normal state. }
  237. var
  238. main : my_main_ptr;
  239. ci, i, rgroup : int;
  240. M : int;
  241. compptr : jpeg_component_info_ptr;
  242. xbuf0, xbuf1 : JSAMPARRAY;
  243. var
  244. help_xbuf0,
  245. help_xbuf1 : JSAMPARRAY; { work around negative offsets }
  246. begin
  247. main := my_main_ptr (cinfo^.main);
  248. M := cinfo^.min_DCT_scaled_size;
  249. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  250. for ci := 0 to pred(cinfo^.num_components) do
  251. begin
  252. rgroup := (compptr^.v_samp_factor * compptr^.DCT_scaled_size) div
  253. cinfo^.min_DCT_scaled_size; { height of a row group of component }
  254. xbuf0 := main^.xbuffer[0]^[ci];
  255. xbuf1 := main^.xbuffer[1]^[ci];
  256. help_xbuf0 := xbuf0;
  257. Dec(JSAMPROW_PTR(help_xbuf0), rgroup);
  258. help_xbuf1 := xbuf1;
  259. Dec(JSAMPROW_PTR(help_xbuf1), rgroup);
  260. for i := 0 to pred(rgroup) do
  261. begin
  262. {xbuf0^[i - rgroup] := xbuf0^[rgroup*(M+1) + i];
  263. xbuf1^[i - rgroup] := xbuf1^[rgroup*(M+1) + i];}
  264. help_xbuf0^[i] := xbuf0^[rgroup*(M+1) + i];
  265. help_xbuf1^[i] := xbuf1^[rgroup*(M+1) + i];
  266. xbuf0^[rgroup*(M+2) + i] := xbuf0^[i];
  267. xbuf1^[rgroup*(M+2) + i] := xbuf1^[i];
  268. end;
  269. Inc(compptr);
  270. end;
  271. end;
  272. {LOCAL}
  273. procedure set_bottom_pointers (cinfo : j_decompress_ptr);
  274. { Change the pointer lists to duplicate the last sample row at the bottom
  275. of the image. whichptr indicates which xbuffer holds the final iMCU row.
  276. Also sets rowgroups_avail to indicate number of nondummy row groups in row. }
  277. var
  278. main : my_main_ptr;
  279. ci, i, rgroup, iMCUheight, rows_left : int;
  280. compptr : jpeg_component_info_ptr;
  281. xbuf : JSAMPARRAY;
  282. begin
  283. main := my_main_ptr (cinfo^.main);
  284. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  285. for ci := 0 to pred(cinfo^.num_components) do
  286. begin
  287. { Count sample rows in one iMCU row and in one row group }
  288. iMCUheight := compptr^.v_samp_factor * compptr^.DCT_scaled_size;
  289. rgroup := iMCUheight div cinfo^.min_DCT_scaled_size;
  290. { Count nondummy sample rows remaining for this component }
  291. rows_left := int (compptr^.downsampled_height mod JDIMENSION (iMCUheight));
  292. if (rows_left = 0) then
  293. rows_left := iMCUheight;
  294. { Count nondummy row groups. Should get same answer for each component,
  295. so we need only do it once. }
  296. if (ci = 0) then
  297. begin
  298. main^.rowgroups_avail := JDIMENSION ((rows_left-1) div rgroup + 1);
  299. end;
  300. { Duplicate the last real sample row rgroup*2 times; this pads out the
  301. last partial rowgroup and ensures at least one full rowgroup of context. }
  302. xbuf := main^.xbuffer[main^.whichptr]^[ci];
  303. for i := 0 to pred(rgroup * 2) do
  304. begin
  305. xbuf^[rows_left + i] := xbuf^[rows_left-1];
  306. end;
  307. Inc(compptr);
  308. end;
  309. end;
  310. { Initialize for a processing pass. }
  311. {METHODDEF}
  312. procedure start_pass_main (cinfo : j_decompress_ptr;
  313. pass_mode : J_BUF_MODE);
  314. var
  315. main : my_main_ptr;
  316. begin
  317. main := my_main_ptr (cinfo^.main);
  318. case (pass_mode) of
  319. JBUF_PASS_THRU:
  320. begin
  321. if (cinfo^.upsample^.need_context_rows) then
  322. begin
  323. main^.pub.process_data := process_data_context_main;
  324. make_funny_pointers(cinfo); { Create the xbuffer[] lists }
  325. main^.whichptr := 0; { Read first iMCU row into xbuffer[0] }
  326. main^.context_state := CTX_PREPARE_FOR_IMCU;
  327. main^.iMCU_row_ctr := 0;
  328. end
  329. else
  330. begin
  331. { Simple case with no context needed }
  332. main^.pub.process_data := process_data_simple_main;
  333. end;
  334. main^.buffer_full := FALSE; { Mark buffer empty }
  335. main^.rowgroup_ctr := 0;
  336. end;
  337. {$ifdef QUANT_2PASS_SUPPORTED}
  338. JBUF_CRANK_DEST:
  339. { For last pass of 2-pass quantization, just crank the postprocessor }
  340. main^.pub.process_data := process_data_crank_post;
  341. {$endif}
  342. else
  343. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  344. end;
  345. end;
  346. { Process some data.
  347. This handles the simple case where no context is required. }
  348. {METHODDEF}
  349. procedure process_data_simple_main (cinfo : j_decompress_ptr;
  350. output_buf : JSAMPARRAY;
  351. var out_row_ctr : JDIMENSION;
  352. out_rows_avail : JDIMENSION);
  353. var
  354. main : my_main_ptr;
  355. rowgroups_avail : JDIMENSION;
  356. var
  357. main_buffer_ptr : JSAMPIMAGE;
  358. begin
  359. main := my_main_ptr (cinfo^.main);
  360. main_buffer_ptr := JSAMPIMAGE(@(main^.buffer));
  361. { Read input data if we haven't filled the main buffer yet }
  362. if (not main^.buffer_full) then
  363. begin
  364. if (cinfo^.coef^.decompress_data (cinfo, main_buffer_ptr)=0) then
  365. exit; { suspension forced, can do nothing more }
  366. main^.buffer_full := TRUE; { OK, we have an iMCU row to work with }
  367. end;
  368. { There are always min_DCT_scaled_size row groups in an iMCU row. }
  369. rowgroups_avail := JDIMENSION (cinfo^.min_DCT_scaled_size);
  370. { Note: at the bottom of the image, we may pass extra garbage row groups
  371. to the postprocessor. The postprocessor has to check for bottom
  372. of image anyway (at row resolution), so no point in us doing it too. }
  373. { Feed the postprocessor }
  374. cinfo^.post^.post_process_data (cinfo, main_buffer_ptr,
  375. main^.rowgroup_ctr, rowgroups_avail,
  376. output_buf, out_row_ctr, out_rows_avail);
  377. { Has postprocessor consumed all the data yet? If so, mark buffer empty }
  378. if (main^.rowgroup_ctr >= rowgroups_avail) then
  379. begin
  380. main^.buffer_full := FALSE;
  381. main^.rowgroup_ctr := 0;
  382. end;
  383. end;
  384. { Process some data.
  385. This handles the case where context rows must be provided. }
  386. {METHODDEF}
  387. procedure process_data_context_main (cinfo : j_decompress_ptr;
  388. output_buf : JSAMPARRAY;
  389. var out_row_ctr : JDIMENSION;
  390. out_rows_avail : JDIMENSION);
  391. var
  392. main : my_main_ptr;
  393. begin
  394. main := my_main_ptr (cinfo^.main);
  395. { Read input data if we haven't filled the main buffer yet }
  396. if (not main^.buffer_full) then
  397. begin
  398. if (cinfo^.coef^.decompress_data (cinfo,
  399. main^.xbuffer[main^.whichptr])=0) then
  400. exit; { suspension forced, can do nothing more }
  401. main^.buffer_full := TRUE; { OK, we have an iMCU row to work with }
  402. Inc(main^.iMCU_row_ctr); { count rows received }
  403. end;
  404. { Postprocessor typically will not swallow all the input data it is handed
  405. in one call (due to filling the output buffer first). Must be prepared
  406. to exit and restart. This switch lets us keep track of how far we got.
  407. Note that each case falls through to the next on successful completion. }
  408. case (main^.context_state) of
  409. CTX_POSTPONED_ROW:
  410. begin
  411. { Call postprocessor using previously set pointers for postponed row }
  412. cinfo^.post^.post_process_data (cinfo, main^.xbuffer[main^.whichptr],
  413. main^.rowgroup_ctr, main^.rowgroups_avail,
  414. output_buf, out_row_ctr, out_rows_avail);
  415. if (main^.rowgroup_ctr < main^.rowgroups_avail) then
  416. exit; { Need to suspend }
  417. main^.context_state := CTX_PREPARE_FOR_IMCU;
  418. if (out_row_ctr >= out_rows_avail) then
  419. exit; { Postprocessor exactly filled output buf }
  420. end;
  421. end;
  422. case (main^.context_state) of
  423. CTX_POSTPONED_ROW,
  424. CTX_PREPARE_FOR_IMCU: {FALLTHROUGH}
  425. begin
  426. { Prepare to process first M-1 row groups of this iMCU row }
  427. main^.rowgroup_ctr := 0;
  428. main^.rowgroups_avail := JDIMENSION (cinfo^.min_DCT_scaled_size - 1);
  429. { Check for bottom of image: if so, tweak pointers to "duplicate"
  430. the last sample row, and adjust rowgroups_avail to ignore padding rows. }
  431. if (main^.iMCU_row_ctr = cinfo^.total_iMCU_rows) then
  432. set_bottom_pointers(cinfo);
  433. main^.context_state := CTX_PROCESS_IMCU;
  434. end;
  435. end;
  436. case (main^.context_state) of
  437. CTX_POSTPONED_ROW,
  438. CTX_PREPARE_FOR_IMCU, {FALLTHROUGH}
  439. CTX_PROCESS_IMCU:
  440. begin
  441. { Call postprocessor using previously set pointers }
  442. cinfo^.post^.post_process_data (cinfo, main^.xbuffer[main^.whichptr],
  443. main^.rowgroup_ctr, main^.rowgroups_avail,
  444. output_buf, out_row_ctr, out_rows_avail);
  445. if (main^.rowgroup_ctr < main^.rowgroups_avail) then
  446. exit; { Need to suspend }
  447. { After the first iMCU, change wraparound pointers to normal state }
  448. if (main^.iMCU_row_ctr = 1) then
  449. set_wraparound_pointers(cinfo);
  450. { Prepare to load new iMCU row using other xbuffer list }
  451. main^.whichptr := main^.whichptr xor 1; { 0=>1 or 1=>0 }
  452. main^.buffer_full := FALSE;
  453. { Still need to process last row group of this iMCU row, }
  454. { which is saved at index M+1 of the other xbuffer }
  455. main^.rowgroup_ctr := JDIMENSION (cinfo^.min_DCT_scaled_size + 1);
  456. main^.rowgroups_avail := JDIMENSION (cinfo^.min_DCT_scaled_size + 2);
  457. main^.context_state := CTX_POSTPONED_ROW;
  458. end;
  459. end;
  460. end;
  461. { Process some data.
  462. Final pass of two-pass quantization: just call the postprocessor.
  463. Source data will be the postprocessor controller's internal buffer. }
  464. {$ifdef QUANT_2PASS_SUPPORTED}
  465. {METHODDEF}
  466. procedure process_data_crank_post (cinfo : j_decompress_ptr;
  467. output_buf : JSAMPARRAY;
  468. var out_row_ctr : JDIMENSION;
  469. out_rows_avail : JDIMENSION);
  470. var
  471. in_row_group_ctr : JDIMENSION;
  472. begin
  473. in_row_group_ctr := 0;
  474. cinfo^.post^.post_process_data (cinfo, JSAMPIMAGE (NIL),
  475. in_row_group_ctr,
  476. JDIMENSION(0),
  477. output_buf,
  478. out_row_ctr,
  479. out_rows_avail);
  480. end;
  481. {$endif} { QUANT_2PASS_SUPPORTED }
  482. { Initialize main buffer controller. }
  483. {GLOBAL}
  484. procedure jinit_d_main_controller (cinfo : j_decompress_ptr;
  485. need_full_buffer : boolean);
  486. var
  487. main : my_main_ptr;
  488. ci, rgroup, ngroups : int;
  489. compptr : jpeg_component_info_ptr;
  490. begin
  491. main := my_main_ptr(
  492. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  493. SIZEOF(my_main_controller)) );
  494. cinfo^.main := jpeg_d_main_controller_ptr(main);
  495. main^.pub.start_pass := start_pass_main;
  496. if (need_full_buffer) then { shouldn't happen }
  497. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  498. { Allocate the workspace.
  499. ngroups is the number of row groups we need.}
  500. if (cinfo^.upsample^.need_context_rows) then
  501. begin
  502. if (cinfo^.min_DCT_scaled_size < 2) then { unsupported, see comments above }
  503. ERREXIT(j_common_ptr(cinfo), JERR_NOTIMPL);
  504. alloc_funny_pointers(cinfo); { Alloc space for xbuffer[] lists }
  505. ngroups := cinfo^.min_DCT_scaled_size + 2;
  506. end
  507. else
  508. begin
  509. ngroups := cinfo^.min_DCT_scaled_size;
  510. end;
  511. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  512. for ci := 0 to pred(cinfo^.num_components) do
  513. begin
  514. rgroup := (compptr^.v_samp_factor * compptr^.DCT_scaled_size) div
  515. cinfo^.min_DCT_scaled_size; { height of a row group of component }
  516. main^.buffer[ci] := cinfo^.mem^.alloc_sarray
  517. (j_common_ptr(cinfo), JPOOL_IMAGE,
  518. compptr^.width_in_blocks * LongWord(compptr^.DCT_scaled_size),
  519. JDIMENSION (rgroup * ngroups));
  520. Inc(compptr);
  521. end;
  522. end;
  523. end.