]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdsample.c
ENH: Final -hopefully- change to jpeg lib. In order to match ITK structure, and be...
[gdcm.git] / src / gdcmjpeg / jdsample.c
1 /*
2  * jdsample.c
3  *
4  * Copyright (C) 1991-1998, 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 upsampling routines.
9  *
10  * Upsampling input data is counted in "row groups".  A row group
11  * is defined to be (v_samp_factor * codec_data_unit / min_codec_data_unit)
12  * sample rows of each component.  Upsampling will normally produce
13  * max_v_samp_factor pixel rows from each row group (but this could vary
14  * if the upsampler is applying a scale factor of its own).
15  *
16  * An excellent reference for image resampling is
17  *   Digital Image Warping, George Wolberg, 1990.
18  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
19  */
20
21 #define JPEG_INTERNALS
22 #include "jinclude.h"
23 #include "jpeglib.h"
24
25
26 /* Pointer to routine to upsample a single component */
27 typedef JMETHOD(void, upsample1_ptr,
28     (j_decompress_ptr cinfo, jpeg_component_info * compptr,
29      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
30
31 /* Private subobject */
32
33 typedef struct {
34   struct jpeg_upsampler pub;  /* public fields */
35
36   /* Color conversion buffer.  When using separate upsampling and color
37    * conversion steps, this buffer holds one upsampled row group until it
38    * has been color converted and output.
39    * Note: we do not allocate any storage for component(s) which are full-size,
40    * ie do not need rescaling.  The corresponding entry of color_buf[] is
41    * simply set to point to the input data array, thereby avoiding copying.
42    */
43   JSAMPARRAY color_buf[MAX_COMPONENTS];
44
45   /* Per-component upsampling method pointers */
46   upsample1_ptr methods[MAX_COMPONENTS];
47
48   int next_row_out;    /* counts rows emitted from color_buf */
49   JDIMENSION rows_to_go;  /* counts rows remaining in image */
50
51   /* Height of an input row group for each component. */
52   int rowgroup_height[MAX_COMPONENTS];
53
54   /* These arrays save pixel expansion factors so that int_expand need not
55    * recompute them each time.  They are unused for other upsampling methods.
56    */
57   UINT8 h_expand[MAX_COMPONENTS];
58   UINT8 v_expand[MAX_COMPONENTS];
59 } my_upsampler;
60
61 typedef my_upsampler * my_upsample_ptr;
62
63
64 /*
65  * Initialize for an upsampling pass.
66  */
67
68 METHODDEF(void)
69 start_pass_upsample (j_decompress_ptr cinfo)
70 {
71   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
72
73   /* Mark the conversion buffer empty */
74   upsample->next_row_out = cinfo->max_v_samp_factor;
75   /* Initialize total-height counter for detecting bottom of image */
76   upsample->rows_to_go = cinfo->output_height;
77 }
78
79
80 /*
81  * Control routine to do upsampling (and color conversion).
82  *
83  * In this version we upsample each component independently.
84  * We upsample one row group into the conversion buffer, then apply
85  * color conversion a row at a time.
86  */
87
88 METHODDEF(void)
89 sep_upsample (j_decompress_ptr cinfo,
90         JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
91         JDIMENSION in_row_groups_avail,
92         JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
93         JDIMENSION out_rows_avail)
94 {
95   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
96   int ci;
97   jpeg_component_info * compptr;
98   JDIMENSION num_rows;
99   (void)in_row_groups_avail;
100
101   /* Fill the conversion buffer, if it's empty */
102   if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
103     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104    ci++, compptr++) {
105       /* Invoke per-component upsample method.  Notice we pass a POINTER
106        * to color_buf[ci], so that fullsize_upsample can change it.
107        */
108       (*upsample->methods[ci]) (cinfo, compptr,
109   input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
110   upsample->color_buf + ci);
111     }
112     upsample->next_row_out = 0;
113   }
114
115   /* Color-convert and emit rows */
116
117   /* How many we have in the buffer: */
118   num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
119   /* Not more than the distance to the end of the image.  Need this test
120    * in case the image height is not a multiple of max_v_samp_factor:
121    */
122   if (num_rows > upsample->rows_to_go) 
123     num_rows = upsample->rows_to_go;
124   /* And not more than what the client can accept: */
125   out_rows_avail -= *out_row_ctr;
126   if (num_rows > out_rows_avail)
127     num_rows = out_rows_avail;
128
129   (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
130              (JDIMENSION) upsample->next_row_out,
131              output_buf + *out_row_ctr,
132              (int) num_rows);
133
134   /* Adjust counts */
135   *out_row_ctr += num_rows;
136   upsample->rows_to_go -= num_rows;
137   upsample->next_row_out += num_rows;
138   /* When the buffer is emptied, declare this input row group consumed */
139   if (upsample->next_row_out >= cinfo->max_v_samp_factor)
140     (*in_row_group_ctr)++;
141 }
142
143
144 /*
145  * These are the routines invoked by sep_upsample to upsample pixel values
146  * of a single component.  One row group is processed per call.
147  */
148
149
150 /*
151  * For full-size components, we just make color_buf[ci] point at the
152  * input buffer, and thus avoid copying any data.  Note that this is
153  * safe only because sep_upsample doesn't declare the input row group
154  * "consumed" until we are done color converting and emitting it.
155  */
156
157 METHODDEF(void)
158 fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
159        JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
160 {
161   (void)cinfo;(void)compptr;
162   *output_data_ptr = input_data;
163 }
164
165
166 /*
167  * This is a no-op version used for "uninteresting" components.
168  * These components will not be referenced by color conversion.
169  */
170
171 METHODDEF(void)
172 noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
173          JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
174 {
175   (void)cinfo;(void)compptr;(void)input_data;
176   *output_data_ptr = NULL;  /* safety check */
177 }
178
179
180 /*
181  * This version handles any integral sampling ratios.
182  * This is not used for typical JPEG files, so it need not be fast.
183  * Nor, for that matter, is it particularly accurate: the algorithm is
184  * simple replication of the input pixel onto the corresponding output
185  * pixels.  The hi-falutin sampling literature refers to this as a
186  * "box filter".  A box filter tends to introduce visible artifacts,
187  * so if you are actually going to use 3:1 or 4:1 sampling ratios
188  * you would be well advised to improve this code.
189  */
190
191 METHODDEF(void)
192 int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
193         JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
194 {
195   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
196   JSAMPARRAY output_data = *output_data_ptr;
197   register JSAMPROW inptr, outptr;
198   register JSAMPLE invalue;
199   register int h;
200   JSAMPROW outend;
201   int h_expand, v_expand;
202   int inrow, outrow;
203
204   h_expand = upsample->h_expand[compptr->component_index];
205   v_expand = upsample->v_expand[compptr->component_index];
206
207   inrow = outrow = 0;
208   while (outrow < cinfo->max_v_samp_factor) {
209     /* Generate one output row with proper horizontal expansion */
210     inptr = input_data[inrow];
211     outptr = output_data[outrow];
212     outend = outptr + cinfo->output_width;
213     while (outptr < outend) {
214       invalue = *inptr++;  /* don't need GETJSAMPLE() here */
215       for (h = h_expand; h > 0; h--) {
216   *outptr++ = invalue;
217       }
218     }
219     /* Generate any additional output rows by duplicating the first one */
220     if (v_expand > 1) {
221       jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
222       v_expand-1, cinfo->output_width);
223     }
224     inrow++;
225     outrow += v_expand;
226   }
227 }
228
229
230 /*
231  * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
232  * It's still a box filter.
233  */
234
235 METHODDEF(void)
236 h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
237          JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
238 {
239   JSAMPARRAY output_data = *output_data_ptr;
240   register JSAMPROW inptr, outptr;
241   register JSAMPLE invalue;
242   JSAMPROW outend;
243   int inrow;
244   (void)compptr;
245
246   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
247     inptr = input_data[inrow];
248     outptr = output_data[inrow];
249     outend = outptr + cinfo->output_width;
250     while (outptr < outend) {
251       invalue = *inptr++;  /* don't need GETJSAMPLE() here */
252       *outptr++ = invalue;
253       *outptr++ = invalue;
254     }
255   }
256 }
257
258
259 /*
260  * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
261  * It's still a box filter.
262  */
263
264 METHODDEF(void)
265 h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
266          JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
267 {
268   JSAMPARRAY output_data = *output_data_ptr;
269   register JSAMPROW inptr, outptr;
270   register JSAMPLE invalue;
271   JSAMPROW outend;
272   int inrow, outrow;
273   (void)compptr;
274
275   inrow = outrow = 0;
276   while (outrow < cinfo->max_v_samp_factor) {
277     inptr = input_data[inrow];
278     outptr = output_data[outrow];
279     outend = outptr + cinfo->output_width;
280     while (outptr < outend) {
281       invalue = *inptr++;  /* don't need GETJSAMPLE() here */
282       *outptr++ = invalue;
283       *outptr++ = invalue;
284     }
285     jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
286           1, cinfo->output_width);
287     inrow++;
288     outrow += 2;
289   }
290 }
291
292
293 /*
294  * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
295  *
296  * The upsampling algorithm is linear interpolation between pixel centers,
297  * also known as a "triangle filter".  This is a good compromise between
298  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
299  * of the way between input pixel centers.
300  *
301  * A note about the "bias" calculations: when rounding fractional values to
302  * integer, we do not want to always round 0.5 up to the next integer.
303  * If we did that, we'd introduce a noticeable bias towards larger values.
304  * Instead, this code is arranged so that 0.5 will be rounded up or down at
305  * alternate pixel locations (a simple ordered dither pattern).
306  */
307
308 METHODDEF(void)
309 h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
310          JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
311 {
312   JSAMPARRAY output_data = *output_data_ptr;
313   register JSAMPROW inptr, outptr;
314   register int invalue;
315   register JDIMENSION colctr;
316   int inrow;
317
318   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
319     inptr = input_data[inrow];
320     outptr = output_data[inrow];
321     /* Special case for first column */
322     invalue = GETJSAMPLE(*inptr++);
323     *outptr++ = (JSAMPLE) invalue;
324     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
325
326     for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
327       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
328       invalue = GETJSAMPLE(*inptr++) * 3;
329       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
330       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
331     }
332
333     /* Special case for last column */
334     invalue = GETJSAMPLE(*inptr);
335     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
336     *outptr++ = (JSAMPLE) invalue;
337   }
338 }
339
340
341 /*
342  * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
343  * Again a triangle filter; see comments for h2v1 case, above.
344  *
345  * It is OK for us to reference the adjacent input rows because we demanded
346  * context from the main buffer controller (see initialization code).
347  */
348
349 METHODDEF(void)
350 h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
351          JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
352 {
353   JSAMPARRAY output_data = *output_data_ptr;
354   register JSAMPROW inptr0, inptr1, outptr;
355 #if BITS_IN_JSAMPLE == 8
356   register int thiscolsum, lastcolsum, nextcolsum;
357 #else
358   register INT32 thiscolsum, lastcolsum, nextcolsum;
359 #endif
360   register JDIMENSION colctr;
361   int inrow, outrow, v;
362
363   inrow = outrow = 0;
364   while (outrow < cinfo->max_v_samp_factor) {
365     for (v = 0; v < 2; v++) {
366       /* inptr0 points to nearest input row, inptr1 points to next nearest */
367       inptr0 = input_data[inrow];
368       if (v == 0)    /* next nearest is row above */
369   inptr1 = input_data[inrow-1];
370       else      /* next nearest is row below */
371   inptr1 = input_data[inrow+1];
372       outptr = output_data[outrow++];
373
374       /* Special case for first column */
375       thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
376       nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
377       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
378       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
379       lastcolsum = thiscolsum; thiscolsum = nextcolsum;
380
381       for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
382   /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
383   /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
384   nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
385   *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
386   *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
387   lastcolsum = thiscolsum; thiscolsum = nextcolsum;
388       }
389
390       /* Special case for last column */
391       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
392       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
393     }
394     inrow++;
395   }
396 }
397
398
399 /*
400  * Module initialization routine for upsampling.
401  */
402
403 GLOBAL(void)
404 jinit_upsampler (j_decompress_ptr cinfo)
405 {
406   my_upsample_ptr upsample;
407   int ci;
408   jpeg_component_info * compptr;
409   boolean need_buffer, do_fancy;
410   int h_in_group, v_in_group, h_out_group, v_out_group;
411
412   upsample = (my_upsample_ptr)
413     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
414         SIZEOF(my_upsampler));
415   cinfo->upsample = (struct jpeg_upsampler *) upsample;
416   upsample->pub.start_pass = start_pass_upsample;
417   upsample->pub.upsample = sep_upsample;
418   upsample->pub.need_context_rows = FALSE; /* until we find out differently */
419
420   if (cinfo->CCIR601_sampling)  /* this isn't supported */
421     ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
422
423   /* jdmainct.c doesn't support context rows when min_codec_data_unit = 1,
424    * so don't ask for it.
425    */
426   do_fancy = cinfo->do_fancy_upsampling && cinfo->min_codec_data_unit > 1;
427
428   /* Verify we can handle the sampling factors, select per-component methods,
429    * and create storage as needed.
430    */
431   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
432        ci++, compptr++) {
433     /* Compute size of an "input group" after IDCT scaling.  This many samples
434      * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
435      */
436     h_in_group = (compptr->h_samp_factor * compptr->codec_data_unit) /
437      cinfo->min_codec_data_unit;
438     v_in_group = (compptr->v_samp_factor * compptr->codec_data_unit) /
439      cinfo->min_codec_data_unit;
440     h_out_group = cinfo->max_h_samp_factor;
441     v_out_group = cinfo->max_v_samp_factor;
442     upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
443     need_buffer = TRUE;
444     if (! compptr->component_needed) {
445       /* Don't bother to upsample an uninteresting component. */
446       upsample->methods[ci] = noop_upsample;
447       need_buffer = FALSE;
448     } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
449       /* Fullsize components can be processed without any work. */
450       upsample->methods[ci] = fullsize_upsample;
451       need_buffer = FALSE;
452     } else if (h_in_group * 2 == h_out_group &&
453          v_in_group == v_out_group) {
454       /* Special cases for 2h1v upsampling */
455       if (do_fancy && compptr->downsampled_width > 2)
456   upsample->methods[ci] = h2v1_fancy_upsample;
457       else
458   upsample->methods[ci] = h2v1_upsample;
459     } else if (h_in_group * 2 == h_out_group &&
460          v_in_group * 2 == v_out_group) {
461       /* Special cases for 2h2v upsampling */
462       if (do_fancy && compptr->downsampled_width > 2) {
463   upsample->methods[ci] = h2v2_fancy_upsample;
464   upsample->pub.need_context_rows = TRUE;
465       } else
466   upsample->methods[ci] = h2v2_upsample;
467     } else if ((h_out_group % h_in_group) == 0 &&
468          (v_out_group % v_in_group) == 0) {
469       /* Generic integral-factors upsampling method */
470       upsample->methods[ci] = int_upsample;
471       upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
472       upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
473     } else
474       ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
475     if (need_buffer) {
476       upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
477   ((j_common_ptr) cinfo, JPOOL_IMAGE,
478    (JDIMENSION) jround_up((long) cinfo->output_width,
479         (long) cinfo->max_h_samp_factor),
480    (JDIMENSION) cinfo->max_v_samp_factor);
481     }
482   }
483 }