home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jcprepct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.9 KB  |  373 lines

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