DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jdsample.cpp (18363B)


      1 /*
      2  * jdsample.c
      3  *
      4  * Copyright (C) 1991-1994, Thomas G. Lane.
      5  * This file is part of the Independent JPEG Group's software.
      6  * For conditions of distribution and use, see the accompanying README file.
      7  *
      8  * This file contains upsampling routines.
      9  *
     10  * Upsampling input data is counted in "row groups".  A row group
     11  * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
     12  * sample rows of each component.  Upsampling will normally produce
     13  * max_v_samp_factor pixel rows from each row group (but this could vary
     14  * if the upsampler is applying a scale factor of its own).
     15  *
     16  * An excellent reference for image resampling is
     17  *   Digital Image Warping, George Wolberg, 1990.
     18  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
     19  */
     20 
     21 #define JPEG_INTERNALS
     22 #include "jinclude.h"
     23 #include "jpeglib.h"
     24 
     25 
     26 /* Pointer to routine to upsample a single component */
     27 typedef JMETHOD ( void, upsample1_ptr,
     28                  ( j_decompress_ptr cinfo, jpeg_component_info * compptr,
     29                    JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) );
     30 
     31 /* Private subobject */
     32 
     33 typedef struct {
     34     struct jpeg_upsampler pub;  /* public fields */
     35 
     36     /* Color conversion buffer.  When using separate upsampling and color
     37      * conversion steps, this buffer holds one upsampled row group until it
     38      * has been color converted and output.
     39      * Note: we do not allocate any storage for component(s) which are full-size,
     40      * ie do not need rescaling.  The corresponding entry of color_buf[] is
     41      * simply set to point to the input data array, thereby avoiding copying.
     42      */
     43     JSAMPARRAY color_buf[MAX_COMPONENTS];
     44 
     45     /* Per-component upsampling method pointers */
     46     upsample1_ptr methods[MAX_COMPONENTS];
     47 
     48     int        next_row_out; /* counts rows emitted from color_buf */
     49     JDIMENSION rows_to_go;  /* counts rows remaining in image */
     50 
     51     /* Height of an input row group for each component. */
     52     int rowgroup_height[MAX_COMPONENTS];
     53 
     54     /* These arrays save pixel expansion factors so that int_expand need not
     55      * recompute them each time.  They are unused for other upsampling methods.
     56      */
     57     UINT8 h_expand[MAX_COMPONENTS];
     58     UINT8 v_expand[MAX_COMPONENTS];
     59 } my_upsampler;
     60 
     61 typedef my_upsampler * my_upsample_ptr;
     62 
     63 
     64 /*
     65  * Initialize for an upsampling pass.
     66  */
     67 
     68 METHODDEF void
     69 start_pass_upsample( j_decompress_ptr cinfo ) {
     70     my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
     71 
     72     /* Mark the conversion buffer empty */
     73     upsample->next_row_out = cinfo->max_v_samp_factor;
     74     /* Initialize total-height counter for detecting bottom of image */
     75     upsample->rows_to_go = cinfo->output_height;
     76 }
     77 
     78 
     79 /*
     80  * Control routine to do upsampling (and color conversion).
     81  *
     82  * In this version we upsample each component independently.
     83  * We upsample one row group into the conversion buffer, then apply
     84  * color conversion a row at a time.
     85  */
     86 
     87 METHODDEF void
     88 sep_upsample( j_decompress_ptr cinfo,
     89               JSAMPIMAGE input_buf, JDIMENSION * in_row_group_ctr,
     90               JDIMENSION in_row_groups_avail,
     91               JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
     92               JDIMENSION out_rows_avail ) {
     93     my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
     94     int ci;
     95     jpeg_component_info * compptr;
     96     JDIMENSION num_rows;
     97 
     98     /* Fill the conversion buffer, if it's empty */
     99     if ( upsample->next_row_out >= cinfo->max_v_samp_factor ) {
    100         for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    101               ci++, compptr++ ) {
    102             /* Invoke per-component upsample method.  Notice we pass a POINTER
    103              * to color_buf[ci], so that fullsize_upsample can change it.
    104              */
    105             ( *upsample->methods[ci] )( cinfo, compptr,
    106                                         input_buf[ci] + ( *in_row_group_ctr * upsample->rowgroup_height[ci] ),
    107                                         upsample->color_buf + ci );
    108         }
    109         upsample->next_row_out = 0;
    110     }
    111 
    112     /* Color-convert and emit rows */
    113 
    114     /* How many we have in the buffer: */
    115     num_rows = (JDIMENSION) ( cinfo->max_v_samp_factor - upsample->next_row_out );
    116     /* Not more than the distance to the end of the image.  Need this test
    117      * in case the image height is not a multiple of max_v_samp_factor:
    118      */
    119     if ( num_rows > upsample->rows_to_go ) {
    120         num_rows = upsample->rows_to_go;
    121     }
    122     /* And not more than what the client can accept: */
    123     out_rows_avail -= *out_row_ctr;
    124     if ( num_rows > out_rows_avail ) {
    125         num_rows = out_rows_avail;
    126     }
    127 
    128     ( *cinfo->cconvert->color_convert )( cinfo, upsample->color_buf,
    129                                          (JDIMENSION) upsample->next_row_out,
    130                                          output_buf + *out_row_ctr,
    131                                          (int) num_rows );
    132 
    133     /* Adjust counts */
    134     *out_row_ctr += num_rows;
    135     upsample->rows_to_go -= num_rows;
    136     upsample->next_row_out += num_rows;
    137     /* When the buffer is emptied, declare this input row group consumed */
    138     if ( upsample->next_row_out >= cinfo->max_v_samp_factor ) {
    139         ( *in_row_group_ctr )++;
    140     }
    141 }
    142 
    143 
    144 /*
    145  * These are the routines invoked by sep_upsample to upsample pixel values
    146  * of a single component.  One row group is processed per call.
    147  */
    148 
    149 
    150 /*
    151  * For full-size components, we just make color_buf[ci] point at the
    152  * input buffer, and thus avoid copying any data.  Note that this is
    153  * safe only because sep_upsample doesn't declare the input row group
    154  * "consumed" until we are done color converting and emitting it.
    155  */
    156 
    157 METHODDEF void
    158 fullsize_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    159                    JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    160     *output_data_ptr = input_data;
    161 }
    162 
    163 
    164 /*
    165  * This is a no-op version used for "uninteresting" components.
    166  * These components will not be referenced by color conversion.
    167  */
    168 
    169 METHODDEF void
    170 noop_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    171                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    172     *output_data_ptr = NULL;/* safety check */
    173 }
    174 
    175 
    176 /*
    177  * This version handles any integral sampling ratios.
    178  * This is not used for typical JPEG files, so it need not be fast.
    179  * Nor, for that matter, is it particularly accurate: the algorithm is
    180  * simple replication of the input pixel onto the corresponding output
    181  * pixels.  The hi-falutin sampling literature refers to this as a
    182  * "box filter".  A box filter tends to introduce visible artifacts,
    183  * so if you are actually going to use 3:1 or 4:1 sampling ratios
    184  * you would be well advised to improve this code.
    185  */
    186 
    187 METHODDEF void
    188 int_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    189               JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    190     my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
    191     JSAMPARRAY output_data = *output_data_ptr;
    192     register JSAMPROW inptr, outptr;
    193     register JSAMPLE invalue;
    194     register int h;
    195     JSAMPROW outend;
    196     int h_expand, v_expand;
    197     int inrow, outrow;
    198 
    199     h_expand = upsample->h_expand[compptr->component_index];
    200     v_expand = upsample->v_expand[compptr->component_index];
    201 
    202     inrow = outrow = 0;
    203     while ( outrow < cinfo->max_v_samp_factor ) {
    204         /* Generate one output row with proper horizontal expansion */
    205         inptr = input_data[inrow];
    206         outptr = output_data[outrow];
    207         outend = outptr + cinfo->output_width;
    208         while ( outptr < outend ) {
    209             invalue = *inptr++;/* don't need GETJSAMPLE() here */
    210             for ( h = h_expand; h > 0; h-- ) {
    211                 *outptr++ = invalue;
    212             }
    213         }
    214         /* Generate any additional output rows by duplicating the first one */
    215         if ( v_expand > 1 ) {
    216             jcopy_sample_rows( output_data, outrow, output_data, outrow + 1,
    217                                v_expand - 1, cinfo->output_width );
    218         }
    219         inrow++;
    220         outrow += v_expand;
    221     }
    222 }
    223 
    224 
    225 /*
    226  * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
    227  * It's still a box filter.
    228  */
    229 
    230 METHODDEF void
    231 h2v1_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    232                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    233     JSAMPARRAY output_data = *output_data_ptr;
    234     register JSAMPROW inptr, outptr;
    235     register JSAMPLE invalue;
    236     JSAMPROW outend;
    237     int inrow;
    238 
    239     for ( inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++ ) {
    240         inptr = input_data[inrow];
    241         outptr = output_data[inrow];
    242         outend = outptr + cinfo->output_width;
    243         while ( outptr < outend ) {
    244             invalue = *inptr++;/* don't need GETJSAMPLE() here */
    245             *outptr++ = invalue;
    246             *outptr++ = invalue;
    247         }
    248     }
    249 }
    250 
    251 
    252 /*
    253  * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
    254  * It's still a box filter.
    255  */
    256 
    257 METHODDEF void
    258 h2v2_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    259                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    260     JSAMPARRAY output_data = *output_data_ptr;
    261     register JSAMPROW inptr, outptr;
    262     register JSAMPLE invalue;
    263     JSAMPROW outend;
    264     int inrow, outrow;
    265 
    266     inrow = outrow = 0;
    267     while ( outrow < cinfo->max_v_samp_factor ) {
    268         inptr = input_data[inrow];
    269         outptr = output_data[outrow];
    270         outend = outptr + cinfo->output_width;
    271         while ( outptr < outend ) {
    272             invalue = *inptr++;/* don't need GETJSAMPLE() here */
    273             *outptr++ = invalue;
    274             *outptr++ = invalue;
    275         }
    276         jcopy_sample_rows( output_data, outrow, output_data, outrow + 1,
    277                            1, cinfo->output_width );
    278         inrow++;
    279         outrow += 2;
    280     }
    281 }
    282 
    283 
    284 /*
    285  * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
    286  *
    287  * The upsampling algorithm is linear interpolation between pixel centers,
    288  * also known as a "triangle filter".  This is a good compromise between
    289  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
    290  * of the way between input pixel centers.
    291  *
    292  * A note about the "bias" calculations: when rounding fractional values to
    293  * integer, we do not want to always round 0.5 up to the next integer.
    294  * If we did that, we'd introduce a noticeable bias towards larger values.
    295  * Instead, this code is arranged so that 0.5 will be rounded up or down at
    296  * alternate pixel locations (a simple ordered dither pattern).
    297  */
    298 
    299 METHODDEF void
    300 h2v1_fancy_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    301                      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    302     JSAMPARRAY output_data = *output_data_ptr;
    303     register JSAMPROW inptr, outptr;
    304     register int invalue;
    305     register JDIMENSION colctr;
    306     int inrow;
    307 
    308     for ( inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++ ) {
    309         inptr = input_data[inrow];
    310         outptr = output_data[inrow];
    311         /* Special case for first column */
    312         invalue = GETJSAMPLE( *inptr++ );
    313         *outptr++ = (JSAMPLE) invalue;
    314         *outptr++ = (JSAMPLE) ( ( invalue * 3 + GETJSAMPLE( *inptr ) + 2 ) >> 2 );
    315 
    316         for ( colctr = compptr->downsampled_width - 2; colctr > 0; colctr-- ) {
    317             /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
    318             invalue = GETJSAMPLE( *inptr++ ) * 3;
    319             *outptr++ = (JSAMPLE) ( ( invalue + GETJSAMPLE( inptr[-2] ) + 1 ) >> 2 );
    320             *outptr++ = (JSAMPLE) ( ( invalue + GETJSAMPLE( *inptr ) + 2 ) >> 2 );
    321         }
    322 
    323         /* Special case for last column */
    324         invalue = GETJSAMPLE( *inptr );
    325         *outptr++ = (JSAMPLE) ( ( invalue * 3 + GETJSAMPLE( inptr[-1] ) + 1 ) >> 2 );
    326         *outptr++ = (JSAMPLE) invalue;
    327     }
    328 }
    329 
    330 
    331 /*
    332  * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
    333  * Again a triangle filter; see comments for h2v1 case, above.
    334  *
    335  * It is OK for us to reference the adjacent input rows because we demanded
    336  * context from the main buffer controller (see initialization code).
    337  */
    338 
    339 METHODDEF void
    340 h2v2_fancy_upsample( j_decompress_ptr cinfo, jpeg_component_info * compptr,
    341                      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr ) {
    342     JSAMPARRAY output_data = *output_data_ptr;
    343     register JSAMPROW inptr0, inptr1, outptr;
    344 #if BITS_IN_JSAMPLE == 8
    345     register int thiscolsum, lastcolsum, nextcolsum;
    346 #else
    347     register INT32 thiscolsum, lastcolsum, nextcolsum;
    348 #endif
    349     register JDIMENSION colctr;
    350     int inrow, outrow, v;
    351 
    352     inrow = outrow = 0;
    353     while ( outrow < cinfo->max_v_samp_factor ) {
    354         for ( v = 0; v < 2; v++ ) {
    355             /* inptr0 points to nearest input row, inptr1 points to next nearest */
    356             inptr0 = input_data[inrow];
    357             if ( v == 0 ) {/* next nearest is row above */
    358                 inptr1 = input_data[inrow - 1];
    359             } else {/* next nearest is row below */
    360                 inptr1 = input_data[inrow + 1];
    361             }
    362             outptr = output_data[outrow++];
    363 
    364             /* Special case for first column */
    365             thiscolsum = GETJSAMPLE( *inptr0++ ) * 3 + GETJSAMPLE( *inptr1++ );
    366             nextcolsum = GETJSAMPLE( *inptr0++ ) * 3 + GETJSAMPLE( *inptr1++ );
    367             *outptr++ = (JSAMPLE) ( ( thiscolsum * 4 + 8 ) >> 4 );
    368             *outptr++ = (JSAMPLE) ( ( thiscolsum * 3 + nextcolsum + 7 ) >> 4 );
    369             lastcolsum = thiscolsum;
    370             thiscolsum = nextcolsum;
    371 
    372             for ( colctr = compptr->downsampled_width - 2; colctr > 0; colctr-- ) {
    373                 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
    374                 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
    375                 nextcolsum = GETJSAMPLE( *inptr0++ ) * 3 + GETJSAMPLE( *inptr1++ );
    376                 *outptr++ = (JSAMPLE) ( ( thiscolsum * 3 + lastcolsum + 8 ) >> 4 );
    377                 *outptr++ = (JSAMPLE) ( ( thiscolsum * 3 + nextcolsum + 7 ) >> 4 );
    378                 lastcolsum = thiscolsum;
    379                 thiscolsum = nextcolsum;
    380             }
    381 
    382             /* Special case for last column */
    383             *outptr++ = (JSAMPLE) ( ( thiscolsum * 3 + lastcolsum + 8 ) >> 4 );
    384             *outptr++ = (JSAMPLE) ( ( thiscolsum * 4 + 7 ) >> 4 );
    385         }
    386         inrow++;
    387     }
    388 }
    389 
    390 
    391 /*
    392  * Module initialization routine for upsampling.
    393  */
    394 
    395 GLOBAL void
    396 jinit_upsampler( j_decompress_ptr cinfo ) {
    397     my_upsample_ptr upsample;
    398     int ci;
    399     jpeg_component_info * compptr;
    400     boolean need_buffer, do_fancy;
    401     int h_in_group, v_in_group, h_out_group, v_out_group;
    402 
    403     upsample = (my_upsample_ptr)
    404                ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    405                                             SIZEOF( my_upsampler ) );
    406     cinfo->upsample = (struct jpeg_upsampler *) upsample;
    407     upsample->pub.start_pass = start_pass_upsample;
    408     upsample->pub.upsample = sep_upsample;
    409     upsample->pub.need_context_rows = FALSE;/* until we find out differently */
    410 
    411     if ( cinfo->CCIR601_sampling ) {/* this isn't supported */
    412         ERREXIT( cinfo, JERR_CCIR601_NOTIMPL );
    413     }
    414 
    415     /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
    416      * so don't ask for it.
    417      */
    418     do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
    419 
    420     /* Verify we can handle the sampling factors, select per-component methods,
    421      * and create storage as needed.
    422      */
    423     for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    424           ci++, compptr++ ) {
    425         /* Compute size of an "input group" after IDCT scaling.  This many samples
    426          * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
    427          */
    428         h_in_group = ( compptr->h_samp_factor * compptr->DCT_scaled_size ) /
    429                      cinfo->min_DCT_scaled_size;
    430         v_in_group = ( compptr->v_samp_factor * compptr->DCT_scaled_size ) /
    431                      cinfo->min_DCT_scaled_size;
    432         h_out_group = cinfo->max_h_samp_factor;
    433         v_out_group = cinfo->max_v_samp_factor;
    434         upsample->rowgroup_height[ci] = v_in_group;/* save for use later */
    435         need_buffer = TRUE;
    436         if ( !compptr->component_needed ) {
    437             /* Don't bother to upsample an uninteresting component. */
    438             upsample->methods[ci] = noop_upsample;
    439             need_buffer = FALSE;
    440         } else if ( h_in_group == h_out_group && v_in_group == v_out_group ) {
    441             /* Fullsize components can be processed without any work. */
    442             upsample->methods[ci] = fullsize_upsample;
    443             need_buffer = FALSE;
    444         } else if ( h_in_group * 2 == h_out_group &&
    445                     v_in_group == v_out_group ) {
    446             /* Special cases for 2h1v upsampling */
    447             if ( ( do_fancy ) && ( compptr->downsampled_width > 2 ) ) {
    448                 upsample->methods[ci] = h2v1_fancy_upsample;
    449             } else {
    450                 upsample->methods[ci] = h2v1_upsample;
    451             }
    452         } else if ( h_in_group * 2 == h_out_group &&
    453                     v_in_group * 2 == v_out_group ) {
    454             /* Special cases for 2h2v upsampling */
    455             if ( ( do_fancy ) && ( compptr->downsampled_width > 2 ) ) {
    456                 upsample->methods[ci] = h2v2_fancy_upsample;
    457                 upsample->pub.need_context_rows = TRUE;
    458             } else {
    459                 upsample->methods[ci] = h2v2_upsample;
    460             }
    461         } else if ( ( h_out_group % h_in_group ) == 0 &&
    462                    ( v_out_group % v_in_group ) == 0 ) {
    463             /* Generic integral-factors upsampling method */
    464             upsample->methods[ci] = int_upsample;
    465             upsample->h_expand[ci] = (UINT8) ( h_out_group / h_in_group );
    466             upsample->v_expand[ci] = (UINT8) ( v_out_group / v_in_group );
    467         } else {
    468             ERREXIT( cinfo, JERR_FRACT_SAMPLE_NOTIMPL );
    469         }
    470         if ( need_buffer ) {
    471             upsample->color_buf[ci] = ( *cinfo->mem->alloc_sarray )
    472                                       ( (j_common_ptr) cinfo, JPOOL_IMAGE,
    473                                        (JDIMENSION) jround_up( (long) cinfo->output_width,
    474                                                               (long) cinfo->max_h_samp_factor ),
    475                                        (JDIMENSION) cinfo->max_v_samp_factor );
    476         }
    477     }
    478 }