jdmainct.c 20.4 KB
Newer Older
wester committed
1 2 3 4 5 6 7
/*
 * jdmainct.c
 *
 * Copyright (C) 1994-1996, Thomas G. Lane.
 * This file is part of the Independent JPEG Group's software.
 * For conditions of distribution and use, see the accompanying README file.
 *
wester committed
8 9
 * This file contains the main_ptr buffer controller for decompression.
 * The main_ptr buffer lies between the JPEG decompressor proper and the
wester committed
10 11 12
 * post-processor; it holds downsampled data in the JPEG colorspace.
 *
 * Note that this code is bypassed in raw-data mode, since the application
wester committed
13
 * supplies the equivalent of the main_ptr buffer in that case.
wester committed
14 15 16 17 18 19 20 21
 */

#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"


/*
wester committed
22
 * In the current system design, the main_ptr buffer need never be a full-image
wester committed
23
 * buffer; any full-height buffers will be found inside the coefficient or
wester committed
24
 * postprocessing controllers.  Nonetheless, the main_ptr controller is not
wester committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 * trivial.  Its responsibility is to provide context rows for upsampling/
 * rescaling, and doing this in an efficient fashion is a bit tricky.
 *
 * Postprocessor input data is counted in "row groups".  A row group
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
 * sample rows of each component.  (We require DCT_scaled_size values to be
 * chosen such that these numbers are integers.  In practice DCT_scaled_size
 * values will likely be powers of two, so we actually have the stronger
 * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
 * Upsampling will typically produce max_v_samp_factor pixel rows from each
 * row group (times any additional scale factor that the upsampler is
 * applying).
 *
 * The coefficient controller will deliver data to us one iMCU row at a time;
 * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
 * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
 * to one row of MCUs when the image is fully interleaved.)  Note that the
 * number of sample rows varies across components, but the number of row
 * groups does not.  Some garbage sample rows may be included in the last iMCU
 * row at the bottom of the image.
 *
 * Depending on the vertical scaling algorithm used, the upsampler may need
 * access to the sample row(s) above and below its current input row group.
 * The upsampler is required to set need_context_rows TRUE at global selection
 * time if so.  When need_context_rows is FALSE, this controller can simply
 * obtain one iMCU row at a time from the coefficient controller and dole it
 * out as row groups to the postprocessor.
 *
 * When need_context_rows is TRUE, this controller guarantees that the buffer
 * passed to postprocessing contains at least one row group's worth of samples
 * above and below the row group(s) being processed.  Note that the context
 * rows "above" the first passed row group appear at negative row offsets in
 * the passed buffer.  At the top and bottom of the image, the required
 * context rows are manufactured by duplicating the first or last real sample
 * row; this avoids having special cases in the upsampling inner loops.
 *
 * The amount of context is fixed at one row group just because that's a
 * convenient number for this controller to work with.  The existing
 * upsamplers really only need one sample row of context.  An upsampler
 * supporting arbitrary output rescaling might wish for more than one row
 * group of context when shrinking the image; tough, we don't handle that.
 * (This is justified by the assumption that downsizing will be handled mostly
 * by adjusting the DCT_scaled_size values, so that the actual scale factor at
 * the upsample step needn't be much less than one.)
 *
 * To provide the desired context, we have to retain the last two row groups
 * of one iMCU row while reading in the next iMCU row.  (The last row group
 * can't be processed until we have another row group for its below-context,
 * and so we have to save the next-to-last group too for its above-context.)
 * We could do this most simply by copying data around in our buffer, but
 * that'd be very slow.  We can avoid copying any data by creating a rather
 * strange pointer structure.  Here's how it works.  We allocate a workspace
 * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
 * of row groups per iMCU row).  We create two sets of redundant pointers to
 * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
 * pointer lists look like this:
 *                   M+1                          M-1
 * master pointer --> 0         master pointer --> 0
 *                    1                            1
 *                   ...                          ...
 *                   M-3                          M-3
 *                   M-2                           M
 *                   M-1                          M+1
 *                    M                           M-2
 *                   M+1                          M-1
 *                    0                            0
 * We read alternate iMCU rows using each master pointer; thus the last two
 * row groups of the previous iMCU row remain un-overwritten in the workspace.
 * The pointer lists are set up so that the required context rows appear to
 * be adjacent to the proper places when we pass the pointer lists to the
 * upsampler.
 *
 * The above pictures describe the normal state of the pointer lists.
 * At top and bottom of the image, we diddle the pointer lists to duplicate
 * the first or last sample row as necessary (this is cheaper than copying
 * sample rows around).
 *
 * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
 * situation each iMCU row provides only one row group so the buffering logic
 * must be different (eg, we must read two iMCU rows before we can emit the
 * first row group).  For now, we simply do not support providing context
 * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
 * be worth providing --- if someone wants a 1/8th-size preview, they probably
 * want it quick and dirty, so a context-free upsampler is sufficient.
 */


/* Private buffer controller object */

typedef struct {
  struct jpeg_d_main_controller pub; /* public fields */

  /* Pointer to allocated workspace (M or M+2 row groups). */
  JSAMPARRAY buffer[MAX_COMPONENTS];

  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */

  /* Remaining fields are only used in the context case. */

  /* These are the master pointers to the funny-order pointer lists. */
  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */

  int whichptr;			/* indicates which pointer set is now in use */
  int context_state;		/* process_data state machine status */
  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
} my_main_controller;

typedef my_main_controller * my_main_ptr;

/* context_state values: */
#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */


/* Forward declarations */
METHODDEF(void) process_data_simple_main
wester committed
144 145
    JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
         JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
wester committed
146
METHODDEF(void) process_data_context_main
wester committed
147 148
    JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
         JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
wester committed
149 150
#ifdef QUANT_2PASS_SUPPORTED
METHODDEF(void) process_data_crank_post
wester committed
151 152
    JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
         JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
wester committed
153 154 155 156 157 158 159 160 161
#endif


LOCAL(void)
alloc_funny_pointers (j_decompress_ptr cinfo)
/* Allocate space for the funny pointer lists.
 * This is done only once, not once per pass.
 */
{
wester committed
162
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
163
  int ci, rgroup;
wester committed
164
  int M = cinfo->min_DCT_scaled_size;
wester committed
165 166 167 168 169 170
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf;

  /* Get top-level space for component array pointers.
   * We alloc both arrays with one call to save a few cycles.
   */
wester committed
171
  main_ptr->xbuffer[0] = (JSAMPIMAGE)
wester committed
172
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
wester committed
173 174
                cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
  main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components;
wester committed
175 176 177

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
wester committed
178 179
    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
      cinfo->min_DCT_scaled_size; /* height of a row group of component */
wester committed
180 181 182 183 184
    /* Get space for pointer lists --- M+4 row groups in each list.
     * We alloc both pointer lists with one call to save a few cycles.
     */
    xbuf = (JSAMPARRAY)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
wester committed
185
                  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
wester committed
186
    xbuf += rgroup;		/* want one row group at negative offsets */
wester committed
187
    main_ptr->xbuffer[0][ci] = xbuf;
wester committed
188
    xbuf += rgroup * (M + 4);
wester committed
189
    main_ptr->xbuffer[1][ci] = xbuf;
wester committed
190 191 192 193 194 195 196
  }
}


LOCAL(void)
make_funny_pointers (j_decompress_ptr cinfo)
/* Create the funny pointer lists discussed in the comments above.
wester committed
197
 * The actual workspace is already allocated (in main_ptr->buffer),
wester committed
198 199 200 201 202
 * and the space for the pointer lists is allocated too.
 * This routine just fills in the curiously ordered lists.
 * This will be repeated at the beginning of each pass.
 */
{
wester committed
203
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
204
  int ci, i, rgroup;
wester committed
205
  int M = cinfo->min_DCT_scaled_size;
wester committed
206 207 208 209 210
  jpeg_component_info *compptr;
  JSAMPARRAY buf, xbuf0, xbuf1;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
wester committed
211 212 213 214
    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
      cinfo->min_DCT_scaled_size; /* height of a row group of component */
    xbuf0 = main_ptr->xbuffer[0][ci];
    xbuf1 = main_ptr->xbuffer[1][ci];
wester committed
215
    /* First copy the workspace pointers as-is */
wester committed
216
    buf = main_ptr->buffer[ci];
wester committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    for (i = 0; i < rgroup * (M + 2); i++) {
      xbuf0[i] = xbuf1[i] = buf[i];
    }
    /* In the second list, put the last four row groups in swapped order */
    for (i = 0; i < rgroup * 2; i++) {
      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
    }
    /* The wraparound pointers at top and bottom will be filled later
     * (see set_wraparound_pointers, below).  Initially we want the "above"
     * pointers to duplicate the first actual data line.  This only needs
     * to happen in xbuffer[0].
     */
    for (i = 0; i < rgroup; i++) {
      xbuf0[i - rgroup] = xbuf0[0];
    }
  }
}


LOCAL(void)
set_wraparound_pointers (j_decompress_ptr cinfo)
/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
 * This changes the pointer list state from top-of-image to the normal state.
 */
{
wester committed
243
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
244
  int ci, i, rgroup;
wester committed
245
  int M = cinfo->min_DCT_scaled_size;
wester committed
246 247 248 249 250
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf0, xbuf1;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
wester committed
251 252 253 254
    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
      cinfo->min_DCT_scaled_size; /* height of a row group of component */
    xbuf0 = main_ptr->xbuffer[0][ci];
    xbuf1 = main_ptr->xbuffer[1][ci];
wester committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    for (i = 0; i < rgroup; i++) {
      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
    }
  }
}


LOCAL(void)
set_bottom_pointers (j_decompress_ptr cinfo)
/* Change the pointer lists to duplicate the last sample row at the bottom
 * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
 * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
 */
{
wester committed
272
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
273 274 275 276 277 278 279
  int ci, i, rgroup, iMCUheight, rows_left;
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Count sample rows in one iMCU row and in one row group */
wester committed
280 281
    iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
    rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
wester committed
282 283 284 285 286 287 288
    /* Count nondummy sample rows remaining for this component */
    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
    if (rows_left == 0) rows_left = iMCUheight;
    /* Count nondummy row groups.  Should get same answer for each component,
     * so we need only do it once.
     */
    if (ci == 0) {
wester committed
289
      main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
wester committed
290 291 292 293
    }
    /* Duplicate the last real sample row rgroup*2 times; this pads out the
     * last partial rowgroup and ensures at least one full rowgroup of context.
     */
wester committed
294
    xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci];
wester committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
    for (i = 0; i < rgroup * 2; i++) {
      xbuf[rows_left + i] = xbuf[rows_left-1];
    }
  }
}


/*
 * Initialize for a processing pass.
 */

METHODDEF(void)
start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
{
wester committed
309
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
310 311 312 313

  switch (pass_mode) {
  case JBUF_PASS_THRU:
    if (cinfo->upsample->need_context_rows) {
wester committed
314
      main_ptr->pub.process_data = process_data_context_main;
wester committed
315
      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
wester committed
316 317 318
      main_ptr->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
      main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
      main_ptr->iMCU_row_ctr = 0;
wester committed
319 320
    } else {
      /* Simple case with no context needed */
wester committed
321
      main_ptr->pub.process_data = process_data_simple_main;
wester committed
322
    }
wester committed
323 324
    main_ptr->buffer_full = FALSE;	/* Mark buffer empty */
    main_ptr->rowgroup_ctr = 0;
wester committed
325 326 327 328
    break;
#ifdef QUANT_2PASS_SUPPORTED
  case JBUF_CRANK_DEST:
    /* For last pass of 2-pass quantization, just crank the postprocessor */
wester committed
329
    main_ptr->pub.process_data = process_data_crank_post;
wester committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    break;
#endif
  default:
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
    break;
  }
}


/*
 * Process some data.
 * This handles the simple case where no context is required.
 */

METHODDEF(void)
process_data_simple_main (j_decompress_ptr cinfo,
wester committed
346 347
              JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
              JDIMENSION out_rows_avail)
wester committed
348
{
wester committed
349
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
350 351
  JDIMENSION rowgroups_avail;

wester committed
352 353 354
  /* Read input data if we haven't filled the main_ptr buffer yet */
  if (! main_ptr->buffer_full) {
    if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer))
wester committed
355
      return;			/* suspension forced, can do nothing more */
wester committed
356
    main_ptr->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
wester committed
357 358 359
  }

  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
wester committed
360
  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
wester committed
361 362 363 364 365 366
  /* Note: at the bottom of the image, we may pass extra garbage row groups
   * to the postprocessor.  The postprocessor has to check for bottom
   * of image anyway (at row resolution), so no point in us doing it too.
   */

  /* Feed the postprocessor */
wester committed
367 368 369
  (*cinfo->post->post_process_data) (cinfo, main_ptr->buffer,
                     &main_ptr->rowgroup_ctr, rowgroups_avail,
                     output_buf, out_row_ctr, out_rows_avail);
wester committed
370 371

  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
wester committed
372 373 374
  if (main_ptr->rowgroup_ctr >= rowgroups_avail) {
    main_ptr->buffer_full = FALSE;
    main_ptr->rowgroup_ctr = 0;
wester committed
375 376 377 378 379 380 381 382 383 384 385
  }
}


/*
 * Process some data.
 * This handles the case where context rows must be provided.
 */

METHODDEF(void)
process_data_context_main (j_decompress_ptr cinfo,
wester committed
386 387
               JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
               JDIMENSION out_rows_avail)
wester committed
388
{
wester committed
389
  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
wester committed
390

wester committed
391 392
  /* Read input data if we haven't filled the main_ptr buffer yet */
  if (! main_ptr->buffer_full) {
wester committed
393
    if (! (*cinfo->coef->decompress_data) (cinfo,
wester committed
394
                       main_ptr->xbuffer[main_ptr->whichptr]))
wester committed
395
      return;			/* suspension forced, can do nothing more */
wester committed
396 397
    main_ptr->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
    main_ptr->iMCU_row_ctr++;	/* count rows received */
wester committed
398 399 400 401 402 403 404
  }

  /* Postprocessor typically will not swallow all the input data it is handed
   * in one call (due to filling the output buffer first).  Must be prepared
   * to exit and restart.  This switch lets us keep track of how far we got.
   * Note that each case falls through to the next on successful completion.
   */
wester committed
405
  switch (main_ptr->context_state) {
wester committed
406 407
  case CTX_POSTPONED_ROW:
    /* Call postprocessor using previously set pointers for postponed row */
wester committed
408 409 410 411
    (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],
            &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,
            output_buf, out_row_ctr, out_rows_avail);
    if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)
wester committed
412
      return;			/* Need to suspend */
wester committed
413
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
wester committed
414 415 416 417 418
    if (*out_row_ctr >= out_rows_avail)
      return;			/* Postprocessor exactly filled output buf */
    /*FALLTHROUGH*/
  case CTX_PREPARE_FOR_IMCU:
    /* Prepare to process first M-1 row groups of this iMCU row */
wester committed
419 420
    main_ptr->rowgroup_ctr = 0;
    main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
wester committed
421 422 423
    /* Check for bottom of image: if so, tweak pointers to "duplicate"
     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
     */
wester committed
424
    if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows)
wester committed
425
      set_bottom_pointers(cinfo);
wester committed
426
    main_ptr->context_state = CTX_PROCESS_IMCU;
wester committed
427 428 429
    /*FALLTHROUGH*/
  case CTX_PROCESS_IMCU:
    /* Call postprocessor using previously set pointers */
wester committed
430 431 432 433
    (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],
            &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,
            output_buf, out_row_ctr, out_rows_avail);
    if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)
wester committed
434 435
      return;			/* Need to suspend */
    /* After the first iMCU, change wraparound pointers to normal state */
wester committed
436
    if (main_ptr->iMCU_row_ctr == 1)
wester committed
437 438
      set_wraparound_pointers(cinfo);
    /* Prepare to load new iMCU row using other xbuffer list */
wester committed
439 440
    main_ptr->whichptr ^= 1;	/* 0=>1 or 1=>0 */
    main_ptr->buffer_full = FALSE;
wester committed
441 442
    /* Still need to process last row group of this iMCU row, */
    /* which is saved at index M+1 of the other xbuffer */
wester committed
443 444 445
    main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
    main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
    main_ptr->context_state = CTX_POSTPONED_ROW;
wester committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459
  }
}


/*
 * Process some data.
 * Final pass of two-pass quantization: just call the postprocessor.
 * Source data will be the postprocessor controller's internal buffer.
 */

#ifdef QUANT_2PASS_SUPPORTED

METHODDEF(void)
process_data_crank_post (j_decompress_ptr cinfo,
wester committed
460 461
             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
             JDIMENSION out_rows_avail)
wester committed
462 463
{
  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
wester committed
464 465
                     (JDIMENSION *) NULL, (JDIMENSION) 0,
                     output_buf, out_row_ctr, out_rows_avail);
wester committed
466 467 468 469 470 471
}

#endif /* QUANT_2PASS_SUPPORTED */


/*
wester committed
472
 * Initialize main_ptr buffer controller.
wester committed
473 474 475 476 477
 */

GLOBAL(void)
jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
{
wester committed
478
  my_main_ptr main_ptr;
wester committed
479 480 481
  int ci, rgroup, ngroups;
  jpeg_component_info *compptr;

wester committed
482
  main_ptr = (my_main_ptr)
wester committed
483
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
wester committed
484 485 486
                SIZEOF(my_main_controller));
  cinfo->main = (struct jpeg_d_main_controller *) main_ptr;
  main_ptr->pub.start_pass = start_pass_main;
wester committed
487 488 489 490 491 492 493 494

  if (need_full_buffer)		/* shouldn't happen */
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);

  /* Allocate the workspace.
   * ngroups is the number of row groups we need.
   */
  if (cinfo->upsample->need_context_rows) {
wester committed
495
    if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
wester committed
496 497
      ERREXIT(cinfo, JERR_NOTIMPL);
    alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
wester committed
498
    ngroups = cinfo->min_DCT_scaled_size + 2;
wester committed
499
  } else {
wester committed
500
    ngroups = cinfo->min_DCT_scaled_size;
wester committed
501 502 503 504
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
wester committed
505 506 507 508 509 510
    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
      cinfo->min_DCT_scaled_size; /* height of a row group of component */
    main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
            ((j_common_ptr) cinfo, JPOOL_IMAGE,
             compptr->width_in_blocks * compptr->DCT_scaled_size,
             (JDIMENSION) (rgroup * ngroups));
wester committed
511 512
  }
}