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