home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / DVPG30FS.ZIP / JDSAMPLE.C < prev    next >
C/C++ Source or Header  |  1993-12-04  |  13KB  |  375 lines

  1. /*
  2.  * jdsample.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 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.  * These routines are invoked via the upsample and
  10.  * upsample_init/term methods.
  11.  *
  12.  * An excellent reference for image resampling is
  13.  *   Digital Image Warping, George Wolberg, 1990.
  14.  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  15.  */
  16.  
  17. #include "jinclude.h"
  18. #include "viewdef.h"
  19.  
  20. extern int more_defaults, jpeg_type;
  21. extern int video_resolution;
  22.  
  23. /*
  24.  * Initialize for upsampling a scan.
  25.  */
  26.  
  27. METHODDEF void
  28. upsample_init (decompress_info_ptr cinfo)
  29. {
  30.   /* no work for now */
  31. }
  32.  
  33.  
  34. /*
  35.  * Upsample pixel values of a single component.
  36.  * This version handles any integral sampling ratios.
  37.  *
  38.  * This is not used for typical JPEG files, so it need not be fast.
  39.  * Nor, for that matter, is it particularly accurate: the algorithm is
  40.  * simple replication of the input pixel onto the corresponding output
  41.  * pixels.  The hi-falutin sampling literature refers to this as a
  42.  * "box filter".  A box filter tends to introduce visible artifacts,
  43.  * so if you are actually going to use 3:1 or 4:1 sampling ratios
  44.  * you would be well advised to improve this code.
  45.  */
  46.  
  47. METHODDEF void
  48. int_upsample (decompress_info_ptr cinfo, int which_component,
  49.           long input_cols, int input_rows,
  50.             long output_cols, int output_rows,
  51.           JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  52.             JSAMPARRAY output_data)
  53. {
  54.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  55.   register JSAMPROW inptr, outptr;
  56.   register JSAMPLE invalue;
  57.   register short h_expand, h;
  58.   short v_expand, v;
  59.   int inrow, outrow;
  60.   register int incol;
  61.  
  62. #ifdef DEBUG            /* for debugging pipeline controller */
  63.   if (input_rows != compptr->v_samp_factor ||
  64.         output_rows != cinfo->max_v_samp_factor ||
  65.         (input_cols % compptr->h_samp_factor) != 0 ||
  66.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  67.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  68.     ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  69. #endif
  70.  
  71.   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  72.   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  73.  
  74.   outrow = 0;
  75.   for (inrow = 0; inrow < input_rows; inrow++) {
  76.     for (v = 0; v < v_expand; v++) {
  77.       inptr = input_data[inrow];
  78.       outptr = output_data[outrow++];
  79.       for (incol = 0; incol < input_cols; incol++) {
  80.     invalue = GETJSAMPLE(*inptr++);
  81.     for (h = 0; h < h_expand; h++) {
  82.       *outptr++ = invalue;
  83.     }
  84.       }
  85.     }
  86.   }
  87. }
  88.  
  89.  
  90. /*
  91.  * Upsample pixel values of a single component.
  92.  * This version handles the common case of 2:1 horizontal and 1:1 vertical.
  93.  *
  94.  * The upsampling algorithm is linear interpolation between pixel centers,
  95.  * also known as a "triangle filter".  This is a good compromise between
  96.  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  97.  * of the way between input pixel centers.
  98.  */
  99.  
  100. METHODDEF void
  101. h2v1_upsample (decompress_info_ptr cinfo, int which_component,
  102.            long input_cols, int input_rows,
  103.            long output_cols, int output_rows,
  104.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  105.              JSAMPARRAY output_data)
  106. {
  107.   register JSAMPROW inptr, outptr;
  108.   register int invalue;
  109.   int inrow;
  110.   register int colctr;
  111.  
  112. #ifdef DEBUG            /* for debugging pipeline controller */
  113.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  114.   if (input_rows != compptr->v_samp_factor ||
  115.         output_rows != cinfo->max_v_samp_factor ||
  116.         (input_cols % compptr->h_samp_factor) != 0 ||
  117.         (output_cols % cinfo->max_h_samp_factor) != 0 ||
  118.         output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  119.      ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  120. #endif
  121.  
  122.   for (inrow = 0; inrow < input_rows; inrow++) {
  123.      inptr = input_data[inrow];
  124.      outptr = output_data[inrow];
  125.     /* Special case for first column */
  126.     invalue = GETJSAMPLE(*inptr++);
  127.     *outptr++ = (JSAMPLE) invalue;
  128.     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  129.  
  130.     for (colctr = input_cols - 2; colctr > 0; colctr--) {
  131.       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  132.       invalue = GETJSAMPLE(*inptr++) * 3;
  133.       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 2) >> 2);
  134.       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  135.     }
  136.  
  137.      /* Special case for last column */
  138.      invalue = GETJSAMPLE(*inptr);
  139.      *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 2) >> 2);
  140.      *outptr++ = (JSAMPLE) invalue;
  141.   }
  142. }
  143.  
  144. /*
  145.  * Upsample pixel values of a single component.
  146.  * This version handles the common case of 2:1 horizontal and 2:1 vertical.
  147.  *
  148.  * The upsampling algorithm is linear interpolation between pixel centers,
  149.  * also known as a "triangle filter".  This is a good compromise between
  150.  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  151.  * of the way between input pixel centers.
  152.  */
  153.  
  154. METHODDEF void
  155. h2v2_upsample (decompress_info_ptr cinfo, int which_component,
  156.            long input_cols, int input_rows,
  157.            long output_cols, int output_rows,
  158.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  159.            JSAMPARRAY output_data)
  160. {
  161.   register JSAMPROW inptr0, inptr1, outptr;
  162. #ifdef EIGHT_BIT_SAMPLES
  163.   register int thiscolsum, lastcolsum, nextcolsum;
  164. #else
  165.   register INT32 thiscolsum, lastcolsum, nextcolsum;
  166. #endif
  167.   int inrow, outrow, v;
  168.   register long colctr;
  169.  
  170. #ifdef DEBUG            /* for debugging pipeline controller */
  171.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  172.   if (input_rows != compptr->v_samp_factor ||
  173.       output_rows != cinfo->max_v_samp_factor ||
  174.       (input_cols % compptr->h_samp_factor) != 0 ||
  175.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  176.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  177.     ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  178. #endif
  179.  
  180.   outrow = 0;
  181.   for (inrow = 0; inrow < input_rows; inrow++) {
  182.      for (v = 0; v < 2; v++) {
  183.       /* inptr0 points to nearest input row, inptr1 points to next nearest */
  184.         inptr0 = input_data[inrow];
  185.       if (v == 0) {        /* next nearest is row above */
  186.     if (inrow == 0)
  187.       inptr1 = above[input_rows-1];
  188.     else
  189.       inptr1 = input_data[inrow-1];
  190.         } else {            /* next nearest is row below */
  191.     if (inrow == input_rows-1)
  192.       inptr1 = below[0];
  193.     else
  194.       inptr1 = input_data[inrow+1];
  195.         }
  196.         outptr = output_data[outrow++];
  197.  
  198.         /* Special case for first column */
  199.         thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  200.         nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  201.         *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  202.         *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
  203.         lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  204.  
  205.         for (colctr = input_cols - 2; colctr > 0; colctr--) {
  206.     /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  207.     /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  208.     nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  209.     *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  210.     *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
  211.     lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  212.         }
  213.  
  214.         /* Special case for last column */
  215.         *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  216.         *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  217.      }
  218.   }
  219. }
  220.  
  221.  
  222.  
  223. /*
  224.  * Upsample pixel values of a single component.
  225.  * This version handles the common case of 2:1 horizontal and 2:1 vertical.
  226.  *
  227.  * The upsampling algorithm a quick and dirty box filter
  228.  *  It is 15% faster for non-quantized images but an error analysis shows
  229.  *  that some pixels are off by 100! But most errors are < 30 and about 75%
  230.  *  of them are < +/- 8 error (same sort of error as 16 bit math gives
  231.  */
  232.  
  233. METHODDEF void
  234. h2v2_quick_upsample (decompress_info_ptr cinfo, int which_component,
  235.              long input_cols, int input_rows,
  236.            long output_cols, int output_rows,
  237.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  238.              JSAMPARRAY output_data)
  239. {
  240.   register JSAMPROW inptr0, outptr;
  241.   int inrow, outrow, nextcolsum;
  242.   register int colctr;
  243.  
  244. outrow = 0;
  245. for (inrow = 0; inrow < input_rows; inrow++) {
  246.     inptr0 = input_data[inrow];
  247.     outptr = output_data[outrow];
  248.  
  249.     *outptr++ =  GETJSAMPLE(*inptr0);
  250.     *outptr++ =  GETJSAMPLE(*inptr0++);
  251.  
  252.     for (colctr = input_cols - 2; colctr > 0; colctr--) {
  253.         nextcolsum = GETJSAMPLE(*inptr0++);
  254.         *outptr++ =  (JSAMPLE) nextcolsum;
  255.         *outptr++ =  (JSAMPLE) nextcolsum;
  256.         }
  257.  
  258.     nextcolsum = (nextcolsum * 3) >> 4;
  259.     *outptr++ = (JSAMPLE) nextcolsum;
  260.     *outptr++ = (JSAMPLE) nextcolsum >> 1;
  261.  
  262.     inptr0 = output_data[outrow++];
  263.     outptr = output_data[outrow++];
  264.     _fmemcpy(outptr, inptr0, (size_t)input_cols << 1);
  265.     }
  266. }
  267.  
  268.  
  269. /*
  270.  * NO Upsampling pixel values of a single component.
  271.  * This version handles the common case of 2:1 horizontal and 2:1 vertical.
  272.  *  The upsampling is left for the color conversion routine to do
  273.  *  Just copy the data to the output struct leaving every 2nd line blank
  274.  *
  275.  * The upsampling algorithm a quick and dirty box filter
  276.  *  It is 15% faster for non-quantized images but an error analysis shows
  277.  *  that some pixels are off by 100! But most errors are < 30 and about 75%
  278.  *  of them are < +/- 8 error (same sort of error as 16 bit math gives
  279.  */
  280.  
  281. METHODDEF void
  282. h2v2_no_upsample (decompress_info_ptr cinfo, int which_component,
  283.              long input_cols, int input_rows,
  284.              long output_cols, int output_rows,
  285.              JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  286.              JSAMPARRAY output_data)
  287. {
  288.   register JSAMPROW inptr0, outptr;
  289.   int inrow;
  290.  
  291.   for (inrow = 0; inrow < input_rows; inrow++) {
  292.         inptr0 = input_data[inrow];
  293.         outptr = output_data[inrow << 1];
  294.         _fmemcpy(outptr, inptr0, (size_t)input_cols);
  295.   }
  296. }
  297.  
  298.  
  299. /*
  300.  * Upsample pixel values of a single component.
  301.  * This version handles the special case of a full-size component.
  302.  */
  303.  
  304. METHODDEF void
  305. fullsize_upsample (decompress_info_ptr cinfo, int which_component,
  306.             long input_cols, int input_rows,
  307.            long output_cols, int output_rows,
  308.             JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  309.            JSAMPARRAY output_data)
  310. {
  311. #ifdef DEBUG            /* for debugging pipeline controller */
  312.   if (input_cols != output_cols || input_rows != output_rows)
  313.     ERREXIT(cinfo->emethods, "Pipeline controller messed up");
  314. #endif
  315.  
  316.   jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
  317. }
  318.  
  319.  
  320.  
  321. /*
  322.  * Clean up after a scan.
  323.  */
  324.  
  325. METHODDEF void
  326. upsample_term (decompress_info_ptr cinfo)
  327. {
  328.   /* no work for now */
  329. }
  330.  
  331.  
  332.  
  333. /*
  334.  * The method selection routine for upsampling.
  335.  * Note that we must select a routine for each component.
  336.  */
  337.  
  338. GLOBAL void
  339. jselupsample (decompress_info_ptr cinfo)
  340. {
  341.   short ci;
  342.   jpeg_component_info * compptr;
  343.  
  344.   if (cinfo->CCIR601_sampling)
  345.      ERREXIT(cinfo->emethods, "CCIR601 upsampling not implemented yet");
  346.  
  347.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  348.      compptr = cinfo->cur_comp_info[ci];
  349.      if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  350.     compptr->v_samp_factor == cinfo->max_v_samp_factor)
  351.         cinfo->methods->upsample[ci] = fullsize_upsample;
  352.      else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  353.           compptr->v_samp_factor == cinfo->max_v_samp_factor)
  354.         cinfo->methods->upsample[ci] = h2v1_upsample;
  355.      else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  356.           compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor){
  357.             if (more_defaults & high_jpeg_quality)
  358.                 cinfo->methods->upsample[ci] = h2v2_upsample;
  359.             else
  360.                 /* only 2 pass quantizer still requires old upsample routine */
  361.                 if (cinfo->two_pass_quantize)
  362.                     cinfo->methods->upsample[ci] = h2v2_quick_upsample;
  363.                 else
  364.                     cinfo->methods->upsample[ci] = h2v2_no_upsample;
  365.             }
  366.      else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  367.           (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0)
  368.         cinfo->methods->upsample[ci] = int_upsample;
  369.      else
  370.         ERREXIT(cinfo->emethods, "Fractional upsampling not implemented yet");
  371.   }
  372.   cinfo->methods->upsample_init = upsample_init;
  373.   cinfo->methods->upsample_term = upsample_term;
  374. }
  375.