< prev index next >

modules/javafx.graphics/src/main/native-iio/libjpeg7/jcmainct.c

Print this page


   1 /*
   2  * jcmainct.c
   3  *
   4  * Copyright (C) 1994-1996, 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 


  51 
  52 
  53 /* Forward declarations */
  54 METHODDEF(void) process_data_simple_main
  55         JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  56              JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  57 #ifdef FULL_MAIN_BUFFER_SUPPORTED
  58 METHODDEF(void) process_data_buffer_main
  59         JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  60              JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  61 #endif
  62 
  63 
  64 /*
  65  * Initialize for a processing pass.
  66  */
  67 
  68 METHODDEF(void)
  69 start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  70 {
  71   my_main_ptr main = (my_main_ptr) cinfo->main;
  72 
  73   /* Do nothing in raw-data mode. */
  74   if (cinfo->raw_data_in)
  75     return;
  76 
  77   main->cur_iMCU_row = 0;       /* initialize counters */
  78   main->rowgroup_ctr = 0;
  79   main->suspended = FALSE;
  80   main->pass_mode = pass_mode;  /* save mode for use by process_data */
  81 
  82   switch (pass_mode) {
  83   case JBUF_PASS_THRU:
  84 #ifdef FULL_MAIN_BUFFER_SUPPORTED
  85     if (main->whole_image[0] != NULL)
  86       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  87 #endif
  88     main->pub.process_data = process_data_simple_main;
  89     break;
  90 #ifdef FULL_MAIN_BUFFER_SUPPORTED
  91   case JBUF_SAVE_SOURCE:
  92   case JBUF_CRANK_DEST:
  93   case JBUF_SAVE_AND_PASS:
  94     if (main->whole_image[0] == NULL)
  95       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  96     main->pub.process_data = process_data_buffer_main;
  97     break;
  98 #endif
  99   default:
 100     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
 101     break;
 102   }
 103 }
 104 
 105 
 106 /*
 107  * Process some data.
 108  * This routine handles the simple pass-through mode,
 109  * where we have only a strip buffer.
 110  */
 111 
 112 METHODDEF(void)
 113 process_data_simple_main (j_compress_ptr cinfo,
 114                           JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
 115                           JDIMENSION in_rows_avail)
 116 {
 117   my_main_ptr main = (my_main_ptr) cinfo->main;
 118 
 119   while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
 120     /* Read input data if we haven't filled the main buffer yet */
 121     if (main->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
 122       (*cinfo->prep->pre_process_data) (cinfo,
 123                                         input_buf, in_row_ctr, in_rows_avail,
 124                                         main->buffer, &main->rowgroup_ctr,
 125                                         (JDIMENSION) cinfo->min_DCT_v_scaled_size);
 126 
 127     /* If we don't have a full iMCU row buffered, return to application for
 128      * more data.  Note that preprocessor will always pad to fill the iMCU row
 129      * at the bottom of the image.
 130      */
 131     if (main->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
 132       return;
 133 
 134     /* Send the completed row to the compressor */
 135     if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
 136       /* If compressor did not consume the whole row, then we must need to
 137        * suspend processing and return to the application.  In this situation
 138        * we pretend we didn't yet consume the last input row; otherwise, if
 139        * it happened to be the last row of the image, the application would
 140        * think we were done.
 141        */
 142       if (! main->suspended) {
 143         (*in_row_ctr)--;
 144         main->suspended = TRUE;
 145       }
 146       return;
 147     }
 148     /* We did finish the row.  Undo our little suspension hack if a previous
 149      * call suspended; then mark the main buffer empty.
 150      */
 151     if (main->suspended) {
 152       (*in_row_ctr)++;
 153       main->suspended = FALSE;
 154     }
 155     main->rowgroup_ctr = 0;
 156     main->cur_iMCU_row++;
 157   }
 158 }
 159 
 160 
 161 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 162 
 163 /*
 164  * Process some data.
 165  * This routine handles all of the modes that use a full-size buffer.
 166  */
 167 
 168 METHODDEF(void)
 169 process_data_buffer_main (j_compress_ptr cinfo,
 170                           JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
 171                           JDIMENSION in_rows_avail)
 172 {
 173   my_main_ptr main = (my_main_ptr) cinfo->main;
 174   int ci;
 175   jpeg_component_info *compptr;
 176   boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
 177 
 178   while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
 179     /* Realign the virtual buffers if at the start of an iMCU row. */
 180     if (main->rowgroup_ctr == 0) {
 181       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 182            ci++, compptr++) {
 183         main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
 184           ((j_common_ptr) cinfo, main->whole_image[ci],
 185            main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
 186            (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);

 187       }
 188       /* In a read pass, pretend we just read some source data. */
 189       if (! writing) {
 190         *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
 191         main->rowgroup_ctr = DCTSIZE;

 192       }
 193     }
 194 
 195     /* If a write pass, read input data until the current iMCU row is full. */
 196     /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
 197     if (writing) {
 198       (*cinfo->prep->pre_process_data) (cinfo,
 199                                         input_buf, in_row_ctr, in_rows_avail,
 200                                         main->buffer, &main->rowgroup_ctr,
 201                                         (JDIMENSION) DCTSIZE);
 202       /* Return to application if we need more data to fill the iMCU row. */
 203       if (main->rowgroup_ctr < DCTSIZE)
 204         return;
 205     }
 206 
 207     /* Emit data, unless this is a sink-only pass. */
 208     if (main->pass_mode != JBUF_SAVE_SOURCE) {
 209       if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
 210         /* If compressor did not consume the whole row, then we must need to
 211          * suspend processing and return to the application.  In this situation
 212          * we pretend we didn't yet consume the last input row; otherwise, if
 213          * it happened to be the last row of the image, the application would
 214          * think we were done.
 215          */
 216         if (! main->suspended) {
 217           (*in_row_ctr)--;
 218           main->suspended = TRUE;
 219         }
 220         return;
 221       }
 222       /* We did finish the row.  Undo our little suspension hack if a previous
 223        * call suspended; then mark the main buffer empty.
 224        */
 225       if (main->suspended) {
 226         (*in_row_ctr)++;
 227         main->suspended = FALSE;
 228       }
 229     }
 230 
 231     /* If get here, we are done with this iMCU row.  Mark buffer empty. */
 232     main->rowgroup_ctr = 0;
 233     main->cur_iMCU_row++;
 234   }
 235 }
 236 
 237 #endif /* FULL_MAIN_BUFFER_SUPPORTED */
 238 
 239 
 240 /*
 241  * Initialize main buffer controller.
 242  */
 243 
 244 GLOBAL(void)
 245 jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 246 {
 247   my_main_ptr main;
 248   int ci;
 249   jpeg_component_info *compptr;
 250 
 251   main = (my_main_ptr)
 252     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 253                                 SIZEOF(my_main_controller));
 254   cinfo->main = (struct jpeg_c_main_controller *) main;
 255   main->pub.start_pass = start_pass_main;
 256 
 257   /* We don't need to create a buffer in raw-data mode. */
 258   if (cinfo->raw_data_in)
 259     return;
 260 
 261   /* Create the buffer.  It holds downsampled data, so each component
 262    * may be of a different size.
 263    */
 264   if (need_full_buffer) {
 265 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 266     /* Allocate a full-image virtual array for each component */
 267     /* Note we pad the bottom to a multiple of the iMCU height */
 268     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 269          ci++, compptr++) {
 270       main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
 271         ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
 272          compptr->width_in_blocks * compptr->DCT_h_scaled_size,
 273          (JDIMENSION) jround_up((long) compptr->height_in_blocks,
 274                                 (long) compptr->v_samp_factor) * DCTSIZE,

 275          (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
 276     }
 277 #else
 278     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
 279 #endif
 280   } else {
 281 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 282     main->whole_image[0] = NULL; /* flag for no virtual arrays */
 283 #endif
 284     /* Allocate a strip buffer for each component */
 285     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 286          ci++, compptr++) {
 287       main->buffer[ci] = (*cinfo->mem->alloc_sarray)
 288         ((j_common_ptr) cinfo, JPOOL_IMAGE,
 289          compptr->width_in_blocks * compptr->DCT_h_scaled_size,
 290          (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
 291     }
 292   }
 293 }
   1 /*
   2  * jcmainct.c
   3  *
   4  * Copyright (C) 1994-1996, Thomas G. Lane.
   5  * Modified 2003-2012 by Guido Vollbeding.
   6  * This file is part of the Independent JPEG Group's software.
   7  * For conditions of distribution and use, see the accompanying README file.
   8  *
   9  * This file contains the main buffer controller for compression.
  10  * The main buffer lies between the pre-processor and the JPEG
  11  * compressor proper; it holds downsampled data in the JPEG colorspace.
  12  */
  13 
  14 #define JPEG_INTERNALS
  15 #include "jinclude.h"
  16 #include "jpeglib.h"
  17 
  18 
  19 /* Note: currently, there is no operating mode in which a full-image buffer
  20  * is needed at this step.  If there were, that mode could not be used with
  21  * "raw data" input, since this module is bypassed in that case.  However,
  22  * we've left the code here for possible use in special applications.
  23  */
  24 #undef FULL_MAIN_BUFFER_SUPPORTED
  25 


  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 mainp = (my_main_ptr) cinfo->main;
  73 
  74   /* Do nothing in raw-data mode. */
  75   if (cinfo->raw_data_in)
  76     return;
  77 
  78   mainp->cur_iMCU_row = 0;   /* initialize counters */
  79   mainp->rowgroup_ctr = 0;
  80   mainp->suspended = FALSE;
  81   mainp->pass_mode = pass_mode;      /* save mode for use by process_data */
  82 
  83   switch (pass_mode) {
  84   case JBUF_PASS_THRU:
  85 #ifdef FULL_MAIN_BUFFER_SUPPORTED
  86     if (mainp->whole_image[0] != NULL)
  87       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  88 #endif
  89     mainp->pub.process_data = process_data_simple_main;
  90     break;
  91 #ifdef FULL_MAIN_BUFFER_SUPPORTED
  92   case JBUF_SAVE_SOURCE:
  93   case JBUF_CRANK_DEST:
  94   case JBUF_SAVE_AND_PASS:
  95     if (mainp->whole_image[0] == NULL)
  96       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  97     mainp->pub.process_data = process_data_buffer_main;
  98     break;
  99 #endif
 100   default:
 101     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
 102     break;
 103   }
 104 }
 105 
 106 
 107 /*
 108  * Process some data.
 109  * This routine handles the simple pass-through mode,
 110  * where we have only a strip buffer.
 111  */
 112 
 113 METHODDEF(void)
 114 process_data_simple_main (j_compress_ptr cinfo,
 115                           JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
 116                           JDIMENSION in_rows_avail)
 117 {
 118   my_main_ptr mainp = (my_main_ptr) cinfo->main;
 119 
 120   while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
 121     /* Read input data if we haven't filled the main buffer yet */
 122     if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
 123       (*cinfo->prep->pre_process_data) (cinfo,
 124                                         input_buf, in_row_ctr, in_rows_avail,
 125                                         mainp->buffer, &mainp->rowgroup_ctr,
 126                                         (JDIMENSION) cinfo->min_DCT_v_scaled_size);
 127 
 128     /* If we don't have a full iMCU row buffered, return to application for
 129      * more data.  Note that preprocessor will always pad to fill the iMCU row
 130      * at the bottom of the image.
 131      */
 132     if (mainp->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
 133       return;
 134 
 135     /* Send the completed row to the compressor */
 136     if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
 137       /* If compressor did not consume the whole row, then we must need to
 138        * suspend processing and return to the application.  In this situation
 139        * we pretend we didn't yet consume the last input row; otherwise, if
 140        * it happened to be the last row of the image, the application would
 141        * think we were done.
 142        */
 143       if (! mainp->suspended) {
 144         (*in_row_ctr)--;
 145         mainp->suspended = TRUE;
 146       }
 147       return;
 148     }
 149     /* We did finish the row.  Undo our little suspension hack if a previous
 150      * call suspended; then mark the main buffer empty.
 151      */
 152     if (mainp->suspended) {
 153       (*in_row_ctr)++;
 154       mainp->suspended = FALSE;
 155     }
 156     mainp->rowgroup_ctr = 0;
 157     mainp->cur_iMCU_row++;
 158   }
 159 }
 160 
 161 
 162 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 163 
 164 /*
 165  * Process some data.
 166  * This routine handles all of the modes that use a full-size buffer.
 167  */
 168 
 169 METHODDEF(void)
 170 process_data_buffer_main (j_compress_ptr cinfo,
 171                           JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
 172                           JDIMENSION in_rows_avail)
 173 {
 174   my_main_ptr mainp = (my_main_ptr) cinfo->main;
 175   int ci;
 176   jpeg_component_info *compptr;
 177   boolean writing = (mainp->pass_mode != JBUF_CRANK_DEST);
 178 
 179   while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
 180     /* Realign the virtual buffers if at the start of an iMCU row. */
 181     if (mainp->rowgroup_ctr == 0) {
 182       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 183            ci++, compptr++) {
 184         mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
 185           ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
 186            ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
 187            (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
 188            writing);
 189       }
 190       /* In a read pass, pretend we just read some source data. */
 191       if (! writing) {
 192         *in_row_ctr += (JDIMENSION)
 193           (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
 194         mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
 195       }
 196     }
 197 
 198     /* If a write pass, read input data until the current iMCU row is full. */
 199     /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
 200     if (writing) {
 201       (*cinfo->prep->pre_process_data) (cinfo,
 202                                         input_buf, in_row_ctr, in_rows_avail,
 203                                         mainp->buffer, &mainp->rowgroup_ctr,
 204                                         (JDIMENSION) cinfo->min_DCT_v_scaled_size);
 205       /* Return to application if we need more data to fill the iMCU row. */
 206       if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
 207         return;
 208     }
 209 
 210     /* Emit data, unless this is a sink-only pass. */
 211     if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
 212       if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
 213         /* If compressor did not consume the whole row, then we must need to
 214          * suspend processing and return to the application.  In this situation
 215          * we pretend we didn't yet consume the last input row; otherwise, if
 216          * it happened to be the last row of the image, the application would
 217          * think we were done.
 218          */
 219         if (! mainp->suspended) {
 220           (*in_row_ctr)--;
 221           mainp->suspended = TRUE;
 222         }
 223         return;
 224       }
 225       /* We did finish the row.  Undo our little suspension hack if a previous
 226        * call suspended; then mark the main buffer empty.
 227        */
 228       if (mainp->suspended) {
 229         (*in_row_ctr)++;
 230         mainp->suspended = FALSE;
 231       }
 232     }
 233 
 234     /* If get here, we are done with this iMCU row.  Mark buffer empty. */
 235     mainp->rowgroup_ctr = 0;
 236     mainp->cur_iMCU_row++;
 237   }
 238 }
 239 
 240 #endif /* FULL_MAIN_BUFFER_SUPPORTED */
 241 
 242 
 243 /*
 244  * Initialize main buffer controller.
 245  */
 246 
 247 GLOBAL(void)
 248 jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 249 {
 250   my_main_ptr mainp;
 251   int ci;
 252   jpeg_component_info *compptr;
 253 
 254   mainp = (my_main_ptr)
 255     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 256                                 SIZEOF(my_main_controller));
 257   cinfo->main = &mainp->pub;
 258   mainp->pub.start_pass = start_pass_main;
 259 
 260   /* We don't need to create a buffer in raw-data mode. */
 261   if (cinfo->raw_data_in)
 262     return;
 263 
 264   /* Create the buffer.  It holds downsampled data, so each component
 265    * may be of a different size.
 266    */
 267   if (need_full_buffer) {
 268 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 269     /* Allocate a full-image virtual array for each component */
 270     /* Note we pad the bottom to a multiple of the iMCU height */
 271     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 272          ci++, compptr++) {
 273       mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
 274         ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
 275          compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
 276          ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
 277                                  (long) compptr->v_samp_factor)) *
 278          ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
 279          (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
 280     }
 281 #else
 282     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
 283 #endif
 284   } else {
 285 #ifdef FULL_MAIN_BUFFER_SUPPORTED
 286     mainp->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       mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
 292         ((j_common_ptr) cinfo, JPOOL_IMAGE,
 293          compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
 294          (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
 295     }
 296   }
 297 }
< prev index next >