Files
mozjpeg/rdcolmap.c
DRC e8b40f3c2b Vastly improve 12-bit JPEG integration
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.
2022-11-04 12:30:33 -05:00

260 lines
7.1 KiB
C

/*
* rdcolmap.c
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1994-1996, Thomas G. Lane.
* libjpeg-turbo Modifications:
* Copyright (C) 2022, D. R. Commander.
* For conditions of distribution and use, see the accompanying README.ijg
* file.
*
* This file implements djpeg's "-map file" switch. It reads a source image
* and constructs a colormap to be supplied to the JPEG decompressor.
*
* Currently, these file formats are supported for the map file:
* GIF: the contents of the GIF's global colormap are used.
* PPM (either text or raw flavor): the entire file is read and
* each unique pixel value is entered in the map.
* Note that reading a large PPM file will be horrendously slow.
* Typically, a PPM-format map file should contain just one pixel
* of each desired color. Such a file can be extracted from an
* ordinary image PPM file with ppmtomap(1).
*
* Rescaling a PPM that has a maxval unequal to _MAXJSAMPLE is not
* currently implemented.
*/
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
#include "jsamplecomp.h"
#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
/* Portions of this code are based on the PBMPLUS library, which is:
**
** Copyright (C) 1988 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
/*
* Add a (potentially) new color to the color map.
*/
LOCAL(void)
add_map_entry(j_decompress_ptr cinfo, int R, int G, int B)
{
_JSAMPROW colormap0 = ((_JSAMPARRAY)cinfo->colormap)[0];
_JSAMPROW colormap1 = ((_JSAMPARRAY)cinfo->colormap)[1];
_JSAMPROW colormap2 = ((_JSAMPARRAY)cinfo->colormap)[2];
int ncolors = cinfo->actual_number_of_colors;
int index;
/* Check for duplicate color. */
for (index = 0; index < ncolors; index++) {
if (colormap0[index] == R && colormap1[index] == G &&
colormap2[index] == B)
return; /* color is already in map */
}
/* Check for map overflow. */
if (ncolors >= (_MAXJSAMPLE + 1))
ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (_MAXJSAMPLE + 1));
/* OK, add color to map. */
colormap0[ncolors] = (_JSAMPLE)R;
colormap1[ncolors] = (_JSAMPLE)G;
colormap2[ncolors] = (_JSAMPLE)B;
cinfo->actual_number_of_colors++;
}
/*
* Extract color map from a GIF file.
*/
LOCAL(void)
read_gif_map(j_decompress_ptr cinfo, FILE *infile)
{
int header[13];
int i, colormaplen;
int R, G, B;
/* Initial 'G' has already been read by read_color_map */
/* Read the rest of the GIF header and logical screen descriptor */
for (i = 1; i < 13; i++) {
if ((header[i] = getc(infile)) == EOF)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
}
/* Verify GIF Header */
if (header[1] != 'I' || header[2] != 'F')
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
/* There must be a global color map. */
if ((header[10] & 0x80) == 0)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
/* OK, fetch it. */
colormaplen = 2 << (header[10] & 0x07);
for (i = 0; i < colormaplen; i++) {
R = getc(infile);
G = getc(infile);
B = getc(infile);
if (R == EOF || G == EOF || B == EOF)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
add_map_entry(cinfo,
R << (BITS_IN_JSAMPLE - 8),
G << (BITS_IN_JSAMPLE - 8),
B << (BITS_IN_JSAMPLE - 8));
}
}
/* Support routines for reading PPM */
LOCAL(int)
pbm_getc(FILE *infile)
/* Read next char, skipping over any comments */
/* A comment/newline sequence is returned as a newline */
{
register int ch;
ch = getc(infile);
if (ch == '#') {
do {
ch = getc(infile);
} while (ch != '\n' && ch != EOF);
}
return ch;
}
LOCAL(unsigned int)
read_pbm_integer(j_decompress_ptr cinfo, FILE *infile)
/* Read an unsigned decimal integer from the PPM file */
/* Swallows one trailing character after the integer */
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
/* This should not be a problem in practice. */
{
register int ch;
register unsigned int val;
/* Skip any leading whitespace */
do {
ch = pbm_getc(infile);
if (ch == EOF)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
if (ch < '0' || ch > '9')
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
val = ch - '0';
while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
val *= 10;
val += ch - '0';
}
return val;
}
/*
* Extract color map from a PPM file.
*/
LOCAL(void)
read_ppm_map(j_decompress_ptr cinfo, FILE *infile)
{
int c;
unsigned int w, h, maxval, row, col;
int R, G, B;
/* Initial 'P' has already been read by read_color_map */
c = getc(infile); /* save format discriminator for a sec */
/* while we fetch the remaining header info */
w = read_pbm_integer(cinfo, infile);
h = read_pbm_integer(cinfo, infile);
maxval = read_pbm_integer(cinfo, infile);
if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
/* For now, we don't support rescaling from an unusual maxval. */
if (maxval != (unsigned int)_MAXJSAMPLE)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
switch (c) {
case '3': /* it's a text-format PPM file */
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
R = read_pbm_integer(cinfo, infile);
G = read_pbm_integer(cinfo, infile);
B = read_pbm_integer(cinfo, infile);
add_map_entry(cinfo, R, G, B);
}
}
break;
case '6': /* it's a raw-format PPM file */
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
R = getc(infile);
G = getc(infile);
B = getc(infile);
if (R == EOF || G == EOF || B == EOF)
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
add_map_entry(cinfo, R, G, B);
}
}
break;
default:
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
break;
}
}
/*
* Main entry point from djpeg.c.
* Input: opened input file (from file name argument on command line).
* Output: colormap and actual_number_of_colors fields are set in cinfo.
*/
GLOBAL(void)
_read_color_map(j_decompress_ptr cinfo, FILE *infile)
{
if (cinfo->data_precision != BITS_IN_JSAMPLE)
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
/* Allocate space for a color map of maximum supported size. */
cinfo->colormap = (*cinfo->mem->alloc_sarray)
((j_common_ptr)cinfo, JPOOL_IMAGE,
(JDIMENSION)(_MAXJSAMPLE + 1), (JDIMENSION)3);
cinfo->actual_number_of_colors = 0; /* initialize map to empty */
/* Read first byte to determine file format */
switch (getc(infile)) {
case 'G':
read_gif_map(cinfo, infile);
break;
case 'P':
read_ppm_map(cinfo, infile);
break;
default:
ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
break;
}
}
#endif /* QUANT_2PASS_SUPPORTED */