/* * Copyright (C)2011-2013, 2017-2018, 2020-2023 D. R. Commander. * All Rights Reserved. * Copyright (C)2015 Viktor Szathmáry. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the libjpeg-turbo Project nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package org.libjpegturbo.turbojpeg; import java.awt.Rectangle; /** * TurboJPEG utility class (cannot be instantiated) */ public final class TJ { private TJ() {} /** * The number of chrominance subsampling options */ public static final int NUMSAMP = 6; /** * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG * or YUV image will contain one chrominance component for every pixel in the * source image. */ public static final int SAMP_444 = 0; /** * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one * chrominance component for every 2x1 block of pixels in the source image. */ public static final int SAMP_422 = 1; /** * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one * chrominance component for every 2x2 block of pixels in the source image. */ public static final int SAMP_420 = 2; /** * Grayscale. The JPEG or YUV image will contain no chrominance components. */ public static final int SAMP_GRAY = 3; /** * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one * chrominance component for every 1x2 block of pixels in the source image. * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo. */ public static final int SAMP_440 = 4; /** * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one * chrominance component for every 4x1 block of pixels in the source image. * JPEG images compressed with 4:1:1 subsampling will be almost exactly the * same size as those compressed with 4:2:0 subsampling, and in the * aggregate, both subsampling methods produce approximately the same * perceptual quality. However, 4:1:1 is better able to reproduce sharp * horizontal features. Note that 4:1:1 subsampling is not fully accelerated * in libjpeg-turbo. */ public static final int SAMP_411 = 5; /** * Unknown subsampling. The JPEG image uses an unusual type of chrominance * subsampling. Such images can be decompressed into packed-pixel images, * but they cannot be *
TJ.PF_BGRX is stored in
* char pixel[], then the red component will be
* pixel[TJ.getRedOffset(TJ.PF_BGRX)].
*
* @param pixelFormat the pixel format (one of {@link #PF_RGB PF_*})
*
* @return the red offset for the given pixel format, or -1 if the pixel
* format does not have a red component.
*/
public static int getRedOffset(int pixelFormat) {
checkPixelFormat(pixelFormat);
return RED_OFFSET[pixelFormat];
}
private static final int[] RED_OFFSET = {
0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
};
/**
* For the given pixel format, returns the number of samples that the green
* component is offset from the start of the pixel. For instance, if an
* 8-bit-per-sample pixel of format TJ.PF_BGRX is stored in
* char pixel[], then the green component will be
* pixel[TJ.getGreenOffset(TJ.PF_BGRX)].
*
* @param pixelFormat the pixel format (one of {@link #PF_RGB PF_*})
*
* @return the green offset for the given pixel format, or -1 if the pixel
* format does not have a green component.
*/
public static int getGreenOffset(int pixelFormat) {
checkPixelFormat(pixelFormat);
return GREEN_OFFSET[pixelFormat];
}
private static final int[] GREEN_OFFSET = {
1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
};
/**
* For the given pixel format, returns the number of samples that the blue
* component is offset from the start of the pixel. For instance, if an
* 8-bit-per-sample pixel of format TJ.PF_BGRX is stored in
* char pixel[], then the blue component will be
* pixel[TJ.getBlueOffset(TJ.PF_BGRX)].
*
* @param pixelFormat the pixel format (one of {@link #PF_RGB PF_*})
*
* @return the blue offset for the given pixel format, or -1 if the pixel
* format does not have a blue component.
*/
public static int getBlueOffset(int pixelFormat) {
checkPixelFormat(pixelFormat);
return BLUE_OFFSET[pixelFormat];
}
private static final int[] BLUE_OFFSET = {
2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
};
/**
* For the given pixel format, returns the number of samples that the alpha
* component is offset from the start of the pixel. For instance, if an
* 8-bit-per-sample pixel of format TJ.PF_BGRA is stored in
* char pixel[], then the alpha component will be
* pixel[TJ.getAlphaOffset(TJ.PF_BGRA)].
*
* @param pixelFormat the pixel format (one of {@link #PF_RGB PF_*})
*
* @return the alpha offset for the given pixel format, or -1 if the pixel
* format does not have a alpha component.
*/
public static int getAlphaOffset(int pixelFormat) {
checkPixelFormat(pixelFormat);
return ALPHA_OFFSET[pixelFormat];
}
private static final int[] ALPHA_OFFSET = {
-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
};
/**
* The number of JPEG colorspaces
*/
public static final int NUMCS = 5;
/**
* RGB colorspace. When compressing the JPEG image, the R, G, and B
* components in the source image are reordered into image planes, but no
* colorspace conversion or subsampling is performed. RGB JPEG images can be
* compressed from and decompressed to packed-pixel images with any of the
* extended RGB or grayscale pixel formats, but they cannot be compressed
* from or decompressed to planar YUV images.
*/
public static final int CS_RGB = 0;
/**
* YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
* mathematical transformation of RGB designed solely for storage and
* transmission. YCbCr images must be converted to RGB before they can
* actually be displayed. In the YCbCr colorspace, the Y (luminance)
* component represents the black & white portion of the original image,
* and the Cb and Cr (chrominance) components represent the color portion of
* the original image. Originally, the analog equivalent of this
* transformation allowed the same signal to drive both black & white and
* color televisions, but JPEG images use YCbCr primarily because it allows
* the color data to be optionally subsampled for the purposes of reducing
* network or disk usage. YCbCr is the most common JPEG colorspace, and
* YCbCr JPEG images can be compressed from and decompressed to packed-pixel
* images with any of the extended RGB or grayscale pixel formats. YCbCr
* JPEG images can also be compressed from and decompressed to planar YUV
* images.
*/
@SuppressWarnings("checkstyle:ConstantName")
public static final int CS_YCbCr = 1;
/**
* Grayscale colorspace. The JPEG image retains only the luminance data (Y
* component), and any color data from the source image is discarded.
* Grayscale JPEG images can be compressed from and decompressed to
* packed-pixel images with any of the extended RGB or grayscale pixel
* formats, or they can be compressed from and decompressed to planar YUV
* images.
*/
public static final int CS_GRAY = 2;
/**
* CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
* components in the source image are reordered into image planes, but no
* colorspace conversion or subsampling is performed. CMYK JPEG images can
* only be compressed from and decompressed to packed-pixel images with the
* CMYK pixel format.
*/
public static final int CS_CMYK = 3;
/**
* YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
* rather a mathematical transformation of CMYK designed solely for storage
* and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
* reversibly transformed into YCCK, and as with YCbCr, the chrominance
* components in the YCCK pixels can be subsampled without incurring major
* perceptual loss. YCCK JPEG images can only be compressed from and
* decompressed to packed-pixel images with the CMYK pixel format.
*/
public static final int CS_YCCK = 4;
/**
* Error handling behavior
*
* Value *
0 [default] Allow the current
* compression/decompression/transform operation to complete unless a fatal
* error is encountered.
* 1 Immediately discontinue the current
* compression/decompression/transform operation if a warning (non-fatal
* error) occurs.
* Value *
0 [default] top-down (X11) order
* 1 bottom-up (Windows, OpenGL) order
* Value *
1-100 (1 = worst quality but
* best compression, 100 = best quality but worst compression)
* [no default; must be explicitly specified]
* The JPEG or YUV image uses (decompression, decoding) or will use (lossy * compression, encoding) the specified level of chrominance subsampling. * *
When pixels are converted from RGB to YCbCr (see {@link #CS_YCbCr}) or * from CMYK to YCCK (see {@link #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". * *
Value *
The JPEG image uses the specified number of bits per sample. * *
Value *
8, 12, or 16
* 12-bit data precision implies {@link #PARAM_OPTIMIZE} unless * {@link #PARAM_ARITHMETIC} is set. */ public static final int PARAM_PRECISION = 7; /** * JPEG colorspace * *
The JPEG image uses (decompression) or will use (lossy compression) the * specified colorspace. * *
Value *
Value *
0 [default] Use smooth upsampling when
* decompressing a JPEG image that was compressed using chrominance
* subsampling. This creates a smooth transition between neighboring
* chrominance components in order to reduce upsampling artifacts in the
* decompressed image.
* 1 Use the fastest chrominance upsampling algorithm
* available, which may combine upsampling with color conversion.
* Value *
0 [default] Use the most accurate DCT/IDCT
* algorithm available.
* 1 Use the fastest DCT/IDCT algorithm available.
* This parameter is provided mainly for backward compatibility with * libjpeg, which historically implemented several different DCT/IDCT * algorithms because of performance limitations with 1990s CPUs. In the * libjpeg-turbo implementation of the TurboJPEG API: * *
Value *
0 [default] The JPEG image will use the default
* Huffman tables.
* 1 Optimal Huffman tables will be computed for the JPEG
* image. For lossless transformation, this can also be specified using
* {@link TJTransform#OPT_OPTIMIZE}.
* Optimized baseline entropy coding will improve compression slightly * (generally 5% or less), but it will reduce compression performance * considerably. */ public static final int PARAM_OPTIMIZE = 11; /** * Progressive entropy coding * *
Value *
0 [default for compression, lossless
* transformation] The lossy JPEG image uses (decompression) or will use
* (compression, lossless transformation) baseline entropy coding.
* 1 The lossy JPEG image uses (decompression) or will use
* (compression, lossless transformation) progressive entropy coding. For
* lossless transformation, this can also be specified using
* {@link TJTransform#OPT_PROGRESSIVE}.
* Progressive entropy coding will generally improve compression relative * to baseline entropy coding, but it will reduce compression and * decompression performance considerably. Can be combined with * {@link #PARAM_ARITHMETIC}. Implies {@link #PARAM_OPTIMIZE} unless * {@link #PARAM_ARITHMETIC} is also set. */ public static final int PARAM_PROGRESSIVE = 12; /** * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless * transformation] * *
Setting this parameter will cause the decompression and transform * functions to return an error if the number of scans in a progressive JPEG * image exceeds the specified limit. The primary purpose of this is to * allow security-critical applications to guard against an exploit of the * progressive JPEG format described in * this report. * *
Value *
0 (no
* limit)]
* Value *
0 [default for compression, lossless
* transformation] The lossy JPEG image uses (decompression) or will use
* (compression, lossless transformation) Huffman entropy coding.
* 1 The lossy JPEG image uses (decompression) or will use
* (compression, lossless transformation) arithmetic entropy coding. For
* lossless transformation, this can also be specified using
* {@link TJTransform#OPT_ARITHMETIC}.
* Arithmetic entropy coding will generally improve compression relative * to Huffman entropy coding, but it will reduce compression and * decompression performance considerably. Can be combined with * {@link #PARAM_PROGRESSIVE}. */ public static final int PARAM_ARITHMETIC = 14; /** * Lossless JPEG * *
Value *
0 [default for compression] The JPEG image is
* (decompression) or will be (compression) lossy/DCT-based.
* 1 The JPEG image is (decompression) or will be
* (compression) lossless/predictive.
* In most cases, compressing and decompressing lossless JPEG images is * considerably slower than compressing and decompressing lossy JPEG images. * Also note that the following features are not available with lossless JPEG * images: *
Value *
1-7 [default for compression:
* 1]
* Value *
0 through precision - 1, where
* precision is the JPEG data precision in bits [default for
* compression: 0]
* A point transform value of 0 is necessary in order to
* generate 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.)
*
* @see #PARAM_LOSSLESS
* @see #PARAM_PRECISION
*/
public static final int PARAM_LOSSLESSPT = 17;
/**
* JPEG restart marker interval in MCU blocks (lossy) or samples (lossless)
* [compression only]
*
*
The nature of entropy coding is such that a corrupt JPEG image cannot * be decompressed beyond the point of corruption unless it contains restart * markers. A restart marker stops and restarts the entropy coding algorithm * so that, if a JPEG image is corrupted, decompression can resume at the * next marker. Thus, adding more restart markers improves the fault * tolerance of the JPEG image, but adding too many restart markers can * adversely affect the compression ratio and performance. * *
Value *
0 (no restart markers)]
* Setting this parameter to a non-zero value sets * {@link #PARAM_RESTARTROWS} to 0. */ public static final int PARAM_RESTARTBLOCKS = 18; /** * JPEG restart marker interval in MCU rows (lossy) or sample rows (lossless) * [compression only] * *
See {@link #PARAM_RESTARTBLOCKS} for a description of restart markers. * *
Value *
0 (no restart markers)]
* Setting this parameter to a non-zero value sets * {@link #PARAM_RESTARTBLOCKS} to 0. */ public static final int PARAM_RESTARTROWS = 19; /** * JPEG horizontal pixel density * *
Value *
1].
* This value is stored in or read from the JPEG header. It does not * affect the contents of the JPEG image. * * @see #PARAM_DENSITYUNITS */ public static final int PARAM_XDENSITY = 20; /** * JPEG vertical pixel density * *
Value *
1].
* This value is stored in or read from the JPEG header. It does not * affect the contents of the JPEG image. * * @see #PARAM_DENSITYUNITS */ public static final int PARAM_YDENSITY = 21; /** * JPEG pixel density units * *
Value *
0 [default for compression] The pixel density of
* the JPEG image is expressed (decompression) or will be expressed
* (compression) in unknown units.
* 1 The pixel density of the JPEG image is expressed
* (decompression) or will be expressed (compression) in units of
* pixels/inch.
* 2 The pixel density of the JPEG image is expressed
* (decompression) or will be expressed (compression) in units of pixels/cm.
* This value is stored in or read from the JPEG header. It does not * affect the contents of the JPEG image. * * @see #PARAM_XDENSITY * @see #PARAM_YDENSITY */ public static final int PARAM_DENSITYUNITS = 22; /** * @deprecated Use {@link #PARAM_BOTTOMUP} instead. */ @Deprecated public static final int FLAG_BOTTOMUP = 2; /** * @deprecated Use {@link #PARAM_FASTUPSAMPLE} instead. */ @Deprecated public static final int FLAG_FASTUPSAMPLE = 256; /** * @deprecated Use {@link #PARAM_FASTDCT} instead. */ @Deprecated public static final int FLAG_FASTDCT = 2048; /** * @deprecated Use {@link #PARAM_FASTDCT} instead. */ @Deprecated public static final int FLAG_ACCURATEDCT = 4096; /** * @deprecated Use {@link #PARAM_STOPONWARNING} instead. */ @Deprecated public static final int FLAG_STOPONWARNING = 8192; /** * @deprecated Use {@link #PARAM_PROGRESSIVE} instead. */ @Deprecated public static final int FLAG_PROGRESSIVE = 16384; /** * @deprecated Use {@link #PARAM_SCANLIMIT} instead. */ @Deprecated public static final int FLAG_LIMITSCANS = 32768; /** * The number of error codes */ public static final int NUMERR = 2; /** * The error was non-fatal and recoverable, but the destination image may * still be corrupt. *
* NOTE: due to the design of the TurboJPEG Java API, only certain methods
* (specifically, {@link TJDecompressor TJDecompressor.decompress*()} methods
* with a void return type) will complete and leave the destination image in
* a fully recoverable state after a non-fatal error occurs.
*/
public static final int ERR_WARNING = 0;
/**
* The error was fatal and non-recoverable.
*/
public static final int ERR_FATAL = 1;
/**
* Returns the maximum size of the buffer (in bytes) required to hold a JPEG
* image with the given width, height, and level of chrominance subsampling.
*
* @param width the width (in pixels) of the JPEG image
*
* @param height the height (in pixels) of the JPEG image
*
* @param jpegSubsamp the level of chrominance subsampling to be used when
* generating the JPEG image (one of {@link #SAMP_444 TJ.SAMP_*}.)
* {@link #SAMP_UNKNOWN} is treated like {@link #SAMP_444}, since a buffer
* large enough to hold a JPEG image with no subsampling should also be large
* enough to hold a JPEG image with an arbitrary level of subsampling. Note
* that lossless JPEG images always use {@link #SAMP_444}.
*
* @return the maximum size of the buffer (in bytes) required to hold a JPEG
* image with the given width, height, and level of chrominance subsampling.
*/
public static native int bufSize(int width, int height, int jpegSubsamp);
/**
* Returns the size of the buffer (in bytes) required to hold a unified
* planar YUV image with the given width, height, and level of chrominance
* subsampling.
*
* @param width the width (in pixels) of the YUV image
*
* @param align row alignment (in bytes) of the YUV image (must be a power of
* 2.) Setting this parameter to n specifies that each row in each plane of
* the YUV image will be padded to the nearest multiple of n bytes
* (1 = unpadded.)
*
* @param height the height (in pixels) of the YUV image
*
* @param subsamp the level of chrominance subsampling used in the YUV
* image (one of {@link #SAMP_444 TJ.SAMP_*})
*
* @return the size of the buffer (in bytes) required to hold a unified
* planar YUV image with the given width, height, and level of chrominance
* subsampling.
*/
public static native int bufSizeYUV(int width, int align, int height,
int subsamp);
/**
* Returns the size of the buffer (in bytes) required to hold a YUV image
* plane with the given parameters.
*
* @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
* 2 = V/Cr)
*
* @param width width (in pixels) of the YUV image. NOTE: this is the width
* of the whole image, not the plane width.
*
* @param stride bytes per row in the image plane.
*
* @param height height (in pixels) of the YUV image. NOTE: this is the
* height of the whole image, not the plane height.
*
* @param subsamp the level of chrominance subsampling used in the YUV
* image (one of {@link #SAMP_444 TJ.SAMP_*})
*
* @return the size of the buffer (in bytes) required to hold a YUV image
* plane with the given parameters.
*/
public static native int planeSizeYUV(int componentID, int width, int stride,
int height, int subsamp);
/**
* Returns the plane width of a YUV image plane with the given parameters.
* Refer to {@link YUVImage} for a description of plane width.
*
* @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
* 2 = V/Cr)
*
* @param width width (in pixels) of the YUV image
*
* @param subsamp the level of chrominance subsampling used in the YUV image
* (one of {@link #SAMP_444 TJ.SAMP_*})
*
* @return the plane width of a YUV image plane with the given parameters.
*/
public static native int planeWidth(int componentID, int width, int subsamp);
/**
* Returns the plane height of a YUV image plane with the given parameters.
* Refer to {@link YUVImage} for a description of plane height.
*
* @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
* 2 = V/Cr)
*
* @param height height (in pixels) of the YUV image
*
* @param subsamp the level of chrominance subsampling used in the YUV image
* (one of {@link #SAMP_444 TJ.SAMP_*})
*
* @return the plane height of a YUV image plane with the given parameters.
*/
public static native int planeHeight(int componentID, int height,
int subsamp);
/**
* Returns a list of fractional scaling factors that the JPEG decompressor
* supports.
*
* @return a list of fractional scaling factors that the JPEG decompressor
* supports.
*/
public static native TJScalingFactor[] getScalingFactors();
/**
* A {@link TJScalingFactor} instance that specifies a scaling factor of 1/1
* (no scaling)
*/
public static final TJScalingFactor UNSCALED = new TJScalingFactor(1, 1);
/**
* A java.awt.Rectangle instance that specifies no cropping
*/
public static final Rectangle UNCROPPED = new Rectangle(0, 0, 0, 0);
static {
TJLoader.load();
}
private static void checkPixelFormat(int pixelFormat) {
if (pixelFormat < 0 || pixelFormat >= NUMPF)
throw new IllegalArgumentException("Invalid pixel format");
}
private static void checkSubsampling(int subsamp) {
if (subsamp < 0 || subsamp >= NUMSAMP)
throw new IllegalArgumentException("Invalid subsampling type");
}
}