The Gordian knot that 7fec5074f9 attempted
to unravel was caused by the fact that there are several
data-precision-dependent (JSAMPLE-dependent) fields and methods in the
exposed libjpeg API structures, and if you change the exposed libjpeg
API structures, then you have to change the whole API. If you change
the whole API, then you have to provide a whole new library to support
the new API, and that makes it difficult to support multiple data
precisions in the same application. (It is not impossible, as example.c
demonstrated, but using data-precision-dependent libjpeg API structures
would have made the cjpeg, djpeg, and jpegtran source code hard to read,
so it made more sense to build, install, and package 12-bit-specific
versions of those applications.)
Unfortunately, the result of that initial integration effort was an
unreadable and unmaintainable mess, which is a problem for a library
that is an ISO/ITU-T reference implementation. Also, as I dug into the
problem of lossless JPEG support, I realized that 16-bit lossless JPEG
images are a thing, and supporting yet another version of the libjpeg
API just for those images is untenable.
In fact, however, the touch points for JSAMPLE in the exposed libjpeg
API structures are minimal:
- The colormap and sample_range_limit fields in jpeg_decompress_struct
- The alloc_sarray() and access_virt_sarray() methods in
jpeg_memory_mgr
- jpeg_write_scanlines() and jpeg_write_raw_data()
- jpeg_read_scanlines() and jpeg_read_raw_data()
- jpeg_skip_scanlines() and jpeg_crop_scanline()
(This is subtle, but both of those functions use JSAMPLE-dependent
opaque structures behind the scenes.)
It is much more readable and maintainable to provide 12-bit-specific
versions of those six top-level API functions and to document that the
aforementioned methods and fields must be type-cast when using 12-bit
samples. Since that eliminates the need to provide a 12-bit-specific
version of the exposed libjpeg API structures, we can:
- Compile only the precision-dependent libjpeg modules (the
coefficient buffer controllers, the colorspace converters, the
DCT/IDCT managers, the main buffer controllers, the preprocessing
and postprocessing controller, the downsampler and upsamplers, the
quantizers, the integer DCT methods, and the IDCT methods) for
multiple data precisions.
- Introduce 12-bit-specific methods into the various internal
structures defined in jpegint.h.
- Create precision-independent data type, macro, method, field, and
function names that are prefixed by an underscore, and use an
internal header to convert those into precision-dependent data
type, macro, method, field, and function names, based on the value
of BITS_IN_JSAMPLE, when compiling the precision-dependent libjpeg
modules.
- Expose precision-dependent jinit*() functions for each of the
precision-dependent libjpeg modules.
- Abstract the precision-dependent libjpeg modules by calling the
appropriate precision-dependent jinit*() function, based on the
value of cinfo->data_precision, from top-level libjpeg API
functions.
142 lines
4.3 KiB
C
142 lines
4.3 KiB
C
/*
|
|
* jdcolext.c
|
|
*
|
|
* This file was part of the Independent JPEG Group's software:
|
|
* Copyright (C) 1991-1997, Thomas G. Lane.
|
|
* libjpeg-turbo Modifications:
|
|
* Copyright (C) 2009, 2011, 2015, 2022, D. R. Commander.
|
|
* For conditions of distribution and use, see the accompanying README.ijg
|
|
* file.
|
|
*
|
|
* This file contains output colorspace conversion routines.
|
|
*/
|
|
|
|
|
|
/* This file is included by jdcolor.c */
|
|
|
|
|
|
/*
|
|
* Convert some rows of samples to the output colorspace.
|
|
*
|
|
* Note that we change from noninterleaved, one-plane-per-component format
|
|
* to interleaved-pixel format. The output buffer is therefore three times
|
|
* as wide as the input buffer.
|
|
* A starting row offset is provided only for the input buffer. The caller
|
|
* can easily adjust the passed output_buf value to accommodate any row
|
|
* offset required on that side.
|
|
*/
|
|
|
|
INLINE
|
|
LOCAL(void)
|
|
ycc_rgb_convert_internal(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
|
|
JDIMENSION input_row, _JSAMPARRAY output_buf,
|
|
int num_rows)
|
|
{
|
|
my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
|
|
register int y, cb, cr;
|
|
register _JSAMPROW outptr;
|
|
register _JSAMPROW inptr0, inptr1, inptr2;
|
|
register JDIMENSION col;
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
/* copy these pointers into registers if possible */
|
|
register _JSAMPLE *range_limit = (_JSAMPLE *)cinfo->sample_range_limit;
|
|
register int *Crrtab = cconvert->Cr_r_tab;
|
|
register int *Cbbtab = cconvert->Cb_b_tab;
|
|
register JLONG *Crgtab = cconvert->Cr_g_tab;
|
|
register JLONG *Cbgtab = cconvert->Cb_g_tab;
|
|
SHIFT_TEMPS
|
|
|
|
while (--num_rows >= 0) {
|
|
inptr0 = input_buf[0][input_row];
|
|
inptr1 = input_buf[1][input_row];
|
|
inptr2 = input_buf[2][input_row];
|
|
input_row++;
|
|
outptr = *output_buf++;
|
|
for (col = 0; col < num_cols; col++) {
|
|
y = inptr0[col];
|
|
cb = inptr1[col];
|
|
cr = inptr2[col];
|
|
/* Range-limiting is essential due to noise introduced by DCT losses. */
|
|
outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
|
|
outptr[RGB_GREEN] = range_limit[y +
|
|
((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
|
|
SCALEBITS))];
|
|
outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
|
|
/* Set unused byte to 0xFF so it can be interpreted as an opaque */
|
|
/* alpha channel value */
|
|
#ifdef RGB_ALPHA
|
|
outptr[RGB_ALPHA] = 0xFF;
|
|
#endif
|
|
outptr += RGB_PIXELSIZE;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Convert grayscale to RGB: just duplicate the graylevel three times.
|
|
* This is provided to support applications that don't want to cope
|
|
* with grayscale as a separate case.
|
|
*/
|
|
|
|
INLINE
|
|
LOCAL(void)
|
|
gray_rgb_convert_internal(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
|
|
JDIMENSION input_row, _JSAMPARRAY output_buf,
|
|
int num_rows)
|
|
{
|
|
register _JSAMPROW inptr, outptr;
|
|
register JDIMENSION col;
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
while (--num_rows >= 0) {
|
|
inptr = input_buf[0][input_row++];
|
|
outptr = *output_buf++;
|
|
for (col = 0; col < num_cols; col++) {
|
|
outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
|
|
/* Set unused byte to 0xFF so it can be interpreted as an opaque */
|
|
/* alpha channel value */
|
|
#ifdef RGB_ALPHA
|
|
outptr[RGB_ALPHA] = 0xFF;
|
|
#endif
|
|
outptr += RGB_PIXELSIZE;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Convert RGB to extended RGB: just swap the order of source pixels
|
|
*/
|
|
|
|
INLINE
|
|
LOCAL(void)
|
|
rgb_rgb_convert_internal(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
|
|
JDIMENSION input_row, _JSAMPARRAY output_buf,
|
|
int num_rows)
|
|
{
|
|
register _JSAMPROW inptr0, inptr1, inptr2;
|
|
register _JSAMPROW outptr;
|
|
register JDIMENSION col;
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
while (--num_rows >= 0) {
|
|
inptr0 = input_buf[0][input_row];
|
|
inptr1 = input_buf[1][input_row];
|
|
inptr2 = input_buf[2][input_row];
|
|
input_row++;
|
|
outptr = *output_buf++;
|
|
for (col = 0; col < num_cols; col++) {
|
|
outptr[RGB_RED] = inptr0[col];
|
|
outptr[RGB_GREEN] = inptr1[col];
|
|
outptr[RGB_BLUE] = inptr2[col];
|
|
/* Set unused byte to 0xFF so it can be interpreted as an opaque */
|
|
/* alpha channel value */
|
|
#ifdef RGB_ALPHA
|
|
outptr[RGB_ALPHA] = 0xFF;
|
|
#endif
|
|
outptr += RGB_PIXELSIZE;
|
|
}
|
|
}
|
|
}
|