src/java.desktop/share/native/libjavajpeg/jdcolor.c
author jiefu
Fri, 15 Nov 2019 20:39:26 +0800
changeset 59110 8c4c358272a9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234232: [TESTBUG] gc/shenandoah/jvmti/TestHeapDump.java fails with -Xcomp Reviewed-by: zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * jdcolor.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1991-1997, Thomas G. Lane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This file is part of the Independent JPEG Group's software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * For conditions of distribution and use, see the accompanying README file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This file contains output colorspace conversion routines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
/* Private subobject */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
  struct jpeg_color_deconverter pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  /* Private state for YCC->RGB conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  int * Cr_r_tab;               /* => table for Cr to R conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  int * Cb_b_tab;               /* => table for Cb to B conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
} my_color_deconverter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
typedef my_color_deconverter * my_cconvert_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**************** YCbCr -> RGB conversion: most common case **************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The conversion equations to be implemented are therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      R = Y                + 1.40200 * Cr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      G = Y - 0.34414 * Cb - 0.71414 * Cr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      B = Y + 1.77200 * Cb
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * To avoid floating-point arithmetic, we represent the fractional constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the products by 2^16, with appropriate rounding, to get the correct answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Notice that Y, being an integral input, does not contribute any fraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * so it need not participate in the rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * For even more speed, we avoid doing any multiplications in the inner loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * by precalculating the constants times Cb and Cr for all possible values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * colorspace anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * values for the G calculation are left scaled up, since we must add them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * together before rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define SCALEBITS       16      /* speediest right-shift on some machines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Initialize tables for YCC->RGB colorspace conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
build_ycc_rgb_table (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  INT32 x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  SHIFT_TEMPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  cconvert->Cr_r_tab = (int *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                (MAXJSAMPLE+1) * SIZEOF(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  cconvert->Cb_b_tab = (int *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                (MAXJSAMPLE+1) * SIZEOF(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  cconvert->Cr_g_tab = (INT32 *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                (MAXJSAMPLE+1) * SIZEOF(INT32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  cconvert->Cb_g_tab = (INT32 *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                (MAXJSAMPLE+1) * SIZEOF(INT32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /* Cr=>R value is nearest int to 1.40200 * x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    cconvert->Cr_r_tab[i] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* Cb=>B value is nearest int to 1.77200 * x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    cconvert->Cb_b_tab[i] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /* Cr=>G value is scaled-up -0.71414 * x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* Cb=>G value is scaled-up -0.34414 * x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* We also add in ONE_HALF so that need not do it in inner loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Convert some rows of samples to the output colorspace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Note that we change from noninterleaved, one-plane-per-component format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * to interleaved-pixel format.  The output buffer is therefore three times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * as wide as the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * A starting row offset is provided only for the input buffer.  The caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * can easily adjust the passed output_buf value to accommodate any row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * offset required on that side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
ycc_rgb_convert (j_decompress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                 JSAMPIMAGE input_buf, JDIMENSION input_row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                 JSAMPARRAY output_buf, int num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  register int y, cb, cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  register JSAMPROW outptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  register JSAMPROW inptr0, inptr1, inptr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  register JDIMENSION col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  JDIMENSION num_cols = cinfo->output_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  /* copy these pointers into registers if possible */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  register int * Crrtab = cconvert->Cr_r_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  register int * Cbbtab = cconvert->Cb_b_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  register INT32 * Crgtab = cconvert->Cr_g_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  SHIFT_TEMPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  while (--num_rows >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    inptr0 = input_buf[0][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    inptr1 = input_buf[1][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    inptr2 = input_buf[2][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    input_row++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    outptr = *output_buf++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    for (col = 0; col < num_cols; col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      y  = GETJSAMPLE(inptr0[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
      cb = GETJSAMPLE(inptr1[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      cr = GETJSAMPLE(inptr2[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      /* Range-limiting is essential due to noise introduced by DCT losses. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      outptr[RGB_GREEN] = range_limit[y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                                 SCALEBITS))];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      outptr += RGB_PIXELSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
/**************** Cases other than YCbCr -> RGB **************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Color conversion for no colorspace change: just copy the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * converting from separate-planes to interleaved representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
null_convert (j_decompress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
              JSAMPIMAGE input_buf, JDIMENSION input_row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
              JSAMPARRAY output_buf, int num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  register JSAMPROW inptr, outptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
  register JDIMENSION count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
  register int num_components = cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
  JDIMENSION num_cols = cinfo->output_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  while (--num_rows >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    for (ci = 0; ci < num_components; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      inptr = input_buf[ci][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      outptr = output_buf[0] + ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      for (count = num_cols; count > 0; count--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        *outptr = *inptr++;     /* needn't bother with GETJSAMPLE() here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        outptr += num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    input_row++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    output_buf++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * Color conversion for grayscale: just copy the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * This also works for YCbCr -> grayscale conversion, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * we just copy the Y (luminance) component and ignore chrominance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
grayscale_convert (j_decompress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                   JSAMPIMAGE input_buf, JDIMENSION input_row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                   JSAMPARRAY output_buf, int num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    num_rows, cinfo->output_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * Convert grayscale to RGB: just duplicate the graylevel three times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * This is provided to support applications that don't want to cope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * with grayscale as a separate case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
gray_rgb_convert (j_decompress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                  JSAMPIMAGE input_buf, JDIMENSION input_row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                  JSAMPARRAY output_buf, int num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
  register JSAMPROW inptr, outptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
  register JDIMENSION col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  JDIMENSION num_cols = cinfo->output_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  while (--num_rows >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    inptr = input_buf[0][input_row++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    outptr = *output_buf++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    for (col = 0; col < num_cols; col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      /* We can dispense with GETJSAMPLE() here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      outptr += RGB_PIXELSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Adobe-style YCCK->CMYK conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * conversion as above, while passing K (black) unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * We assume build_ycc_rgb_table has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
ycck_cmyk_convert (j_decompress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                   JSAMPIMAGE input_buf, JDIMENSION input_row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                   JSAMPARRAY output_buf, int num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
  register int y, cb, cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  register JSAMPROW outptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
  register JDIMENSION col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
  JDIMENSION num_cols = cinfo->output_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  /* copy these pointers into registers if possible */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  register int * Crrtab = cconvert->Cr_r_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  register int * Cbbtab = cconvert->Cb_b_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
  register INT32 * Crgtab = cconvert->Cr_g_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  SHIFT_TEMPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  while (--num_rows >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    inptr0 = input_buf[0][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    inptr1 = input_buf[1][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    inptr2 = input_buf[2][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    inptr3 = input_buf[3][input_row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    input_row++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    outptr = *output_buf++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    for (col = 0; col < num_cols; col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
      y  = GETJSAMPLE(inptr0[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      cb = GETJSAMPLE(inptr1[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
      cr = GETJSAMPLE(inptr2[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
      /* Range-limiting is essential due to noise introduced by DCT losses. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
      outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                 SCALEBITS)))];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
      /* K passes through unchanged */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
      outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
      outptr += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * Empty method for start_pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
start_pass_dcolor (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
  /* no work needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Module initialization routine for output colorspace conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
jinit_color_deconverter (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
  my_cconvert_ptr cconvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
  cconvert = (my_cconvert_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                SIZEOF(my_color_deconverter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  cconvert->pub.start_pass = start_pass_dcolor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
  /* Make sure num_components agrees with jpeg_color_space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  switch (cinfo->jpeg_color_space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  case JCS_GRAYSCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    if (cinfo->num_components != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
  case JCS_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
  case JCS_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    if (cinfo->num_components != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  case JCS_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  case JCS_YCCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    if (cinfo->num_components != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
  default:                      /* JCS_UNKNOWN can be anything */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    if (cinfo->num_components < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
  /* Set out_color_components and conversion method based on requested space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
   * Also clear the component_needed flags for any unused components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
   * so that earlier pipeline stages can avoid useless computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
  switch (cinfo->out_color_space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  case JCS_GRAYSCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    cinfo->out_color_components = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        cinfo->jpeg_color_space == JCS_YCbCr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
      cconvert->pub.color_convert = grayscale_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      /* For color->grayscale conversion, only the Y (0) component is needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
      for (ci = 1; ci < cinfo->num_components; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        cinfo->comp_info[ci].component_needed = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  case JCS_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    cinfo->out_color_components = RGB_PIXELSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    if (cinfo->jpeg_color_space == JCS_YCbCr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      cconvert->pub.color_convert = ycc_rgb_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      build_ycc_rgb_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      cconvert->pub.color_convert = gray_rgb_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      cconvert->pub.color_convert = null_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  case JCS_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    cinfo->out_color_components = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (cinfo->jpeg_color_space == JCS_YCCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
      cconvert->pub.color_convert = ycck_cmyk_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      build_ycc_rgb_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
      cconvert->pub.color_convert = null_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /* Permit null conversion to same output space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
      cinfo->out_color_components = cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      cconvert->pub.color_convert = null_convert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    } else                      /* unsupported non-null conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
  if (cinfo->quantize_colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    cinfo->output_components = 1; /* single colormapped output component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    cinfo->output_components = cinfo->out_color_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}