jcsample.c File Reference

#include "jinclude.h"
#include "jpeglib.h"

Go to the source code of this file.

Data Structures

struct  my_downsampler

Defines

#define JPEG_INTERNALS

Typedefs

typedef my_downsamplermy_downsample_ptr

Functions

 expand_right_edge (JSAMPARRAY image_data, int num_rows, JDIMENSION input_cols, JDIMENSION output_cols)
 fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 int_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)
 jinit_downsampler (j_compress_ptr cinfo)
typedef JMETHOD (void, downsample1_ptr,(j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data))
 sep_downsample (j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_index, JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
 start_pass_downsample (j_compress_ptr cinfo)


Define Documentation

#define JPEG_INTERNALS

Definition at line 48 of file jcsample.c.


Typedef Documentation

Definition at line 67 of file jcsample.c.


Function Documentation

expand_right_edge ( JSAMPARRAY  image_data,
int  num_rows,
JDIMENSION  input_cols,
JDIMENSION  output_cols 
)

Definition at line 87 of file jcsample.c.

Referenced by fullsize_downsample(), fullsize_smooth_downsample(), h2v1_downsample(), h2v2_downsample(), h2v2_smooth_downsample(), and int_downsample().

00089 {
00090   register JSAMPROW ptr;
00091   register JSAMPLE pixval;
00092   register int count;
00093   int row;
00094   int numcols = (int) (output_cols - input_cols);
00095 
00096   if (numcols > 0) {
00097     for (row = 0; row < num_rows; row++) {
00098       ptr = image_data[row] + input_cols;
00099       pixval = ptr[-1];     /* don't need GETJSAMPLE() here */
00100       for (count = numcols; count > 0; count--)
00101     *ptr++ = pixval;
00102     }
00103   }
00104 }

fullsize_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 187 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and jcopy_sample_rows().

Referenced by jinit_downsampler().

00189 {
00190   /* Copy the data */
00191   jcopy_sample_rows(input_data, 0, output_data, 0,
00192             cinfo->max_v_samp_factor, cinfo->image_width);
00193   /* Edge-expand */
00194   expand_right_edge(output_data, cinfo->max_v_samp_factor,
00195             cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
00196 }

fullsize_smooth_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 392 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and GETJSAMPLE.

Referenced by jinit_downsampler().

00394 {
00395   int outrow;
00396   JDIMENSION colctr;
00397   JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
00398   register JSAMPROW inptr, above_ptr, below_ptr, outptr;
00399   INT32 membersum, neighsum, memberscale, neighscale;
00400   int colsum, lastcolsum, nextcolsum;
00401 
00402   /* Expand input data enough to let all the output samples be generated
00403    * by the standard loop.  Special-casing padded output would be more
00404    * efficient.
00405    */
00406   expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
00407             cinfo->image_width, output_cols);
00408 
00409   /* Each of the eight neighbor pixels contributes a fraction SF to the
00410    * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
00411    * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
00412    * Also recall that SF = smoothing_factor / 1024.
00413    */
00414 
00415   memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
00416   neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
00417 
00418   for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
00419     outptr = output_data[outrow];
00420     inptr = input_data[outrow];
00421     above_ptr = input_data[outrow-1];
00422     below_ptr = input_data[outrow+1];
00423 
00424     /* Special case for first column */
00425     colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
00426          GETJSAMPLE(*inptr);
00427     membersum = GETJSAMPLE(*inptr++);
00428     nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
00429          GETJSAMPLE(*inptr);
00430     neighsum = colsum + (colsum - membersum) + nextcolsum;
00431     membersum = membersum * memberscale + neighsum * neighscale;
00432     *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
00433     lastcolsum = colsum; colsum = nextcolsum;
00434 
00435     for (colctr = output_cols - 2; colctr > 0; colctr--) {
00436       membersum = GETJSAMPLE(*inptr++);
00437       above_ptr++; below_ptr++;
00438       nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
00439            GETJSAMPLE(*inptr);
00440       neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
00441       membersum = membersum * memberscale + neighsum * neighscale;
00442       *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
00443       lastcolsum = colsum; colsum = nextcolsum;
00444     }
00445 
00446     /* Special case for last column */
00447     membersum = GETJSAMPLE(*inptr);
00448     neighsum = lastcolsum + (colsum - membersum) + colsum;
00449     membersum = membersum * memberscale + neighsum * neighscale;
00450     *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
00451 
00452   }
00453 }

h2v1_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 212 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and GETJSAMPLE.

Referenced by jinit_downsampler().

00214 {
00215   int outrow;
00216   JDIMENSION outcol;
00217   JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
00218   register JSAMPROW inptr, outptr;
00219   register int bias;
00220 
00221   /* Expand input data enough to let all the output samples be generated
00222    * by the standard loop.  Special-casing padded output would be more
00223    * efficient.
00224    */
00225   expand_right_edge(input_data, cinfo->max_v_samp_factor,
00226             cinfo->image_width, output_cols * 2);
00227 
00228   for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
00229     outptr = output_data[outrow];
00230     inptr = input_data[outrow];
00231     bias = 0;           /* bias = 0,1,0,1,... for successive samples */
00232     for (outcol = 0; outcol < output_cols; outcol++) {
00233       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
00234                   + bias) >> 1);
00235       bias ^= 1;        /* 0=>1, 1=>0 */
00236       inptr += 2;
00237     }
00238   }
00239 }

h2v2_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 249 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and GETJSAMPLE.

Referenced by jinit_downsampler().

00251 {
00252   int inrow, outrow;
00253   JDIMENSION outcol;
00254   JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
00255   register JSAMPROW inptr0, inptr1, outptr;
00256   register int bias;
00257 
00258   /* Expand input data enough to let all the output samples be generated
00259    * by the standard loop.  Special-casing padded output would be more
00260    * efficient.
00261    */
00262   expand_right_edge(input_data, cinfo->max_v_samp_factor,
00263             cinfo->image_width, output_cols * 2);
00264 
00265   inrow = 0;
00266   for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
00267     outptr = output_data[outrow];
00268     inptr0 = input_data[inrow];
00269     inptr1 = input_data[inrow+1];
00270     bias = 1;           /* bias = 1,2,1,2,... for successive samples */
00271     for (outcol = 0; outcol < output_cols; outcol++) {
00272       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
00273                   GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
00274                   + bias) >> 2);
00275       bias ^= 3;        /* 1=>2, 2=>1 */
00276       inptr0 += 2; inptr1 += 2;
00277     }
00278     inrow += 2;
00279   }
00280 }

h2v2_smooth_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 292 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and GETJSAMPLE.

Referenced by jinit_downsampler().

00294 {
00295   int inrow, outrow;
00296   JDIMENSION colctr;
00297   JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
00298   register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
00299   INT32 membersum, neighsum, memberscale, neighscale;
00300 
00301   /* Expand input data enough to let all the output samples be generated
00302    * by the standard loop.  Special-casing padded output would be more
00303    * efficient.
00304    */
00305   expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
00306             cinfo->image_width, output_cols * 2);
00307 
00308   /* We don't bother to form the individual "smoothed" input pixel values;
00309    * we can directly compute the output which is the average of the four
00310    * smoothed values.  Each of the four member pixels contributes a fraction
00311    * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
00312    * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
00313    * output.  The four corner-adjacent neighbor pixels contribute a fraction
00314    * SF to just one smoothed pixel, or SF/4 to the final output; while the
00315    * eight edge-adjacent neighbors contribute SF to each of two smoothed
00316    * pixels, or SF/2 overall.  In order to use integer arithmetic, these
00317    * factors are scaled by 2^16 = 65536.
00318    * Also recall that SF = smoothing_factor / 1024.
00319    */
00320 
00321   memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
00322   neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
00323 
00324   inrow = 0;
00325   for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
00326     outptr = output_data[outrow];
00327     inptr0 = input_data[inrow];
00328     inptr1 = input_data[inrow+1];
00329     above_ptr = input_data[inrow-1];
00330     below_ptr = input_data[inrow+2];
00331 
00332     /* Special case for first column: pretend column -1 is same as column 0 */
00333     membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
00334         GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
00335     neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
00336            GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
00337            GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
00338            GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
00339     neighsum += neighsum;
00340     neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
00341         GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
00342     membersum = membersum * memberscale + neighsum * neighscale;
00343     *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
00344     inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
00345 
00346     for (colctr = output_cols - 2; colctr > 0; colctr--) {
00347       /* sum of pixels directly mapped to this output element */
00348       membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
00349           GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
00350       /* sum of edge-neighbor pixels */
00351       neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
00352          GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
00353          GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
00354          GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
00355       /* The edge-neighbors count twice as much as corner-neighbors */
00356       neighsum += neighsum;
00357       /* Add in the corner-neighbors */
00358       neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
00359           GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
00360       /* form final output scaled up by 2^16 */
00361       membersum = membersum * memberscale + neighsum * neighscale;
00362       /* round, descale and output it */
00363       *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
00364       inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
00365     }
00366 
00367     /* Special case for last column */
00368     membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
00369         GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
00370     neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
00371            GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
00372            GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
00373            GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
00374     neighsum += neighsum;
00375     neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
00376         GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
00377     membersum = membersum * memberscale + neighsum * neighscale;
00378     *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
00379 
00380     inrow += 2;
00381   }
00382 }

int_downsample ( j_compress_ptr  cinfo,
jpeg_component_info compptr,
JSAMPARRAY  input_data,
JSAMPARRAY  output_data 
)

Definition at line 140 of file jcsample.c.

References DCTSIZE, expand_right_edge(), and GETJSAMPLE.

Referenced by jinit_downsampler().

00142 {
00143   int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
00144   JDIMENSION outcol, outcol_h;  /* outcol_h == outcol*h_expand */
00145   JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
00146   JSAMPROW inptr, outptr;
00147   INT32 outvalue;
00148 
00149   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
00150   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
00151   numpix = h_expand * v_expand;
00152   numpix2 = numpix/2;
00153 
00154   /* Expand input data enough to let all the output samples be generated
00155    * by the standard loop.  Special-casing padded output would be more
00156    * efficient.
00157    */
00158   expand_right_edge(input_data, cinfo->max_v_samp_factor,
00159             cinfo->image_width, output_cols * h_expand);
00160 
00161   inrow = 0;
00162   for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
00163     outptr = output_data[outrow];
00164     for (outcol = 0, outcol_h = 0; outcol < output_cols;
00165      outcol++, outcol_h += h_expand) {
00166       outvalue = 0;
00167       for (v = 0; v < v_expand; v++) {
00168     inptr = input_data[inrow+v] + outcol_h;
00169     for (h = 0; h < h_expand; h++) {
00170       outvalue += (INT32) GETJSAMPLE(*inptr++);
00171     }
00172       }
00173       *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
00174     }
00175     inrow += v_expand;
00176   }
00177 }

jinit_downsampler ( j_compress_ptr  cinfo  ) 

Definition at line 464 of file jcsample.c.

References compptr, ERREXIT, FALSE, fullsize_downsample(), fullsize_smooth_downsample(), h2v1_downsample(), h2v2_downsample(), h2v2_smooth_downsample(), jpeg_component_info::h_samp_factor, int_downsample(), JPOOL_IMAGE, my_downsampler::methods, jpeg_downsampler::need_context_rows, my_downsampler::pub, sep_downsample(), SIZEOF, start_pass_downsample(), TRACEMS, TRUE, and jpeg_component_info::v_samp_factor.

Referenced by jinit_compress_master().

00465 {
00466   my_downsample_ptr downsample;
00467   int ci;
00468   jpeg_component_info * compptr;
00469   boolean smoothok = TRUE;
00470 
00471   downsample = (my_downsample_ptr)
00472     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00473                 SIZEOF(my_downsampler));
00474   cinfo->downsample = (struct jpeg_downsampler *) downsample;
00475   downsample->pub.start_pass = start_pass_downsample;
00476   downsample->pub.downsample = sep_downsample;
00477   downsample->pub.need_context_rows = FALSE;
00478 
00479   if (cinfo->CCIR601_sampling)
00480     ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
00481 
00482   /* Verify we can handle the sampling factors, and set up method pointers */
00483   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00484        ci++, compptr++) {
00485     if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
00486     compptr->v_samp_factor == cinfo->max_v_samp_factor) {
00487 #ifdef INPUT_SMOOTHING_SUPPORTED
00488       if (cinfo->smoothing_factor) {
00489     downsample->methods[ci] = fullsize_smooth_downsample;
00490     downsample->pub.need_context_rows = TRUE;
00491       } else
00492 #endif
00493     downsample->methods[ci] = fullsize_downsample;
00494     } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
00495            compptr->v_samp_factor == cinfo->max_v_samp_factor) {
00496       smoothok = FALSE;
00497       downsample->methods[ci] = h2v1_downsample;
00498     } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
00499            compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
00500 #ifdef INPUT_SMOOTHING_SUPPORTED
00501       if (cinfo->smoothing_factor) {
00502     downsample->methods[ci] = h2v2_smooth_downsample;
00503     downsample->pub.need_context_rows = TRUE;
00504       } else
00505 #endif
00506     downsample->methods[ci] = h2v2_downsample;
00507     } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
00508            (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
00509       smoothok = FALSE;
00510       downsample->methods[ci] = int_downsample;
00511     } else
00512       ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
00513   }
00514 
00515 #ifdef INPUT_SMOOTHING_SUPPORTED
00516   if (cinfo->smoothing_factor && !smoothok)
00517     TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
00518 #endif
00519 }

typedef JMETHOD ( void  ,
downsample1_ptr  ,
(j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)   
)

sep_downsample ( j_compress_ptr  cinfo,
JSAMPIMAGE  input_buf,
JDIMENSION  in_row_index,
JSAMPIMAGE  output_buf,
JDIMENSION  out_row_group_index 
)

Definition at line 114 of file jcsample.c.

References compptr, my_downsampler::methods, and jpeg_component_info::v_samp_factor.

Referenced by jinit_downsampler().

00117 {
00118   my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
00119   int ci;
00120   jpeg_component_info * compptr;
00121   JSAMPARRAY in_ptr, out_ptr;
00122 
00123   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00124        ci++, compptr++) {
00125     in_ptr = input_buf[ci] + in_row_index;
00126     out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
00127     (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
00128   }
00129 }

start_pass_downsample ( j_compress_ptr  cinfo  ) 

Definition at line 75 of file jcsample.c.

Referenced by jinit_downsampler().

00076 {
00077   /* no work for now */
00078 }


Generated on Fri Feb 19 02:31:02 2010 for AVR32 - IJG JPEG Decoder Example by  doxygen 1.5.5