0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jcmainct.c
0N/A *
0N/A * Copyright (C) 1994-1996, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains the main buffer controller for compression.
0N/A * The main buffer lies between the pre-processor and the JPEG
0N/A * compressor proper; it holds downsampled data in the JPEG colorspace.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A
0N/A/* Note: currently, there is no operating mode in which a full-image buffer
0N/A * is needed at this step. If there were, that mode could not be used with
0N/A * "raw data" input, since this module is bypassed in that case. However,
0N/A * we've left the code here for possible use in special applications.
0N/A */
0N/A#undef FULL_MAIN_BUFFER_SUPPORTED
0N/A
0N/A
0N/A/* Private buffer controller object */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_c_main_controller pub; /* public fields */
0N/A
0N/A JDIMENSION cur_iMCU_row; /* number of current iMCU row */
0N/A JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
0N/A boolean suspended; /* remember if we suspended output */
0N/A J_BUF_MODE pass_mode; /* current operating mode */
0N/A
0N/A /* If using just a strip buffer, this points to the entire set of buffers
0N/A * (we allocate one for each component). In the full-image case, this
0N/A * points to the currently accessible strips of the virtual arrays.
0N/A */
0N/A JSAMPARRAY buffer[MAX_COMPONENTS];
0N/A
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A /* If using full-image storage, this array holds pointers to virtual-array
0N/A * control blocks for each component. Unused if not full-image storage.
0N/A */
0N/A jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
0N/A#endif
0N/A} my_main_controller;
0N/A
0N/Atypedef my_main_controller * my_main_ptr;
0N/A
0N/A
0N/A/* Forward declarations */
0N/AMETHODDEF(void) process_data_simple_main
0N/A JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
0N/A JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/AMETHODDEF(void) process_data_buffer_main
0N/A JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
0N/A JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
0N/A#endif
0N/A
0N/A
0N/A/*
0N/A * Initialize for a processing pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
0N/A{
0N/A my_main_ptr _main = (my_main_ptr) cinfo->main;
0N/A
0N/A /* Do nothing in raw-data mode. */
0N/A if (cinfo->raw_data_in)
0N/A return;
0N/A
0N/A _main->cur_iMCU_row = 0; /* initialize counters */
0N/A _main->rowgroup_ctr = 0;
0N/A _main->suspended = FALSE;
0N/A _main->pass_mode = pass_mode; /* save mode for use by process_data */
0N/A
0N/A switch (pass_mode) {
0N/A case JBUF_PASS_THRU:
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A if (_main->whole_image[0] != NULL)
0N/A ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
0N/A#endif
0N/A _main->pub.process_data = process_data_simple_main;
0N/A break;
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A case JBUF_SAVE_SOURCE:
0N/A case JBUF_CRANK_DEST:
0N/A case JBUF_SAVE_AND_PASS:
0N/A if (_main->whole_image[0] == NULL)
0N/A ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
0N/A _main->pub.process_data = process_data_buffer_main;
0N/A break;
0N/A#endif
0N/A default:
0N/A ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
0N/A break;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Process some data.
0N/A * This routine handles the simple pass-through mode,
0N/A * where we have only a strip buffer.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Aprocess_data_simple_main (j_compress_ptr cinfo,
0N/A JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
0N/A JDIMENSION in_rows_avail)
0N/A{
0N/A my_main_ptr _main = (my_main_ptr) cinfo->main;
0N/A
0N/A while (_main->cur_iMCU_row < cinfo->total_iMCU_rows) {
0N/A /* Read input data if we haven't filled the main buffer yet */
0N/A if (_main->rowgroup_ctr < DCTSIZE)
0N/A (*cinfo->prep->pre_process_data) (cinfo,
0N/A input_buf, in_row_ctr, in_rows_avail,
0N/A _main->buffer, &_main->rowgroup_ctr,
0N/A (JDIMENSION) DCTSIZE);
0N/A
0N/A /* If we don't have a full iMCU row buffered, return to application for
0N/A * more data. Note that preprocessor will always pad to fill the iMCU row
0N/A * at the bottom of the image.
0N/A */
0N/A if (_main->rowgroup_ctr != DCTSIZE)
0N/A return;
0N/A
0N/A /* Send the completed row to the compressor */
0N/A if (! (*cinfo->coef->compress_data) (cinfo, _main->buffer)) {
0N/A /* If compressor did not consume the whole row, then we must need to
0N/A * suspend processing and return to the application. In this situation
0N/A * we pretend we didn't yet consume the last input row; otherwise, if
0N/A * it happened to be the last row of the image, the application would
0N/A * think we were done.
0N/A */
0N/A if (! _main->suspended) {
0N/A (*in_row_ctr)--;
0N/A _main->suspended = TRUE;
0N/A }
0N/A return;
0N/A }
0N/A /* We did finish the row. Undo our little suspension hack if a previous
0N/A * call suspended; then mark the main buffer empty.
0N/A */
0N/A if (_main->suspended) {
0N/A (*in_row_ctr)++;
0N/A _main->suspended = FALSE;
0N/A }
0N/A _main->rowgroup_ctr = 0;
0N/A _main->cur_iMCU_row++;
0N/A }
0N/A}
0N/A
0N/A
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A
0N/A/*
0N/A * Process some data.
0N/A * This routine handles all of the modes that use a full-size buffer.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Aprocess_data_buffer_main (j_compress_ptr cinfo,
0N/A JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
0N/A JDIMENSION in_rows_avail)
0N/A{
0N/A my_main_ptr _main = (my_main_ptr) cinfo->main;
0N/A int ci;
0N/A jpeg_component_info *compptr;
0N/A boolean writing = (_main->pass_mode != JBUF_CRANK_DEST);
0N/A
0N/A while (_main->cur_iMCU_row < cinfo->total_iMCU_rows) {
0N/A /* Realign the virtual buffers if at the start of an iMCU row. */
0N/A if (_main->rowgroup_ctr == 0) {
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A _main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
0N/A ((j_common_ptr) cinfo, _main->whole_image[ci],
0N/A _main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
0N/A (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
0N/A }
0N/A /* In a read pass, pretend we just read some source data. */
0N/A if (! writing) {
0N/A *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
0N/A _main->rowgroup_ctr = DCTSIZE;
0N/A }
0N/A }
0N/A
0N/A /* If a write pass, read input data until the current iMCU row is full. */
0N/A /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
0N/A if (writing) {
0N/A (*cinfo->prep->pre_process_data) (cinfo,
0N/A input_buf, in_row_ctr, in_rows_avail,
0N/A _main->buffer, &_main->rowgroup_ctr,
0N/A (JDIMENSION) DCTSIZE);
0N/A /* Return to application if we need more data to fill the iMCU row. */
0N/A if (_main->rowgroup_ctr < DCTSIZE)
0N/A return;
0N/A }
0N/A
0N/A /* Emit data, unless this is a sink-only pass. */
0N/A if (_main->pass_mode != JBUF_SAVE_SOURCE) {
0N/A if (! (*cinfo->coef->compress_data) (cinfo, _main->buffer)) {
0N/A /* If compressor did not consume the whole row, then we must need to
0N/A * suspend processing and return to the application. In this situation
0N/A * we pretend we didn't yet consume the last input row; otherwise, if
0N/A * it happened to be the last row of the image, the application would
0N/A * think we were done.
0N/A */
0N/A if (! _main->suspended) {
0N/A (*in_row_ctr)--;
0N/A _main->suspended = TRUE;
0N/A }
0N/A return;
0N/A }
0N/A /* We did finish the row. Undo our little suspension hack if a previous
0N/A * call suspended; then mark the main buffer empty.
0N/A */
0N/A if (_main->suspended) {
0N/A (*in_row_ctr)++;
0N/A _main->suspended = FALSE;
0N/A }
0N/A }
0N/A
0N/A /* If get here, we are done with this iMCU row. Mark buffer empty. */
0N/A _main->rowgroup_ctr = 0;
0N/A _main->cur_iMCU_row++;
0N/A }
0N/A}
0N/A
0N/A#endif /* FULL_MAIN_BUFFER_SUPPORTED */
0N/A
0N/A
0N/A/*
0N/A * Initialize main buffer controller.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
0N/A{
0N/A my_main_ptr _main;
0N/A int ci;
0N/A jpeg_component_info *compptr;
0N/A
0N/A _main = (my_main_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_main_controller));
0N/A cinfo->main = (struct jpeg_c_main_controller *) _main;
0N/A _main->pub.start_pass = start_pass_main;
0N/A
0N/A /* We don't need to create a buffer in raw-data mode. */
0N/A if (cinfo->raw_data_in)
0N/A return;
0N/A
0N/A /* Create the buffer. It holds downsampled data, so each component
0N/A * may be of a different size.
0N/A */
0N/A if (need_full_buffer) {
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A /* Allocate a full-image virtual array for each component */
0N/A /* Note we pad the bottom to a multiple of the iMCU height */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A _main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
0N/A compptr->width_in_blocks * DCTSIZE,
0N/A (JDIMENSION) jround_up((long) compptr->height_in_blocks,
0N/A (long) compptr->v_samp_factor) * DCTSIZE,
0N/A (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
0N/A }
0N/A#else
0N/A ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
0N/A#endif
0N/A } else {
0N/A#ifdef FULL_MAIN_BUFFER_SUPPORTED
0N/A _main->whole_image[0] = NULL; /* flag for no virtual arrays */
0N/A#endif
0N/A /* Allocate a strip buffer for each component */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A _main->buffer[ci] = (*cinfo->mem->alloc_sarray)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A compptr->width_in_blocks * DCTSIZE,
0N/A (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
0N/A }
0N/A }
0N/A}