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