Path: blob/master/thirdparty/libjpeg-turbo/src/jcmainct.c
9904 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* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright (C) 2022, 2024, D. R. Commander.9* For conditions of distribution and use, see the accompanying README.ijg10* file.11*12* This file contains the main buffer controller for compression.13* The main buffer lies between the pre-processor and the JPEG14* compressor proper; it holds downsampled data in the JPEG colorspace.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"20#include "jsamplecomp.h"212223#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)2425/* 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*/39_JSAMPARRAY buffer[MAX_COMPONENTS];40} my_main_controller;4142typedef my_main_controller *my_main_ptr;434445/* Forward declarations */46METHODDEF(void) process_data_simple_main(j_compress_ptr cinfo,47_JSAMPARRAY input_buf,48JDIMENSION *in_row_ctr,49JDIMENSION in_rows_avail);505152/*53* Initialize for a processing pass.54*/5556METHODDEF(void)57start_pass_main(j_compress_ptr cinfo, J_BUF_MODE pass_mode)58{59my_main_ptr main_ptr = (my_main_ptr)cinfo->main;6061/* Do nothing in raw-data mode. */62if (cinfo->raw_data_in)63return;6465if (pass_mode != JBUF_PASS_THRU)66ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);6768main_ptr->cur_iMCU_row = 0; /* initialize counters */69main_ptr->rowgroup_ctr = 0;70main_ptr->suspended = FALSE;71main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */72main_ptr->pub._process_data = process_data_simple_main;73}747576/*77* Process some data.78* This routine handles the simple pass-through mode,79* where we have only a strip buffer.80*/8182METHODDEF(void)83process_data_simple_main(j_compress_ptr cinfo, _JSAMPARRAY input_buf,84JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)85{86my_main_ptr main_ptr = (my_main_ptr)cinfo->main;87JDIMENSION data_unit = cinfo->master->lossless ? 1 : DCTSIZE;8889while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {90/* Read input data if we haven't filled the main buffer yet */91if (main_ptr->rowgroup_ctr < data_unit)92(*cinfo->prep->_pre_process_data) (cinfo, input_buf, in_row_ctr,93in_rows_avail, main_ptr->buffer,94&main_ptr->rowgroup_ctr, data_unit);9596/* If we don't have a full iMCU row buffered, return to application for97* more data. Note that preprocessor will always pad to fill the iMCU row98* at the bottom of the image.99*/100if (main_ptr->rowgroup_ctr != data_unit)101return;102103/* Send the completed row to the compressor */104if (!(*cinfo->coef->_compress_data) (cinfo, main_ptr->buffer)) {105/* If compressor did not consume the whole row, then we must need to106* suspend processing and return to the application. In this situation107* we pretend we didn't yet consume the last input row; otherwise, if108* it happened to be the last row of the image, the application would109* think we were done.110*/111if (!main_ptr->suspended) {112(*in_row_ctr)--;113main_ptr->suspended = TRUE;114}115return;116}117/* We did finish the row. Undo our little suspension hack if a previous118* call suspended; then mark the main buffer empty.119*/120if (main_ptr->suspended) {121(*in_row_ctr)++;122main_ptr->suspended = FALSE;123}124main_ptr->rowgroup_ctr = 0;125main_ptr->cur_iMCU_row++;126}127}128129130/*131* Initialize main buffer controller.132*/133134GLOBAL(void)135_jinit_c_main_controller(j_compress_ptr cinfo, boolean need_full_buffer)136{137my_main_ptr main_ptr;138int ci;139jpeg_component_info *compptr;140int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;141142#ifdef C_LOSSLESS_SUPPORTED143if (cinfo->master->lossless) {144#if BITS_IN_JSAMPLE == 8145if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)146#else147if (cinfo->data_precision > BITS_IN_JSAMPLE ||148cinfo->data_precision < BITS_IN_JSAMPLE - 3)149#endif150ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);151} else152#endif153{154if (cinfo->data_precision != BITS_IN_JSAMPLE)155ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);156}157158main_ptr = (my_main_ptr)159(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,160sizeof(my_main_controller));161cinfo->main = (struct jpeg_c_main_controller *)main_ptr;162main_ptr->pub.start_pass = start_pass_main;163164/* We don't need to create a buffer in raw-data mode. */165if (cinfo->raw_data_in)166return;167168/* Create the buffer. It holds downsampled data, so each component169* may be of a different size.170*/171if (need_full_buffer) {172ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);173} else {174/* Allocate a strip buffer for each component */175for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;176ci++, compptr++) {177main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)178((j_common_ptr)cinfo, JPOOL_IMAGE,179compptr->width_in_blocks * data_unit,180(JDIMENSION)(compptr->v_samp_factor * data_unit));181}182}183}184185#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */186187188