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

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