Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdmainct.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdmainct.c6*7* Copyright (C) 1994-1996, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains the main buffer controller for decompression.12* The main buffer lies between the JPEG decompressor proper and the13* post-processor; it holds downsampled data in the JPEG colorspace.14*15* Note that this code is bypassed in raw-data mode, since the application16* supplies the equivalent of the main buffer in that case.17*/1819#define JPEG_INTERNALS20#include "jinclude.h"21#include "jpeglib.h"222324/*25* In the current system design, the main buffer need never be a full-image26* buffer; any full-height buffers will be found inside the coefficient or27* postprocessing controllers. Nonetheless, the main controller is not28* trivial. Its responsibility is to provide context rows for upsampling/29* rescaling, and doing this in an efficient fashion is a bit tricky.30*31* Postprocessor input data is counted in "row groups". A row group32* is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)33* sample rows of each component. (We require DCT_scaled_size values to be34* chosen such that these numbers are integers. In practice DCT_scaled_size35* values will likely be powers of two, so we actually have the stronger36* condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)37* Upsampling will typically produce max_v_samp_factor pixel rows from each38* row group (times any additional scale factor that the upsampler is39* applying).40*41* The coefficient controller will deliver data to us one iMCU row at a time;42* each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or43* exactly min_DCT_scaled_size row groups. (This amount of data corresponds44* to one row of MCUs when the image is fully interleaved.) Note that the45* number of sample rows varies across components, but the number of row46* groups does not. Some garbage sample rows may be included in the last iMCU47* row at the bottom of the image.48*49* Depending on the vertical scaling algorithm used, the upsampler may need50* access to the sample row(s) above and below its current input row group.51* The upsampler is required to set need_context_rows TRUE at global selection52* time if so. When need_context_rows is FALSE, this controller can simply53* obtain one iMCU row at a time from the coefficient controller and dole it54* out as row groups to the postprocessor.55*56* When need_context_rows is TRUE, this controller guarantees that the buffer57* passed to postprocessing contains at least one row group's worth of samples58* above and below the row group(s) being processed. Note that the context59* rows "above" the first passed row group appear at negative row offsets in60* the passed buffer. At the top and bottom of the image, the required61* context rows are manufactured by duplicating the first or last real sample62* row; this avoids having special cases in the upsampling inner loops.63*64* The amount of context is fixed at one row group just because that's a65* convenient number for this controller to work with. The existing66* upsamplers really only need one sample row of context. An upsampler67* supporting arbitrary output rescaling might wish for more than one row68* group of context when shrinking the image; tough, we don't handle that.69* (This is justified by the assumption that downsizing will be handled mostly70* by adjusting the DCT_scaled_size values, so that the actual scale factor at71* the upsample step needn't be much less than one.)72*73* To provide the desired context, we have to retain the last two row groups74* of one iMCU row while reading in the next iMCU row. (The last row group75* can't be processed until we have another row group for its below-context,76* and so we have to save the next-to-last group too for its above-context.)77* We could do this most simply by copying data around in our buffer, but78* that'd be very slow. We can avoid copying any data by creating a rather79* strange pointer structure. Here's how it works. We allocate a workspace80* consisting of M+2 row groups (where M = min_DCT_scaled_size is the number81* of row groups per iMCU row). We create two sets of redundant pointers to82* the workspace. Labeling the physical row groups 0 to M+1, the synthesized83* pointer lists look like this:84* M+1 M-185* master pointer --> 0 master pointer --> 086* 1 187* ... ...88* M-3 M-389* M-2 M90* M-1 M+191* M M-292* M+1 M-193* 0 094* We read alternate iMCU rows using each master pointer; thus the last two95* row groups of the previous iMCU row remain un-overwritten in the workspace.96* The pointer lists are set up so that the required context rows appear to97* be adjacent to the proper places when we pass the pointer lists to the98* upsampler.99*100* The above pictures describe the normal state of the pointer lists.101* At top and bottom of the image, we diddle the pointer lists to duplicate102* the first or last sample row as necessary (this is cheaper than copying103* sample rows around).104*105* This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that106* situation each iMCU row provides only one row group so the buffering logic107* must be different (eg, we must read two iMCU rows before we can emit the108* first row group). For now, we simply do not support providing context109* rows when min_DCT_scaled_size is 1. That combination seems unlikely to110* be worth providing --- if someone wants a 1/8th-size preview, they probably111* want it quick and dirty, so a context-free upsampler is sufficient.112*/113114115/* Private buffer controller object */116117typedef struct {118struct jpeg_d_main_controller pub; /* public fields */119120/* Pointer to allocated workspace (M or M+2 row groups). */121JSAMPARRAY buffer[MAX_COMPONENTS];122123boolean buffer_full; /* Have we gotten an iMCU row from decoder? */124JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */125126/* Remaining fields are only used in the context case. */127128/* These are the master pointers to the funny-order pointer lists. */129JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */130131int whichptr; /* indicates which pointer set is now in use */132int context_state; /* process_data state machine status */133JDIMENSION rowgroups_avail; /* row groups available to postprocessor */134JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */135} my_main_controller;136137typedef my_main_controller * my_main_ptr;138139/* context_state values: */140#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */141#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */142#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */143144145/* Forward declarations */146METHODDEF(void) process_data_simple_main147JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,148JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));149METHODDEF(void) process_data_context_main150JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,151JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));152#ifdef QUANT_2PASS_SUPPORTED153METHODDEF(void) process_data_crank_post154JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,155JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));156#endif157158159LOCAL(void)160alloc_funny_pointers (j_decompress_ptr cinfo)161/* Allocate space for the funny pointer lists.162* This is done only once, not once per pass.163*/164{165my_main_ptr _main = (my_main_ptr) cinfo->main;166int ci, rgroup;167int M = cinfo->min_DCT_scaled_size;168jpeg_component_info *compptr;169JSAMPARRAY xbuf;170171/* Get top-level space for component array pointers.172* We alloc both arrays with one call to save a few cycles.173*/174_main->xbuffer[0] = (JSAMPIMAGE)175(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,176cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));177_main->xbuffer[1] = _main->xbuffer[0] + cinfo->num_components;178179for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;180ci++, compptr++) {181rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /182cinfo->min_DCT_scaled_size; /* height of a row group of component */183/* Get space for pointer lists --- M+4 row groups in each list.184* We alloc both pointer lists with one call to save a few cycles.185*/186xbuf = (JSAMPARRAY)187(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,1882 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));189xbuf += rgroup; /* want one row group at negative offsets */190_main->xbuffer[0][ci] = xbuf;191xbuf += rgroup * (M + 4);192_main->xbuffer[1][ci] = xbuf;193}194}195196197LOCAL(void)198make_funny_pointers (j_decompress_ptr cinfo)199/* Create the funny pointer lists discussed in the comments above.200* The actual workspace is already allocated (in main->buffer),201* and the space for the pointer lists is allocated too.202* This routine just fills in the curiously ordered lists.203* This will be repeated at the beginning of each pass.204*/205{206my_main_ptr _main = (my_main_ptr) cinfo->main;207int ci, i, rgroup;208int M = cinfo->min_DCT_scaled_size;209jpeg_component_info *compptr;210JSAMPARRAY buf, xbuf0, xbuf1;211212for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;213ci++, compptr++) {214rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /215cinfo->min_DCT_scaled_size; /* height of a row group of component */216xbuf0 = _main->xbuffer[0][ci];217xbuf1 = _main->xbuffer[1][ci];218/* First copy the workspace pointers as-is */219buf = _main->buffer[ci];220for (i = 0; i < rgroup * (M + 2); i++) {221xbuf0[i] = xbuf1[i] = buf[i];222}223/* In the second list, put the last four row groups in swapped order */224for (i = 0; i < rgroup * 2; i++) {225xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];226xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];227}228/* The wraparound pointers at top and bottom will be filled later229* (see set_wraparound_pointers, below). Initially we want the "above"230* pointers to duplicate the first actual data line. This only needs231* to happen in xbuffer[0].232*/233for (i = 0; i < rgroup; i++) {234xbuf0[i - rgroup] = xbuf0[0];235}236}237}238239240LOCAL(void)241set_wraparound_pointers (j_decompress_ptr cinfo)242/* Set up the "wraparound" pointers at top and bottom of the pointer lists.243* This changes the pointer list state from top-of-image to the normal state.244*/245{246my_main_ptr _main = (my_main_ptr) cinfo->main;247int ci, i, rgroup;248int M = cinfo->min_DCT_scaled_size;249jpeg_component_info *compptr;250JSAMPARRAY xbuf0, xbuf1;251252for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;253ci++, compptr++) {254rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /255cinfo->min_DCT_scaled_size; /* height of a row group of component */256xbuf0 = _main->xbuffer[0][ci];257xbuf1 = _main->xbuffer[1][ci];258for (i = 0; i < rgroup; i++) {259xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];260xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];261xbuf0[rgroup*(M+2) + i] = xbuf0[i];262xbuf1[rgroup*(M+2) + i] = xbuf1[i];263}264}265}266267268LOCAL(void)269set_bottom_pointers (j_decompress_ptr cinfo)270/* Change the pointer lists to duplicate the last sample row at the bottom271* of the image. whichptr indicates which xbuffer holds the final iMCU row.272* Also sets rowgroups_avail to indicate number of nondummy row groups in row.273*/274{275my_main_ptr _main = (my_main_ptr) cinfo->main;276int ci, i, rgroup, iMCUheight, rows_left;277jpeg_component_info *compptr;278JSAMPARRAY xbuf;279280for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;281ci++, compptr++) {282/* Count sample rows in one iMCU row and in one row group */283iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;284rgroup = iMCUheight / cinfo->min_DCT_scaled_size;285/* Count nondummy sample rows remaining for this component */286rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);287if (rows_left == 0) rows_left = iMCUheight;288/* Count nondummy row groups. Should get same answer for each component,289* so we need only do it once.290*/291if (ci == 0) {292_main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);293}294/* Duplicate the last real sample row rgroup*2 times; this pads out the295* last partial rowgroup and ensures at least one full rowgroup of context.296*/297xbuf = _main->xbuffer[_main->whichptr][ci];298for (i = 0; i < rgroup * 2; i++) {299xbuf[rows_left + i] = xbuf[rows_left-1];300}301}302}303304305/*306* Initialize for a processing pass.307*/308309METHODDEF(void)310start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)311{312my_main_ptr _main = (my_main_ptr) cinfo->main;313314switch (pass_mode) {315case JBUF_PASS_THRU:316if (cinfo->upsample->need_context_rows) {317_main->pub.process_data = process_data_context_main;318make_funny_pointers(cinfo); /* Create the xbuffer[] lists */319_main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */320_main->context_state = CTX_PREPARE_FOR_IMCU;321_main->iMCU_row_ctr = 0;322} else {323/* Simple case with no context needed */324_main->pub.process_data = process_data_simple_main;325}326_main->buffer_full = FALSE; /* Mark buffer empty */327_main->rowgroup_ctr = 0;328break;329#ifdef QUANT_2PASS_SUPPORTED330case JBUF_CRANK_DEST:331/* For last pass of 2-pass quantization, just crank the postprocessor */332_main->pub.process_data = process_data_crank_post;333break;334#endif335default:336ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);337break;338}339}340341342/*343* Process some data.344* This handles the simple case where no context is required.345*/346347METHODDEF(void)348process_data_simple_main (j_decompress_ptr cinfo,349JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,350JDIMENSION out_rows_avail)351{352my_main_ptr _main = (my_main_ptr) cinfo->main;353JDIMENSION rowgroups_avail;354355/* Read input data if we haven't filled the main buffer yet */356if (! _main->buffer_full) {357if (! (*cinfo->coef->decompress_data) (cinfo, _main->buffer))358return; /* suspension forced, can do nothing more */359_main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */360}361362/* There are always min_DCT_scaled_size row groups in an iMCU row. */363rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;364/* Note: at the bottom of the image, we may pass extra garbage row groups365* to the postprocessor. The postprocessor has to check for bottom366* of image anyway (at row resolution), so no point in us doing it too.367*/368369/* Feed the postprocessor */370(*cinfo->post->post_process_data) (cinfo, _main->buffer,371&_main->rowgroup_ctr, rowgroups_avail,372output_buf, out_row_ctr, out_rows_avail);373374/* Has postprocessor consumed all the data yet? If so, mark buffer empty */375if (_main->rowgroup_ctr >= rowgroups_avail) {376_main->buffer_full = FALSE;377_main->rowgroup_ctr = 0;378}379}380381382/*383* Process some data.384* This handles the case where context rows must be provided.385*/386387METHODDEF(void)388process_data_context_main (j_decompress_ptr cinfo,389JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,390JDIMENSION out_rows_avail)391{392my_main_ptr _main = (my_main_ptr) cinfo->main;393394/* Read input data if we haven't filled the _main buffer yet */395if (! _main->buffer_full) {396if (! (*cinfo->coef->decompress_data) (cinfo,397_main->xbuffer[_main->whichptr]))398return; /* suspension forced, can do nothing more */399_main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */400_main->iMCU_row_ctr++; /* count rows received */401}402403/* Postprocessor typically will not swallow all the input data it is handed404* in one call (due to filling the output buffer first). Must be prepared405* to exit and restart. This switch lets us keep track of how far we got.406* Note that each case falls through to the next on successful completion.407*/408switch (_main->context_state) {409case CTX_POSTPONED_ROW:410/* Call postprocessor using previously set pointers for postponed row */411(*cinfo->post->post_process_data) (cinfo, _main->xbuffer[_main->whichptr],412&_main->rowgroup_ctr, _main->rowgroups_avail,413output_buf, out_row_ctr, out_rows_avail);414if (_main->rowgroup_ctr < _main->rowgroups_avail)415return; /* Need to suspend */416_main->context_state = CTX_PREPARE_FOR_IMCU;417if (*out_row_ctr >= out_rows_avail)418return; /* Postprocessor exactly filled output buf */419/*FALLTHROUGH*/420case CTX_PREPARE_FOR_IMCU:421/* Prepare to process first M-1 row groups of this iMCU row */422_main->rowgroup_ctr = 0;423_main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);424/* Check for bottom of image: if so, tweak pointers to "duplicate"425* the last sample row, and adjust rowgroups_avail to ignore padding rows.426*/427if (_main->iMCU_row_ctr == cinfo->total_iMCU_rows)428set_bottom_pointers(cinfo);429_main->context_state = CTX_PROCESS_IMCU;430/*FALLTHROUGH*/431case CTX_PROCESS_IMCU:432/* Call postprocessor using previously set pointers */433(*cinfo->post->post_process_data) (cinfo, _main->xbuffer[_main->whichptr],434&_main->rowgroup_ctr, _main->rowgroups_avail,435output_buf, out_row_ctr, out_rows_avail);436if (_main->rowgroup_ctr < _main->rowgroups_avail)437return; /* Need to suspend */438/* After the first iMCU, change wraparound pointers to normal state */439if (_main->iMCU_row_ctr == 1)440set_wraparound_pointers(cinfo);441/* Prepare to load new iMCU row using other xbuffer list */442_main->whichptr ^= 1; /* 0=>1 or 1=>0 */443_main->buffer_full = FALSE;444/* Still need to process last row group of this iMCU row, */445/* which is saved at index M+1 of the other xbuffer */446_main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);447_main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);448_main->context_state = CTX_POSTPONED_ROW;449}450}451452453/*454* Process some data.455* Final pass of two-pass quantization: just call the postprocessor.456* Source data will be the postprocessor controller's internal buffer.457*/458459#ifdef QUANT_2PASS_SUPPORTED460461METHODDEF(void)462process_data_crank_post (j_decompress_ptr cinfo,463JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,464JDIMENSION out_rows_avail)465{466(*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,467(JDIMENSION *) NULL, (JDIMENSION) 0,468output_buf, out_row_ctr, out_rows_avail);469}470471#endif /* QUANT_2PASS_SUPPORTED */472473474/*475* Initialize main buffer controller.476*/477478GLOBAL(void)479jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)480{481my_main_ptr _main;482int ci, rgroup, ngroups;483jpeg_component_info *compptr;484485_main = (my_main_ptr)486(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,487SIZEOF(my_main_controller));488cinfo->main = (struct jpeg_d_main_controller *) _main;489_main->pub.start_pass = start_pass_main;490491if (need_full_buffer) /* shouldn't happen */492ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);493494/* Allocate the workspace.495* ngroups is the number of row groups we need.496*/497if (cinfo->upsample->need_context_rows) {498if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */499ERREXIT(cinfo, JERR_NOTIMPL);500alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */501ngroups = cinfo->min_DCT_scaled_size + 2;502} else {503ngroups = cinfo->min_DCT_scaled_size;504}505506for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;507ci++, compptr++) {508rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /509cinfo->min_DCT_scaled_size; /* height of a row group of component */510_main->buffer[ci] = (*cinfo->mem->alloc_sarray)511((j_common_ptr) cinfo, JPOOL_IMAGE,512compptr->width_in_blocks * compptr->DCT_scaled_size,513(JDIMENSION) (rgroup * ngroups));514}515}516517518