michael@0: /* michael@0: * jcprepct.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 compression preprocessing controller. michael@0: * This controller manages the color conversion, downsampling, michael@0: * and edge expansion steps. michael@0: * michael@0: * Most of the complexity here is associated with buffering input rows michael@0: * as required by the downsampler. See the comments at the head of michael@0: * jcsample.c for the downsampler's needs. 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: /* At present, jcsample.c can request context rows only for smoothing. michael@0: * In the future, we might also need context rows for CCIR601 sampling michael@0: * or other more-complex downsampling procedures. The code to support michael@0: * context rows should be compiled only if needed. michael@0: */ michael@0: #ifdef INPUT_SMOOTHING_SUPPORTED michael@0: #define CONTEXT_ROWS_SUPPORTED michael@0: #endif michael@0: michael@0: michael@0: /* michael@0: * For the simple (no-context-row) case, we just need to buffer one michael@0: * row group's worth of pixels for the downsampling step. At the bottom of michael@0: * the image, we pad to a full row group by replicating the last pixel row. michael@0: * The downsampler's last output row is then replicated if needed to pad michael@0: * out to a full iMCU row. michael@0: * michael@0: * When providing context rows, we must buffer three row groups' worth of michael@0: * pixels. Three row groups are physically allocated, but the row pointer michael@0: * arrays are made five row groups high, with the extra pointers above and michael@0: * below "wrapping around" to point to the last and first real row groups. michael@0: * This allows the downsampler to access the proper context rows. michael@0: * At the top and bottom of the image, we create dummy context rows by michael@0: * copying the first or last real pixel row. This copying could be avoided michael@0: * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the michael@0: * trouble on the compression side. michael@0: */ michael@0: michael@0: michael@0: /* Private buffer controller object */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_c_prep_controller pub; /* public fields */ michael@0: michael@0: /* Downsampling input buffer. This buffer holds color-converted data michael@0: * until we have enough to do a downsample step. michael@0: */ michael@0: JSAMPARRAY color_buf[MAX_COMPONENTS]; michael@0: michael@0: JDIMENSION rows_to_go; /* counts rows remaining in source image */ michael@0: int next_buf_row; /* index of next row to store in color_buf */ michael@0: michael@0: #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */ michael@0: int this_row_group; /* starting row index of group to process */ michael@0: int next_buf_stop; /* downsample when we reach this index */ michael@0: #endif michael@0: } my_prep_controller; michael@0: michael@0: typedef my_prep_controller * my_prep_ptr; 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_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode) michael@0: { michael@0: my_prep_ptr prep = (my_prep_ptr) cinfo->prep; michael@0: michael@0: if (pass_mode != JBUF_PASS_THRU) michael@0: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); michael@0: michael@0: /* Initialize total-height counter for detecting bottom of image */ michael@0: prep->rows_to_go = cinfo->image_height; michael@0: /* Mark the conversion buffer empty */ michael@0: prep->next_buf_row = 0; michael@0: #ifdef CONTEXT_ROWS_SUPPORTED michael@0: /* Preset additional state variables for context mode. michael@0: * These aren't used in non-context mode, so we needn't test which mode. michael@0: */ michael@0: prep->this_row_group = 0; michael@0: /* Set next_buf_stop to stop after two row groups have been read in. */ michael@0: prep->next_buf_stop = 2 * cinfo->max_v_samp_factor; michael@0: #endif michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Expand an image vertically from height input_rows to height output_rows, michael@0: * by duplicating the bottom row. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols, michael@0: int input_rows, int output_rows) michael@0: { michael@0: register int row; michael@0: michael@0: for (row = input_rows; row < output_rows; row++) { michael@0: jcopy_sample_rows(image_data, input_rows-1, image_data, row, michael@0: 1, num_cols); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Process some data in the simple no-context case. michael@0: * michael@0: * Preprocessor output data is counted in "row groups". A row group michael@0: * is defined to be v_samp_factor sample rows of each component. michael@0: * Downsampling will produce this much data from each max_v_samp_factor michael@0: * input rows. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: pre_process_data (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, michael@0: JDIMENSION in_rows_avail, michael@0: JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, michael@0: JDIMENSION out_row_groups_avail) michael@0: { michael@0: my_prep_ptr prep = (my_prep_ptr) cinfo->prep; michael@0: int numrows, ci; michael@0: JDIMENSION inrows; michael@0: jpeg_component_info * compptr; michael@0: michael@0: while (*in_row_ctr < in_rows_avail && michael@0: *out_row_group_ctr < out_row_groups_avail) { michael@0: /* Do color conversion to fill the conversion buffer. */ michael@0: inrows = in_rows_avail - *in_row_ctr; michael@0: numrows = cinfo->max_v_samp_factor - prep->next_buf_row; michael@0: numrows = (int) MIN((JDIMENSION) numrows, inrows); michael@0: (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, michael@0: prep->color_buf, michael@0: (JDIMENSION) prep->next_buf_row, michael@0: numrows); michael@0: *in_row_ctr += numrows; michael@0: prep->next_buf_row += numrows; michael@0: prep->rows_to_go -= numrows; michael@0: /* If at bottom of image, pad to fill the conversion buffer. */ michael@0: if (prep->rows_to_go == 0 && michael@0: prep->next_buf_row < cinfo->max_v_samp_factor) { michael@0: for (ci = 0; ci < cinfo->num_components; ci++) { michael@0: expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, michael@0: prep->next_buf_row, cinfo->max_v_samp_factor); michael@0: } michael@0: prep->next_buf_row = cinfo->max_v_samp_factor; michael@0: } michael@0: /* If we've filled the conversion buffer, empty it. */ michael@0: if (prep->next_buf_row == cinfo->max_v_samp_factor) { michael@0: (*cinfo->downsample->downsample) (cinfo, michael@0: prep->color_buf, (JDIMENSION) 0, michael@0: output_buf, *out_row_group_ctr); michael@0: prep->next_buf_row = 0; michael@0: (*out_row_group_ctr)++; michael@0: } michael@0: /* If at bottom of image, pad the output to a full iMCU height. michael@0: * Note we assume the caller is providing a one-iMCU-height output buffer! michael@0: */ michael@0: if (prep->rows_to_go == 0 && michael@0: *out_row_group_ctr < out_row_groups_avail) { michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: expand_bottom_edge(output_buf[ci], michael@0: compptr->width_in_blocks * DCTSIZE, michael@0: (int) (*out_row_group_ctr * compptr->v_samp_factor), michael@0: (int) (out_row_groups_avail * compptr->v_samp_factor)); michael@0: } michael@0: *out_row_group_ctr = out_row_groups_avail; michael@0: break; /* can exit outer loop without test */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: #ifdef CONTEXT_ROWS_SUPPORTED michael@0: michael@0: /* michael@0: * Process some data in the context case. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: pre_process_context (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, michael@0: JDIMENSION in_rows_avail, michael@0: JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, michael@0: JDIMENSION out_row_groups_avail) michael@0: { michael@0: my_prep_ptr prep = (my_prep_ptr) cinfo->prep; michael@0: int numrows, ci; michael@0: int buf_height = cinfo->max_v_samp_factor * 3; michael@0: JDIMENSION inrows; michael@0: michael@0: while (*out_row_group_ctr < out_row_groups_avail) { michael@0: if (*in_row_ctr < in_rows_avail) { michael@0: /* Do color conversion to fill the conversion buffer. */ michael@0: inrows = in_rows_avail - *in_row_ctr; michael@0: numrows = prep->next_buf_stop - prep->next_buf_row; michael@0: numrows = (int) MIN((JDIMENSION) numrows, inrows); michael@0: (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, michael@0: prep->color_buf, michael@0: (JDIMENSION) prep->next_buf_row, michael@0: numrows); michael@0: /* Pad at top of image, if first time through */ michael@0: if (prep->rows_to_go == cinfo->image_height) { michael@0: for (ci = 0; ci < cinfo->num_components; ci++) { michael@0: int row; michael@0: for (row = 1; row <= cinfo->max_v_samp_factor; row++) { michael@0: jcopy_sample_rows(prep->color_buf[ci], 0, michael@0: prep->color_buf[ci], -row, michael@0: 1, cinfo->image_width); michael@0: } michael@0: } michael@0: } michael@0: *in_row_ctr += numrows; michael@0: prep->next_buf_row += numrows; michael@0: prep->rows_to_go -= numrows; michael@0: } else { michael@0: /* Return for more data, unless we are at the bottom of the image. */ michael@0: if (prep->rows_to_go != 0) michael@0: break; michael@0: /* When at bottom of image, pad to fill the conversion buffer. */ michael@0: if (prep->next_buf_row < prep->next_buf_stop) { michael@0: for (ci = 0; ci < cinfo->num_components; ci++) { michael@0: expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, michael@0: prep->next_buf_row, prep->next_buf_stop); michael@0: } michael@0: prep->next_buf_row = prep->next_buf_stop; michael@0: } michael@0: } michael@0: /* If we've gotten enough data, downsample a row group. */ michael@0: if (prep->next_buf_row == prep->next_buf_stop) { michael@0: (*cinfo->downsample->downsample) (cinfo, michael@0: prep->color_buf, michael@0: (JDIMENSION) prep->this_row_group, michael@0: output_buf, *out_row_group_ctr); michael@0: (*out_row_group_ctr)++; michael@0: /* Advance pointers with wraparound as necessary. */ michael@0: prep->this_row_group += cinfo->max_v_samp_factor; michael@0: if (prep->this_row_group >= buf_height) michael@0: prep->this_row_group = 0; michael@0: if (prep->next_buf_row >= buf_height) michael@0: prep->next_buf_row = 0; michael@0: prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Create the wrapped-around downsampling input buffer needed for context mode. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: create_context_buffer (j_compress_ptr cinfo) michael@0: { michael@0: my_prep_ptr prep = (my_prep_ptr) cinfo->prep; michael@0: int rgroup_height = cinfo->max_v_samp_factor; michael@0: int ci, i; michael@0: jpeg_component_info * compptr; michael@0: JSAMPARRAY true_buffer, fake_buffer; michael@0: michael@0: /* Grab enough space for fake row pointers for all the components; michael@0: * we need five row groups' worth of pointers for each component. michael@0: */ michael@0: fake_buffer = (JSAMPARRAY) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (cinfo->num_components * 5 * rgroup_height) * michael@0: SIZEOF(JSAMPROW)); michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Allocate the actual buffer space (3 row groups) for this component. michael@0: * We make the buffer wide enough to allow the downsampler to edge-expand michael@0: * horizontally within the buffer, if it so chooses. michael@0: */ michael@0: true_buffer = (*cinfo->mem->alloc_sarray) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE * michael@0: cinfo->max_h_samp_factor) / compptr->h_samp_factor), michael@0: (JDIMENSION) (3 * rgroup_height)); michael@0: /* Copy true buffer row pointers into the middle of the fake row array */ michael@0: MEMCOPY(fake_buffer + rgroup_height, true_buffer, michael@0: 3 * rgroup_height * SIZEOF(JSAMPROW)); michael@0: /* Fill in the above and below wraparound pointers */ michael@0: for (i = 0; i < rgroup_height; i++) { michael@0: fake_buffer[i] = true_buffer[2 * rgroup_height + i]; michael@0: fake_buffer[4 * rgroup_height + i] = true_buffer[i]; michael@0: } michael@0: prep->color_buf[ci] = fake_buffer + rgroup_height; michael@0: fake_buffer += 5 * rgroup_height; /* point to space for next component */ michael@0: } michael@0: } michael@0: michael@0: #endif /* CONTEXT_ROWS_SUPPORTED */ michael@0: michael@0: michael@0: /* michael@0: * Initialize preprocessing controller. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer) michael@0: { michael@0: my_prep_ptr prep; michael@0: int ci; michael@0: jpeg_component_info * compptr; michael@0: michael@0: if (need_full_buffer) /* safety check */ michael@0: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); michael@0: michael@0: prep = (my_prep_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_prep_controller)); michael@0: cinfo->prep = (struct jpeg_c_prep_controller *) prep; michael@0: prep->pub.start_pass = start_pass_prep; michael@0: michael@0: /* Allocate the color conversion buffer. michael@0: * We make the buffer wide enough to allow the downsampler to edge-expand michael@0: * horizontally within the buffer, if it so chooses. michael@0: */ michael@0: if (cinfo->downsample->need_context_rows) { michael@0: /* Set up to provide context rows */ michael@0: #ifdef CONTEXT_ROWS_SUPPORTED michael@0: prep->pub.pre_process_data = pre_process_context; michael@0: create_context_buffer(cinfo); michael@0: #else michael@0: ERREXIT(cinfo, JERR_NOT_COMPILED); michael@0: #endif michael@0: } else { michael@0: /* No context, just make it tall enough for one row group */ michael@0: prep->pub.pre_process_data = pre_process_data; michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: prep->color_buf[ci] = (*cinfo->mem->alloc_sarray) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE * michael@0: cinfo->max_h_samp_factor) / compptr->h_samp_factor), michael@0: (JDIMENSION) cinfo->max_v_samp_factor); michael@0: } michael@0: } michael@0: }