jdmainct.c 20 KB

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