Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jcmainct.c
Views: 11784
/*1* jcmainct.c2*3* Copyright (C) 1994-1996, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains the main buffer controller for compression.8* The main buffer lies between the pre-processor and the JPEG9* compressor proper; it holds downsampled data in the JPEG colorspace.10*/1112#define JPEG_INTERNALS13#include "jinclude.h"14#include "jpeglib.h"151617/* Note: currently, there is no operating mode in which a full-image buffer18* is needed at this step. If there were, that mode could not be used with19* "raw data" input, since this module is bypassed in that case. However,20* we've left the code here for possible use in special applications.21*/22#undef FULL_MAIN_BUFFER_SUPPORTED232425/* Private buffer controller object */2627typedef struct {28struct jpeg_c_main_controller pub; /* public fields */2930JDIMENSION cur_iMCU_row; /* number of current iMCU row */31JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */32boolean suspended; /* remember if we suspended output */33J_BUF_MODE pass_mode; /* current operating mode */3435/* If using just a strip buffer, this points to the entire set of buffers36* (we allocate one for each component). In the full-image case, this37* points to the currently accessible strips of the virtual arrays.38*/39JSAMPARRAY buffer[MAX_COMPONENTS];4041#ifdef FULL_MAIN_BUFFER_SUPPORTED42/* If using full-image storage, this array holds pointers to virtual-array43* control blocks for each component. Unused if not full-image storage.44*/45jvirt_sarray_ptr whole_image[MAX_COMPONENTS];46#endif47} my_main_controller;4849typedef my_main_controller * my_main_ptr;505152/* Forward declarations */53METHODDEF(void) process_data_simple_main54JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,55JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));56#ifdef FULL_MAIN_BUFFER_SUPPORTED57METHODDEF(void) process_data_buffer_main58JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,59JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));60#endif616263/*64* Initialize for a processing pass.65*/6667METHODDEF(void)68start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)69{70my_main_ptr main = (my_main_ptr) cinfo->main;7172/* Do nothing in raw-data mode. */73if (cinfo->raw_data_in)74return;7576main->cur_iMCU_row = 0; /* initialize counters */77main->rowgroup_ctr = 0;78main->suspended = FALSE;79main->pass_mode = pass_mode; /* save mode for use by process_data */8081switch (pass_mode) {82case JBUF_PASS_THRU:83#ifdef FULL_MAIN_BUFFER_SUPPORTED84if (main->whole_image[0] != NULL)85ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);86#endif87main->pub.process_data = process_data_simple_main;88break;89#ifdef FULL_MAIN_BUFFER_SUPPORTED90case JBUF_SAVE_SOURCE:91case JBUF_CRANK_DEST:92case JBUF_SAVE_AND_PASS:93if (main->whole_image[0] == NULL)94ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);95main->pub.process_data = process_data_buffer_main;96break;97#endif98default:99ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);100break;101}102}103104105/*106* Process some data.107* This routine handles the simple pass-through mode,108* where we have only a strip buffer.109*/110111METHODDEF(void)112process_data_simple_main (j_compress_ptr cinfo,113JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,114JDIMENSION in_rows_avail)115{116my_main_ptr main = (my_main_ptr) cinfo->main;117118while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {119/* Read input data if we haven't filled the main buffer yet */120if (main->rowgroup_ctr < DCTSIZE)121(*cinfo->prep->pre_process_data) (cinfo,122input_buf, in_row_ctr, in_rows_avail,123main->buffer, &main->rowgroup_ctr,124(JDIMENSION) DCTSIZE);125126/* If we don't have a full iMCU row buffered, return to application for127* more data. Note that preprocessor will always pad to fill the iMCU row128* at the bottom of the image.129*/130if (main->rowgroup_ctr != DCTSIZE)131return;132133/* Send the completed row to the compressor */134if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {135/* If compressor did not consume the whole row, then we must need to136* suspend processing and return to the application. In this situation137* we pretend we didn't yet consume the last input row; otherwise, if138* it happened to be the last row of the image, the application would139* think we were done.140*/141if (! main->suspended) {142(*in_row_ctr)--;143main->suspended = TRUE;144}145return;146}147/* We did finish the row. Undo our little suspension hack if a previous148* call suspended; then mark the main buffer empty.149*/150if (main->suspended) {151(*in_row_ctr)++;152main->suspended = FALSE;153}154main->rowgroup_ctr = 0;155main->cur_iMCU_row++;156}157}158159160#ifdef FULL_MAIN_BUFFER_SUPPORTED161162/*163* Process some data.164* This routine handles all of the modes that use a full-size buffer.165*/166167METHODDEF(void)168process_data_buffer_main (j_compress_ptr cinfo,169JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,170JDIMENSION in_rows_avail)171{172my_main_ptr main = (my_main_ptr) cinfo->main;173int ci;174jpeg_component_info *compptr;175boolean writing = (main->pass_mode != JBUF_CRANK_DEST);176177while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {178/* Realign the virtual buffers if at the start of an iMCU row. */179if (main->rowgroup_ctr == 0) {180for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;181ci++, compptr++) {182main->buffer[ci] = (*cinfo->mem->access_virt_sarray)183((j_common_ptr) cinfo, main->whole_image[ci],184main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),185(JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);186}187/* In a read pass, pretend we just read some source data. */188if (! writing) {189*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;190main->rowgroup_ctr = DCTSIZE;191}192}193194/* If a write pass, read input data until the current iMCU row is full. */195/* Note: preprocessor will pad if necessary to fill the last iMCU row. */196if (writing) {197(*cinfo->prep->pre_process_data) (cinfo,198input_buf, in_row_ctr, in_rows_avail,199main->buffer, &main->rowgroup_ctr,200(JDIMENSION) DCTSIZE);201/* Return to application if we need more data to fill the iMCU row. */202if (main->rowgroup_ctr < DCTSIZE)203return;204}205206/* Emit data, unless this is a sink-only pass. */207if (main->pass_mode != JBUF_SAVE_SOURCE) {208if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {209/* If compressor did not consume the whole row, then we must need to210* suspend processing and return to the application. In this situation211* we pretend we didn't yet consume the last input row; otherwise, if212* it happened to be the last row of the image, the application would213* think we were done.214*/215if (! main->suspended) {216(*in_row_ctr)--;217main->suspended = TRUE;218}219return;220}221/* We did finish the row. Undo our little suspension hack if a previous222* call suspended; then mark the main buffer empty.223*/224if (main->suspended) {225(*in_row_ctr)++;226main->suspended = FALSE;227}228}229230/* If get here, we are done with this iMCU row. Mark buffer empty. */231main->rowgroup_ctr = 0;232main->cur_iMCU_row++;233}234}235236#endif /* FULL_MAIN_BUFFER_SUPPORTED */237238239/*240* Initialize main buffer controller.241*/242243GLOBAL(void)244jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)245{246my_main_ptr main;247int ci;248jpeg_component_info *compptr;249250main = (my_main_ptr)251(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,252SIZEOF(my_main_controller));253cinfo->main = (struct jpeg_c_main_controller *) main;254main->pub.start_pass = start_pass_main;255256/* We don't need to create a buffer in raw-data mode. */257if (cinfo->raw_data_in)258return;259260/* Create the buffer. It holds downsampled data, so each component261* may be of a different size.262*/263if (need_full_buffer) {264#ifdef FULL_MAIN_BUFFER_SUPPORTED265/* Allocate a full-image virtual array for each component */266/* Note we pad the bottom to a multiple of the iMCU height */267for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;268ci++, compptr++) {269main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)270((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,271compptr->width_in_blocks * DCTSIZE,272(JDIMENSION) jround_up((long) compptr->height_in_blocks,273(long) compptr->v_samp_factor) * DCTSIZE,274(JDIMENSION) (compptr->v_samp_factor * DCTSIZE));275}276#else277ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);278#endif279} else {280#ifdef FULL_MAIN_BUFFER_SUPPORTED281main->whole_image[0] = NULL; /* flag for no virtual arrays */282#endif283/* Allocate a strip buffer for each component */284for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;285ci++, compptr++) {286main->buffer[ci] = (*cinfo->mem->alloc_sarray)287((j_common_ptr) cinfo, JPOOL_IMAGE,288compptr->width_in_blocks * DCTSIZE,289(JDIMENSION) (compptr->v_samp_factor * DCTSIZE));290}291}292}293294295