home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jcmainct.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  9.7 KB  |  298 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.  * jcmainct.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 main buffer controller for compression.
  18.  * The main buffer lies between the pre-processor and the JPEG
  19.  * compressor proper; it holds downsampled data in the JPEG colorspace.
  20.  */
  21.  
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25.  
  26.  
  27. /* Note: currently, there is no operating mode in which a full-image buffer
  28.  * is needed at this step.  If there were, that mode could not be used with
  29.  * "raw data" input, since this module is bypassed in that case.  However,
  30.  * we've left the code here for possible use in special applications.
  31.  */
  32. #undef FULL_MAIN_BUFFER_SUPPORTED
  33.  
  34.  
  35. /* Private buffer controller object */
  36.  
  37. typedef struct {
  38.   struct jpeg_c_main_controller pub; /* public fields */
  39.  
  40.   JDIMENSION cur_iMCU_row;    /* number of current iMCU row */
  41.   JDIMENSION rowgroup_ctr;    /* counts row groups received in iMCU row */
  42.   boolean suspended;        /* remember if we suspended output */
  43.   J_BUF_MODE pass_mode;        /* current operating mode */
  44.  
  45.   /* If using just a strip buffer, this points to the entire set of buffers
  46.    * (we allocate one for each component).  In the full-image case, this
  47.    * points to the currently accessible strips of the virtual arrays.
  48.    */
  49.   JSAMPARRAY buffer[MAX_COMPONENTS];
  50.  
  51. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  52.   /* If using full-image storage, this array holds pointers to virtual-array
  53.    * control blocks for each component.  Unused if not full-image storage.
  54.    */
  55.   jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
  56. #endif
  57. } my_main_controller;
  58.  
  59. typedef my_main_controller * my_main_ptr;
  60.  
  61.  
  62. /* Forward declarations */
  63. void process_data_simple_main
  64.     (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  65.          JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
  66. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  67. void process_data_buffer_main
  68.     (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  69.          JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
  70. #endif
  71.  
  72.  
  73. /*
  74.  * Initialize for a processing pass.
  75.  */
  76.  
  77. void start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  78. {
  79.   my_main_ptr main = (my_main_ptr) cinfo->main;
  80.  
  81.   /* Do nothing in raw-data mode. */
  82.   if (cinfo->raw_data_in)
  83.     return;
  84.  
  85.   main->cur_iMCU_row = 0;    /* initialize counters */
  86.   main->rowgroup_ctr = 0;
  87.   main->suspended = FALSE;
  88.   main->pass_mode = pass_mode;    /* save mode for use by process_data */
  89.  
  90.   switch (pass_mode) {
  91.   case JBUF_PASS_THRU:
  92. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  93.     if (main->whole_image[0] != NULL)
  94.       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  95. #endif
  96.     main->pub.process_data = process_data_simple_main;
  97.     break;
  98. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  99.   case JBUF_SAVE_SOURCE:
  100.   case JBUF_CRANK_DEST:
  101.   case JBUF_SAVE_AND_PASS:
  102.     if (main->whole_image[0] == NULL)
  103.       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  104.     main->pub.process_data = process_data_buffer_main;
  105.     break;
  106. #endif
  107.   default:
  108.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  109.     break;
  110.   }
  111. }
  112.  
  113.  
  114. /*
  115.  * Process some data.
  116.  * This routine handles the simple pass-through mode,
  117.  * where we have only a strip buffer.
  118.  */
  119.  
  120. void process_data_simple_main (j_compress_ptr cinfo,
  121.               JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  122.               JDIMENSION in_rows_avail)
  123. {
  124.   my_main_ptr main = (my_main_ptr) cinfo->main;
  125.  
  126.   while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  127.     /* Read input data if we haven't filled the main buffer yet */
  128.     if (main->rowgroup_ctr < DCTSIZE)
  129.       (*cinfo->prep->pre_process_data) (cinfo,
  130.                     input_buf, in_row_ctr, in_rows_avail,
  131.                     main->buffer, &main->rowgroup_ctr,
  132.                     (JDIMENSION) DCTSIZE);
  133.  
  134.     /* If we don't have a full iMCU row buffered, return to application for
  135.      * more data.  Note that preprocessor will always pad to fill the iMCU row
  136.      * at the bottom of the image.
  137.      */
  138.     if (main->rowgroup_ctr != DCTSIZE)
  139.       return;
  140.  
  141.     /* Send the completed row to the compressor */
  142.     if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  143.       /* If compressor did not consume the whole row, then we must need to
  144.        * suspend processing and return to the application.  In this situation
  145.        * we pretend we didn't yet consume the last input row; otherwise, if
  146.        * it happened to be the last row of the image, the application would
  147.        * think we were done.
  148.        */
  149.       if (! main->suspended) {
  150.     (*in_row_ctr)--;
  151.     main->suspended = TRUE;
  152.       }
  153.       return;
  154.     }
  155.     /* We did finish the row.  Undo our little suspension hack if a previous
  156.      * call suspended; then mark the main buffer empty.
  157.      */
  158.     if (main->suspended) {
  159.       (*in_row_ctr)++;
  160.       main->suspended = FALSE;
  161.     }
  162.     main->rowgroup_ctr = 0;
  163.     main->cur_iMCU_row++;
  164.   }
  165. }
  166.  
  167.  
  168. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  169.  
  170. /*
  171.  * Process some data.
  172.  * This routine handles all of the modes that use a full-size buffer.
  173.  */
  174.  
  175. void process_data_buffer_main (j_compress_ptr cinfo,
  176.               JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  177.               JDIMENSION in_rows_avail)
  178. {
  179.   my_main_ptr main = (my_main_ptr) cinfo->main;
  180.   int ci;
  181.   jpeg_component_info *compptr;
  182.   boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
  183.  
  184.   while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  185.     /* Realign the virtual buffers if at the start of an iMCU row. */
  186.     if (main->rowgroup_ctr == 0) {
  187.       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  188.        ci++, compptr++) {
  189.     main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  190.       ((j_common_ptr) cinfo, main->whole_image[ci],
  191.        main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
  192.        (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
  193.       }
  194.       /* In a read pass, pretend we just read some source data. */
  195.       if (! writing) {
  196.     *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
  197.     main->rowgroup_ctr = DCTSIZE;
  198.       }
  199.     }
  200.  
  201.     /* If a write pass, read input data until the current iMCU row is full. */
  202.     /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  203.     if (writing) {
  204.       (*cinfo->prep->pre_process_data) (cinfo,
  205.                     input_buf, in_row_ctr, in_rows_avail,
  206.                     main->buffer, &main->rowgroup_ctr,
  207.                     (JDIMENSION) DCTSIZE);
  208.       /* Return to application if we need more data to fill the iMCU row. */
  209.       if (main->rowgroup_ctr < DCTSIZE)
  210.     return;
  211.     }
  212.  
  213.     /* Emit data, unless this is a sink-only pass. */
  214.     if (main->pass_mode != JBUF_SAVE_SOURCE) {
  215.       if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  216.     /* If compressor did not consume the whole row, then we must need to
  217.      * suspend processing and return to the application.  In this situation
  218.      * we pretend we didn't yet consume the last input row; otherwise, if
  219.      * it happened to be the last row of the image, the application would
  220.      * think we were done.
  221.      */
  222.     if (! main->suspended) {
  223.       (*in_row_ctr)--;
  224.       main->suspended = TRUE;
  225.     }
  226.     return;
  227.       }
  228.       /* We did finish the row.  Undo our little suspension hack if a previous
  229.        * call suspended; then mark the main buffer empty.
  230.        */
  231.       if (main->suspended) {
  232.     (*in_row_ctr)++;
  233.     main->suspended = FALSE;
  234.       }
  235.     }
  236.  
  237.     /* If get here, we are done with this iMCU row.  Mark buffer empty. */
  238.     main->rowgroup_ctr = 0;
  239.     main->cur_iMCU_row++;
  240.   }
  241. }
  242.  
  243. #endif /* FULL_MAIN_BUFFER_SUPPORTED */
  244.  
  245.  
  246. /*
  247.  * Initialize main buffer controller.
  248.  */
  249.  
  250. GLOBAL(void)
  251. jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  252. {
  253.   my_main_ptr main;
  254.   int ci;
  255.   jpeg_component_info *compptr;
  256.  
  257.   main = (my_main_ptr) cinfo->mem->alloc_small (JPOOL_IMAGE, sizeof(my_main_controller));
  258.   cinfo->main = (struct jpeg_c_main_controller *) main;
  259.   main->pub.start_pass = start_pass_main;
  260.  
  261.   /* We don't need to create a buffer in raw-data mode. */
  262.   if (cinfo->raw_data_in)
  263.     return;
  264.  
  265.   /* Create the buffer.  It holds downsampled data, so each component
  266.    * may be of a different size.
  267.    */
  268.   if (need_full_buffer) {
  269. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  270.     /* Allocate a full-image virtual array for each component */
  271.     /* Note we pad the bottom to a multiple of the iMCU height */
  272.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  273.      ci++, compptr++) {
  274.       main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  275.     ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  276.      compptr->width_in_blocks * DCTSIZE,
  277.      (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  278.                 (long) compptr->v_samp_factor) * DCTSIZE,
  279.      (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  280.     }
  281. #else
  282.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  283. #endif
  284.   } else {
  285. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  286.     main->whole_image[0] = NULL; /* flag for no virtual arrays */
  287. #endif
  288.     /* Allocate a strip buffer for each component */
  289.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  290.      ci++, compptr++) {
  291.       main->buffer[ci] = cinfo->mem->alloc_sarray
  292.     (JPOOL_IMAGE,
  293.      compptr->width_in_blocks * DCTSIZE,
  294.      (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  295.     }
  296.   }
  297. }
  298.