jdmainct.c 20 KB

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