< prev index next >

modules/javafx.graphics/src/main/native-iio/libjpeg7/jdmerge.c

Print this page


   1 /*
   2  * jdmerge.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 code for merged upsampling/color conversion.
   9  *
  10  * This file combines functions from jdsample.c and jdcolor.c;
  11  * read those files first to understand what's going on.
  12  *
  13  * When the chroma components are to be upsampled by simple replication
  14  * (ie, box filtering), we can save some work in color conversion by
  15  * calculating all the output pixels corresponding to a pair of chroma
  16  * samples at one time.  In the conversion equations
  17  *      R = Y           + K1 * Cr
  18  *      G = Y + K2 * Cb + K3 * Cr
  19  *      B = Y + K4 * Cb
  20  * only the Y term varies among the group of pixels corresponding to a pair
  21  * of chroma samples, so the rest of the terms can be calculated just once.
  22  * At typical sampling ratios, this eliminates half or three-quarters of the
  23  * multiplications needed for color conversion.
  24  *
  25  * This file currently provides implementations for the following cases:
  26  *      YCbCr => RGB color conversion only.
  27  *      Sampling ratios of 2h1v or 2h2v.
  28  *      No scaling needed at upsample time.
  29  *      Corner-aligned (non-CCIR601) sampling alignment.
  30  * Other special cases could be added, but in most applications these are
  31  * the only common cases.  (For uncommon cases we fall back on the more
  32  * general code in jdsample.c and jdcolor.c.)
  33  */
  34 
  35 #define JPEG_INTERNALS
  36 #include "jinclude.h"
  37 #include "jpeglib.h"
  38 
  39 #ifdef UPSAMPLE_MERGING_SUPPORTED
  40 
  41 






  42 /* Private subobject */
  43 
  44 typedef struct {
  45   struct jpeg_upsampler pub;    /* public fields */
  46 
  47   /* Pointer to routine to do actual upsampling/conversion of one row group */
  48   JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
  49                            JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  50                            JSAMPARRAY output_buf));
  51 
  52   /* Private state for YCC->RGB conversion */
  53   int * Cr_r_tab;               /* => table for Cr to R conversion */
  54   int * Cb_b_tab;               /* => table for Cb to B conversion */
  55   INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
  56   INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
  57 
  58   /* For 2:1 vertical sampling, we produce two output rows at a time.
  59    * We need a "spare" row buffer to hold the second output row if the
  60    * application provides just a one-row buffer; we also use the spare
  61    * to discard the dummy last row if the image height is odd.
  62    */
  63   JSAMPROW spare_row;
  64   boolean spare_full;           /* T if spare buffer is occupied */
  65 
  66   JDIMENSION out_row_width;     /* samples per output row */
  67   JDIMENSION rows_to_go;        /* counts rows remaining in image */
  68 } my_upsampler;
  69 
  70 typedef my_upsampler * my_upsample_ptr;
  71 
  72 #define SCALEBITS       16      /* speediest right-shift on some machines */
  73 #define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
  74 #define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  75 
  76 
  77 /*
  78  * Initialize tables for YCC->RGB colorspace conversion.
  79  * This is taken directly from jdcolor.c; see that file for more info.
  80  */
  81 
  82 LOCAL(void)
  83 build_ycc_rgb_table (j_decompress_ptr cinfo)









































  84 {
  85   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  86   int i;
  87   INT32 x;
  88   SHIFT_TEMPS
  89 
  90   upsample->Cr_r_tab = (int *)
  91     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  92                                 (MAXJSAMPLE+1) * SIZEOF(int));
  93   upsample->Cb_b_tab = (int *)
  94     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  95                                 (MAXJSAMPLE+1) * SIZEOF(int));
  96   upsample->Cr_g_tab = (INT32 *)
  97     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  98                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
  99   upsample->Cb_g_tab = (INT32 *)
 100     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 101                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
 102 
 103   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
 104     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
 105     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
 106     /* Cr=>R value is nearest int to 1.40200 * x */
 107     upsample->Cr_r_tab[i] = (int)
 108                     RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
 109     /* Cb=>B value is nearest int to 1.77200 * x */
 110     upsample->Cb_b_tab[i] = (int)
 111                     RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
 112     /* Cr=>G value is scaled-up -0.71414 * x */
 113     upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
 114     /* Cb=>G value is scaled-up -0.34414 * x */
 115     /* We also add in ONE_HALF so that need not do it in inner loop */
 116     upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
 117   }
 118 }
 119 
 120 
 121 /*
 122  * Initialize for an upsampling pass.
 123  */
 124 
 125 METHODDEF(void)
 126 start_pass_merged_upsample (j_decompress_ptr cinfo)
 127 {
 128   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
 129 
 130   /* Mark the spare buffer empty */
 131   upsample->spare_full = FALSE;
 132   /* Initialize total-height counter for detecting bottom of image */
 133   upsample->rows_to_go = cinfo->output_height;
 134 }
 135 
 136 


 357   }
 358 }
 359 
 360 
 361 /*
 362  * Module initialization routine for merged upsampling/color conversion.
 363  *
 364  * NB: this is called under the conditions determined by use_merged_upsample()
 365  * in jdmaster.c.  That routine MUST correspond to the actual capabilities
 366  * of this module; no safety checks are made here.
 367  */
 368 
 369 GLOBAL(void)
 370 jinit_merged_upsampler (j_decompress_ptr cinfo)
 371 {
 372   my_upsample_ptr upsample;
 373 
 374   upsample = (my_upsample_ptr)
 375     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 376                                 SIZEOF(my_upsampler));
 377   cinfo->upsample = (struct jpeg_upsampler *) upsample;
 378   upsample->pub.start_pass = start_pass_merged_upsample;
 379   upsample->pub.need_context_rows = FALSE;
 380 
 381   upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
 382 
 383   if (cinfo->max_v_samp_factor == 2) {
 384     upsample->pub.upsample = merged_2v_upsample;
 385     upsample->upmethod = h2v2_merged_upsample;
 386     /* Allocate a spare row buffer */
 387     upsample->spare_row = (JSAMPROW)
 388       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 389                 (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
 390   } else {
 391     upsample->pub.upsample = merged_1v_upsample;
 392     upsample->upmethod = h2v1_merged_upsample;
 393     /* No spare row needed */
 394     upsample->spare_row = NULL;
 395   }
 396 



 397   build_ycc_rgb_table(cinfo);
 398 }
 399 
 400 #endif /* UPSAMPLE_MERGING_SUPPORTED */
   1 /*
   2  * jdmerge.c
   3  *
   4  * Copyright (C) 1994-1996, Thomas G. Lane.
   5  * Modified 2013-2017 by Guido Vollbeding.
   6  * This file is part of the Independent JPEG Group's software.
   7  * For conditions of distribution and use, see the accompanying README file.
   8  *
   9  * This file contains code for merged upsampling/color conversion.
  10  *
  11  * This file combines functions from jdsample.c and jdcolor.c;
  12  * read those files first to understand what's going on.
  13  *
  14  * When the chroma components are to be upsampled by simple replication
  15  * (ie, box filtering), we can save some work in color conversion by
  16  * calculating all the output pixels corresponding to a pair of chroma
  17  * samples at one time.  In the conversion equations
  18  *      R = Y           + K1 * Cr
  19  *      G = Y + K2 * Cb + K3 * Cr
  20  *      B = Y + K4 * Cb
  21  * only the Y term varies among the group of pixels corresponding to a pair
  22  * of chroma samples, so the rest of the terms can be calculated just once.
  23  * At typical sampling ratios, this eliminates half or three-quarters of the
  24  * multiplications needed for color conversion.
  25  *
  26  * This file currently provides implementations for the following cases:
  27  *      YCC => RGB color conversion only (YCbCr or BG_YCC).
  28  *      Sampling ratios of 2h1v or 2h2v.
  29  *      No scaling needed at upsample time.
  30  *      Corner-aligned (non-CCIR601) sampling alignment.
  31  * Other special cases could be added, but in most applications these are
  32  * the only common cases.  (For uncommon cases we fall back on the more
  33  * general code in jdsample.c and jdcolor.c.)
  34  */
  35 
  36 #define JPEG_INTERNALS
  37 #include "jinclude.h"
  38 #include "jpeglib.h"
  39 
  40 #ifdef UPSAMPLE_MERGING_SUPPORTED
  41 
  42 
  43 #if RANGE_BITS < 2
  44   /* Deliberate syntax err */
  45   Sorry, this code requires 2 or more range extension bits.
  46 #endif
  47 
  48 
  49 /* Private subobject */
  50 
  51 typedef struct {
  52   struct jpeg_upsampler pub;    /* public fields */
  53 
  54   /* Pointer to routine to do actual upsampling/conversion of one row group */
  55   JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
  56                            JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  57                            JSAMPARRAY output_buf));
  58 
  59   /* Private state for YCC->RGB conversion */
  60   int * Cr_r_tab;               /* => table for Cr to R conversion */
  61   int * Cb_b_tab;               /* => table for Cb to B conversion */
  62   INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
  63   INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
  64 
  65   /* For 2:1 vertical sampling, we produce two output rows at a time.
  66    * We need a "spare" row buffer to hold the second output row if the
  67    * application provides just a one-row buffer; we also use the spare
  68    * to discard the dummy last row if the image height is odd.
  69    */
  70   JSAMPROW spare_row;
  71   boolean spare_full;           /* T if spare buffer is occupied */
  72 
  73   JDIMENSION out_row_width;     /* samples per output row */
  74   JDIMENSION rows_to_go;        /* counts rows remaining in image */
  75 } my_upsampler;
  76 
  77 typedef my_upsampler * my_upsample_ptr;
  78 
  79 #define SCALEBITS       16      /* speediest right-shift on some machines */
  80 #define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
  81 #define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  82 
  83 
  84 /*
  85  * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  86  * This is taken directly from jdcolor.c; see that file for more info.
  87  */
  88 
  89 LOCAL(void)
  90 build_ycc_rgb_table (j_decompress_ptr cinfo)
  91 /* Normal case, sYCC */
  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.402 * x */
 115     upsample->Cr_r_tab[i] = (int)
 116                     RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
 117     /* Cb=>B value is nearest int to 1.772 * x */
 118     upsample->Cb_b_tab[i] = (int)
 119                     RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
 120     /* Cr=>G value is scaled-up -0.714136286 * x */
 121     upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
 122     /* Cb=>G value is scaled-up -0.344136286 * 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.344136286)) * x + ONE_HALF;
 125   }
 126 }
 127 
 128 
 129 LOCAL(void)
 130 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
 131 /* Wide gamut case, bg-sYCC */
 132 {
 133   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
 134   int i;
 135   INT32 x;
 136   SHIFT_TEMPS
 137 
 138   upsample->Cr_r_tab = (int *)
 139     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 140                                 (MAXJSAMPLE+1) * SIZEOF(int));
 141   upsample->Cb_b_tab = (int *)
 142     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 143                                 (MAXJSAMPLE+1) * SIZEOF(int));
 144   upsample->Cr_g_tab = (INT32 *)
 145     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 146                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
 147   upsample->Cb_g_tab = (INT32 *)
 148     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 149                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
 150 
 151   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
 152     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
 153     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
 154     /* Cr=>R value is nearest int to 2.804 * x */
 155     upsample->Cr_r_tab[i] = (int)
 156                     RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
 157     /* Cb=>B value is nearest int to 3.544 * x */
 158     upsample->Cb_b_tab[i] = (int)
 159                     RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
 160     /* Cr=>G value is scaled-up -1.428272572 * x */
 161     upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
 162     /* Cb=>G value is scaled-up -0.688272572 * x */
 163     /* We also add in ONE_HALF so that need not do it in inner loop */
 164     upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
 165   }
 166 }
 167 
 168 
 169 /*
 170  * Initialize for an upsampling pass.
 171  */
 172 
 173 METHODDEF(void)
 174 start_pass_merged_upsample (j_decompress_ptr cinfo)
 175 {
 176   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
 177 
 178   /* Mark the spare buffer empty */
 179   upsample->spare_full = FALSE;
 180   /* Initialize total-height counter for detecting bottom of image */
 181   upsample->rows_to_go = cinfo->output_height;
 182 }
 183 
 184 


 405   }
 406 }
 407 
 408 
 409 /*
 410  * Module initialization routine for merged upsampling/color conversion.
 411  *
 412  * NB: this is called under the conditions determined by use_merged_upsample()
 413  * in jdmaster.c.  That routine MUST correspond to the actual capabilities
 414  * of this module; no safety checks are made here.
 415  */
 416 
 417 GLOBAL(void)
 418 jinit_merged_upsampler (j_decompress_ptr cinfo)
 419 {
 420   my_upsample_ptr upsample;
 421 
 422   upsample = (my_upsample_ptr)
 423     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 424                                 SIZEOF(my_upsampler));
 425   cinfo->upsample = &upsample->pub;
 426   upsample->pub.start_pass = start_pass_merged_upsample;
 427   upsample->pub.need_context_rows = FALSE;
 428 
 429   upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
 430 
 431   if (cinfo->max_v_samp_factor == 2) {
 432     upsample->pub.upsample = merged_2v_upsample;
 433     upsample->upmethod = h2v2_merged_upsample;
 434     /* Allocate a spare row buffer */
 435     upsample->spare_row = (JSAMPROW)
 436       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 437                 (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
 438   } else {
 439     upsample->pub.upsample = merged_1v_upsample;
 440     upsample->upmethod = h2v1_merged_upsample;
 441     /* No spare row needed */
 442     upsample->spare_row = NULL;
 443   }
 444 
 445   if (cinfo->jpeg_color_space == JCS_BG_YCC)
 446     build_bg_ycc_rgb_table(cinfo);
 447   else
 448     build_ycc_rgb_table(cinfo);
 449 }
 450 
 451 #endif /* UPSAMPLE_MERGING_SUPPORTED */
< prev index next >