Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcprepct.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcprepct.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 compression preprocessing controller.12* This controller manages the color conversion, downsampling,13* and edge expansion steps.14*15* Most of the complexity here is associated with buffering input rows16* as required by the downsampler. See the comments at the head of17* jcsample.c for the downsampler's needs.18*/1920#define JPEG_INTERNALS21#include "jinclude.h"22#include "jpeglib.h"232425/* At present, jcsample.c can request context rows only for smoothing.26* In the future, we might also need context rows for CCIR601 sampling27* or other more-complex downsampling procedures. The code to support28* context rows should be compiled only if needed.29*/30#ifdef INPUT_SMOOTHING_SUPPORTED31#define CONTEXT_ROWS_SUPPORTED32#endif333435/*36* For the simple (no-context-row) case, we just need to buffer one37* row group's worth of pixels for the downsampling step. At the bottom of38* the image, we pad to a full row group by replicating the last pixel row.39* The downsampler's last output row is then replicated if needed to pad40* out to a full iMCU row.41*42* When providing context rows, we must buffer three row groups' worth of43* pixels. Three row groups are physically allocated, but the row pointer44* arrays are made five row groups high, with the extra pointers above and45* below "wrapping around" to point to the last and first real row groups.46* This allows the downsampler to access the proper context rows.47* At the top and bottom of the image, we create dummy context rows by48* copying the first or last real pixel row. This copying could be avoided49* by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the50* trouble on the compression side.51*/525354/* Private buffer controller object */5556typedef struct {57struct jpeg_c_prep_controller pub; /* public fields */5859/* Downsampling input buffer. This buffer holds color-converted data60* until we have enough to do a downsample step.61*/62JSAMPARRAY color_buf[MAX_COMPONENTS];6364JDIMENSION rows_to_go; /* counts rows remaining in source image */65int next_buf_row; /* index of next row to store in color_buf */6667#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */68int this_row_group; /* starting row index of group to process */69int next_buf_stop; /* downsample when we reach this index */70#endif71} my_prep_controller;7273typedef my_prep_controller * my_prep_ptr;747576/*77* Initialize for a processing pass.78*/7980METHODDEF(void)81start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)82{83my_prep_ptr prep = (my_prep_ptr) cinfo->prep;8485if (pass_mode != JBUF_PASS_THRU)86ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);8788/* Initialize total-height counter for detecting bottom of image */89prep->rows_to_go = cinfo->image_height;90/* Mark the conversion buffer empty */91prep->next_buf_row = 0;92#ifdef CONTEXT_ROWS_SUPPORTED93/* Preset additional state variables for context mode.94* These aren't used in non-context mode, so we needn't test which mode.95*/96prep->this_row_group = 0;97/* Set next_buf_stop to stop after two row groups have been read in. */98prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;99#endif100}101102103/*104* Expand an image vertically from height input_rows to height output_rows,105* by duplicating the bottom row.106*/107108LOCAL(void)109expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,110int input_rows, int output_rows)111{112register int row;113114for (row = input_rows; row < output_rows; row++) {115jcopy_sample_rows(image_data, input_rows-1, image_data, row,1161, num_cols);117}118}119120121/*122* Process some data in the simple no-context case.123*124* Preprocessor output data is counted in "row groups". A row group125* is defined to be v_samp_factor sample rows of each component.126* Downsampling will produce this much data from each max_v_samp_factor127* input rows.128*/129130METHODDEF(void)131pre_process_data (j_compress_ptr cinfo,132JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,133JDIMENSION in_rows_avail,134JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,135JDIMENSION out_row_groups_avail)136{137my_prep_ptr prep = (my_prep_ptr) cinfo->prep;138int numrows, ci;139JDIMENSION inrows;140jpeg_component_info * compptr;141142while (*in_row_ctr < in_rows_avail &&143*out_row_group_ctr < out_row_groups_avail) {144/* Do color conversion to fill the conversion buffer. */145inrows = in_rows_avail - *in_row_ctr;146numrows = cinfo->max_v_samp_factor - prep->next_buf_row;147numrows = (int) MIN((JDIMENSION) numrows, inrows);148(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,149prep->color_buf,150(JDIMENSION) prep->next_buf_row,151numrows);152*in_row_ctr += numrows;153prep->next_buf_row += numrows;154prep->rows_to_go -= numrows;155/* If at bottom of image, pad to fill the conversion buffer. */156if (prep->rows_to_go == 0 &&157prep->next_buf_row < cinfo->max_v_samp_factor) {158for (ci = 0; ci < cinfo->num_components; ci++) {159expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,160prep->next_buf_row, cinfo->max_v_samp_factor);161}162prep->next_buf_row = cinfo->max_v_samp_factor;163}164/* If we've filled the conversion buffer, empty it. */165if (prep->next_buf_row == cinfo->max_v_samp_factor) {166(*cinfo->downsample->downsample) (cinfo,167prep->color_buf, (JDIMENSION) 0,168output_buf, *out_row_group_ctr);169prep->next_buf_row = 0;170(*out_row_group_ctr)++;171}172/* If at bottom of image, pad the output to a full iMCU height.173* Note we assume the caller is providing a one-iMCU-height output buffer!174*/175if (prep->rows_to_go == 0 &&176*out_row_group_ctr < out_row_groups_avail) {177for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;178ci++, compptr++) {179expand_bottom_edge(output_buf[ci],180compptr->width_in_blocks * DCTSIZE,181(int) (*out_row_group_ctr * compptr->v_samp_factor),182(int) (out_row_groups_avail * compptr->v_samp_factor));183}184*out_row_group_ctr = out_row_groups_avail;185break; /* can exit outer loop without test */186}187}188}189190191#ifdef CONTEXT_ROWS_SUPPORTED192193/*194* Process some data in the context case.195*/196197METHODDEF(void)198pre_process_context (j_compress_ptr cinfo,199JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,200JDIMENSION in_rows_avail,201JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,202JDIMENSION out_row_groups_avail)203{204my_prep_ptr prep = (my_prep_ptr) cinfo->prep;205int numrows, ci;206int buf_height = cinfo->max_v_samp_factor * 3;207JDIMENSION inrows;208209while (*out_row_group_ctr < out_row_groups_avail) {210if (*in_row_ctr < in_rows_avail) {211/* Do color conversion to fill the conversion buffer. */212inrows = in_rows_avail - *in_row_ctr;213numrows = prep->next_buf_stop - prep->next_buf_row;214numrows = (int) MIN((JDIMENSION) numrows, inrows);215(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,216prep->color_buf,217(JDIMENSION) prep->next_buf_row,218numrows);219/* Pad at top of image, if first time through */220if (prep->rows_to_go == cinfo->image_height) {221for (ci = 0; ci < cinfo->num_components; ci++) {222int row;223for (row = 1; row <= cinfo->max_v_samp_factor; row++) {224jcopy_sample_rows(prep->color_buf[ci], 0,225prep->color_buf[ci], -row,2261, cinfo->image_width);227}228}229}230*in_row_ctr += numrows;231prep->next_buf_row += numrows;232prep->rows_to_go -= numrows;233} else {234/* Return for more data, unless we are at the bottom of the image. */235if (prep->rows_to_go != 0)236break;237/* When at bottom of image, pad to fill the conversion buffer. */238if (prep->next_buf_row < prep->next_buf_stop) {239for (ci = 0; ci < cinfo->num_components; ci++) {240expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,241prep->next_buf_row, prep->next_buf_stop);242}243prep->next_buf_row = prep->next_buf_stop;244}245}246/* If we've gotten enough data, downsample a row group. */247if (prep->next_buf_row == prep->next_buf_stop) {248(*cinfo->downsample->downsample) (cinfo,249prep->color_buf,250(JDIMENSION) prep->this_row_group,251output_buf, *out_row_group_ctr);252(*out_row_group_ctr)++;253/* Advance pointers with wraparound as necessary. */254prep->this_row_group += cinfo->max_v_samp_factor;255if (prep->this_row_group >= buf_height)256prep->this_row_group = 0;257if (prep->next_buf_row >= buf_height)258prep->next_buf_row = 0;259prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;260}261}262}263264265/*266* Create the wrapped-around downsampling input buffer needed for context mode.267*/268269LOCAL(void)270create_context_buffer (j_compress_ptr cinfo)271{272my_prep_ptr prep = (my_prep_ptr) cinfo->prep;273int rgroup_height = cinfo->max_v_samp_factor;274int ci, i;275jpeg_component_info * compptr;276JSAMPARRAY true_buffer, fake_buffer;277278/* Grab enough space for fake row pointers for all the components;279* we need five row groups' worth of pointers for each component.280*/281fake_buffer = (JSAMPARRAY)282(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,283(cinfo->num_components * 5 * rgroup_height) *284SIZEOF(JSAMPROW));285286for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;287ci++, compptr++) {288/* Allocate the actual buffer space (3 row groups) for this component.289* We make the buffer wide enough to allow the downsampler to edge-expand290* horizontally within the buffer, if it so chooses.291*/292true_buffer = (*cinfo->mem->alloc_sarray)293((j_common_ptr) cinfo, JPOOL_IMAGE,294(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *295cinfo->max_h_samp_factor) / compptr->h_samp_factor),296(JDIMENSION) (3 * rgroup_height));297/* Copy true buffer row pointers into the middle of the fake row array */298MEMCOPY(fake_buffer + rgroup_height, true_buffer,2993 * rgroup_height * SIZEOF(JSAMPROW));300/* Fill in the above and below wraparound pointers */301for (i = 0; i < rgroup_height; i++) {302fake_buffer[i] = true_buffer[2 * rgroup_height + i];303fake_buffer[4 * rgroup_height + i] = true_buffer[i];304}305prep->color_buf[ci] = fake_buffer + rgroup_height;306fake_buffer += 5 * rgroup_height; /* point to space for next component */307}308}309310#endif /* CONTEXT_ROWS_SUPPORTED */311312313/*314* Initialize preprocessing controller.315*/316317GLOBAL(void)318jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)319{320my_prep_ptr prep;321int ci;322jpeg_component_info * compptr;323324if (need_full_buffer) /* safety check */325ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);326327prep = (my_prep_ptr)328(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,329SIZEOF(my_prep_controller));330cinfo->prep = (struct jpeg_c_prep_controller *) prep;331prep->pub.start_pass = start_pass_prep;332333/* Allocate the color conversion buffer.334* We make the buffer wide enough to allow the downsampler to edge-expand335* horizontally within the buffer, if it so chooses.336*/337if (cinfo->downsample->need_context_rows) {338/* Set up to provide context rows */339#ifdef CONTEXT_ROWS_SUPPORTED340prep->pub.pre_process_data = pre_process_context;341create_context_buffer(cinfo);342#else343ERREXIT(cinfo, JERR_NOT_COMPILED);344#endif345} else {346/* No context, just make it tall enough for one row group */347prep->pub.pre_process_data = pre_process_data;348for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;349ci++, compptr++) {350prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)351((j_common_ptr) cinfo, JPOOL_IMAGE,352(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *353cinfo->max_h_samp_factor) / compptr->h_samp_factor),354(JDIMENSION) cinfo->max_v_samp_factor);355}356}357}358359360