source: golgotha/src/i4/loaders/jpg/jdpostct.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 10.3 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9/*
10 * jdpostct.c
11 *
12 * Copyright (C) 1994-1996, Thomas G. Lane.
13 * This file is part of the Independent JPEG Group's software.
14 * For conditions of distribution and use, see the accompanying README file.
15 *
16 * This file contains the decompression postprocessing controller.
17 * This controller manages the upsampling, color conversion, and color
18 * quantization/reduction steps; specifically, it controls the buffering
19 * between upsample/color conversion and color quantization/reduction.
20 *
21 * If no color quantization/reduction is required, then this module has no
22 * work to do, and it just hands off to the upsample/color conversion code.
23 * An integrated upsample/convert/quantize process would replace this module
24 * entirely.
25 */
26
27#define JPEG_INTERNALS
28#include "loaders/jpg/jinclude.h"
29#include "loaders/jpg/jpeglib.h"
30
31
32/* Private buffer controller object */
33
34typedef struct {
35  struct jpeg_d_post_controller pub; /* public fields */
36
37  /* Color quantization source buffer: this holds output data from
38   * the upsample/color conversion step to be passed to the quantizer.
39   * For two-pass color quantization, we need a full-image buffer;
40   * for one-pass operation, a strip buffer is sufficient.
41   */
42  jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
43  JSAMPARRAY buffer;            /* strip buffer, or current strip of virtual */
44  JDIMENSION strip_height;      /* buffer size in rows */
45  /* for two-pass mode only: */
46  JDIMENSION starting_row;      /* row # of first row in current strip */
47  JDIMENSION next_row;          /* index of next row to fill/empty in strip */
48} my_post_controller;
49
50typedef my_post_controller * my_post_ptr;
51
52
53/* Forward declarations */
54METHODDEF(void) post_process_1pass
55        JPP((j_decompress_ptr cinfo,
56             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
57             JDIMENSION in_row_groups_avail,
58             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
59             JDIMENSION out_rows_avail));
60#ifdef QUANT_2PASS_SUPPORTED
61METHODDEF(void) post_process_prepass
62        JPP((j_decompress_ptr cinfo,
63             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
64             JDIMENSION in_row_groups_avail,
65             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
66             JDIMENSION out_rows_avail));
67METHODDEF(void) post_process_2pass
68        JPP((j_decompress_ptr cinfo,
69             JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
70             JDIMENSION in_row_groups_avail,
71             JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
72             JDIMENSION out_rows_avail));
73#endif
74
75
76/*
77 * Initialize for a processing pass.
78 */
79
80METHODDEF(void)
81start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
82{
83  my_post_ptr post = (my_post_ptr) cinfo->post;
84
85  switch (pass_mode) {
86  case JBUF_PASS_THRU:
87    if (cinfo->quantize_colors) {
88      /* Single-pass processing with color quantization. */
89      post->pub.post_process_data = post_process_1pass;
90      /* We could be doing buffered-image output before starting a 2-pass
91       * color quantization; in that case, jinit_d_post_controller did not
92       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
93       */
94      if (post->buffer == NULL) {
95        post->buffer = (*cinfo->mem->access_virt_sarray)
96          ((j_common_ptr) cinfo, post->whole_image,
97           (JDIMENSION) 0, post->strip_height, TRUE);
98      }
99    } else {
100      /* For single-pass processing without color quantization,
101       * I have no work to do; just call the upsampler directly.
102       */
103      post->pub.post_process_data = cinfo->upsample->upsample;
104    }
105    break;
106#ifdef QUANT_2PASS_SUPPORTED
107  case JBUF_SAVE_AND_PASS:
108    /* First pass of 2-pass quantization */
109    if (post->whole_image == NULL)
110      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
111    post->pub.post_process_data = post_process_prepass;
112    break;
113  case JBUF_CRANK_DEST:
114    /* Second pass of 2-pass quantization */
115    if (post->whole_image == NULL)
116      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117    post->pub.post_process_data = post_process_2pass;
118    break;
119#endif /* QUANT_2PASS_SUPPORTED */
120  default:
121    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122    break;
123  }
124  post->starting_row = post->next_row = 0;
125}
126
127
128/*
129 * Process some data in the one-pass (strip buffer) case.
130 * This is used for color precision reduction as well as one-pass quantization.
131 */
132
133METHODDEF(void)
134post_process_1pass (j_decompress_ptr cinfo,
135                    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
136                    JDIMENSION in_row_groups_avail,
137                    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
138                    JDIMENSION out_rows_avail)
139{
140  my_post_ptr post = (my_post_ptr) cinfo->post;
141  JDIMENSION num_rows, max_rows;
142
143  /* Fill the buffer, but not more than what we can dump out in one go. */
144  /* Note we rely on the upsampler to detect bottom of image. */
145  max_rows = out_rows_avail - *out_row_ctr;
146  if (max_rows > post->strip_height)
147    max_rows = post->strip_height;
148  num_rows = 0;
149  (*cinfo->upsample->upsample) (cinfo,
150                input_buf, in_row_group_ctr, in_row_groups_avail,
151                post->buffer, &num_rows, max_rows);
152  /* Quantize and emit data. */
153  (*cinfo->cquantize->color_quantize) (cinfo,
154                post->buffer, output_buf + *out_row_ctr, (int) num_rows);
155  *out_row_ctr += num_rows;
156}
157
158
159#ifdef QUANT_2PASS_SUPPORTED
160
161/*
162 * Process some data in the first pass of 2-pass quantization.
163 */
164
165METHODDEF(void)
166post_process_prepass (j_decompress_ptr cinfo,
167                      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
168                      JDIMENSION in_row_groups_avail,
169                      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
170                      JDIMENSION out_rows_avail)
171{
172  my_post_ptr post = (my_post_ptr) cinfo->post;
173  JDIMENSION old_next_row, num_rows;
174
175  /* Reposition virtual buffer if at start of strip. */
176  if (post->next_row == 0) {
177    post->buffer = (*cinfo->mem->access_virt_sarray)
178        ((j_common_ptr) cinfo, post->whole_image,
179         post->starting_row, post->strip_height, TRUE);
180  }
181
182  /* Upsample some data (up to a strip height's worth). */
183  old_next_row = post->next_row;
184  (*cinfo->upsample->upsample) (cinfo,
185                input_buf, in_row_group_ctr, in_row_groups_avail,
186                post->buffer, &post->next_row, post->strip_height);
187
188  /* Allow quantizer to scan new data.  No data is emitted, */
189  /* but we advance out_row_ctr so outer loop can tell when we're done. */
190  if (post->next_row > old_next_row) {
191    num_rows = post->next_row - old_next_row;
192    (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
193                                         (JSAMPARRAY) NULL, (int) num_rows);
194    *out_row_ctr += num_rows;
195  }
196
197  /* Advance if we filled the strip. */
198  if (post->next_row >= post->strip_height) {
199    post->starting_row += post->strip_height;
200    post->next_row = 0;
201  }
202}
203
204
205/*
206 * Process some data in the second pass of 2-pass quantization.
207 */
208
209METHODDEF(void)
210post_process_2pass (j_decompress_ptr cinfo,
211                    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
212                    JDIMENSION in_row_groups_avail,
213                    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
214                    JDIMENSION out_rows_avail)
215{
216  my_post_ptr post = (my_post_ptr) cinfo->post;
217  JDIMENSION num_rows, max_rows;
218
219  /* Reposition virtual buffer if at start of strip. */
220  if (post->next_row == 0) {
221    post->buffer = (*cinfo->mem->access_virt_sarray)
222        ((j_common_ptr) cinfo, post->whole_image,
223         post->starting_row, post->strip_height, FALSE);
224  }
225
226  /* Determine number of rows to emit. */
227  num_rows = post->strip_height - post->next_row; /* available in strip */
228  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
229  if (num_rows > max_rows)
230    num_rows = max_rows;
231  /* We have to check bottom of image here, can't depend on upsampler. */
232  max_rows = cinfo->output_height - post->starting_row;
233  if (num_rows > max_rows)
234    num_rows = max_rows;
235
236  /* Quantize and emit data. */
237  (*cinfo->cquantize->color_quantize) (cinfo,
238                post->buffer + post->next_row, output_buf + *out_row_ctr,
239                (int) num_rows);
240  *out_row_ctr += num_rows;
241
242  /* Advance if we filled the strip. */
243  post->next_row += num_rows;
244  if (post->next_row >= post->strip_height) {
245    post->starting_row += post->strip_height;
246    post->next_row = 0;
247  }
248}
249
250#endif /* QUANT_2PASS_SUPPORTED */
251
252
253/*
254 * Initialize postprocessing controller.
255 */
256
257GLOBAL(void)
258jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
259{
260  my_post_ptr post;
261
262  post = (my_post_ptr)
263    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
264                                SIZEOF(my_post_controller));
265  cinfo->post = (struct jpeg_d_post_controller *) post;
266  post->pub.start_pass = start_pass_dpost;
267  post->whole_image = NULL;     /* flag for no virtual arrays */
268  post->buffer = NULL;          /* flag for no strip buffer */
269
270  /* Create the quantization buffer, if needed */
271  if (cinfo->quantize_colors) {
272    /* The buffer strip height is max_v_samp_factor, which is typically
273     * an efficient number of rows for upsampling to return.
274     * (In the presence of output rescaling, we might want to be smarter?)
275     */
276    post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
277    if (need_full_buffer) {
278      /* Two-pass color quantization: need full-image storage. */
279      /* We round up the number of rows to a multiple of the strip height. */
280#ifdef QUANT_2PASS_SUPPORTED
281      post->whole_image = (*cinfo->mem->request_virt_sarray)
282        ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
283         cinfo->output_width * cinfo->out_color_components,
284         (JDIMENSION) jround_up((long) cinfo->output_height,
285                                (long) post->strip_height),
286         post->strip_height);
287#else
288      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
289#endif /* QUANT_2PASS_SUPPORTED */
290    } else {
291      /* One-pass color quantization: just make a strip buffer. */
292      post->buffer = (*cinfo->mem->alloc_sarray)
293        ((j_common_ptr) cinfo, JPOOL_IMAGE,
294         cinfo->output_width * cinfo->out_color_components,
295         post->strip_height);
296    }
297  }
298}
Note: See TracBrowser for help on using the repository browser.