Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcmainct.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcmainct.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 compression.12* The main buffer lies between the pre-processor and the JPEG13* compressor proper; it holds downsampled data in the JPEG colorspace.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"192021/* Note: currently, there is no operating mode in which a full-image buffer22* is needed at this step. If there were, that mode could not be used with23* "raw data" input, since this module is bypassed in that case. However,24* we've left the code here for possible use in special applications.25*/26#undef FULL_MAIN_BUFFER_SUPPORTED272829/* Private buffer controller object */3031typedef struct {32struct jpeg_c_main_controller pub; /* public fields */3334JDIMENSION cur_iMCU_row; /* number of current iMCU row */35JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */36boolean suspended; /* remember if we suspended output */37J_BUF_MODE pass_mode; /* current operating mode */3839/* If using just a strip buffer, this points to the entire set of buffers40* (we allocate one for each component). In the full-image case, this41* points to the currently accessible strips of the virtual arrays.42*/43JSAMPARRAY buffer[MAX_COMPONENTS];4445#ifdef FULL_MAIN_BUFFER_SUPPORTED46/* If using full-image storage, this array holds pointers to virtual-array47* control blocks for each component. Unused if not full-image storage.48*/49jvirt_sarray_ptr whole_image[MAX_COMPONENTS];50#endif51} my_main_controller;5253typedef my_main_controller * my_main_ptr;545556/* Forward declarations */57METHODDEF(void) process_data_simple_main58JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,59JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));60#ifdef FULL_MAIN_BUFFER_SUPPORTED61METHODDEF(void) process_data_buffer_main62JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,63JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));64#endif656667/*68* Initialize for a processing pass.69*/7071METHODDEF(void)72start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)73{74my_main_ptr _main = (my_main_ptr) cinfo->main;7576/* Do nothing in raw-data mode. */77if (cinfo->raw_data_in)78return;7980_main->cur_iMCU_row = 0; /* initialize counters */81_main->rowgroup_ctr = 0;82_main->suspended = FALSE;83_main->pass_mode = pass_mode; /* save mode for use by process_data */8485switch (pass_mode) {86case JBUF_PASS_THRU:87#ifdef FULL_MAIN_BUFFER_SUPPORTED88if (_main->whole_image[0] != NULL)89ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);90#endif91_main->pub.process_data = process_data_simple_main;92break;93#ifdef FULL_MAIN_BUFFER_SUPPORTED94case JBUF_SAVE_SOURCE:95case JBUF_CRANK_DEST:96case JBUF_SAVE_AND_PASS:97if (_main->whole_image[0] == NULL)98ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);99_main->pub.process_data = process_data_buffer_main;100break;101#endif102default:103ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);104break;105}106}107108109/*110* Process some data.111* This routine handles the simple pass-through mode,112* where we have only a strip buffer.113*/114115METHODDEF(void)116process_data_simple_main (j_compress_ptr cinfo,117JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,118JDIMENSION in_rows_avail)119{120my_main_ptr _main = (my_main_ptr) cinfo->main;121122while (_main->cur_iMCU_row < cinfo->total_iMCU_rows) {123/* Read input data if we haven't filled the main buffer yet */124if (_main->rowgroup_ctr < DCTSIZE)125(*cinfo->prep->pre_process_data) (cinfo,126input_buf, in_row_ctr, in_rows_avail,127_main->buffer, &_main->rowgroup_ctr,128(JDIMENSION) DCTSIZE);129130/* If we don't have a full iMCU row buffered, return to application for131* more data. Note that preprocessor will always pad to fill the iMCU row132* at the bottom of the image.133*/134if (_main->rowgroup_ctr != DCTSIZE)135return;136137/* Send the completed row to the compressor */138if (! (*cinfo->coef->compress_data) (cinfo, _main->buffer)) {139/* If compressor did not consume the whole row, then we must need to140* suspend processing and return to the application. In this situation141* we pretend we didn't yet consume the last input row; otherwise, if142* it happened to be the last row of the image, the application would143* think we were done.144*/145if (! _main->suspended) {146(*in_row_ctr)--;147_main->suspended = TRUE;148}149return;150}151/* We did finish the row. Undo our little suspension hack if a previous152* call suspended; then mark the main buffer empty.153*/154if (_main->suspended) {155(*in_row_ctr)++;156_main->suspended = FALSE;157}158_main->rowgroup_ctr = 0;159_main->cur_iMCU_row++;160}161}162163164#ifdef FULL_MAIN_BUFFER_SUPPORTED165166/*167* Process some data.168* This routine handles all of the modes that use a full-size buffer.169*/170171METHODDEF(void)172process_data_buffer_main (j_compress_ptr cinfo,173JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,174JDIMENSION in_rows_avail)175{176my_main_ptr _main = (my_main_ptr) cinfo->main;177int ci;178jpeg_component_info *compptr;179boolean writing = (_main->pass_mode != JBUF_CRANK_DEST);180181while (_main->cur_iMCU_row < cinfo->total_iMCU_rows) {182/* Realign the virtual buffers if at the start of an iMCU row. */183if (_main->rowgroup_ctr == 0) {184for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;185ci++, compptr++) {186_main->buffer[ci] = (*cinfo->mem->access_virt_sarray)187((j_common_ptr) cinfo, _main->whole_image[ci],188_main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),189(JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);190}191/* In a read pass, pretend we just read some source data. */192if (! writing) {193*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;194_main->rowgroup_ctr = DCTSIZE;195}196}197198/* If a write pass, read input data until the current iMCU row is full. */199/* Note: preprocessor will pad if necessary to fill the last iMCU row. */200if (writing) {201(*cinfo->prep->pre_process_data) (cinfo,202input_buf, in_row_ctr, in_rows_avail,203_main->buffer, &_main->rowgroup_ctr,204(JDIMENSION) DCTSIZE);205/* Return to application if we need more data to fill the iMCU row. */206if (_main->rowgroup_ctr < DCTSIZE)207return;208}209210/* Emit data, unless this is a sink-only pass. */211if (_main->pass_mode != JBUF_SAVE_SOURCE) {212if (! (*cinfo->coef->compress_data) (cinfo, _main->buffer)) {213/* If compressor did not consume the whole row, then we must need to214* suspend processing and return to the application. In this situation215* we pretend we didn't yet consume the last input row; otherwise, if216* it happened to be the last row of the image, the application would217* think we were done.218*/219if (! _main->suspended) {220(*in_row_ctr)--;221_main->suspended = TRUE;222}223return;224}225/* We did finish the row. Undo our little suspension hack if a previous226* call suspended; then mark the main buffer empty.227*/228if (_main->suspended) {229(*in_row_ctr)++;230_main->suspended = FALSE;231}232}233234/* If get here, we are done with this iMCU row. Mark buffer empty. */235_main->rowgroup_ctr = 0;236_main->cur_iMCU_row++;237}238}239240#endif /* FULL_MAIN_BUFFER_SUPPORTED */241242243/*244* Initialize main buffer controller.245*/246247GLOBAL(void)248jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)249{250my_main_ptr _main;251int ci;252jpeg_component_info *compptr;253254_main = (my_main_ptr)255(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,256SIZEOF(my_main_controller));257cinfo->main = (struct jpeg_c_main_controller *) _main;258_main->pub.start_pass = start_pass_main;259260/* We don't need to create a buffer in raw-data mode. */261if (cinfo->raw_data_in)262return;263264/* Create the buffer. It holds downsampled data, so each component265* may be of a different size.266*/267if (need_full_buffer) {268#ifdef FULL_MAIN_BUFFER_SUPPORTED269/* Allocate a full-image virtual array for each component */270/* Note we pad the bottom to a multiple of the iMCU height */271for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;272ci++, compptr++) {273_main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)274((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,275compptr->width_in_blocks * DCTSIZE,276(JDIMENSION) jround_up((long) compptr->height_in_blocks,277(long) compptr->v_samp_factor) * DCTSIZE,278(JDIMENSION) (compptr->v_samp_factor * DCTSIZE));279}280#else281ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);282#endif283} else {284#ifdef FULL_MAIN_BUFFER_SUPPORTED285_main->whole_image[0] = NULL; /* flag for no virtual arrays */286#endif287/* Allocate a strip buffer for each component */288for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;289ci++, compptr++) {290_main->buffer[ci] = (*cinfo->mem->alloc_sarray)291((j_common_ptr) cinfo, JPOOL_IMAGE,292compptr->width_in_blocks * DCTSIZE,293(JDIMENSION) (compptr->v_samp_factor * DCTSIZE));294}295}296}297298299