TurboJPEG 3 API overhaul
(ChangeLog update forthcoming)
- Prefix all function names with "tj3" and remove version suffixes from
function names. (Future API overhauls will increment the prefix to
"tj4", etc., thus retaining backward API/ABI compatibility without
versioning each individual function.)
- Replace stateless boolean flags (including TJ*FLAG_ARITHMETIC and
TJ*FLAG_LOSSLESS, which were never released) with stateful integer
parameters, the value of which persists between function calls.
* Use parameters for the JPEG quality and subsampling as well, in
order to eliminate the awkwardness of specifying function arguments
that weren't relevant for lossless compression.
* tj3DecompressHeader() now stores all relevant information about the
JPEG image, including the width, height, subsampling type, entropy
coding type, etc. in parameters rather than returning that
information in its arguments.
* TJ*FLAG_LIMITSCANS has been reimplemented as an integer parameter
(TJ*PARAM_SCANLIMIT) that allows the number of scans to be
specified.
- Use the const keyword for all pointer arguments to unmodified
buffers, as well as for both dimensions of 2D pointers. Addresses
#395.
- Use size_t rather than unsigned long to represent buffer sizes, since
unsigned long is a 32-bit type on Windows. Addresses #24.
- Return 0 from all buffer size functions if an error occurs, rather
than awkwardly trying to return -1 in an unsigned data type.
- Implement 12-bit and 16-bit data precision using dedicated
compression, decompression, and image I/O functions/methods.
* Suffix the names of all data-precision-specific functions with 8,
12, or 16.
* Because the YUV functions are intended to be used for video, they
are currently only implemented with 8-bit data precision, but they
can be expanded to 12-bit data precision in the future, if
necessary.
* Extend TJUnitTest and TJBench to test 12-bit and 16-bit data
precision, using a new -precision option.
* Add appropriate regression tests for all of the above to the 'test'
target.
* Extend tjbenchtest to test 12-bit and 16-bit data precision, and
add separate 'tjtest12' and 'tjtest16' targets.
* BufferedImage I/O in the Java API is currently limited to 8-bit
data precision, since the BufferedImage class does not
straightforwardly support higher data precisions.
* Extend the PPM reader to convert 12-bit and 16-bit PBMPLUS files
to grayscale or CMYK pixels, as it already does for 8-bit files.
- Properly accommodate lossless JPEG using dedicated parameters
(TJ*PARAM_LOSSLESS, TJ*PARAM_LOSSLESSPSV, and TJ*PARAM_LOSSLESSPT),
rather than using a flag and awkwardly repurposing the JPEG quality.
Update TJBench to properly reflect whether a JPEG image is lossless.
- Re-organize the TJBench usage screen.
- Update the Java docs using Java 11, to improve the formatting and
eliminate HTML frames.
- Use the accurate integer DCT algorithm by default for both
compression and decompression, since the "fast" algorithm is a legacy
feature, it does not pass the ISO compliance tests, and it is not
actually faster on modern x86 CPUs.
* Remove the -accuratedct option from TJBench and TJExample.
- Re-implement the 'tjtest' target using a CMake script that enables
the appropriate tests, depending on the data precision and whether or
not the Java API is part of the build.
- Consolidate the C and Java versions of tjbenchtest into one script.
- Consolidate the C and Java versions of tjexampletest into one script.
- Combine all initialization functions into a single function
(tj3Init()) that accepts an integer parameter specifying the
subsystems to initialize.
- Enable decompression scaling explicitly, using a new function/method
(tj3SetScalingFactor()/TJDecompressor.setScalingFactor()), rather
than implicitly using awkward "desired width"/"desired height"
parameters.
- Introduce a new macro/constant (TJUNSCALED/TJ.UNSCALED) that maps to
a scaling factor of 1/1.
- Implement partial image decompression, using a new function/method
(tj3SetCroppingRegion()/TJDecompressor.setCroppingRegion()) and
TJBench option (-crop). Extend tjbenchtest to test the new feature.
Addresses #1.
- Allow the JPEG colorspace to be specified explicitly when
compressing, using a new parameter (TJ*PARAM_COLORSPACE). This
allows JPEG images with the RGB and CMYK colorspaces to be created.
- Remove the error/difference image feature from TJBench. Identical
images to the ones that TJBench created can be generated using
ImageMagick with
'magick composite <original_image> <output_image> -compose difference <diff_image>'
- Handle JPEG images with unknown subsampling types. TJ*PARAM_SUBSAMP
is set to TJ*SAMP_UNKNOWN (== -1) for such images, but they can still
be decompressed fully into packed-pixel images or losslessly
transformed (with the exception of lossless cropping.) They cannot
be partially decompressed or decompressed into planar YUV images.
Note also that TJBench, due to its lack of support for imperfect
transforms, requires that the subsampling type be known when
rotating, flipping, or transversely transposing an image. Addresses
#436
- The Java version of TJBench now has identical functionality to the C
version. This was accomplished by (somewhat hackishly) calling the
TurboJPEG C image I/O functions through JNI and copying the pixels
between the C heap and the Java heap.
- Add parameters (TJ*PARAM_RESTARTROWS and TJ*PARAM_RESTARTBLOCKS) and
a TJBench option (-restart) to allow the restart marker interval to
be specified when compressing. Eliminate the undocumented TJ_RESTART
environment variable.
- Add a parameter (TJ*PARAM_OPTIMIZE), a transform option
(TJ*OPT_OPTIMIZE), and a TJBench option (-optimize) to allow
optimized baseline Huffman coding to be specified when compressing.
Eliminate the undocumented TJ_OPTIMIZE environment variable.
- Add parameters (TJ*PARAM_XDENSITY, TJ*PARAM_DENSITY, and
TJ*DENSITYUNITS) to allow the pixel density to be specified when
compressing or saving a Windows BMP image and to be queried when
decompressing or loading a Windows BMP image. Addresses #77.
- Refactor the fuzz targets to use the new API.
* Extend decompression coverage to 12-bit and 16-bit data precision.
* Replace the awkward cjpeg12 and cjpeg16 targets with proper
TurboJPEG-based compress12, compress12-lossless, and
compress16-lossless targets
- Fix innocuous UBSan warnings uncovered by the new fuzzers.
- Implement previous versions of the TurboJPEG API by wrapping the new
functions (tested by running the 2.1.x versions of TJBench, via
tjbenchtest, and TJUnitTest against the new implementation.)
* Remove all JNI functions for deprecated Java methods and implement
the deprecated methods using pure Java wrappers. It should be
understood that backward API compatibility in Java applies only to
the Java classes and that one cannot mix and match a JAR file from
one version of libjpeg-turbo with a JNI library from another
version.
- tj3Destroy() now silently accepts a NULL handle.
- tj3Alloc() and tj3Free() now return/accept void pointers, as malloc()
and free() do.
- The image I/O functions now accept a TurboJPEG instance handle, which
is used to transmit/receive parameters and to receive error
information.
Closes #517
This commit is contained in:
@@ -39,9 +39,6 @@ import java.io.*;
|
||||
*/
|
||||
public class TJCompressor implements Closeable {
|
||||
|
||||
private static final String NO_ASSOC_ERROR =
|
||||
"No source image is associated with this instance";
|
||||
|
||||
/**
|
||||
* Create a TurboJPEG compressor instance.
|
||||
*/
|
||||
@@ -50,9 +47,9 @@ public class TJCompressor implements Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a TurboJPEG compressor instance and associate the packed-pixel
|
||||
* source image stored in <code>srcImage</code> with the newly created
|
||||
* instance.
|
||||
* Create a TurboJPEG compressor instance and associate the 8-bit-per-sample
|
||||
* packed-pixel source image stored in <code>srcImage</code> with the newly
|
||||
* created instance.
|
||||
*
|
||||
* @param srcImage see {@link #setSourceImage} for description
|
||||
*
|
||||
@@ -75,9 +72,9 @@ public class TJCompressor implements Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a TurboJPEG compressor instance and associate the packed-pixel
|
||||
* source image stored in <code>srcImage</code> with the newly created
|
||||
* instance.
|
||||
* Create a TurboJPEG compressor instance and associate the 8-bit-per-sample
|
||||
* packed-pixel source image stored in <code>srcImage</code> with the newly
|
||||
* created instance.
|
||||
*
|
||||
* @param srcImage see
|
||||
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
|
||||
@@ -100,8 +97,8 @@ public class TJCompressor implements Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a packed-pixel RGB, grayscale, or CMYK source image with this
|
||||
* compressor instance.
|
||||
* Associate an 8-bit-per-sample packed-pixel RGB, grayscale, or CMYK source
|
||||
* image with this compressor instance.
|
||||
*
|
||||
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK
|
||||
* source image to be compressed or encoded. This buffer is not modified.
|
||||
@@ -116,15 +113,13 @@ public class TJCompressor implements Closeable {
|
||||
* which the JPEG or YUV image should be compressed/encoded
|
||||
*
|
||||
* @param pitch bytes per row in the source image. Normally this should be
|
||||
* <code>width *
|
||||
* </code>{@link TJ#getPixelSize TJ.getPixelSize}<code>(pixelFormat)</code>,
|
||||
* if the source image is unpadded. However, you can use this parameter to,
|
||||
* for instance, specify that the rows in the source image are padded to the
|
||||
* nearest multiple of 4 bytes or to compress/encode a JPEG or YUV image from
|
||||
* a region of a larger source image. You can also be clever and use this
|
||||
* parameter to skip rows, etc. Setting this parameter to 0 is the
|
||||
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>,
|
||||
* if the source image is unpadded. (Setting this parameter to 0 is the
|
||||
* equivalent of setting it to <code>width *
|
||||
* </code>{@link TJ#getPixelSize TJ.getPixelSize}<code>(pixelFormat)</code>.
|
||||
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However,
|
||||
* you can also use this parameter to specify the row alignment/padding of
|
||||
* the source image, to skip rows, or to compress/encode a JPEG or YUV image
|
||||
* from a specific region of a larger source image.
|
||||
*
|
||||
* @param height height (in pixels) of the region in the source image from
|
||||
* which the JPEG or YUV image should be compressed/encoded
|
||||
@@ -139,7 +134,7 @@ public class TJCompressor implements Closeable {
|
||||
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
|
||||
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
|
||||
throw new IllegalArgumentException("Invalid argument in setSourceImage()");
|
||||
srcBuf = srcImage;
|
||||
srcBuf8 = srcImage;
|
||||
srcWidth = width;
|
||||
if (pitch == 0)
|
||||
srcPitch = width * TJ.getPixelSize(pixelFormat);
|
||||
@@ -149,13 +144,127 @@ public class TJCompressor implements Closeable {
|
||||
srcPixelFormat = pixelFormat;
|
||||
srcX = x;
|
||||
srcY = y;
|
||||
srcBuf12 = null;
|
||||
srcBuf16 = null;
|
||||
srcBufInt = null;
|
||||
srcYUVImage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a packed-pixel RGB or grayscale source image with this
|
||||
* compressor instance.
|
||||
* Associate a 12-bit-per-sample packed-pixel RGB, grayscale, or CMYK source
|
||||
* image with this compressor instance. Note that 12-bit-per-sample
|
||||
* packed-pixel source images can only be compressed into 12-bit-per-sample
|
||||
* JPEG images.
|
||||
*
|
||||
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK
|
||||
* source image to be compressed. This buffer is not modified.
|
||||
*
|
||||
* @param x x offset (in pixels) of the region in the source image from which
|
||||
* the JPEG image should be compressed
|
||||
*
|
||||
* @param y y offset (in pixels) of the region in the source image from which
|
||||
* the JPEG image should be compressed
|
||||
*
|
||||
* @param width width (in pixels) of the region in the source image from
|
||||
* which the JPEG image should be compressed
|
||||
*
|
||||
* @param pitch samples per row in the source image. Normally this should be
|
||||
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>,
|
||||
* if the source image is unpadded. (Setting this parameter to 0 is the
|
||||
* equivalent of setting it to <code>width *
|
||||
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However,
|
||||
* you can also use this parameter to specify the row alignment/padding of
|
||||
* the source image, to skip rows, or to compress a JPEG image from a
|
||||
* specific region of a larger source image.
|
||||
*
|
||||
* @param height height (in pixels) of the region in the source image from
|
||||
* which the JPEG image should be compressed
|
||||
*
|
||||
* @param pixelFormat pixel format of the source image (one of
|
||||
* {@link TJ#PF_RGB TJ.PF_*})
|
||||
*/
|
||||
public void setSourceImage12(short[] srcImage, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat)
|
||||
throws TJException {
|
||||
if (handle == 0) init();
|
||||
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
|
||||
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
|
||||
throw new IllegalArgumentException("Invalid argument in setSourceImage()");
|
||||
srcBuf12 = srcImage;
|
||||
srcWidth = width;
|
||||
if (pitch == 0)
|
||||
srcPitch = width * TJ.getPixelSize(pixelFormat);
|
||||
else
|
||||
srcPitch = pitch;
|
||||
srcHeight = height;
|
||||
srcPixelFormat = pixelFormat;
|
||||
srcX = x;
|
||||
srcY = y;
|
||||
srcBuf8 = null;
|
||||
srcBuf16 = null;
|
||||
srcBufInt = null;
|
||||
srcYUVImage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a 16-bit-per-sample packed-pixel RGB, grayscale, or CMYK source
|
||||
* image with this compressor instance. Note that 16-bit-per-sample
|
||||
* packed-pixel source images can only be compressed into 16-bit-per-sample
|
||||
* lossless JPEG images.
|
||||
*
|
||||
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK
|
||||
* source image to be compressed. This buffer is not modified.
|
||||
*
|
||||
* @param x x offset (in pixels) of the region in the source image from which
|
||||
* the JPEG image should be compressed
|
||||
*
|
||||
* @param y y offset (in pixels) of the region in the source image from which
|
||||
* the JPEG image should be compressed
|
||||
*
|
||||
* @param width width (in pixels) of the region in the source image from
|
||||
* which the JPEG image should be compressed
|
||||
*
|
||||
* @param pitch samples per row in the source image. Normally this should be
|
||||
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>,
|
||||
* if the source image is unpadded. (Setting this parameter to 0 is the
|
||||
* equivalent of setting it to <code>width *
|
||||
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However,
|
||||
* you can also use this parameter to specify the row alignment/padding of
|
||||
* the source image, to skip rows, or to compress a JPEG image from a
|
||||
* specific region of a larger source image.
|
||||
*
|
||||
* @param height height (in pixels) of the region in the source image from
|
||||
* which the JPEG image should be compressed
|
||||
*
|
||||
* @param pixelFormat pixel format of the source image (one of
|
||||
* {@link TJ#PF_RGB TJ.PF_*})
|
||||
*/
|
||||
public void setSourceImage16(short[] srcImage, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat)
|
||||
throws TJException {
|
||||
if (handle == 0) init();
|
||||
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
|
||||
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
|
||||
throw new IllegalArgumentException("Invalid argument in setSourceImage()");
|
||||
srcBuf16 = srcImage;
|
||||
srcWidth = width;
|
||||
if (pitch == 0)
|
||||
srcPitch = width * TJ.getPixelSize(pixelFormat);
|
||||
else
|
||||
srcPitch = pitch;
|
||||
srcHeight = height;
|
||||
srcPixelFormat = pixelFormat;
|
||||
srcX = x;
|
||||
srcY = y;
|
||||
srcBuf8 = null;
|
||||
srcBuf12 = null;
|
||||
srcBufInt = null;
|
||||
srcYUVImage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate an 8-bit-per-pixel packed-pixel RGB or grayscale source image
|
||||
* with this compressor instance.
|
||||
*
|
||||
* @param srcImage a <code>BufferedImage</code> instance containing a
|
||||
* packed-pixel RGB or grayscale source image to be compressed or encoded.
|
||||
@@ -225,7 +334,7 @@ public class TJCompressor implements Closeable {
|
||||
srcStride = sm.getScanlineStride();
|
||||
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
|
||||
srcBufInt = db.getData();
|
||||
srcBuf = null;
|
||||
srcBuf8 = null;
|
||||
} else {
|
||||
ComponentSampleModel sm =
|
||||
(ComponentSampleModel)srcImage.getSampleModel();
|
||||
@@ -234,14 +343,16 @@ public class TJCompressor implements Closeable {
|
||||
throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage");
|
||||
srcPitch = sm.getScanlineStride();
|
||||
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
|
||||
srcBuf = db.getData();
|
||||
srcBuf8 = db.getData();
|
||||
srcBufInt = null;
|
||||
}
|
||||
srcYUVImage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a planar YUV source image with this compressor instance.
|
||||
* Associate an 8-bit-per-sample planar YUV source image with this compressor
|
||||
* instance. This method sets {@link TJ#PARAM_SUBSAMP} to the chrominance
|
||||
* subsampling level of the source image.
|
||||
*
|
||||
* @param srcImage planar YUV source image to be compressed. This image is
|
||||
* not modified.
|
||||
@@ -251,54 +362,53 @@ public class TJCompressor implements Closeable {
|
||||
if (srcImage == null)
|
||||
throw new IllegalArgumentException("Invalid argument in setSourceImage()");
|
||||
srcYUVImage = srcImage;
|
||||
srcBuf = null;
|
||||
set(TJ.PARAM_SUBSAMP, srcImage.getSubsamp());
|
||||
srcBuf8 = null;
|
||||
srcBufInt = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the level of chrominance subsampling for subsequent compress/encode
|
||||
* operations. When pixels are converted from RGB to YCbCr (see
|
||||
* {@link TJ#CS_YCbCr}) or from CMYK to YCCK (see {@link TJ#CS_YCCK}) as part
|
||||
* of the JPEG compression process, some of the Cb and Cr (chrominance)
|
||||
* components can be discarded or averaged together to produce a smaller
|
||||
* image with little perceptible loss of image clarity. (The human eye is
|
||||
* more sensitive to small changes in brightness than to small changes in
|
||||
* color.) This is called "chrominance subsampling".
|
||||
* <p>
|
||||
* NOTE: This method has no effect when compressing a JPEG image from a
|
||||
* planar YUV source image. In that case, the level of chrominance
|
||||
* subsampling in the JPEG image is determined by the source image.
|
||||
* Furthermore, this method has no effect when encoding to a pre-allocated
|
||||
* {@link YUVImage} instance. In that case, the level of chrominance
|
||||
* subsampling is determined by the destination image.
|
||||
* Set the value of a compression parameter.
|
||||
*
|
||||
* @param newSubsamp the level of chrominance subsampling to use in
|
||||
* subsequent compress/encode oeprations (one of
|
||||
* {@link TJ#SAMP_444 TJ.SAMP_*})
|
||||
* @param param one of {@link TJ#PARAM_STOPONWARNING TJ.PARAM_*}
|
||||
*
|
||||
* @param value value of the compression parameter (refer to
|
||||
* {@link TJ#PARAM_STOPONWARNING parameter documentation})
|
||||
*/
|
||||
public void setSubsamp(int newSubsamp) {
|
||||
if (newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
|
||||
public native void set(int param, int value);
|
||||
|
||||
/**
|
||||
* Get the value of a compression parameter.
|
||||
*
|
||||
* @param param one of {@link TJ#PARAM_STOPONWARNING TJ.PARAM_*}
|
||||
*
|
||||
* @return the value of the specified compression parameter, or -1 if the
|
||||
* value is unknown.
|
||||
*/
|
||||
public native int get(int param);
|
||||
|
||||
/**
|
||||
* @deprecated Use
|
||||
* <code>{@link #set set}({@link TJ#PARAM_SUBSAMP}, ...)</code> instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public void setSubsamp(int subsamp) {
|
||||
if (subsamp < 0 || subsamp >= TJ.NUMSAMP)
|
||||
throw new IllegalArgumentException("Invalid argument in setSubsamp()");
|
||||
subsamp = newSubsamp;
|
||||
set(TJ.PARAM_SUBSAMP, subsamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the JPEG image quality level for subsequent compress operations.
|
||||
*
|
||||
* @param quality the new JPEG image quality level (1 to 100, 1 = worst,
|
||||
* 100 = best.) When generating a lossless JPEG image (see
|
||||
* {@link TJ#FLAG_LOSSLESS}), <code>quality</code> is
|
||||
* <code>psv * 10 + Pt</code>, where <code>psv</code> is the predictor
|
||||
* selection value (1-7) and <code>Pt</code> is the point transform (0-7). A
|
||||
* point transform value of 0 is necessary in order to create a fully
|
||||
* lossless JPEG image. (A non-zero point transform value right-shifts the
|
||||
* input samples by the specified number of bits, which is effectively a form
|
||||
* of lossy color quantization.)
|
||||
* @deprecated Use
|
||||
* <code>{@link #set set}({@link TJ#PARAM_QUALITY}, ...)</code> instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public void setJPEGQuality(int quality) {
|
||||
if (quality < 1 || quality > 100)
|
||||
throw new IllegalArgumentException("Invalid argument in setJPEGQuality()");
|
||||
jpegQuality = quality;
|
||||
set(TJ.PARAM_QUALITY, quality);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,139 +417,180 @@ public class TJCompressor implements Closeable {
|
||||
* buffer.
|
||||
*
|
||||
* @param dstBuf buffer that will receive the JPEG image. Use
|
||||
* {@link TJ#bufSize} to determine the maximum size for this buffer based on
|
||||
* the source image's width and height and the desired level of chrominance
|
||||
* subsampling.
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of
|
||||
* {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
|
||||
* {@link TJ#bufSize TJ.bufSize()} to determine the maximum size for this
|
||||
* buffer based on the source image's width and height and the desired level
|
||||
* of chrominance subsampling (see {@link TJ#PARAM_SUBSAMP}.)
|
||||
*/
|
||||
public void compress(byte[] dstBuf, int flags) throws TJException {
|
||||
if (dstBuf == null || flags < 0)
|
||||
public void compress(byte[] dstBuf) throws TJException {
|
||||
if (dstBuf == null)
|
||||
throw new IllegalArgumentException("Invalid argument in compress()");
|
||||
if (srcBuf == null && srcBufInt == null && srcYUVImage == null)
|
||||
throw new IllegalStateException(NO_ASSOC_ERROR);
|
||||
if (jpegQuality < 0)
|
||||
throw new IllegalStateException("JPEG Quality not set");
|
||||
if (subsamp < 0 && srcYUVImage == null)
|
||||
throw new IllegalStateException("Subsampling level not set");
|
||||
|
||||
if (srcYUVImage != null)
|
||||
compressedSize = compressFromYUV(srcYUVImage.getPlanes(),
|
||||
srcYUVImage.getOffsets(),
|
||||
srcYUVImage.getWidth(),
|
||||
srcYUVImage.getStrides(),
|
||||
srcYUVImage.getHeight(),
|
||||
srcYUVImage.getSubsamp(),
|
||||
dstBuf, jpegQuality, flags);
|
||||
else if (srcBuf != null)
|
||||
compressedSize = compress(srcBuf, srcX, srcY, srcWidth, srcPitch,
|
||||
srcHeight, srcPixelFormat, dstBuf, subsamp,
|
||||
jpegQuality, flags);
|
||||
if (srcYUVImage != null) {
|
||||
checkSubsampling();
|
||||
if (get(TJ.PARAM_SUBSAMP) != srcYUVImage.getSubsamp())
|
||||
throw new IllegalStateException("TJ.PARAM_SUBSAMP must match subsampling level of YUV image");
|
||||
compressedSize = compressFromYUV8(srcYUVImage.getPlanes(),
|
||||
srcYUVImage.getOffsets(),
|
||||
srcYUVImage.getWidth(),
|
||||
srcYUVImage.getStrides(),
|
||||
srcYUVImage.getHeight(), dstBuf);
|
||||
} else if (srcBuf8 != null)
|
||||
compressedSize = compress8(srcBuf8, srcX, srcY, srcWidth, srcPitch,
|
||||
srcHeight, srcPixelFormat, dstBuf);
|
||||
else if (srcBuf12 != null)
|
||||
compressedSize = compress12(srcBuf12, srcX, srcY, srcWidth, srcPitch,
|
||||
srcHeight, srcPixelFormat, dstBuf);
|
||||
else if (srcBuf16 != null)
|
||||
compressedSize = compress16(srcBuf16, srcX, srcY, srcWidth, srcPitch,
|
||||
srcHeight, srcPixelFormat, dstBuf);
|
||||
else if (srcBufInt != null)
|
||||
compressedSize = compress(srcBufInt, srcX, srcY, srcWidth, srcStride,
|
||||
srcHeight, srcPixelFormat, dstBuf, subsamp,
|
||||
jpegQuality, flags);
|
||||
compressedSize = compress8(srcBufInt, srcX, srcY, srcWidth, srcStride,
|
||||
srcHeight, srcPixelFormat, dstBuf);
|
||||
else
|
||||
throw new IllegalStateException("No source image is associated with this instance");
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #set set()} and {@link #compress(byte[])} instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public void compress(byte[] dstBuf, int flags) throws TJException {
|
||||
if (flags < 0)
|
||||
throw new IllegalArgumentException("Invalid argument in compress()");
|
||||
processFlags(flags);
|
||||
compress(dstBuf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress the packed-pixel or planar YUV source image associated with this
|
||||
* compressor instance and return a buffer containing a JPEG image.
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of
|
||||
* {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
|
||||
*
|
||||
* @return a buffer containing a JPEG image. The length of this buffer will
|
||||
* not be equal to the size of the JPEG image. Use {@link
|
||||
* #getCompressedSize} to obtain the size of the JPEG image.
|
||||
* not be equal to the size of the JPEG image. Use
|
||||
* {@link #getCompressedSize} to obtain the size of the JPEG image.
|
||||
*/
|
||||
public byte[] compress(int flags) throws TJException {
|
||||
public byte[] compress() throws TJException {
|
||||
byte[] buf;
|
||||
if (srcYUVImage != null) {
|
||||
buf = new byte[TJ.bufSize(srcYUVImage.getWidth(),
|
||||
srcYUVImage.getHeight(),
|
||||
srcYUVImage.getSubsamp())];
|
||||
} else {
|
||||
checkSourceImage();
|
||||
checkSubsampling();
|
||||
int subsamp = get(TJ.PARAM_SUBSAMP);
|
||||
buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)];
|
||||
}
|
||||
compress(buf, flags);
|
||||
compress(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the packed-pixel source image associated with this compressor
|
||||
* instance into a planar YUV image and store it in the given
|
||||
* {@link YUVImage} instance. This method performs color conversion (which
|
||||
* is accelerated in the libjpeg-turbo implementation) but does not execute
|
||||
* any of the other steps in the JPEG compression process. Encoding CMYK
|
||||
* source images into YUV images is not supported.
|
||||
* @deprecated Use {@link #set set()} and {@link #compress()} instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public byte[] compress(int flags) throws TJException {
|
||||
processFlags(flags);
|
||||
return compress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the 8-bit-per-sample packed-pixel source image associated with this
|
||||
* compressor instance into an 8-bit-per-sample planar YUV image and store it
|
||||
* in the given {@link YUVImage} instance. This method performs color
|
||||
* conversion (which is accelerated in the libjpeg-turbo implementation) but
|
||||
* does not execute any of the other steps in the JPEG compression process.
|
||||
* Encoding CMYK source images into YUV images is not supported. This method
|
||||
* sets {@link TJ#PARAM_SUBSAMP} to the chrominance subsampling level of the
|
||||
* destination image.
|
||||
*
|
||||
* @param dstImage {@link YUVImage} instance that will receive the planar YUV
|
||||
* image
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of
|
||||
* {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
|
||||
*/
|
||||
public void encodeYUV(YUVImage dstImage, int flags) throws TJException {
|
||||
if (dstImage == null || flags < 0)
|
||||
public void encodeYUV(YUVImage dstImage) throws TJException {
|
||||
if (dstImage == null)
|
||||
throw new IllegalArgumentException("Invalid argument in encodeYUV()");
|
||||
if (srcBuf == null && srcBufInt == null)
|
||||
throw new IllegalStateException(NO_ASSOC_ERROR);
|
||||
if (srcBuf8 == null && srcBufInt == null)
|
||||
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance");
|
||||
if (srcYUVImage != null)
|
||||
throw new IllegalStateException("Source image is not correct type");
|
||||
checkSubsampling();
|
||||
if (srcWidth != dstImage.getWidth() || srcHeight != dstImage.getHeight())
|
||||
throw new IllegalStateException("Destination image is the wrong size");
|
||||
set(TJ.PARAM_SUBSAMP, dstImage.getSubsamp());
|
||||
|
||||
if (srcBufInt != null) {
|
||||
encodeYUV(srcBufInt, srcX, srcY, srcWidth, srcStride, srcHeight,
|
||||
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
|
||||
dstImage.getStrides(), dstImage.getSubsamp(), flags);
|
||||
encodeYUV8(srcBufInt, srcX, srcY, srcWidth, srcStride, srcHeight,
|
||||
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
|
||||
dstImage.getStrides());
|
||||
} else {
|
||||
encodeYUV(srcBuf, srcX, srcY, srcWidth, srcPitch, srcHeight,
|
||||
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
|
||||
dstImage.getStrides(), dstImage.getSubsamp(), flags);
|
||||
encodeYUV8(srcBuf8, srcX, srcY, srcWidth, srcPitch, srcHeight,
|
||||
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
|
||||
dstImage.getStrides());
|
||||
}
|
||||
compressedSize = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the packed-pixel source image associated with this compressor
|
||||
* instance into a unified planar YUV image and return a {@link YUVImage}
|
||||
* instance containing the encoded image. This method performs color
|
||||
* conversion (which is accelerated in the libjpeg-turbo implementation) but
|
||||
* does not execute any of the other steps in the JPEG compression process.
|
||||
* Encoding CMYK source images into YUV images is not supported.
|
||||
* @deprecated Use {@link #set set()} and {@link #encodeYUV(YUVImage)}
|
||||
* instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public void encodeYUV(YUVImage dstImage, int flags) throws TJException {
|
||||
if (flags < 0)
|
||||
throw new IllegalArgumentException("Invalid argument in encodeYUV()");
|
||||
|
||||
processFlags(flags);
|
||||
encodeYUV(dstImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the 8-bit-per-sample packed-pixel source image associated with this
|
||||
* compressor instance into an 8-bit-per-sample unified planar YUV image and
|
||||
* return a {@link YUVImage} instance containing the encoded image. This
|
||||
* method performs color conversion (which is accelerated in the
|
||||
* libjpeg-turbo implementation) but does not execute any of the other steps
|
||||
* in the JPEG compression process. Encoding CMYK source images into YUV
|
||||
* images is not supported.
|
||||
*
|
||||
* @param align row alignment (in bytes) of the YUV image (must be a power of
|
||||
* 2.) Setting this parameter to n will cause each row in each plane of the
|
||||
* YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of
|
||||
* {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
|
||||
*
|
||||
* @return a {@link YUVImage} instance containing the unified planar YUV
|
||||
* encoded image
|
||||
*/
|
||||
public YUVImage encodeYUV(int align, int flags) throws TJException {
|
||||
checkSourceImage();
|
||||
public YUVImage encodeYUV(int align) throws TJException {
|
||||
if (srcBuf8 == null && srcBufInt == null)
|
||||
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance");
|
||||
checkSubsampling();
|
||||
if (align < 1 || ((align & (align - 1)) != 0))
|
||||
throw new IllegalStateException("Invalid argument in encodeYUV()");
|
||||
YUVImage dstYUVImage = new YUVImage(srcWidth, align, srcHeight, subsamp);
|
||||
encodeYUV(dstYUVImage, flags);
|
||||
YUVImage dstYUVImage = new YUVImage(srcWidth, align, srcHeight,
|
||||
get(TJ.PARAM_SUBSAMP));
|
||||
encodeYUV(dstYUVImage);
|
||||
return dstYUVImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the packed-pixel source image associated with this compressor
|
||||
* instance into separate Y, U (Cb), and V (Cr) image planes and return a
|
||||
* {@link YUVImage} instance containing the encoded image planes. This
|
||||
* method performs color conversion (which is accelerated in the
|
||||
* libjpeg-turbo implementation) but does not execute any of the other steps
|
||||
* in the JPEG compression process. Encoding CMYK source images into YUV
|
||||
* images is not supported.
|
||||
* @deprecated Use {@link #set set()} and {@link #encodeYUV(int)} instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public YUVImage encodeYUV(int align, int flags) throws TJException {
|
||||
processFlags(flags);
|
||||
return encodeYUV(align);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the 8-bit-per-sample packed-pixel source image associated with this
|
||||
* compressor instance into separate 8-bit-per-sample Y, U (Cb), and V (Cr)
|
||||
* image planes and return a {@link YUVImage} instance containing the encoded
|
||||
* image planes. This method performs color conversion (which is accelerated
|
||||
* in the libjpeg-turbo implementation) but does not execute any of the other
|
||||
* steps in the JPEG compression process. Encoding CMYK source images into
|
||||
* YUV images is not supported.
|
||||
*
|
||||
* @param strides an array of integers, each specifying the number of bytes
|
||||
* per row in the corresponding plane of the YUV source image. Setting the
|
||||
@@ -449,19 +600,28 @@ public class TJCompressor implements Closeable {
|
||||
* adjust the strides in order to add an arbitrary amount of row padding to
|
||||
* each plane.
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of
|
||||
* {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
|
||||
*
|
||||
* @return a {@link YUVImage} instance containing the encoded image planes
|
||||
*/
|
||||
public YUVImage encodeYUV(int[] strides, int flags) throws TJException {
|
||||
checkSourceImage();
|
||||
public YUVImage encodeYUV(int[] strides) throws TJException {
|
||||
if (srcBuf8 == null && srcBufInt == null)
|
||||
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance");
|
||||
checkSubsampling();
|
||||
YUVImage dstYUVImage = new YUVImage(srcWidth, strides, srcHeight, subsamp);
|
||||
encodeYUV(dstYUVImage, flags);
|
||||
YUVImage dstYUVImage = new YUVImage(srcWidth, strides, srcHeight,
|
||||
get(TJ.PARAM_SUBSAMP));
|
||||
encodeYUV(dstYUVImage);
|
||||
return dstYUVImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #set set()} and {@link #encodeYUV(int[])} instead.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
@Deprecated
|
||||
public YUVImage encodeYUV(int[] strides, int flags) throws TJException {
|
||||
processFlags(flags);
|
||||
return encodeYUV(strides);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the image (in bytes) generated by the most recent
|
||||
* compress operation.
|
||||
@@ -493,55 +653,82 @@ public class TJCompressor implements Closeable {
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void processFlags(int flags) {
|
||||
set(TJ.PARAM_BOTTOMUP, (flags & TJ.FLAG_BOTTOMUP) != 0 ? 1 : 0);
|
||||
|
||||
if (get(TJ.PARAM_QUALITY) >= 96 || (flags & TJ.FLAG_ACCURATEDCT) != 0)
|
||||
set(TJ.PARAM_FASTDCT, 0);
|
||||
else
|
||||
set(TJ.PARAM_FASTDCT, 1);
|
||||
|
||||
set(TJ.PARAM_STOPONWARNING, (flags & TJ.FLAG_STOPONWARNING) != 0 ? 1 : 0);
|
||||
set(TJ.PARAM_PROGRESSIVE, (flags & TJ.FLAG_PROGRESSIVE) != 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
private void checkSubsampling() {
|
||||
if (get(TJ.PARAM_SUBSAMP) == TJ.SAMP_UNKNOWN)
|
||||
throw new IllegalStateException("TJ.PARAM_SUBSAMP must be specified");
|
||||
}
|
||||
|
||||
private native void init() throws TJException;
|
||||
|
||||
private native void destroy() throws TJException;
|
||||
|
||||
// JPEG size in bytes is returned
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native int compress(byte[] srcBuf, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp,
|
||||
int jpegQual, int flags) throws TJException;
|
||||
private native int compress8(byte[] srcBuf, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native int compress(int[] srcBuf, int x, int y, int width,
|
||||
int stride, int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp,
|
||||
int jpegQual, int flags) throws TJException;
|
||||
private native int compress12(short[] srcBuf, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native int compressFromYUV(byte[][] srcPlanes, int[] srcOffsets,
|
||||
int width, int[] srcStrides, int height, int subsamp, byte[] jpegBuf,
|
||||
int jpegQual, int flags)
|
||||
private native int compress16(short[] srcBuf, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native int compress8(int[] srcBuf, int x, int y, int width,
|
||||
int stride, int height, int pixelFormat, byte[] jpegBuf)
|
||||
throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native void encodeYUV(byte[] srcBuf, int x, int y, int width,
|
||||
private native int compressFromYUV8(byte[][] srcPlanes, int[] srcOffsets,
|
||||
int width, int[] srcStrides, int height, byte[] jpegBuf)
|
||||
throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native void encodeYUV8(byte[] srcBuf, int x, int y, int width,
|
||||
int pitch, int height, int pixelFormat, byte[][] dstPlanes,
|
||||
int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
|
||||
throws TJException;
|
||||
int[] dstOffsets, int[] dstStrides) throws TJException;
|
||||
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
private native void encodeYUV(int[] srcBuf, int x, int y, int width,
|
||||
private native void encodeYUV8(int[] srcBuf, int x, int y, int width,
|
||||
int srcStride, int height, int pixelFormat, byte[][] dstPlanes,
|
||||
int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
|
||||
throws TJException;
|
||||
int[] dstOffsets, int[] dstStrides) throws TJException;
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
* Ugly hack alert. It isn't straightforward to load 12-bit-per-sample and
|
||||
* 16-bit-per-sample images using the ImageIO and BufferedImage classes, and
|
||||
* ImageIO doesn't support PBMPLUS files anyhow. This method accesses
|
||||
* tj3LoadImage() through JNI and copies the pixel data between the C and
|
||||
* Java heaps. Currently it is undocumented and used only by TJBench.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod")
|
||||
public native Object loadImage(int precision, String fileName, int[] width,
|
||||
int align, int[] height, int[] pixelFormat)
|
||||
throws TJException;
|
||||
|
||||
static {
|
||||
TJLoader.load();
|
||||
}
|
||||
|
||||
private void checkSourceImage() {
|
||||
if (srcWidth < 1 || srcHeight < 1)
|
||||
throw new IllegalStateException(NO_ASSOC_ERROR);
|
||||
}
|
||||
|
||||
private void checkSubsampling() {
|
||||
if (subsamp < 0)
|
||||
throw new IllegalStateException("Subsampling level not set");
|
||||
}
|
||||
|
||||
private long handle = 0;
|
||||
private byte[] srcBuf = null;
|
||||
private byte[] srcBuf8 = null;
|
||||
private short[] srcBuf12 = null;
|
||||
private short[] srcBuf16 = null;
|
||||
private int[] srcBufInt = null;
|
||||
private int srcWidth = 0;
|
||||
private int srcHeight = 0;
|
||||
@@ -551,8 +738,6 @@ public class TJCompressor implements Closeable {
|
||||
private int srcStride = 0;
|
||||
private int srcPixelFormat = -1;
|
||||
private YUVImage srcYUVImage = null;
|
||||
private int subsamp = -1;
|
||||
private int jpegQuality = -1;
|
||||
private int compressedSize = 0;
|
||||
private ByteOrder byteOrder = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user