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