home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / DOS / GRAFISCH / JPGSRC5A / JCMAINCT.C < prev    next >
C/C++ Source or Header  |  1994-08-04  |  10KB  |  299 lines

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