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