michael@0: /* michael@0: * jdmainct.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1994-1996, Thomas G. Lane. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright (C) 2010, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains the main buffer controller for decompression. michael@0: * The main buffer lies between the JPEG decompressor proper and the michael@0: * post-processor; it holds downsampled data in the JPEG colorspace. michael@0: * michael@0: * Note that this code is bypassed in raw-data mode, since the application michael@0: * supplies the equivalent of the main buffer in that case. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jpegcomp.h" michael@0: michael@0: michael@0: /* michael@0: * In the current system design, the main buffer need never be a full-image michael@0: * buffer; any full-height buffers will be found inside the coefficient or michael@0: * postprocessing controllers. Nonetheless, the main controller is not michael@0: * trivial. Its responsibility is to provide context rows for upsampling/ michael@0: * rescaling, and doing this in an efficient fashion is a bit tricky. michael@0: * michael@0: * Postprocessor input data is counted in "row groups". A row group michael@0: * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) michael@0: * sample rows of each component. (We require DCT_scaled_size values to be michael@0: * chosen such that these numbers are integers. In practice DCT_scaled_size michael@0: * values will likely be powers of two, so we actually have the stronger michael@0: * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.) michael@0: * Upsampling will typically produce max_v_samp_factor pixel rows from each michael@0: * row group (times any additional scale factor that the upsampler is michael@0: * applying). michael@0: * michael@0: * The coefficient controller will deliver data to us one iMCU row at a time; michael@0: * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or michael@0: * exactly min_DCT_scaled_size row groups. (This amount of data corresponds michael@0: * to one row of MCUs when the image is fully interleaved.) Note that the michael@0: * number of sample rows varies across components, but the number of row michael@0: * groups does not. Some garbage sample rows may be included in the last iMCU michael@0: * row at the bottom of the image. michael@0: * michael@0: * Depending on the vertical scaling algorithm used, the upsampler may need michael@0: * access to the sample row(s) above and below its current input row group. michael@0: * The upsampler is required to set need_context_rows TRUE at global selection michael@0: * time if so. When need_context_rows is FALSE, this controller can simply michael@0: * obtain one iMCU row at a time from the coefficient controller and dole it michael@0: * out as row groups to the postprocessor. michael@0: * michael@0: * When need_context_rows is TRUE, this controller guarantees that the buffer michael@0: * passed to postprocessing contains at least one row group's worth of samples michael@0: * above and below the row group(s) being processed. Note that the context michael@0: * rows "above" the first passed row group appear at negative row offsets in michael@0: * the passed buffer. At the top and bottom of the image, the required michael@0: * context rows are manufactured by duplicating the first or last real sample michael@0: * row; this avoids having special cases in the upsampling inner loops. michael@0: * michael@0: * The amount of context is fixed at one row group just because that's a michael@0: * convenient number for this controller to work with. The existing michael@0: * upsamplers really only need one sample row of context. An upsampler michael@0: * supporting arbitrary output rescaling might wish for more than one row michael@0: * group of context when shrinking the image; tough, we don't handle that. michael@0: * (This is justified by the assumption that downsizing will be handled mostly michael@0: * by adjusting the DCT_scaled_size values, so that the actual scale factor at michael@0: * the upsample step needn't be much less than one.) michael@0: * michael@0: * To provide the desired context, we have to retain the last two row groups michael@0: * of one iMCU row while reading in the next iMCU row. (The last row group michael@0: * can't be processed until we have another row group for its below-context, michael@0: * and so we have to save the next-to-last group too for its above-context.) michael@0: * We could do this most simply by copying data around in our buffer, but michael@0: * that'd be very slow. We can avoid copying any data by creating a rather michael@0: * strange pointer structure. Here's how it works. We allocate a workspace michael@0: * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number michael@0: * of row groups per iMCU row). We create two sets of redundant pointers to michael@0: * the workspace. Labeling the physical row groups 0 to M+1, the synthesized michael@0: * pointer lists look like this: michael@0: * M+1 M-1 michael@0: * master pointer --> 0 master pointer --> 0 michael@0: * 1 1 michael@0: * ... ... michael@0: * M-3 M-3 michael@0: * M-2 M michael@0: * M-1 M+1 michael@0: * M M-2 michael@0: * M+1 M-1 michael@0: * 0 0 michael@0: * We read alternate iMCU rows using each master pointer; thus the last two michael@0: * row groups of the previous iMCU row remain un-overwritten in the workspace. michael@0: * The pointer lists are set up so that the required context rows appear to michael@0: * be adjacent to the proper places when we pass the pointer lists to the michael@0: * upsampler. michael@0: * michael@0: * The above pictures describe the normal state of the pointer lists. michael@0: * At top and bottom of the image, we diddle the pointer lists to duplicate michael@0: * the first or last sample row as necessary (this is cheaper than copying michael@0: * sample rows around). michael@0: * michael@0: * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that michael@0: * situation each iMCU row provides only one row group so the buffering logic michael@0: * must be different (eg, we must read two iMCU rows before we can emit the michael@0: * first row group). For now, we simply do not support providing context michael@0: * rows when min_DCT_scaled_size is 1. That combination seems unlikely to michael@0: * be worth providing --- if someone wants a 1/8th-size preview, they probably michael@0: * want it quick and dirty, so a context-free upsampler is sufficient. michael@0: */ michael@0: michael@0: michael@0: /* Private buffer controller object */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_d_main_controller pub; /* public fields */ michael@0: michael@0: /* Pointer to allocated workspace (M or M+2 row groups). */ michael@0: JSAMPARRAY buffer[MAX_COMPONENTS]; michael@0: michael@0: boolean buffer_full; /* Have we gotten an iMCU row from decoder? */ michael@0: JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */ michael@0: michael@0: /* Remaining fields are only used in the context case. */ michael@0: michael@0: /* These are the master pointers to the funny-order pointer lists. */ michael@0: JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */ michael@0: michael@0: int whichptr; /* indicates which pointer set is now in use */ michael@0: int context_state; /* process_data state machine status */ michael@0: JDIMENSION rowgroups_avail; /* row groups available to postprocessor */ michael@0: JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */ michael@0: } my_main_controller; michael@0: michael@0: typedef my_main_controller * my_main_ptr; michael@0: michael@0: /* context_state values: */ michael@0: #define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */ michael@0: #define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */ michael@0: #define CTX_POSTPONED_ROW 2 /* feeding postponed row group */ michael@0: michael@0: michael@0: /* Forward declarations */ michael@0: METHODDEF(void) process_data_simple_main michael@0: JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, michael@0: JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); michael@0: METHODDEF(void) process_data_context_main michael@0: JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, michael@0: JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); michael@0: #ifdef QUANT_2PASS_SUPPORTED michael@0: METHODDEF(void) process_data_crank_post michael@0: JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, michael@0: JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); michael@0: #endif michael@0: michael@0: michael@0: LOCAL(void) michael@0: alloc_funny_pointers (j_decompress_ptr cinfo) michael@0: /* Allocate space for the funny pointer lists. michael@0: * This is done only once, not once per pass. michael@0: */ michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: int ci, rgroup; michael@0: int M = cinfo->_min_DCT_scaled_size; michael@0: jpeg_component_info *compptr; michael@0: JSAMPARRAY xbuf; michael@0: michael@0: /* Get top-level space for component array pointers. michael@0: * We alloc both arrays with one call to save a few cycles. michael@0: */ michael@0: main_ptr->xbuffer[0] = (JSAMPIMAGE) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: cinfo->num_components * 2 * SIZEOF(JSAMPARRAY)); michael@0: main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; /* height of a row group of component */ michael@0: /* Get space for pointer lists --- M+4 row groups in each list. michael@0: * We alloc both pointer lists with one call to save a few cycles. michael@0: */ michael@0: xbuf = (JSAMPARRAY) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW)); michael@0: xbuf += rgroup; /* want one row group at negative offsets */ michael@0: main_ptr->xbuffer[0][ci] = xbuf; michael@0: xbuf += rgroup * (M + 4); michael@0: main_ptr->xbuffer[1][ci] = xbuf; michael@0: } michael@0: } michael@0: michael@0: michael@0: LOCAL(void) michael@0: make_funny_pointers (j_decompress_ptr cinfo) michael@0: /* Create the funny pointer lists discussed in the comments above. michael@0: * The actual workspace is already allocated (in main_ptr->buffer), michael@0: * and the space for the pointer lists is allocated too. michael@0: * This routine just fills in the curiously ordered lists. michael@0: * This will be repeated at the beginning of each pass. michael@0: */ michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: int ci, i, rgroup; michael@0: int M = cinfo->_min_DCT_scaled_size; michael@0: jpeg_component_info *compptr; michael@0: JSAMPARRAY buf, xbuf0, xbuf1; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; /* height of a row group of component */ michael@0: xbuf0 = main_ptr->xbuffer[0][ci]; michael@0: xbuf1 = main_ptr->xbuffer[1][ci]; michael@0: /* First copy the workspace pointers as-is */ michael@0: buf = main_ptr->buffer[ci]; michael@0: for (i = 0; i < rgroup * (M + 2); i++) { michael@0: xbuf0[i] = xbuf1[i] = buf[i]; michael@0: } michael@0: /* In the second list, put the last four row groups in swapped order */ michael@0: for (i = 0; i < rgroup * 2; i++) { michael@0: xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i]; michael@0: xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i]; michael@0: } michael@0: /* The wraparound pointers at top and bottom will be filled later michael@0: * (see set_wraparound_pointers, below). Initially we want the "above" michael@0: * pointers to duplicate the first actual data line. This only needs michael@0: * to happen in xbuffer[0]. michael@0: */ michael@0: for (i = 0; i < rgroup; i++) { michael@0: xbuf0[i - rgroup] = xbuf0[0]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: LOCAL(void) michael@0: set_wraparound_pointers (j_decompress_ptr cinfo) michael@0: /* Set up the "wraparound" pointers at top and bottom of the pointer lists. michael@0: * This changes the pointer list state from top-of-image to the normal state. michael@0: */ michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: int ci, i, rgroup; michael@0: int M = cinfo->_min_DCT_scaled_size; michael@0: jpeg_component_info *compptr; michael@0: JSAMPARRAY xbuf0, xbuf1; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; /* height of a row group of component */ michael@0: xbuf0 = main_ptr->xbuffer[0][ci]; michael@0: xbuf1 = main_ptr->xbuffer[1][ci]; michael@0: for (i = 0; i < rgroup; i++) { michael@0: xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i]; michael@0: xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i]; michael@0: xbuf0[rgroup*(M+2) + i] = xbuf0[i]; michael@0: xbuf1[rgroup*(M+2) + i] = xbuf1[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: LOCAL(void) michael@0: set_bottom_pointers (j_decompress_ptr cinfo) michael@0: /* Change the pointer lists to duplicate the last sample row at the bottom michael@0: * of the image. whichptr indicates which xbuffer holds the final iMCU row. michael@0: * Also sets rowgroups_avail to indicate number of nondummy row groups in row. michael@0: */ michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: int ci, i, rgroup, iMCUheight, rows_left; michael@0: jpeg_component_info *compptr; michael@0: JSAMPARRAY xbuf; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Count sample rows in one iMCU row and in one row group */ michael@0: iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size; michael@0: rgroup = iMCUheight / cinfo->_min_DCT_scaled_size; michael@0: /* Count nondummy sample rows remaining for this component */ michael@0: rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight); michael@0: if (rows_left == 0) rows_left = iMCUheight; michael@0: /* Count nondummy row groups. Should get same answer for each component, michael@0: * so we need only do it once. michael@0: */ michael@0: if (ci == 0) { michael@0: main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1); michael@0: } michael@0: /* Duplicate the last real sample row rgroup*2 times; this pads out the michael@0: * last partial rowgroup and ensures at least one full rowgroup of context. michael@0: */ michael@0: xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci]; michael@0: for (i = 0; i < rgroup * 2; i++) { michael@0: xbuf[rows_left + i] = xbuf[rows_left-1]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for a processing pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: michael@0: switch (pass_mode) { michael@0: case JBUF_PASS_THRU: michael@0: if (cinfo->upsample->need_context_rows) { michael@0: main_ptr->pub.process_data = process_data_context_main; michael@0: make_funny_pointers(cinfo); /* Create the xbuffer[] lists */ michael@0: main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */ michael@0: main_ptr->context_state = CTX_PREPARE_FOR_IMCU; michael@0: main_ptr->iMCU_row_ctr = 0; michael@0: } else { michael@0: /* Simple case with no context needed */ michael@0: main_ptr->pub.process_data = process_data_simple_main; michael@0: } michael@0: main_ptr->buffer_full = FALSE; /* Mark buffer empty */ michael@0: main_ptr->rowgroup_ctr = 0; michael@0: break; michael@0: #ifdef QUANT_2PASS_SUPPORTED michael@0: case JBUF_CRANK_DEST: michael@0: /* For last pass of 2-pass quantization, just crank the postprocessor */ michael@0: main_ptr->pub.process_data = process_data_crank_post; michael@0: break; michael@0: #endif michael@0: default: michael@0: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Process some data. michael@0: * This handles the simple case where no context is required. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: process_data_simple_main (j_decompress_ptr cinfo, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: JDIMENSION rowgroups_avail; michael@0: michael@0: /* Read input data if we haven't filled the main buffer yet */ michael@0: if (! main_ptr->buffer_full) { michael@0: if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer)) michael@0: return; /* suspension forced, can do nothing more */ michael@0: main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ michael@0: } michael@0: michael@0: /* There are always min_DCT_scaled_size row groups in an iMCU row. */ michael@0: rowgroups_avail = (JDIMENSION) cinfo->_min_DCT_scaled_size; michael@0: /* Note: at the bottom of the image, we may pass extra garbage row groups michael@0: * to the postprocessor. The postprocessor has to check for bottom michael@0: * of image anyway (at row resolution), so no point in us doing it too. michael@0: */ michael@0: michael@0: /* Feed the postprocessor */ michael@0: (*cinfo->post->post_process_data) (cinfo, main_ptr->buffer, michael@0: &main_ptr->rowgroup_ctr, rowgroups_avail, michael@0: output_buf, out_row_ctr, out_rows_avail); michael@0: michael@0: /* Has postprocessor consumed all the data yet? If so, mark buffer empty */ michael@0: if (main_ptr->rowgroup_ctr >= rowgroups_avail) { michael@0: main_ptr->buffer_full = FALSE; michael@0: main_ptr->rowgroup_ctr = 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Process some data. michael@0: * This handles the case where context rows must be provided. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: process_data_context_main (j_decompress_ptr cinfo, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: { michael@0: my_main_ptr main_ptr = (my_main_ptr) cinfo->main; michael@0: michael@0: /* Read input data if we haven't filled the main buffer yet */ michael@0: if (! main_ptr->buffer_full) { michael@0: if (! (*cinfo->coef->decompress_data) (cinfo, michael@0: main_ptr->xbuffer[main_ptr->whichptr])) michael@0: return; /* suspension forced, can do nothing more */ michael@0: main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ michael@0: main_ptr->iMCU_row_ctr++; /* count rows received */ michael@0: } michael@0: michael@0: /* Postprocessor typically will not swallow all the input data it is handed michael@0: * in one call (due to filling the output buffer first). Must be prepared michael@0: * to exit and restart. This switch lets us keep track of how far we got. michael@0: * Note that each case falls through to the next on successful completion. michael@0: */ michael@0: switch (main_ptr->context_state) { michael@0: case CTX_POSTPONED_ROW: michael@0: /* Call postprocessor using previously set pointers for postponed row */ michael@0: (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], michael@0: &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, michael@0: output_buf, out_row_ctr, out_rows_avail); michael@0: if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) michael@0: return; /* Need to suspend */ michael@0: main_ptr->context_state = CTX_PREPARE_FOR_IMCU; michael@0: if (*out_row_ctr >= out_rows_avail) michael@0: return; /* Postprocessor exactly filled output buf */ michael@0: /*FALLTHROUGH*/ michael@0: case CTX_PREPARE_FOR_IMCU: michael@0: /* Prepare to process first M-1 row groups of this iMCU row */ michael@0: main_ptr->rowgroup_ctr = 0; michael@0: main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size - 1); michael@0: /* Check for bottom of image: if so, tweak pointers to "duplicate" michael@0: * the last sample row, and adjust rowgroups_avail to ignore padding rows. michael@0: */ michael@0: if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows) michael@0: set_bottom_pointers(cinfo); michael@0: main_ptr->context_state = CTX_PROCESS_IMCU; michael@0: /*FALLTHROUGH*/ michael@0: case CTX_PROCESS_IMCU: michael@0: /* Call postprocessor using previously set pointers */ michael@0: (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], michael@0: &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, michael@0: output_buf, out_row_ctr, out_rows_avail); michael@0: if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) michael@0: return; /* Need to suspend */ michael@0: /* After the first iMCU, change wraparound pointers to normal state */ michael@0: if (main_ptr->iMCU_row_ctr == 1) michael@0: set_wraparound_pointers(cinfo); michael@0: /* Prepare to load new iMCU row using other xbuffer list */ michael@0: main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */ michael@0: main_ptr->buffer_full = FALSE; michael@0: /* Still need to process last row group of this iMCU row, */ michael@0: /* which is saved at index M+1 of the other xbuffer */ michael@0: main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 1); michael@0: main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 2); michael@0: main_ptr->context_state = CTX_POSTPONED_ROW; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Process some data. michael@0: * Final pass of two-pass quantization: just call the postprocessor. michael@0: * Source data will be the postprocessor controller's internal buffer. michael@0: */ michael@0: michael@0: #ifdef QUANT_2PASS_SUPPORTED michael@0: michael@0: METHODDEF(void) michael@0: process_data_crank_post (j_decompress_ptr cinfo, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: { michael@0: (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL, michael@0: (JDIMENSION *) NULL, (JDIMENSION) 0, michael@0: output_buf, out_row_ctr, out_rows_avail); michael@0: } michael@0: michael@0: #endif /* QUANT_2PASS_SUPPORTED */ michael@0: michael@0: michael@0: /* michael@0: * Initialize main buffer controller. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer) michael@0: { michael@0: my_main_ptr main_ptr; michael@0: int ci, rgroup, ngroups; michael@0: jpeg_component_info *compptr; michael@0: michael@0: main_ptr = (my_main_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_main_controller)); michael@0: cinfo->main = (struct jpeg_d_main_controller *) main_ptr; michael@0: main_ptr->pub.start_pass = start_pass_main; michael@0: michael@0: if (need_full_buffer) /* shouldn't happen */ michael@0: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); michael@0: michael@0: /* Allocate the workspace. michael@0: * ngroups is the number of row groups we need. michael@0: */ michael@0: if (cinfo->upsample->need_context_rows) { michael@0: if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */ michael@0: ERREXIT(cinfo, JERR_NOTIMPL); michael@0: alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */ michael@0: ngroups = cinfo->_min_DCT_scaled_size + 2; michael@0: } else { michael@0: ngroups = cinfo->_min_DCT_scaled_size; michael@0: } michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; /* height of a row group of component */ michael@0: main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: compptr->width_in_blocks * compptr->_DCT_scaled_size, michael@0: (JDIMENSION) (rgroup * ngroups)); michael@0: } michael@0: }