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.
162 lines
5.5 KiB
C
162 lines
5.5 KiB
C
/*
|
|
* jdtrans.c
|
|
*
|
|
* This file was part of the Independent JPEG Group's software:
|
|
* Copyright (C) 1995-1997, Thomas G. Lane.
|
|
* libjpeg-turbo Modifications:
|
|
* Copyright (C) 2020, 2022, D. R. Commander.
|
|
* For conditions of distribution and use, see the accompanying README.ijg
|
|
* file.
|
|
*
|
|
* This file contains library routines for transcoding decompression,
|
|
* that is, reading raw DCT coefficient arrays from an input JPEG file.
|
|
* The routines in jdapimin.c will also be needed by a transcoder.
|
|
*/
|
|
|
|
#define JPEG_INTERNALS
|
|
#include "jinclude.h"
|
|
#include "jpeglib.h"
|
|
#include "jpegapicomp.h"
|
|
|
|
|
|
/* Forward declarations */
|
|
LOCAL(void) transdecode_master_selection(j_decompress_ptr cinfo);
|
|
|
|
|
|
/*
|
|
* Read the coefficient arrays from a JPEG file.
|
|
* jpeg_read_header must be completed before calling this.
|
|
*
|
|
* The entire image is read into a set of virtual coefficient-block arrays,
|
|
* one per component. The return value is a pointer to the array of
|
|
* virtual-array descriptors. These can be manipulated directly via the
|
|
* JPEG memory manager, or handed off to jpeg_write_coefficients().
|
|
* To release the memory occupied by the virtual arrays, call
|
|
* jpeg_finish_decompress() when done with the data.
|
|
*
|
|
* An alternative usage is to simply obtain access to the coefficient arrays
|
|
* during a buffered-image-mode decompression operation. This is allowed
|
|
* after any jpeg_finish_output() call. The arrays can be accessed until
|
|
* jpeg_finish_decompress() is called. (Note that any call to the library
|
|
* may reposition the arrays, so don't rely on access_virt_barray() results
|
|
* to stay valid across library calls.)
|
|
*
|
|
* Returns NULL if suspended. This case need be checked only if
|
|
* a suspending data source is used.
|
|
*/
|
|
|
|
GLOBAL(jvirt_barray_ptr *)
|
|
jpeg_read_coefficients(j_decompress_ptr cinfo)
|
|
{
|
|
if (cinfo->global_state == DSTATE_READY) {
|
|
/* First call: initialize active modules */
|
|
transdecode_master_selection(cinfo);
|
|
cinfo->global_state = DSTATE_RDCOEFS;
|
|
}
|
|
if (cinfo->global_state == DSTATE_RDCOEFS) {
|
|
/* Absorb whole file into the coef buffer */
|
|
for (;;) {
|
|
int retcode;
|
|
/* Call progress monitor hook if present */
|
|
if (cinfo->progress != NULL)
|
|
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
|
|
/* Absorb some more input */
|
|
retcode = (*cinfo->inputctl->consume_input) (cinfo);
|
|
if (retcode == JPEG_SUSPENDED)
|
|
return NULL;
|
|
if (retcode == JPEG_REACHED_EOI)
|
|
break;
|
|
/* Advance progress counter if appropriate */
|
|
if (cinfo->progress != NULL &&
|
|
(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
|
|
if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
|
|
/* startup underestimated number of scans; ratchet up one scan */
|
|
cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
|
|
}
|
|
}
|
|
}
|
|
/* Set state so that jpeg_finish_decompress does the right thing */
|
|
cinfo->global_state = DSTATE_STOPPING;
|
|
}
|
|
/* At this point we should be in state DSTATE_STOPPING if being used
|
|
* standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
|
|
* to the coefficients during a full buffered-image-mode decompression.
|
|
*/
|
|
if ((cinfo->global_state == DSTATE_STOPPING ||
|
|
cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
|
|
return cinfo->coef->coef_arrays;
|
|
}
|
|
/* Oops, improper usage */
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
return NULL; /* keep compiler happy */
|
|
}
|
|
|
|
|
|
/*
|
|
* Master selection of decompression modules for transcoding.
|
|
* This substitutes for jdmaster.c's initialization of the full decompressor.
|
|
*/
|
|
|
|
LOCAL(void)
|
|
transdecode_master_selection(j_decompress_ptr cinfo)
|
|
{
|
|
/* This is effectively a buffered-image operation. */
|
|
cinfo->buffered_image = TRUE;
|
|
|
|
#if JPEG_LIB_VERSION >= 80
|
|
/* Compute output image dimensions and related values. */
|
|
jpeg_core_output_dimensions(cinfo);
|
|
#endif
|
|
|
|
/* Entropy decoding: either Huffman or arithmetic coding. */
|
|
if (cinfo->arith_code) {
|
|
#ifdef D_ARITH_CODING_SUPPORTED
|
|
jinit_arith_decoder(cinfo);
|
|
#else
|
|
ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
|
|
#endif
|
|
} else {
|
|
if (cinfo->progressive_mode) {
|
|
#ifdef D_PROGRESSIVE_SUPPORTED
|
|
jinit_phuff_decoder(cinfo);
|
|
#else
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
#endif
|
|
} else
|
|
jinit_huff_decoder(cinfo);
|
|
}
|
|
|
|
/* Always get a full-image coefficient buffer. */
|
|
#ifdef WITH_12BIT
|
|
if (cinfo->data_precision == 12)
|
|
j12init_d_coef_controller(cinfo, TRUE);
|
|
else
|
|
#endif
|
|
jinit_d_coef_controller(cinfo, TRUE);
|
|
|
|
/* We can now tell the memory manager to allocate virtual arrays. */
|
|
(*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
|
|
|
|
/* Initialize input side of decompressor to consume first scan. */
|
|
(*cinfo->inputctl->start_input_pass) (cinfo);
|
|
|
|
/* Initialize progress monitoring. */
|
|
if (cinfo->progress != NULL) {
|
|
int nscans;
|
|
/* Estimate number of scans to set pass_limit. */
|
|
if (cinfo->progressive_mode) {
|
|
/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
|
|
nscans = 2 + 3 * cinfo->num_components;
|
|
} else if (cinfo->inputctl->has_multiple_scans) {
|
|
/* For a nonprogressive multiscan file, estimate 1 scan per component. */
|
|
nscans = cinfo->num_components;
|
|
} else {
|
|
nscans = 1;
|
|
}
|
|
cinfo->progress->pass_counter = 0L;
|
|
cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
|
|
cinfo->progress->completed_passes = 0;
|
|
cinfo->progress->total_passes = 1;
|
|
}
|
|
}
|