Back to index

plt-scheme  4.2.1
jdpostct.c
Go to the documentation of this file.
00001 /*
00002  * jdpostct.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the decompression postprocessing controller.
00009  * This controller manages the upsampling, color conversion, and color
00010  * quantization/reduction steps; specifically, it controls the buffering
00011  * between upsample/color conversion and color quantization/reduction.
00012  *
00013  * If no color quantization/reduction is required, then this module has no
00014  * work to do, and it just hands off to the upsample/color conversion code.
00015  * An integrated upsample/convert/quantize process would replace this module
00016  * entirely.
00017  */
00018 
00019 #define JPEG_INTERNALS
00020 #include "jinclude.h"
00021 #include "jpeglib.h"
00022 
00023 
00024 /* Private buffer controller object */
00025 
00026 typedef struct {
00027   struct jpeg_d_post_controller pub; /* public fields */
00028 
00029   /* Color quantization source buffer: this holds output data from
00030    * the upsample/color conversion step to be passed to the quantizer.
00031    * For two-pass color quantization, we need a full-image buffer;
00032    * for one-pass operation, a strip buffer is sufficient.
00033    */
00034   jvirt_sarray_ptr whole_image;    /* virtual array, or NULL if one-pass */
00035   JSAMPARRAY buffer;        /* strip buffer, or current strip of virtual */
00036   JDIMENSION strip_height;  /* buffer size in rows */
00037   /* for two-pass mode only: */
00038   JDIMENSION starting_row;  /* row # of first row in current strip */
00039   JDIMENSION next_row;             /* index of next row to fill/empty in strip */
00040 } my_post_controller;
00041 
00042 typedef my_post_controller * my_post_ptr;
00043 
00044 
00045 /* Forward declarations */
00046 METHODDEF(void) post_process_1pass
00047        JPP((j_decompress_ptr cinfo,
00048             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00049             JDIMENSION in_row_groups_avail,
00050             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00051             JDIMENSION out_rows_avail));
00052 #ifdef QUANT_2PASS_SUPPORTED
00053 METHODDEF(void) post_process_prepass
00054        JPP((j_decompress_ptr cinfo,
00055             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00056             JDIMENSION in_row_groups_avail,
00057             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00058             JDIMENSION out_rows_avail));
00059 METHODDEF(void) post_process_2pass
00060        JPP((j_decompress_ptr cinfo,
00061             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00062             JDIMENSION in_row_groups_avail,
00063             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00064             JDIMENSION out_rows_avail));
00065 #endif
00066 
00067 
00068 /*
00069  * Initialize for a processing pass.
00070  */
00071 
00072 METHODDEF(void)
00073 start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
00074 {
00075   my_post_ptr post = (my_post_ptr) cinfo->post;
00076 
00077   switch (pass_mode) {
00078   case JBUF_PASS_THRU:
00079     if (cinfo->quantize_colors) {
00080       /* Single-pass processing with color quantization. */
00081       post->pub.post_process_data = post_process_1pass;
00082       /* We could be doing buffered-image output before starting a 2-pass
00083        * color quantization; in that case, jinit_d_post_controller did not
00084        * allocate a strip buffer.  Use the virtual-array buffer as workspace.
00085        */
00086       if (post->buffer == NULL) {
00087        post->buffer = (*cinfo->mem->access_virt_sarray)
00088          ((j_common_ptr) cinfo, post->whole_image,
00089           (JDIMENSION) 0, post->strip_height, TRUE);
00090       }
00091     } else {
00092       /* For single-pass processing without color quantization,
00093        * I have no work to do; just call the upsampler directly.
00094        */
00095       post->pub.post_process_data = cinfo->upsample->upsample;
00096     }
00097     break;
00098 #ifdef QUANT_2PASS_SUPPORTED
00099   case JBUF_SAVE_AND_PASS:
00100     /* First pass of 2-pass quantization */
00101     if (post->whole_image == NULL)
00102       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00103     post->pub.post_process_data = post_process_prepass;
00104     break;
00105   case JBUF_CRANK_DEST:
00106     /* Second pass of 2-pass quantization */
00107     if (post->whole_image == NULL)
00108       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00109     post->pub.post_process_data = post_process_2pass;
00110     break;
00111 #endif /* QUANT_2PASS_SUPPORTED */
00112   default:
00113     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00114     break;
00115   }
00116   post->starting_row = post->next_row = 0;
00117 }
00118 
00119 
00120 /*
00121  * Process some data in the one-pass (strip buffer) case.
00122  * This is used for color precision reduction as well as one-pass quantization.
00123  */
00124 
00125 METHODDEF(void)
00126 post_process_1pass (j_decompress_ptr cinfo,
00127                   JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00128                   JDIMENSION in_row_groups_avail,
00129                   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00130                   JDIMENSION out_rows_avail)
00131 {
00132   my_post_ptr post = (my_post_ptr) cinfo->post;
00133   JDIMENSION num_rows, max_rows;
00134 
00135   /* Fill the buffer, but not more than what we can dump out in one go. */
00136   /* Note we rely on the upsampler to detect bottom of image. */
00137   max_rows = out_rows_avail - *out_row_ctr;
00138   if (max_rows > post->strip_height)
00139     max_rows = post->strip_height;
00140   num_rows = 0;
00141   (*cinfo->upsample->upsample) (cinfo,
00142               input_buf, in_row_group_ctr, in_row_groups_avail,
00143               post->buffer, &num_rows, max_rows);
00144   /* Quantize and emit data. */
00145   (*cinfo->cquantize->color_quantize) (cinfo,
00146               post->buffer, output_buf + *out_row_ctr, (int) num_rows);
00147   *out_row_ctr += num_rows;
00148 }
00149 
00150 
00151 #ifdef QUANT_2PASS_SUPPORTED
00152 
00153 /*
00154  * Process some data in the first pass of 2-pass quantization.
00155  */
00156 
00157 METHODDEF(void)
00158 post_process_prepass (j_decompress_ptr cinfo,
00159                     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00160                     JDIMENSION in_row_groups_avail,
00161                     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00162                     JDIMENSION out_rows_avail)
00163 {
00164   my_post_ptr post = (my_post_ptr) cinfo->post;
00165   JDIMENSION old_next_row, num_rows;
00166 
00167   /* Reposition virtual buffer if at start of strip. */
00168   if (post->next_row == 0) {
00169     post->buffer = (*cinfo->mem->access_virt_sarray)
00170        ((j_common_ptr) cinfo, post->whole_image,
00171         post->starting_row, post->strip_height, TRUE);
00172   }
00173 
00174   /* Upsample some data (up to a strip height's worth). */
00175   old_next_row = post->next_row;
00176   (*cinfo->upsample->upsample) (cinfo,
00177               input_buf, in_row_group_ctr, in_row_groups_avail,
00178               post->buffer, &post->next_row, post->strip_height);
00179 
00180   /* Allow quantizer to scan new data.  No data is emitted, */
00181   /* but we advance out_row_ctr so outer loop can tell when we're done. */
00182   if (post->next_row > old_next_row) {
00183     num_rows = post->next_row - old_next_row;
00184     (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
00185                                     (JSAMPARRAY) NULL, (int) num_rows);
00186     *out_row_ctr += num_rows;
00187   }
00188 
00189   /* Advance if we filled the strip. */
00190   if (post->next_row >= post->strip_height) {
00191     post->starting_row += post->strip_height;
00192     post->next_row = 0;
00193   }
00194 }
00195 
00196 
00197 /*
00198  * Process some data in the second pass of 2-pass quantization.
00199  */
00200 
00201 METHODDEF(void)
00202 post_process_2pass (j_decompress_ptr cinfo,
00203                   JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00204                   JDIMENSION in_row_groups_avail,
00205                   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00206                   JDIMENSION out_rows_avail)
00207 {
00208   my_post_ptr post = (my_post_ptr) cinfo->post;
00209   JDIMENSION num_rows, max_rows;
00210 
00211   /* Reposition virtual buffer if at start of strip. */
00212   if (post->next_row == 0) {
00213     post->buffer = (*cinfo->mem->access_virt_sarray)
00214        ((j_common_ptr) cinfo, post->whole_image,
00215         post->starting_row, post->strip_height, FALSE);
00216   }
00217 
00218   /* Determine number of rows to emit. */
00219   num_rows = post->strip_height - post->next_row; /* available in strip */
00220   max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
00221   if (num_rows > max_rows)
00222     num_rows = max_rows;
00223   /* We have to check bottom of image here, can't depend on upsampler. */
00224   max_rows = cinfo->output_height - post->starting_row;
00225   if (num_rows > max_rows)
00226     num_rows = max_rows;
00227 
00228   /* Quantize and emit data. */
00229   (*cinfo->cquantize->color_quantize) (cinfo,
00230               post->buffer + post->next_row, output_buf + *out_row_ctr,
00231               (int) num_rows);
00232   *out_row_ctr += num_rows;
00233 
00234   /* Advance if we filled the strip. */
00235   post->next_row += num_rows;
00236   if (post->next_row >= post->strip_height) {
00237     post->starting_row += post->strip_height;
00238     post->next_row = 0;
00239   }
00240 }
00241 
00242 #endif /* QUANT_2PASS_SUPPORTED */
00243 
00244 
00245 /*
00246  * Initialize postprocessing controller.
00247  */
00248 
00249 GLOBAL(void)
00250 jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
00251 {
00252   my_post_ptr post;
00253 
00254   post = (my_post_ptr)
00255     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00256                             SIZEOF(my_post_controller));
00257   cinfo->post = (struct jpeg_d_post_controller *) post;
00258   post->pub.start_pass = start_pass_dpost;
00259   post->whole_image = NULL; /* flag for no virtual arrays */
00260   post->buffer = NULL;             /* flag for no strip buffer */
00261 
00262   /* Create the quantization buffer, if needed */
00263   if (cinfo->quantize_colors) {
00264     /* The buffer strip height is max_v_samp_factor, which is typically
00265      * an efficient number of rows for upsampling to return.
00266      * (In the presence of output rescaling, we might want to be smarter?)
00267      */
00268     post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
00269     if (need_full_buffer) {
00270       /* Two-pass color quantization: need full-image storage. */
00271       /* We round up the number of rows to a multiple of the strip height. */
00272 #ifdef QUANT_2PASS_SUPPORTED
00273       post->whole_image = (*cinfo->mem->request_virt_sarray)
00274        ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
00275         cinfo->output_width * cinfo->out_color_components,
00276         (JDIMENSION) jround_up((long) cinfo->output_height,
00277                             (long) post->strip_height),
00278         post->strip_height);
00279 #else
00280       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00281 #endif /* QUANT_2PASS_SUPPORTED */
00282     } else {
00283       /* One-pass color quantization: just make a strip buffer. */
00284       post->buffer = (*cinfo->mem->alloc_sarray)
00285        ((j_common_ptr) cinfo, JPOOL_IMAGE,
00286         cinfo->output_width * cinfo->out_color_components,
00287         post->strip_height);
00288     }
00289   }
00290 }