home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jcprepct.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  12.6 KB  |  356 lines

  1. //-------------------------------------------------------------------------//
  2. //          Windows Graphics Programming: Win32 GDI and DirectDraw         //
  3. //                        ISBN  0-13-086985-6                              //
  4. //                                                                         //
  5. //  Modified by: Yuan, Feng                             www.fengyuan.com   //
  6. //  Changes    : C++, exception, in-memory source, BGR byte order          //
  7. //  Version    : 1.00.000, May 31, 2000                                    //
  8. //-------------------------------------------------------------------------//
  9.  
  10. /*
  11.  * jcprepct.c
  12.  *
  13.  * Copyright (C) 1994-1996, Thomas G. Lane.
  14.  * This file is part of the Independent JPEG Group's software.
  15.  * For conditions of distribution and use, see the accompanying README file.
  16.  *
  17.  * This file contains the compression preprocessing controller.
  18.  * This controller manages the color conversion, downsampling,
  19.  * and edge expansion steps.
  20.  *
  21.  * Most of the complexity here is associated with buffering input rows
  22.  * as required by the downsampler.  See the comments at the head of
  23.  * jcsample.c for the downsampler's needs.
  24.  */
  25.  
  26. #define JPEG_INTERNALS
  27. #include "jinclude.h"
  28. #include "jpeglib.h"
  29.  
  30.  
  31. /* At present, jcsample.c can request context rows only for smoothing.
  32.  * In the future, we might also need context rows for CCIR601 sampling
  33.  * or other more-complex downsampling procedures.  The code to support
  34.  * context rows should be compiled only if needed.
  35.  */
  36. #ifdef INPUT_SMOOTHING_SUPPORTED
  37. #define CONTEXT_ROWS_SUPPORTED
  38. #endif
  39.  
  40.  
  41. /*
  42.  * For the simple (no-context-row) case, we just need to buffer one
  43.  * row group's worth of pixels for the downsampling step.  At the bottom of
  44.  * the image, we pad to a full row group by replicating the last pixel row.
  45.  * The downsampler's last output row is then replicated if needed to pad
  46.  * out to a full iMCU row.
  47.  *
  48.  * When providing context rows, we must buffer three row groups' worth of
  49.  * pixels.  Three row groups are physically allocated, but the row pointer
  50.  * arrays are made five row groups high, with the extra pointers above and
  51.  * below "wrapping around" to point to the last and first real row groups.
  52.  * This allows the downsampler to access the proper context rows.
  53.  * At the top and bottom of the image, we create dummy context rows by
  54.  * copying the first or last real pixel row.  This copying could be avoided
  55.  * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
  56.  * trouble on the compression side.
  57.  */
  58.  
  59.  
  60. /* Private buffer controller object */
  61.  
  62. typedef struct {
  63.   struct jpeg_c_prep_controller pub; /* public fields */
  64.  
  65.   /* Downsampling input buffer.  This buffer holds color-converted data
  66.    * until we have enough to do a downsample step.
  67.    */
  68.   JSAMPARRAY color_buf[MAX_COMPONENTS];
  69.  
  70.   JDIMENSION rows_to_go;    /* counts rows remaining in source image */
  71.   int next_buf_row;        /* index of next row to store in color_buf */
  72.  
  73. #ifdef CONTEXT_ROWS_SUPPORTED    /* only needed for context case */
  74.   int this_row_group;        /* starting row index of group to process */
  75.   int next_buf_stop;        /* downsample when we reach this index */
  76. #endif
  77. } my_prep_controller;
  78.  
  79. typedef my_prep_controller * my_prep_ptr;
  80.  
  81.  
  82. // Initialize for a processing pass.
  83. void start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  84. {
  85.   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  86.  
  87.   if (pass_mode != JBUF_PASS_THRU)
  88.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  89.  
  90.   /* Initialize total-height counter for detecting bottom of image */
  91.   prep->rows_to_go = cinfo->image_height;
  92.   /* Mark the conversion buffer empty */
  93.   prep->next_buf_row = 0;
  94. #ifdef CONTEXT_ROWS_SUPPORTED
  95.   /* Preset additional state variables for context mode.
  96.    * These aren't used in non-context mode, so we needn't test which mode.
  97.    */
  98.   prep->this_row_group = 0;
  99.   /* Set next_buf_stop to stop after two row groups have been read in. */
  100.   prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
  101. #endif
  102. }
  103.  
  104.  
  105. /*
  106.  * Expand an image vertically from height input_rows to height output_rows,
  107.  * by duplicating the bottom row.
  108.  */
  109.  
  110. LOCAL(void)
  111. expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  112.             int input_rows, int output_rows)
  113. {
  114.   register int row;
  115.  
  116.   for (row = input_rows; row < output_rows; row++) {
  117.     jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  118.               1, num_cols);
  119.   }
  120. }
  121.  
  122.  
  123. /*
  124.  * Process some data in the simple no-context case.
  125.  *
  126.  * Preprocessor output data is counted in "row groups".  A row group
  127.  * is defined to be v_samp_factor sample rows of each component.
  128.  * Downsampling will produce this much data from each max_v_samp_factor
  129.  * input rows.
  130.  */
  131.  
  132. void pre_process_data (j_compress_ptr cinfo,
  133.           JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  134.           JDIMENSION in_rows_avail,
  135.           JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  136.           JDIMENSION out_row_groups_avail)
  137. {
  138.   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  139.   int numrows, ci;
  140.   JDIMENSION inrows;
  141.   jpeg_component_info * compptr;
  142.  
  143.   while (*in_row_ctr < in_rows_avail &&
  144.      *out_row_group_ctr < out_row_groups_avail) {
  145.     /* Do color conversion to fill the conversion buffer. */
  146.     inrows = in_rows_avail - *in_row_ctr;
  147.     numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  148.     numrows = (int) MIN((JDIMENSION) numrows, inrows);
  149.     (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  150.                        prep->color_buf,
  151.                        (JDIMENSION) prep->next_buf_row,
  152.                        numrows);
  153.     *in_row_ctr += numrows;
  154.     prep->next_buf_row += numrows;
  155.     prep->rows_to_go -= numrows;
  156.     /* If at bottom of image, pad to fill the conversion buffer. */
  157.     if (prep->rows_to_go == 0 &&
  158.     prep->next_buf_row < cinfo->max_v_samp_factor) {
  159.       for (ci = 0; ci < cinfo->num_components; ci++) {
  160.     expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  161.                prep->next_buf_row, cinfo->max_v_samp_factor);
  162.       }
  163.       prep->next_buf_row = cinfo->max_v_samp_factor;
  164.     }
  165.     /* If we've filled the conversion buffer, empty it. */
  166.     if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  167.       (*cinfo->downsample->downsample) (cinfo,
  168.                     prep->color_buf, (JDIMENSION) 0,
  169.                     output_buf, *out_row_group_ctr);
  170.       prep->next_buf_row = 0;
  171.       (*out_row_group_ctr)++;
  172.     }
  173.     /* If at bottom of image, pad the output to a full iMCU height.
  174.      * Note we assume the caller is providing a one-iMCU-height output buffer!
  175.      */
  176.     if (prep->rows_to_go == 0 &&
  177.     *out_row_group_ctr < out_row_groups_avail) {
  178.       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  179.        ci++, compptr++) {
  180.     expand_bottom_edge(output_buf[ci],
  181.                compptr->width_in_blocks * DCTSIZE,
  182.                (int) (*out_row_group_ctr * compptr->v_samp_factor),
  183.                (int) (out_row_groups_avail * compptr->v_samp_factor));
  184.       }
  185.       *out_row_group_ctr = out_row_groups_avail;
  186.       break;            /* can exit outer loop without test */
  187.     }
  188.   }
  189. }
  190.  
  191.  
  192. #ifdef CONTEXT_ROWS_SUPPORTED
  193.  
  194. /*
  195.  * Process some data in the context case.
  196.  */
  197.  
  198. void pre_process_context (j_compress_ptr cinfo,
  199.              JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  200.              JDIMENSION in_rows_avail,
  201.              JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  202.              JDIMENSION out_row_groups_avail)
  203. {
  204.   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  205.   int numrows, ci;
  206.   int buf_height = cinfo->max_v_samp_factor * 3;
  207.   JDIMENSION inrows;
  208.  
  209.   while (*out_row_group_ctr < out_row_groups_avail) {
  210.     if (*in_row_ctr < in_rows_avail) {
  211.       /* Do color conversion to fill the conversion buffer. */
  212.       inrows = in_rows_avail - *in_row_ctr;
  213.       numrows = prep->next_buf_stop - prep->next_buf_row;
  214.       numrows = (int) MIN((JDIMENSION) numrows, inrows);
  215.       (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  216.                      prep->color_buf,
  217.                      (JDIMENSION) prep->next_buf_row,
  218.                      numrows);
  219.       /* Pad at top of image, if first time through */
  220.       if (prep->rows_to_go == cinfo->image_height) {
  221.     for (ci = 0; ci < cinfo->num_components; ci++) {
  222.       int row;
  223.       for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  224.         jcopy_sample_rows(prep->color_buf[ci], 0,
  225.                   prep->color_buf[ci], -row,
  226.                   1, cinfo->image_width);
  227.       }
  228.     }
  229.       }
  230.       *in_row_ctr += numrows;
  231.       prep->next_buf_row += numrows;
  232.       prep->rows_to_go -= numrows;
  233.     } else {
  234.       /* Return for more data, unless we are at the bottom of the image. */
  235.       if (prep->rows_to_go != 0)
  236.     break;
  237.       /* When at bottom of image, pad to fill the conversion buffer. */
  238.       if (prep->next_buf_row < prep->next_buf_stop) {
  239.     for (ci = 0; ci < cinfo->num_components; ci++) {
  240.       expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  241.                  prep->next_buf_row, prep->next_buf_stop);
  242.     }
  243.     prep->next_buf_row = prep->next_buf_stop;
  244.       }
  245.     }
  246.     /* If we've gotten enough data, downsample a row group. */
  247.     if (prep->next_buf_row == prep->next_buf_stop) {
  248.       (*cinfo->downsample->downsample) (cinfo,
  249.                     prep->color_buf,
  250.                     (JDIMENSION) prep->this_row_group,
  251.                     output_buf, *out_row_group_ctr);
  252.       (*out_row_group_ctr)++;
  253.       /* Advance pointers with wraparound as necessary. */
  254.       prep->this_row_group += cinfo->max_v_samp_factor;
  255.       if (prep->this_row_group >= buf_height)
  256.     prep->this_row_group = 0;
  257.       if (prep->next_buf_row >= buf_height)
  258.     prep->next_buf_row = 0;
  259.       prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  260.     }
  261.   }
  262. }
  263.  
  264.  
  265. /*
  266.  * Create the wrapped-around downsampling input buffer needed for context mode.
  267.  */
  268.  
  269. LOCAL(void)
  270. create_context_buffer (j_compress_ptr cinfo)
  271. {
  272.   my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  273.   int rgroup_height = cinfo->max_v_samp_factor;
  274.   int ci, i;
  275.   jpeg_component_info * compptr;
  276.   JSAMPARRAY true_buffer, fake_buffer;
  277.  
  278.   /* Grab enough space for fake row pointers for all the components;
  279.    * we need five row groups' worth of pointers for each component.
  280.    */
  281.   fake_buffer = (JSAMPARRAY)
  282.     cinfo->mem->alloc_small (JPOOL_IMAGE,
  283.                 (cinfo->num_components * 5 * rgroup_height) * sizeof(JSAMPROW));
  284.  
  285.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  286.        ci++, compptr++) {
  287.     /* Allocate the actual buffer space (3 row groups) for this component.
  288.      * We make the buffer wide enough to allow the downsampler to edge-expand
  289.      * horizontally within the buffer, if it so chooses.
  290.      */
  291.     true_buffer = cinfo->mem->alloc_sarray
  292.       (JPOOL_IMAGE,
  293.        (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  294.               cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  295.        (JDIMENSION) (3 * rgroup_height));
  296.     /* Copy true buffer row pointers into the middle of the fake row array */
  297.         memcpy(fake_buffer + rgroup_height, true_buffer,
  298.         3 * rgroup_height * sizeof(JSAMPROW));
  299.     /* Fill in the above and below wraparound pointers */
  300.     for (i = 0; i < rgroup_height; i++) {
  301.       fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  302.       fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  303.     }
  304.     prep->color_buf[ci] = fake_buffer + rgroup_height;
  305.     fake_buffer += 5 * rgroup_height; /* point to space for next component */
  306.   }
  307. }
  308.  
  309. #endif /* CONTEXT_ROWS_SUPPORTED */
  310.  
  311.  
  312. /*
  313.  * Initialize preprocessing controller.
  314.  */
  315.  
  316. GLOBAL(void)
  317. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  318. {
  319.   my_prep_ptr prep;
  320.   int ci;
  321.   jpeg_component_info * compptr;
  322.  
  323.   if (need_full_buffer)        /* safety check */
  324.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  325.  
  326.   prep = (my_prep_ptr)
  327.     cinfo->mem->alloc_small(JPOOL_IMAGE, sizeof(my_prep_controller));
  328.   cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  329.   prep->pub.start_pass = start_pass_prep;
  330.  
  331.   /* Allocate the color conversion buffer.
  332.    * We make the buffer wide enough to allow the downsampler to edge-expand
  333.    * horizontally within the buffer, if it so chooses.
  334.    */
  335.   if (cinfo->downsample->need_context_rows) {
  336.     /* Set up to provide context rows */
  337. #ifdef CONTEXT_ROWS_SUPPORTED
  338.     prep->pub.pre_process_data = pre_process_context;
  339.     create_context_buffer(cinfo);
  340. #else
  341.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  342. #endif
  343.   } else {
  344.     /* No context, just make it tall enough for one row group */
  345.     prep->pub.pre_process_data = pre_process_data;
  346.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  347.      ci++, compptr++) {
  348.       prep->color_buf[ci] = cinfo->mem->alloc_sarray
  349.     (JPOOL_IMAGE,
  350.      (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  351.             cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  352.      (JDIMENSION) cinfo->max_v_samp_factor);
  353.     }
  354.   }
  355. }
  356.