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