home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jdsample.c < prev    next >
C/C++ Source or Header  |  1992-01-31  |  6KB  |  221 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 un-subsampling routines.
  9.  * These routines are invoked via the unsubsample and
  10.  * unsubsample_init/term methods.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /*
  17.  * Initialize for un-subsampling a scan.
  18.  */
  19.  
  20. METHODDEF void
  21. unsubsample_init (decompress_info_ptr cinfo)
  22. {
  23.   /* no work for now */
  24. }
  25.  
  26.  
  27. /*
  28.  * Un-subsample pixel values of a single component.
  29.  * This version handles any integral sampling ratios.
  30.  * This is not used for typical JPEG files, so it need not be fast.
  31.  */
  32.  
  33. METHODDEF void
  34. int_unsubsample (decompress_info_ptr cinfo, int which_component,
  35.          long input_cols, int input_rows,
  36.          long output_cols, int output_rows,
  37.          JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  38.          JSAMPARRAY output_data)
  39. {
  40.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  41.   register JSAMPROW inptr, outptr;
  42.   register JSAMPLE invalue;
  43.   register short h_expand, h;
  44.   short v_expand, v;
  45.   int inrow, outrow;
  46.   register long incol;
  47.  
  48. #ifdef DEBUG            /* for debugging pipeline controller */
  49.   if (input_rows != compptr->v_samp_factor ||
  50.       output_rows != cinfo->max_v_samp_factor ||
  51.       (input_cols % compptr->h_samp_factor) != 0 ||
  52.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  53.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  54.     ERREXIT(cinfo->emethods, "Bogus unsubsample parameters");
  55. #endif
  56.  
  57.   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  58.   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  59.  
  60.   outrow = 0;
  61.   for (inrow = 0; inrow < input_rows; inrow++) {
  62.     for (v = 0; v < v_expand; v++) {
  63.       inptr = input_data[inrow];
  64.       outptr = output_data[outrow++];
  65.       for (incol = 0; incol < input_cols; incol++) {
  66.     invalue = GETJSAMPLE(*inptr++);
  67.     for (h = 0; h < h_expand; h++) {
  68.       *outptr++ = invalue;
  69.     }
  70.       }
  71.     }
  72.   }
  73. }
  74.  
  75.  
  76. /*
  77.  * Un-subsample pixel values of a single component.
  78.  * This version handles the extremely common case of
  79.  * horizontal expansion by 2 and any integral vertical expansion.
  80.  */
  81.  
  82. METHODDEF void
  83. h2_unsubsample (decompress_info_ptr cinfo, int which_component,
  84.         long input_cols, int input_rows,
  85.         long output_cols, int output_rows,
  86.         JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  87.         JSAMPARRAY output_data)
  88. {
  89.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  90.   register JSAMPROW inptr, outptr;
  91.   register JSAMPLE invalue;
  92.   short v_expand, v;
  93.   int inrow, outrow;
  94.   register long incol;
  95.  
  96. #ifdef DEBUG            /* for debugging pipeline controller */
  97.   if (input_rows != compptr->v_samp_factor ||
  98.       output_rows != cinfo->max_v_samp_factor ||
  99.       (input_cols % compptr->h_samp_factor) != 0 ||
  100.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  101.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  102.     ERREXIT(cinfo->emethods, "Bogus unsubsample parameters");
  103. #endif
  104.  
  105.   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  106.  
  107. /* The subsampled image width will always be a multiple of DCTSIZE,
  108.  * so we can unroll the inner loop.
  109.  */
  110.  
  111.   outrow = 0;
  112.   for (inrow = 0; inrow < input_rows; inrow++) {
  113.     for (v = 0; v < v_expand; v++) {
  114.       inptr = input_data[inrow];
  115.       outptr = output_data[outrow++];
  116. #if DCTSIZE == 8
  117.       for (incol = 0; incol < input_cols; incol += DCTSIZE) {
  118.     invalue = GETJSAMPLE(*inptr++);
  119.     *outptr++ = invalue;
  120.     *outptr++ = invalue;
  121.     invalue = GETJSAMPLE(*inptr++);
  122.     *outptr++ = invalue;
  123.     *outptr++ = invalue;
  124.     invalue = GETJSAMPLE(*inptr++);
  125.     *outptr++ = invalue;
  126.     *outptr++ = invalue;
  127.     invalue = GETJSAMPLE(*inptr++);
  128.     *outptr++ = invalue;
  129.     *outptr++ = invalue;
  130.     invalue = GETJSAMPLE(*inptr++);
  131.     *outptr++ = invalue;
  132.     *outptr++ = invalue;
  133.     invalue = GETJSAMPLE(*inptr++);
  134.     *outptr++ = invalue;
  135.     *outptr++ = invalue;
  136.     invalue = GETJSAMPLE(*inptr++);
  137.     *outptr++ = invalue;
  138.     *outptr++ = invalue;
  139.     invalue = GETJSAMPLE(*inptr++);
  140.     *outptr++ = invalue;
  141.     *outptr++ = invalue;
  142.       }
  143. #else /* nonstandard DCTSIZE */
  144.       for (incol = 0; incol < input_cols; incol++) {
  145.     invalue = GETJSAMPLE(*inptr++);
  146.     *outptr++ = invalue;
  147.     *outptr++ = invalue;
  148.       }
  149. #endif
  150.     }
  151.   }
  152. }
  153.  
  154.  
  155. /*
  156.  * Un-subsample pixel values of a single component.
  157.  * This version handles the special case of a full-size component.
  158.  */
  159.  
  160. METHODDEF void
  161. fullsize_unsubsample (decompress_info_ptr cinfo, int which_component,
  162.               long input_cols, int input_rows,
  163.               long output_cols, int output_rows,
  164.               JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  165.               JSAMPARRAY output_data)
  166. {
  167. #ifdef DEBUG            /* for debugging pipeline controller */
  168.   if (input_cols != output_cols || input_rows != output_rows)
  169.     ERREXIT(cinfo->emethods, "Pipeline controller messed up");
  170. #endif
  171.  
  172.   jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
  173. }
  174.  
  175.  
  176.  
  177. /*
  178.  * Clean up after a scan.
  179.  */
  180.  
  181. METHODDEF void
  182. unsubsample_term (decompress_info_ptr cinfo)
  183. {
  184.   /* no work for now */
  185. }
  186.  
  187.  
  188.  
  189. /*
  190.  * The method selection routine for unsubsampling.
  191.  * Note that we must select a routine for each component.
  192.  */
  193.  
  194. GLOBAL void
  195. jselunsubsample (decompress_info_ptr cinfo)
  196. {
  197.   short ci;
  198.   jpeg_component_info * compptr;
  199.  
  200.   if (cinfo->CCIR601_sampling)
  201.     ERREXIT(cinfo->emethods, "CCIR601 subsampling not implemented yet");
  202.  
  203.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  204.     compptr = cinfo->cur_comp_info[ci];
  205.     if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  206.     compptr->v_samp_factor == cinfo->max_v_samp_factor)
  207.       cinfo->methods->unsubsample[ci] = fullsize_unsubsample;
  208.     else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  209.          (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0)
  210.       cinfo->methods->unsubsample[ci] = h2_unsubsample;
  211.     else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  212.          (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0)
  213.       cinfo->methods->unsubsample[ci] = int_unsubsample;
  214.     else
  215.       ERREXIT(cinfo->emethods, "Fractional subsampling not implemented yet");
  216.   }
  217.  
  218.   cinfo->methods->unsubsample_init = unsubsample_init;
  219.   cinfo->methods->unsubsample_term = unsubsample_term;
  220. }
  221.