jdsample.c
Go to the documentation of this file.
00001 /*
00002  * jdsample.c
00003  *
00004  * Copyright (C) 1991-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 upsampling routines.
00009  *
00010  * Upsampling input data is counted in "row groups".  A row group
00011  * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
00012  * sample rows of each component.  Upsampling will normally produce
00013  * max_v_samp_factor pixel rows from each row group (but this could vary
00014  * if the upsampler is applying a scale factor of its own).
00015  *
00016  * An excellent reference for image resampling is
00017  *   Digital Image Warping, George Wolberg, 1990.
00018  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
00019  */
00020 
00021 #define JPEG_INTERNALS
00022 #include "jinclude.h"
00023 #include "jpeglib.h"
00024 
00025 
00026 /* Pointer to routine to upsample a single component */
00027 typedef JMETHOD(void, upsample1_ptr,
00028                 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00029                  JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
00030 
00031 /* Private subobject */
00032 
00033 typedef struct {
00034   struct jpeg_upsampler pub;    /* public fields */
00035 
00036   /* Color conversion buffer.  When using separate upsampling and color
00037    * conversion steps, this buffer holds one upsampled row group until it
00038    * has been color converted and output.
00039    * Note: we do not allocate any storage for component(s) which are full-size,
00040    * ie do not need rescaling.  The corresponding entry of color_buf[] is
00041    * simply set to point to the input data array, thereby avoiding copying.
00042    */
00043   JSAMPARRAY color_buf[MAX_COMPONENTS];
00044 
00045   /* Per-component upsampling method pointers */
00046   upsample1_ptr methods[MAX_COMPONENTS];
00047 
00048   int next_row_out;             /* counts rows emitted from color_buf */
00049   JDIMENSION rows_to_go;        /* counts rows remaining in image */
00050 
00051   /* Height of an input row group for each component. */
00052   int rowgroup_height[MAX_COMPONENTS];
00053 
00054   /* These arrays save pixel expansion factors so that int_expand need not
00055    * recompute them each time.  They are unused for other upsampling methods.
00056    */
00057   UINT8 h_expand[MAX_COMPONENTS];
00058   UINT8 v_expand[MAX_COMPONENTS];
00059 } my_upsampler;
00060 
00061 typedef my_upsampler * my_upsample_ptr;
00062 
00063 
00064 /*
00065  * Initialize for an upsampling pass.
00066  */
00067 
00068 METHODDEF(void)
00069 start_pass_upsample (j_decompress_ptr cinfo)
00070 {
00071   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
00072 
00073   /* Mark the conversion buffer empty */
00074   upsample->next_row_out = cinfo->max_v_samp_factor;
00075   /* Initialize total-height counter for detecting bottom of image */
00076   upsample->rows_to_go = cinfo->output_height;
00077 }
00078 
00079 
00080 /*
00081  * Control routine to do upsampling (and color conversion).
00082  *
00083  * In this version we upsample each component independently.
00084  * We upsample one row group into the conversion buffer, then apply
00085  * color conversion a row at a time.
00086  */
00087 
00088 METHODDEF(void)
00089 sep_upsample (j_decompress_ptr cinfo,
00090               JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00091               JDIMENSION in_row_groups_avail,
00092               JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00093               JDIMENSION out_rows_avail)
00094 {
00095   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
00096   int ci;
00097   jpeg_component_info * compptr;
00098   JDIMENSION num_rows;
00099 
00100   /* Fill the conversion buffer, if it's empty */
00101   if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
00102     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00103          ci++, compptr++) {
00104       /* Invoke per-component upsample method.  Notice we pass a POINTER
00105        * to color_buf[ci], so that fullsize_upsample can change it.
00106        */
00107       (*upsample->methods[ci]) (cinfo, compptr,
00108         input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
00109         upsample->color_buf + ci);
00110     }
00111     upsample->next_row_out = 0;
00112   }
00113 
00114   /* Color-convert and emit rows */
00115 
00116   /* How many we have in the buffer: */
00117   num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
00118   /* Not more than the distance to the end of the image.  Need this test
00119    * in case the image height is not a multiple of max_v_samp_factor:
00120    */
00121   if (num_rows > upsample->rows_to_go) 
00122     num_rows = upsample->rows_to_go;
00123   /* And not more than what the client can accept: */
00124   out_rows_avail -= *out_row_ctr;
00125   if (num_rows > out_rows_avail)
00126     num_rows = out_rows_avail;
00127 
00128   (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
00129                                      (JDIMENSION) upsample->next_row_out,
00130                                      output_buf + *out_row_ctr,
00131                                      (int) num_rows);
00132 
00133   /* Adjust counts */
00134   *out_row_ctr += num_rows;
00135   upsample->rows_to_go -= num_rows;
00136   upsample->next_row_out += num_rows;
00137   /* When the buffer is emptied, declare this input row group consumed */
00138   if (upsample->next_row_out >= cinfo->max_v_samp_factor)
00139     (*in_row_group_ctr)++;
00140 }
00141 
00142 
00143 /*
00144  * These are the routines invoked by sep_upsample to upsample pixel values
00145  * of a single component.  One row group is processed per call.
00146  */
00147 
00148 
00149 /*
00150  * For full-size components, we just make color_buf[ci] point at the
00151  * input buffer, and thus avoid copying any data.  Note that this is
00152  * safe only because sep_upsample doesn't declare the input row group
00153  * "consumed" until we are done color converting and emitting it.
00154  */
00155 
00156 METHODDEF(void)
00157 fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00158                    JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00159 {
00160   *output_data_ptr = input_data;
00161 }
00162 
00163 
00164 /*
00165  * This is a no-op version used for "uninteresting" components.
00166  * These components will not be referenced by color conversion.
00167  */
00168 
00169 METHODDEF(void)
00170 noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00171                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00172 {
00173   *output_data_ptr = NULL;      /* safety check */
00174 }
00175 
00176 
00177 /*
00178  * This version handles any integral sampling ratios.
00179  * This is not used for typical JPEG files, so it need not be fast.
00180  * Nor, for that matter, is it particularly accurate: the algorithm is
00181  * simple replication of the input pixel onto the corresponding output
00182  * pixels.  The hi-falutin sampling literature refers to this as a
00183  * "box filter".  A box filter tends to introduce visible artifacts,
00184  * so if you are actually going to use 3:1 or 4:1 sampling ratios
00185  * you would be well advised to improve this code.
00186  */
00187 
00188 METHODDEF(void)
00189 int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00190               JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00191 {
00192   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
00193   JSAMPARRAY output_data = *output_data_ptr;
00194   register JSAMPROW inptr, outptr;
00195   register JSAMPLE invalue;
00196   register int h;
00197   JSAMPROW outend;
00198   int h_expand, v_expand;
00199   int inrow, outrow;
00200 
00201   h_expand = upsample->h_expand[compptr->component_index];
00202   v_expand = upsample->v_expand[compptr->component_index];
00203 
00204   inrow = outrow = 0;
00205   while (outrow < cinfo->max_v_samp_factor) {
00206     /* Generate one output row with proper horizontal expansion */
00207     inptr = input_data[inrow];
00208     outptr = output_data[outrow];
00209     outend = outptr + cinfo->output_width;
00210     while (outptr < outend) {
00211       invalue = *inptr++;       /* don't need GETJSAMPLE() here */
00212       for (h = h_expand; h > 0; h--) {
00213         *outptr++ = invalue;
00214       }
00215     }
00216     /* Generate any additional output rows by duplicating the first one */
00217     if (v_expand > 1) {
00218       jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
00219                         v_expand-1, cinfo->output_width);
00220     }
00221     inrow++;
00222     outrow += v_expand;
00223   }
00224 }
00225 
00226 
00227 /*
00228  * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
00229  * It's still a box filter.
00230  */
00231 
00232 METHODDEF(void)
00233 h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00234                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00235 {
00236   JSAMPARRAY output_data = *output_data_ptr;
00237   register JSAMPROW inptr, outptr;
00238   register JSAMPLE invalue;
00239   JSAMPROW outend;
00240   int inrow;
00241 
00242   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
00243     inptr = input_data[inrow];
00244     outptr = output_data[inrow];
00245     outend = outptr + cinfo->output_width;
00246     while (outptr < outend) {
00247       invalue = *inptr++;       /* don't need GETJSAMPLE() here */
00248       *outptr++ = invalue;
00249       *outptr++ = invalue;
00250     }
00251   }
00252 }
00253 
00254 
00255 /*
00256  * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
00257  * It's still a box filter.
00258  */
00259 
00260 METHODDEF(void)
00261 h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00262                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00263 {
00264   JSAMPARRAY output_data = *output_data_ptr;
00265   register JSAMPROW inptr, outptr;
00266   register JSAMPLE invalue;
00267   JSAMPROW outend;
00268   int inrow, outrow;
00269 
00270   inrow = outrow = 0;
00271   while (outrow < cinfo->max_v_samp_factor) {
00272     inptr = input_data[inrow];
00273     outptr = output_data[outrow];
00274     outend = outptr + cinfo->output_width;
00275     while (outptr < outend) {
00276       invalue = *inptr++;       /* don't need GETJSAMPLE() here */
00277       *outptr++ = invalue;
00278       *outptr++ = invalue;
00279     }
00280     jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
00281                       1, cinfo->output_width);
00282     inrow++;
00283     outrow += 2;
00284   }
00285 }
00286 
00287 
00288 /*
00289  * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
00290  *
00291  * The upsampling algorithm is linear interpolation between pixel centers,
00292  * also known as a "triangle filter".  This is a good compromise between
00293  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
00294  * of the way between input pixel centers.
00295  *
00296  * A note about the "bias" calculations: when rounding fractional values to
00297  * integer, we do not want to always round 0.5 up to the next integer.
00298  * If we did that, we'd introduce a noticeable bias towards larger values.
00299  * Instead, this code is arranged so that 0.5 will be rounded up or down at
00300  * alternate pixel locations (a simple ordered dither pattern).
00301  */
00302 
00303 METHODDEF(void)
00304 h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00305                      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00306 {
00307   JSAMPARRAY output_data = *output_data_ptr;
00308   register JSAMPROW inptr, outptr;
00309   register int invalue;
00310   register JDIMENSION colctr;
00311   int inrow;
00312 
00313   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
00314     inptr = input_data[inrow];
00315     outptr = output_data[inrow];
00316     /* Special case for first column */
00317     invalue = GETJSAMPLE(*inptr++);
00318     *outptr++ = (JSAMPLE) invalue;
00319     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
00320 
00321     for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
00322       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
00323       invalue = GETJSAMPLE(*inptr++) * 3;
00324       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
00325       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
00326     }
00327 
00328     /* Special case for last column */
00329     invalue = GETJSAMPLE(*inptr);
00330     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
00331     *outptr++ = (JSAMPLE) invalue;
00332   }
00333 }
00334 
00335 
00336 /*
00337  * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
00338  * Again a triangle filter; see comments for h2v1 case, above.
00339  *
00340  * It is OK for us to reference the adjacent input rows because we demanded
00341  * context from the main buffer controller (see initialization code).
00342  */
00343 
00344 METHODDEF(void)
00345 h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00346                      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
00347 {
00348   JSAMPARRAY output_data = *output_data_ptr;
00349   register JSAMPROW inptr0, inptr1, outptr;
00350 #if BITS_IN_JSAMPLE == 8
00351   register int thiscolsum, lastcolsum, nextcolsum;
00352 #else
00353   register INT32 thiscolsum, lastcolsum, nextcolsum;
00354 #endif
00355   register JDIMENSION colctr;
00356   int inrow, outrow, v;
00357 
00358   inrow = outrow = 0;
00359   while (outrow < cinfo->max_v_samp_factor) {
00360     for (v = 0; v < 2; v++) {
00361       /* inptr0 points to nearest input row, inptr1 points to next nearest */
00362       inptr0 = input_data[inrow];
00363       if (v == 0)               /* next nearest is row above */
00364         inptr1 = input_data[inrow-1];
00365       else                      /* next nearest is row below */
00366         inptr1 = input_data[inrow+1];
00367       outptr = output_data[outrow++];
00368 
00369       /* Special case for first column */
00370       thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
00371       nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
00372       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
00373       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
00374       lastcolsum = thiscolsum; thiscolsum = nextcolsum;
00375 
00376       for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
00377         /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
00378         /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
00379         nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
00380         *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
00381         *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
00382         lastcolsum = thiscolsum; thiscolsum = nextcolsum;
00383       }
00384 
00385       /* Special case for last column */
00386       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
00387       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
00388     }
00389     inrow++;
00390   }
00391 }
00392 
00393 
00394 /*
00395  * Module initialization routine for upsampling.
00396  */
00397 
00398 GLOBAL(void)
00399 jinit_upsampler (j_decompress_ptr cinfo)
00400 {
00401   my_upsample_ptr upsample;
00402   int ci;
00403   jpeg_component_info * compptr;
00404   boolean need_buffer, do_fancy;
00405   int h_in_group, v_in_group, h_out_group, v_out_group;
00406 
00407   upsample = (my_upsample_ptr)
00408     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00409                                 SIZEOF(my_upsampler));
00410   cinfo->upsample = (struct jpeg_upsampler *) upsample;
00411   upsample->pub.start_pass = start_pass_upsample;
00412   upsample->pub.upsample = sep_upsample;
00413   upsample->pub.need_context_rows = FALSE; /* until we find out differently */
00414 
00415   if (cinfo->CCIR601_sampling)  /* this isn't supported */
00416     ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
00417 
00418   /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
00419    * so don't ask for it.
00420    */
00421   do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
00422 
00423   /* Verify we can handle the sampling factors, select per-component methods,
00424    * and create storage as needed.
00425    */
00426   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00427        ci++, compptr++) {
00428     /* Compute size of an "input group" after IDCT scaling.  This many samples
00429      * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
00430      */
00431     h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
00432                  cinfo->min_DCT_scaled_size;
00433     v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
00434                  cinfo->min_DCT_scaled_size;
00435     h_out_group = cinfo->max_h_samp_factor;
00436     v_out_group = cinfo->max_v_samp_factor;
00437     upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
00438     need_buffer = TRUE;
00439     if (! compptr->component_needed) {
00440       /* Don't bother to upsample an uninteresting component. */
00441       upsample->methods[ci] = noop_upsample;
00442       need_buffer = FALSE;
00443     } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
00444       /* Fullsize components can be processed without any work. */
00445       upsample->methods[ci] = fullsize_upsample;
00446       need_buffer = FALSE;
00447     } else if (h_in_group * 2 == h_out_group &&
00448                v_in_group == v_out_group) {
00449       /* Special cases for 2h1v upsampling */
00450       if (do_fancy && compptr->downsampled_width > 2)
00451         upsample->methods[ci] = h2v1_fancy_upsample;
00452       else
00453         upsample->methods[ci] = h2v1_upsample;
00454     } else if (h_in_group * 2 == h_out_group &&
00455                v_in_group * 2 == v_out_group) {
00456       /* Special cases for 2h2v upsampling */
00457       if (do_fancy && compptr->downsampled_width > 2) {
00458         upsample->methods[ci] = h2v2_fancy_upsample;
00459         upsample->pub.need_context_rows = TRUE;
00460       } else
00461         upsample->methods[ci] = h2v2_upsample;
00462     } else if ((h_out_group % h_in_group) == 0 &&
00463                (v_out_group % v_in_group) == 0) {
00464       /* Generic integral-factors upsampling method */
00465       upsample->methods[ci] = int_upsample;
00466       upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
00467       upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
00468     } else
00469       ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
00470     if (need_buffer) {
00471       upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
00472         ((j_common_ptr) cinfo, JPOOL_IMAGE,
00473          (JDIMENSION) jround_up((long) cinfo->output_width,
00474                                 (long) cinfo->max_h_samp_factor),
00475          (JDIMENSION) cinfo->max_v_samp_factor);
00476     }
00477   }
00478 }


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