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