source: golgotha/src/i4/loaders/jpg/jdmerge.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: 14.5 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 * jdmerge.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 code for merged upsampling/color conversion.
17 *
18 * This file combines functions from jdsample.c and jdcolor.c;
19 * read those files first to understand what's going on.
20 *
21 * When the chroma components are to be upsampled by simple replication
22 * (ie, box filtering), we can save some work in color conversion by
23 * calculating all the output pixels corresponding to a pair of chroma
24 * samples at one time.  In the conversion equations
25 *      R = Y           + K1 * Cr
26 *      G = Y + K2 * Cb + K3 * Cr
27 *      B = Y + K4 * Cb
28 * only the Y term varies among the group of pixels corresponding to a pair
29 * of chroma samples, so the rest of the terms can be calculated just once.
30 * At typical sampling ratios, this eliminates half or three-quarters of the
31 * multiplications needed for color conversion.
32 *
33 * This file currently provides implementations for the following cases:
34 *      YCbCr => RGB color conversion only.
35 *      Sampling ratios of 2h1v or 2h2v.
36 *      No scaling needed at upsample time.
37 *      Corner-aligned (non-CCIR601) sampling alignment.
38 * Other special cases could be added, but in most applications these are
39 * the only common cases.  (For uncommon cases we fall back on the more
40 * general code in jdsample.c and jdcolor.c.)
41 */
42
43#define JPEG_INTERNALS
44#include "loaders/jpg/jinclude.h"
45#include "loaders/jpg/jpeglib.h"
46
47#ifdef UPSAMPLE_MERGING_SUPPORTED
48
49
50/* Private subobject */
51
52typedef struct {
53  struct jpeg_upsampler pub;    /* public fields */
54
55  /* Pointer to routine to do actual upsampling/conversion of one row group */
56  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
57                           JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
58                           JSAMPARRAY output_buf));
59
60  /* Private state for YCC->RGB conversion */
61  int * Cr_r_tab;               /* => table for Cr to R conversion */
62  int * Cb_b_tab;               /* => table for Cb to B conversion */
63  INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
64  INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
65
66  /* For 2:1 vertical sampling, we produce two output rows at a time.
67   * We need a "spare" row buffer to hold the second output row if the
68   * application provides just a one-row buffer; we also use the spare
69   * to discard the dummy last row if the image height is odd.
70   */
71  JSAMPROW spare_row;
72  boolean spare_full;           /* T if spare buffer is occupied */
73
74  JDIMENSION out_row_width;     /* samples per output row */
75  JDIMENSION rows_to_go;        /* counts rows remaining in image */
76} my_upsampler;
77
78typedef my_upsampler * my_upsample_ptr;
79
80#define SCALEBITS       16      /* speediest right-shift on some machines */
81#define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
82#define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
83
84
85/*
86 * Initialize tables for YCC->RGB colorspace conversion.
87 * This is taken directly from jdcolor.c; see that file for more info.
88 */
89
90LOCAL(void)
91build_ycc_rgb_table (j_decompress_ptr cinfo)
92{
93  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
94  int i;
95  INT32 x;
96  SHIFT_TEMPS
97
98  upsample->Cr_r_tab = (int *)
99    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
100                                (MAXJSAMPLE+1) * SIZEOF(int));
101  upsample->Cb_b_tab = (int *)
102    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
103                                (MAXJSAMPLE+1) * SIZEOF(int));
104  upsample->Cr_g_tab = (INT32 *)
105    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
106                                (MAXJSAMPLE+1) * SIZEOF(INT32));
107  upsample->Cb_g_tab = (INT32 *)
108    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
109                                (MAXJSAMPLE+1) * SIZEOF(INT32));
110
111  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
112    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
113    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
114    /* Cr=>R value is nearest int to 1.40200 * x */
115    upsample->Cr_r_tab[i] = (int)
116                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
117    /* Cb=>B value is nearest int to 1.77200 * x */
118    upsample->Cb_b_tab[i] = (int)
119                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
120    /* Cr=>G value is scaled-up -0.71414 * x */
121    upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
122    /* Cb=>G value is scaled-up -0.34414 * x */
123    /* We also add in ONE_HALF so that need not do it in inner loop */
124    upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
125  }
126}
127
128
129/*
130 * Initialize for an upsampling pass.
131 */
132
133METHODDEF(void)
134start_pass_merged_upsample (j_decompress_ptr cinfo)
135{
136  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
137
138  /* Mark the spare buffer empty */
139  upsample->spare_full = FALSE;
140  /* Initialize total-height counter for detecting bottom of image */
141  upsample->rows_to_go = cinfo->output_height;
142}
143
144
145/*
146 * Control routine to do upsampling (and color conversion).
147 *
148 * The control routine just handles the row buffering considerations.
149 */
150
151METHODDEF(void)
152merged_2v_upsample (j_decompress_ptr cinfo,
153                    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
154                    JDIMENSION in_row_groups_avail,
155                    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
156                    JDIMENSION out_rows_avail)
157/* 2:1 vertical sampling case: may need a spare row. */
158{
159  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
160  JSAMPROW work_ptrs[2];
161  JDIMENSION num_rows;          /* number of rows returned to caller */
162
163  if (upsample->spare_full) {
164    /* If we have a spare row saved from a previous cycle, just return it. */
165    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
166                      1, upsample->out_row_width);
167    num_rows = 1;
168    upsample->spare_full = FALSE;
169  } else {
170    /* Figure number of rows to return to caller. */
171    num_rows = 2;
172    /* Not more than the distance to the end of the image. */
173    if (num_rows > upsample->rows_to_go)
174      num_rows = upsample->rows_to_go;
175    /* And not more than what the client can accept: */
176    out_rows_avail -= *out_row_ctr;
177    if (num_rows > out_rows_avail)
178      num_rows = out_rows_avail;
179    /* Create output pointer array for upsampler. */
180    work_ptrs[0] = output_buf[*out_row_ctr];
181    if (num_rows > 1) {
182      work_ptrs[1] = output_buf[*out_row_ctr + 1];
183    } else {
184      work_ptrs[1] = upsample->spare_row;
185      upsample->spare_full = TRUE;
186    }
187    /* Now do the upsampling. */
188    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
189  }
190
191  /* Adjust counts */
192  *out_row_ctr += num_rows;
193  upsample->rows_to_go -= num_rows;
194  /* When the buffer is emptied, declare this input row group consumed */
195  if (! upsample->spare_full)
196    (*in_row_group_ctr)++;
197}
198
199
200METHODDEF(void)
201merged_1v_upsample (j_decompress_ptr cinfo,
202                    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
203                    JDIMENSION in_row_groups_avail,
204                    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
205                    JDIMENSION out_rows_avail)
206/* 1:1 vertical sampling case: much easier, never need a spare row. */
207{
208  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
209
210  /* Just do the upsampling. */
211  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
212                         output_buf + *out_row_ctr);
213  /* Adjust counts */
214  (*out_row_ctr)++;
215  (*in_row_group_ctr)++;
216}
217
218
219/*
220 * These are the routines invoked by the control routines to do
221 * the actual upsampling/conversion.  One row group is processed per call.
222 *
223 * Note: since we may be writing directly into application-supplied buffers,
224 * we have to be honest about the output width; we can't assume the buffer
225 * has been rounded up to an even width.
226 */
227
228
229/*
230 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
231 */
232
233METHODDEF(void)
234h2v1_merged_upsample (j_decompress_ptr cinfo,
235                      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
236                      JSAMPARRAY output_buf)
237{
238  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
239  register int y, cred, cgreen, cblue;
240  int cb, cr;
241  register JSAMPROW outptr;
242  JSAMPROW inptr0, inptr1, inptr2;
243  JDIMENSION col;
244  /* copy these pointers into registers if possible */
245  register JSAMPLE * range_limit = cinfo->sample_range_limit;
246  int * Crrtab = upsample->Cr_r_tab;
247  int * Cbbtab = upsample->Cb_b_tab;
248  INT32 * Crgtab = upsample->Cr_g_tab;
249  INT32 * Cbgtab = upsample->Cb_g_tab;
250  SHIFT_TEMPS
251
252  inptr0 = input_buf[0][in_row_group_ctr];
253  inptr1 = input_buf[1][in_row_group_ctr];
254  inptr2 = input_buf[2][in_row_group_ctr];
255  outptr = output_buf[0];
256  /* Loop for each pair of output pixels */
257  for (col = cinfo->output_width >> 1; col > 0; col--) {
258    /* Do the chroma part of the calculation */
259    cb = GETJSAMPLE(*inptr1++);
260    cr = GETJSAMPLE(*inptr2++);
261    cred = Crrtab[cr];
262    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
263    cblue = Cbbtab[cb];
264    /* Fetch 2 Y values and emit 2 pixels */
265    y  = GETJSAMPLE(*inptr0++);
266    outptr[RGB_RED] =   range_limit[y + cred];
267    outptr[RGB_GREEN] = range_limit[y + cgreen];
268    outptr[RGB_BLUE] =  range_limit[y + cblue];
269    outptr += RGB_PIXELSIZE;
270    y  = GETJSAMPLE(*inptr0++);
271    outptr[RGB_RED] =   range_limit[y + cred];
272    outptr[RGB_GREEN] = range_limit[y + cgreen];
273    outptr[RGB_BLUE] =  range_limit[y + cblue];
274    outptr += RGB_PIXELSIZE;
275  }
276  /* If image width is odd, do the last output column separately */
277  if (cinfo->output_width & 1) {
278    cb = GETJSAMPLE(*inptr1);
279    cr = GETJSAMPLE(*inptr2);
280    cred = Crrtab[cr];
281    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
282    cblue = Cbbtab[cb];
283    y  = GETJSAMPLE(*inptr0);
284    outptr[RGB_RED] =   range_limit[y + cred];
285    outptr[RGB_GREEN] = range_limit[y + cgreen];
286    outptr[RGB_BLUE] =  range_limit[y + cblue];
287  }
288}
289
290
291/*
292 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
293 */
294
295METHODDEF(void)
296h2v2_merged_upsample (j_decompress_ptr cinfo,
297                      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
298                      JSAMPARRAY output_buf)
299{
300  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
301  register int y, cred, cgreen, cblue;
302  int cb, cr;
303  register JSAMPROW outptr0, outptr1;
304  JSAMPROW inptr00, inptr01, inptr1, inptr2;
305  JDIMENSION col;
306  /* copy these pointers into registers if possible */
307  register JSAMPLE * range_limit = cinfo->sample_range_limit;
308  int * Crrtab = upsample->Cr_r_tab;
309  int * Cbbtab = upsample->Cb_b_tab;
310  INT32 * Crgtab = upsample->Cr_g_tab;
311  INT32 * Cbgtab = upsample->Cb_g_tab;
312  SHIFT_TEMPS
313
314  inptr00 = input_buf[0][in_row_group_ctr*2];
315  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
316  inptr1 = input_buf[1][in_row_group_ctr];
317  inptr2 = input_buf[2][in_row_group_ctr];
318  outptr0 = output_buf[0];
319  outptr1 = output_buf[1];
320  /* Loop for each group of output pixels */
321  for (col = cinfo->output_width >> 1; col > 0; col--) {
322    /* Do the chroma part of the calculation */
323    cb = GETJSAMPLE(*inptr1++);
324    cr = GETJSAMPLE(*inptr2++);
325    cred = Crrtab[cr];
326    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
327    cblue = Cbbtab[cb];
328    /* Fetch 4 Y values and emit 4 pixels */
329    y  = GETJSAMPLE(*inptr00++);
330    outptr0[RGB_RED] =   range_limit[y + cred];
331    outptr0[RGB_GREEN] = range_limit[y + cgreen];
332    outptr0[RGB_BLUE] =  range_limit[y + cblue];
333    outptr0 += RGB_PIXELSIZE;
334    y  = GETJSAMPLE(*inptr00++);
335    outptr0[RGB_RED] =   range_limit[y + cred];
336    outptr0[RGB_GREEN] = range_limit[y + cgreen];
337    outptr0[RGB_BLUE] =  range_limit[y + cblue];
338    outptr0 += RGB_PIXELSIZE;
339    y  = GETJSAMPLE(*inptr01++);
340    outptr1[RGB_RED] =   range_limit[y + cred];
341    outptr1[RGB_GREEN] = range_limit[y + cgreen];
342    outptr1[RGB_BLUE] =  range_limit[y + cblue];
343    outptr1 += RGB_PIXELSIZE;
344    y  = GETJSAMPLE(*inptr01++);
345    outptr1[RGB_RED] =   range_limit[y + cred];
346    outptr1[RGB_GREEN] = range_limit[y + cgreen];
347    outptr1[RGB_BLUE] =  range_limit[y + cblue];
348    outptr1 += RGB_PIXELSIZE;
349  }
350  /* If image width is odd, do the last output column separately */
351  if (cinfo->output_width & 1) {
352    cb = GETJSAMPLE(*inptr1);
353    cr = GETJSAMPLE(*inptr2);
354    cred = Crrtab[cr];
355    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
356    cblue = Cbbtab[cb];
357    y  = GETJSAMPLE(*inptr00);
358    outptr0[RGB_RED] =   range_limit[y + cred];
359    outptr0[RGB_GREEN] = range_limit[y + cgreen];
360    outptr0[RGB_BLUE] =  range_limit[y + cblue];
361    y  = GETJSAMPLE(*inptr01);
362    outptr1[RGB_RED] =   range_limit[y + cred];
363    outptr1[RGB_GREEN] = range_limit[y + cgreen];
364    outptr1[RGB_BLUE] =  range_limit[y + cblue];
365  }
366}
367
368
369/*
370 * Module initialization routine for merged upsampling/color conversion.
371 *
372 * NB: this is called under the conditions determined by use_merged_upsample()
373 * in jdmaster.c.  That routine MUST correspond to the actual capabilities
374 * of this module; no safety checks are made here.
375 */
376
377GLOBAL(void)
378jinit_merged_upsampler (j_decompress_ptr cinfo)
379{
380  my_upsample_ptr upsample;
381
382  upsample = (my_upsample_ptr)
383    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
384                                SIZEOF(my_upsampler));
385  cinfo->upsample = (struct jpeg_upsampler *) upsample;
386  upsample->pub.start_pass = start_pass_merged_upsample;
387  upsample->pub.need_context_rows = FALSE;
388
389  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
390
391  if (cinfo->max_v_samp_factor == 2) {
392    upsample->pub.upsample = merged_2v_upsample;
393    upsample->upmethod = h2v2_merged_upsample;
394    /* Allocate a spare row buffer */
395    upsample->spare_row = (JSAMPROW)
396      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
397                (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
398  } else {
399    upsample->pub.upsample = merged_1v_upsample;
400    upsample->upmethod = h2v1_merged_upsample;
401    /* No spare row needed */
402    upsample->spare_row = NULL;
403  }
404
405  build_ycc_rgb_table(cinfo);
406}
407
408#endif /* UPSAMPLE_MERGING_SUPPORTED */
Note: See TracBrowser for help on using the repository browser.