jcprepct.c
Go to the documentation of this file.
00001 /*
00002  * jcprepct.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the compression preprocessing controller.
00009  * This controller manages the color conversion, downsampling,
00010  * and edge expansion steps.
00011  *
00012  * Most of the complexity here is associated with buffering input rows
00013  * as required by the downsampler.  See the comments at the head of
00014  * jcsample.c for the downsampler's needs.
00015  */
00016 
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 
00021 
00022 /* At present, jcsample.c can request context rows only for smoothing.
00023  * In the future, we might also need context rows for CCIR601 sampling
00024  * or other more-complex downsampling procedures.  The code to support
00025  * context rows should be compiled only if needed.
00026  */
00027 #ifdef INPUT_SMOOTHING_SUPPORTED
00028 #define CONTEXT_ROWS_SUPPORTED
00029 #endif
00030 
00031 
00032 /*
00033  * For the simple (no-context-row) case, we just need to buffer one
00034  * row group's worth of pixels for the downsampling step.  At the bottom of
00035  * the image, we pad to a full row group by replicating the last pixel row.
00036  * The downsampler's last output row is then replicated if needed to pad
00037  * out to a full iMCU row.
00038  *
00039  * When providing context rows, we must buffer three row groups' worth of
00040  * pixels.  Three row groups are physically allocated, but the row pointer
00041  * arrays are made five row groups high, with the extra pointers above and
00042  * below "wrapping around" to point to the last and first real row groups.
00043  * This allows the downsampler to access the proper context rows.
00044  * At the top and bottom of the image, we create dummy context rows by
00045  * copying the first or last real pixel row.  This copying could be avoided
00046  * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
00047  * trouble on the compression side.
00048  */
00049 
00050 
00051 /* Private buffer controller object */
00052 
00053 typedef struct {
00054   struct jpeg_c_prep_controller pub; /* public fields */
00055 
00056   /* Downsampling input buffer.  This buffer holds color-converted data
00057    * until we have enough to do a downsample step.
00058    */
00059   JSAMPARRAY color_buf[MAX_COMPONENTS];
00060 
00061   JDIMENSION rows_to_go;        /* counts rows remaining in source image */
00062   int next_buf_row;             /* index of next row to store in color_buf */
00063 
00064 #ifdef CONTEXT_ROWS_SUPPORTED   /* only needed for context case */
00065   int this_row_group;           /* starting row index of group to process */
00066   int next_buf_stop;            /* downsample when we reach this index */
00067 #endif
00068 } my_prep_controller;
00069 
00070 typedef my_prep_controller * my_prep_ptr;
00071 
00072 
00073 /*
00074  * Initialize for a processing pass.
00075  */
00076 
00077 METHODDEF(void)
00078 start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00079 {
00080   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00081 
00082   if (pass_mode != JBUF_PASS_THRU)
00083     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00084 
00085   /* Initialize total-height counter for detecting bottom of image */
00086   prep->rows_to_go = cinfo->image_height;
00087   /* Mark the conversion buffer empty */
00088   prep->next_buf_row = 0;
00089 #ifdef CONTEXT_ROWS_SUPPORTED
00090   /* Preset additional state variables for context mode.
00091    * These aren't used in non-context mode, so we needn't test which mode.
00092    */
00093   prep->this_row_group = 0;
00094   /* Set next_buf_stop to stop after two row groups have been read in. */
00095   prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
00096 #endif
00097 }
00098 
00099 
00100 /*
00101  * Expand an image vertically from height input_rows to height output_rows,
00102  * by duplicating the bottom row.
00103  */
00104 
00105 LOCAL(void)
00106 expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
00107                     int input_rows, int output_rows)
00108 {
00109   register int row;
00110 
00111   for (row = input_rows; row < output_rows; row++) {
00112     jcopy_sample_rows(image_data, input_rows-1, image_data, row,
00113                       1, num_cols);
00114   }
00115 }
00116 
00117 
00118 /*
00119  * Process some data in the simple no-context case.
00120  *
00121  * Preprocessor output data is counted in "row groups".  A row group
00122  * is defined to be v_samp_factor sample rows of each component.
00123  * Downsampling will produce this much data from each max_v_samp_factor
00124  * input rows.
00125  */
00126 
00127 METHODDEF(void)
00128 pre_process_data (j_compress_ptr cinfo,
00129                   JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00130                   JDIMENSION in_rows_avail,
00131                   JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
00132                   JDIMENSION out_row_groups_avail)
00133 {
00134   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00135   int numrows, ci;
00136   JDIMENSION inrows;
00137   jpeg_component_info * compptr;
00138 
00139   while (*in_row_ctr < in_rows_avail &&
00140          *out_row_group_ctr < out_row_groups_avail) {
00141     /* Do color conversion to fill the conversion buffer. */
00142     inrows = in_rows_avail - *in_row_ctr;
00143     numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
00144     numrows = (int) MIN((JDIMENSION) numrows, inrows);
00145     (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
00146                                        prep->color_buf,
00147                                        (JDIMENSION) prep->next_buf_row,
00148                                        numrows);
00149     *in_row_ctr += numrows;
00150     prep->next_buf_row += numrows;
00151     prep->rows_to_go -= numrows;
00152     /* If at bottom of image, pad to fill the conversion buffer. */
00153     if (prep->rows_to_go == 0 &&
00154         prep->next_buf_row < cinfo->max_v_samp_factor) {
00155       for (ci = 0; ci < cinfo->num_components; ci++) {
00156         expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
00157                            prep->next_buf_row, cinfo->max_v_samp_factor);
00158       }
00159       prep->next_buf_row = cinfo->max_v_samp_factor;
00160     }
00161     /* If we've filled the conversion buffer, empty it. */
00162     if (prep->next_buf_row == cinfo->max_v_samp_factor) {
00163       (*cinfo->downsample->downsample) (cinfo,
00164                                         prep->color_buf, (JDIMENSION) 0,
00165                                         output_buf, *out_row_group_ctr);
00166       prep->next_buf_row = 0;
00167       (*out_row_group_ctr)++;
00168     }
00169     /* If at bottom of image, pad the output to a full iMCU height.
00170      * Note we assume the caller is providing a one-iMCU-height output buffer!
00171      */
00172     if (prep->rows_to_go == 0 &&
00173         *out_row_group_ctr < out_row_groups_avail) {
00174       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00175            ci++, compptr++) {
00176         expand_bottom_edge(output_buf[ci],
00177                            compptr->width_in_blocks * DCTSIZE,
00178                            (int) (*out_row_group_ctr * compptr->v_samp_factor),
00179                            (int) (out_row_groups_avail * compptr->v_samp_factor));
00180       }
00181       *out_row_group_ctr = out_row_groups_avail;
00182       break;                    /* can exit outer loop without test */
00183     }
00184   }
00185 }
00186 
00187 
00188 #ifdef CONTEXT_ROWS_SUPPORTED
00189 
00190 /*
00191  * Process some data in the context case.
00192  */
00193 
00194 METHODDEF(void)
00195 pre_process_context (j_compress_ptr cinfo,
00196                      JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00197                      JDIMENSION in_rows_avail,
00198                      JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
00199                      JDIMENSION out_row_groups_avail)
00200 {
00201   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00202   int numrows, ci;
00203   int buf_height = cinfo->max_v_samp_factor * 3;
00204   JDIMENSION inrows;
00205 
00206   while (*out_row_group_ctr < out_row_groups_avail) {
00207     if (*in_row_ctr < in_rows_avail) {
00208       /* Do color conversion to fill the conversion buffer. */
00209       inrows = in_rows_avail - *in_row_ctr;
00210       numrows = prep->next_buf_stop - prep->next_buf_row;
00211       numrows = (int) MIN((JDIMENSION) numrows, inrows);
00212       (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
00213                                          prep->color_buf,
00214                                          (JDIMENSION) prep->next_buf_row,
00215                                          numrows);
00216       /* Pad at top of image, if first time through */
00217       if (prep->rows_to_go == cinfo->image_height) {
00218         for (ci = 0; ci < cinfo->num_components; ci++) {
00219           int row;
00220           for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
00221             jcopy_sample_rows(prep->color_buf[ci], 0,
00222                               prep->color_buf[ci], -row,
00223                               1, cinfo->image_width);
00224           }
00225         }
00226       }
00227       *in_row_ctr += numrows;
00228       prep->next_buf_row += numrows;
00229       prep->rows_to_go -= numrows;
00230     } else {
00231       /* Return for more data, unless we are at the bottom of the image. */
00232       if (prep->rows_to_go != 0)
00233         break;
00234       /* When at bottom of image, pad to fill the conversion buffer. */
00235       if (prep->next_buf_row < prep->next_buf_stop) {
00236         for (ci = 0; ci < cinfo->num_components; ci++) {
00237           expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
00238                              prep->next_buf_row, prep->next_buf_stop);
00239         }
00240         prep->next_buf_row = prep->next_buf_stop;
00241       }
00242     }
00243     /* If we've gotten enough data, downsample a row group. */
00244     if (prep->next_buf_row == prep->next_buf_stop) {
00245       (*cinfo->downsample->downsample) (cinfo,
00246                                         prep->color_buf,
00247                                         (JDIMENSION) prep->this_row_group,
00248                                         output_buf, *out_row_group_ctr);
00249       (*out_row_group_ctr)++;
00250       /* Advance pointers with wraparound as necessary. */
00251       prep->this_row_group += cinfo->max_v_samp_factor;
00252       if (prep->this_row_group >= buf_height)
00253         prep->this_row_group = 0;
00254       if (prep->next_buf_row >= buf_height)
00255         prep->next_buf_row = 0;
00256       prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
00257     }
00258   }
00259 }
00260 
00261 
00262 /*
00263  * Create the wrapped-around downsampling input buffer needed for context mode.
00264  */
00265 
00266 LOCAL(void)
00267 create_context_buffer (j_compress_ptr cinfo)
00268 {
00269   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00270   int rgroup_height = cinfo->max_v_samp_factor;
00271   int ci, i;
00272   jpeg_component_info * compptr;
00273   JSAMPARRAY true_buffer, fake_buffer;
00274 
00275   /* Grab enough space for fake row pointers for all the components;
00276    * we need five row groups' worth of pointers for each component.
00277    */
00278   fake_buffer = (JSAMPARRAY)
00279     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00280                                 (cinfo->num_components * 5 * rgroup_height) *
00281                                 SIZEOF(JSAMPROW));
00282 
00283   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00284        ci++, compptr++) {
00285     /* Allocate the actual buffer space (3 row groups) for this component.
00286      * We make the buffer wide enough to allow the downsampler to edge-expand
00287      * horizontally within the buffer, if it so chooses.
00288      */
00289     true_buffer = (*cinfo->mem->alloc_sarray)
00290       ((j_common_ptr) cinfo, JPOOL_IMAGE,
00291        (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
00292                       cinfo->max_h_samp_factor) / compptr->h_samp_factor),
00293        (JDIMENSION) (3 * rgroup_height));
00294     /* Copy true buffer row pointers into the middle of the fake row array */
00295     MEMCOPY(fake_buffer + rgroup_height, true_buffer,
00296             3 * rgroup_height * SIZEOF(JSAMPROW));
00297     /* Fill in the above and below wraparound pointers */
00298     for (i = 0; i < rgroup_height; i++) {
00299       fake_buffer[i] = true_buffer[2 * rgroup_height + i];
00300       fake_buffer[4 * rgroup_height + i] = true_buffer[i];
00301     }
00302     prep->color_buf[ci] = fake_buffer + rgroup_height;
00303     fake_buffer += 5 * rgroup_height; /* point to space for next component */
00304   }
00305 }
00306 
00307 #endif /* CONTEXT_ROWS_SUPPORTED */
00308 
00309 
00310 /*
00311  * Initialize preprocessing controller.
00312  */
00313 
00314 GLOBAL(void)
00315 jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
00316 {
00317   my_prep_ptr prep;
00318   int ci;
00319   jpeg_component_info * compptr;
00320 
00321   if (need_full_buffer)         /* safety check */
00322     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00323 
00324   prep = (my_prep_ptr)
00325     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00326                                 SIZEOF(my_prep_controller));
00327   cinfo->prep = (struct jpeg_c_prep_controller *) prep;
00328   prep->pub.start_pass = start_pass_prep;
00329 
00330   /* Allocate the color conversion buffer.
00331    * We make the buffer wide enough to allow the downsampler to edge-expand
00332    * horizontally within the buffer, if it so chooses.
00333    */
00334   if (cinfo->downsample->need_context_rows) {
00335     /* Set up to provide context rows */
00336 #ifdef CONTEXT_ROWS_SUPPORTED
00337     prep->pub.pre_process_data = pre_process_context;
00338     create_context_buffer(cinfo);
00339 #else
00340     ERREXIT(cinfo, JERR_NOT_COMPILED);
00341 #endif
00342   } else {
00343     /* No context, just make it tall enough for one row group */
00344     prep->pub.pre_process_data = pre_process_data;
00345     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00346          ci++, compptr++) {
00347       prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
00348         ((j_common_ptr) cinfo, JPOOL_IMAGE,
00349          (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
00350                         cinfo->max_h_samp_factor) / compptr->h_samp_factor),
00351          (JDIMENSION) cinfo->max_v_samp_factor);
00352     }
00353   }
00354 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55