Path: blob/master/3rdparty/libjpeg-turbo/src/jcmainct.c
16337 views
/*1* jcmainct.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* It was modified by The libjpeg-turbo Project to include only code relevant6* to libjpeg-turbo.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains the main buffer controller for compression.11* The main buffer lies between the pre-processor and the JPEG12* compressor proper; it holds downsampled data in the JPEG colorspace.13*/1415#define JPEG_INTERNALS16#include "jinclude.h"17#include "jpeglib.h"181920/* Private buffer controller object */2122typedef struct {23struct jpeg_c_main_controller pub; /* public fields */2425JDIMENSION cur_iMCU_row; /* number of current iMCU row */26JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */27boolean suspended; /* remember if we suspended output */28J_BUF_MODE pass_mode; /* current operating mode */2930/* If using just a strip buffer, this points to the entire set of buffers31* (we allocate one for each component). In the full-image case, this32* points to the currently accessible strips of the virtual arrays.33*/34JSAMPARRAY buffer[MAX_COMPONENTS];35} my_main_controller;3637typedef my_main_controller *my_main_ptr;383940/* Forward declarations */41METHODDEF(void) process_data_simple_main42(j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,43JDIMENSION in_rows_avail);444546/*47* Initialize for a processing pass.48*/4950METHODDEF(void)51start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)52{53my_main_ptr main_ptr = (my_main_ptr) cinfo->main;5455/* Do nothing in raw-data mode. */56if (cinfo->raw_data_in)57return;5859if (pass_mode != JBUF_PASS_THRU)60ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);6162main_ptr->cur_iMCU_row = 0; /* initialize counters */63main_ptr->rowgroup_ctr = 0;64main_ptr->suspended = FALSE;65main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */66main_ptr->pub.process_data = process_data_simple_main;67}686970/*71* Process some data.72* This routine handles the simple pass-through mode,73* where we have only a strip buffer.74*/7576METHODDEF(void)77process_data_simple_main (j_compress_ptr cinfo,78JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,79JDIMENSION in_rows_avail)80{81my_main_ptr main_ptr = (my_main_ptr) cinfo->main;8283while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {84/* Read input data if we haven't filled the main buffer yet */85if (main_ptr->rowgroup_ctr < DCTSIZE)86(*cinfo->prep->pre_process_data) (cinfo,87input_buf, in_row_ctr, in_rows_avail,88main_ptr->buffer, &main_ptr->rowgroup_ctr,89(JDIMENSION) DCTSIZE);9091/* If we don't have a full iMCU row buffered, return to application for92* more data. Note that preprocessor will always pad to fill the iMCU row93* at the bottom of the image.94*/95if (main_ptr->rowgroup_ctr != DCTSIZE)96return;9798/* Send the completed row to the compressor */99if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {100/* If compressor did not consume the whole row, then we must need to101* suspend processing and return to the application. In this situation102* we pretend we didn't yet consume the last input row; otherwise, if103* it happened to be the last row of the image, the application would104* think we were done.105*/106if (! main_ptr->suspended) {107(*in_row_ctr)--;108main_ptr->suspended = TRUE;109}110return;111}112/* We did finish the row. Undo our little suspension hack if a previous113* call suspended; then mark the main buffer empty.114*/115if (main_ptr->suspended) {116(*in_row_ctr)++;117main_ptr->suspended = FALSE;118}119main_ptr->rowgroup_ctr = 0;120main_ptr->cur_iMCU_row++;121}122}123124125/*126* Initialize main buffer controller.127*/128129GLOBAL(void)130jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)131{132my_main_ptr main_ptr;133int ci;134jpeg_component_info *compptr;135136main_ptr = (my_main_ptr)137(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,138sizeof(my_main_controller));139cinfo->main = (struct jpeg_c_main_controller *) main_ptr;140main_ptr->pub.start_pass = start_pass_main;141142/* We don't need to create a buffer in raw-data mode. */143if (cinfo->raw_data_in)144return;145146/* Create the buffer. It holds downsampled data, so each component147* may be of a different size.148*/149if (need_full_buffer) {150ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);151} else {152/* Allocate a strip buffer for each component */153for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;154ci++, compptr++) {155main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)156((j_common_ptr) cinfo, JPOOL_IMAGE,157compptr->width_in_blocks * DCTSIZE,158(JDIMENSION) (compptr->v_samp_factor * DCTSIZE));159}160}161}162163164