2 * Copyright (C)2011-2013, 2017-2018, 2020-2021 D. R. Commander.
4 * Copyright (C)2015 Viktor Szathmáry. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of the libjpeg-turbo Project nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 package org.libjpegturbo.turbojpeg;
34 * TurboJPEG utility class (cannot be instantiated)
36 public final class TJ {
41 * The number of chrominance subsampling options
43 public static final int NUMSAMP = 6;
45 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG
46 * or YUV image will contain one chrominance component for every pixel in the
49 public static final int SAMP_444 = 0;
51 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
52 * chrominance component for every 2x1 block of pixels in the source image.
54 public static final int SAMP_422 = 1;
56 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
57 * chrominance component for every 2x2 block of pixels in the source image.
59 public static final int SAMP_420 = 2;
61 * Grayscale. The JPEG or YUV image will contain no chrominance components.
63 public static final int SAMP_GRAY = 3;
65 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
66 * chrominance component for every 1x2 block of pixels in the source image.
67 * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
69 public static final int SAMP_440 = 4;
71 * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
72 * chrominance component for every 4x1 block of pixels in the source image.
73 * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
74 * same size as those compressed with 4:2:0 subsampling, and in the
75 * aggregate, both subsampling methods produce approximately the same
76 * perceptual quality. However, 4:1:1 is better able to reproduce sharp
77 * horizontal features. Note that 4:1:1 subsampling is not fully accelerated
80 public static final int SAMP_411 = 5;
84 * Returns the MCU block width for the given level of chrominance
87 * @param subsamp the level of chrominance subsampling (one of
88 * <code>SAMP_*</code>)
90 * @return the MCU block width for the given level of chrominance
93 public static int getMCUWidth(int subsamp) {
94 checkSubsampling(subsamp);
95 return MCU_WIDTH[subsamp];
98 private static final int[] MCU_WIDTH = {
104 * Returns the MCU block height for the given level of chrominance
107 * @param subsamp the level of chrominance subsampling (one of
108 * <code>SAMP_*</code>)
110 * @return the MCU block height for the given level of chrominance
113 public static int getMCUHeight(int subsamp) {
114 checkSubsampling(subsamp);
115 return MCU_HEIGHT[subsamp];
118 private static final int[] MCU_HEIGHT = {
124 * The number of pixel formats
126 public static final int NUMPF = 12;
128 * RGB pixel format. The red, green, and blue components in the image are
129 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
130 * address within each pixel.
132 public static final int PF_RGB = 0;
134 * BGR pixel format. The red, green, and blue components in the image are
135 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
136 * address within each pixel.
138 public static final int PF_BGR = 1;
140 * RGBX pixel format. The red, green, and blue components in the image are
141 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
142 * address within each pixel. The X component is ignored when compressing
143 * and undefined when decompressing.
145 public static final int PF_RGBX = 2;
147 * BGRX pixel format. The red, green, and blue components in the image are
148 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
149 * address within each pixel. The X component is ignored when compressing
150 * and undefined when decompressing.
152 public static final int PF_BGRX = 3;
154 * XBGR pixel format. The red, green, and blue components in the image are
155 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
156 * address within each pixel. The X component is ignored when compressing
157 * and undefined when decompressing.
159 public static final int PF_XBGR = 4;
161 * XRGB pixel format. The red, green, and blue components in the image are
162 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
163 * address within each pixel. The X component is ignored when compressing
164 * and undefined when decompressing.
166 public static final int PF_XRGB = 5;
168 * Grayscale pixel format. Each 1-byte pixel represents a luminance
169 * (brightness) level from 0 to 255.
171 public static final int PF_GRAY = 6;
173 * RGBA pixel format. This is the same as {@link #PF_RGBX}, except that when
174 * decompressing, the X byte is guaranteed to be 0xFF, which can be
175 * interpreted as an opaque alpha channel.
177 public static final int PF_RGBA = 7;
179 * BGRA pixel format. This is the same as {@link #PF_BGRX}, except that when
180 * decompressing, the X byte is guaranteed to be 0xFF, which can be
181 * interpreted as an opaque alpha channel.
183 public static final int PF_BGRA = 8;
185 * ABGR pixel format. This is the same as {@link #PF_XBGR}, except that when
186 * decompressing, the X byte is guaranteed to be 0xFF, which can be
187 * interpreted as an opaque alpha channel.
189 public static final int PF_ABGR = 9;
191 * ARGB pixel format. This is the same as {@link #PF_XRGB}, except that when
192 * decompressing, the X byte is guaranteed to be 0xFF, which can be
193 * interpreted as an opaque alpha channel.
195 public static final int PF_ARGB = 10;
197 * CMYK pixel format. Unlike RGB, which is an additive color model used
198 * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
199 * color model used primarily for printing. In the CMYK color model, the
200 * value of each color component typically corresponds to an amount of cyan,
201 * magenta, yellow, or black ink that is applied to a white background. In
202 * order to convert between CMYK and RGB, it is necessary to use a color
203 * management system (CMS.) A CMS will attempt to map colors within the
204 * printer's gamut to perceptually similar colors in the display's gamut and
205 * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
206 * be defined with a simple formula. Thus, such a conversion is out of scope
207 * for a codec library. However, the TurboJPEG API allows for compressing
208 * CMYK pixels into a YCCK JPEG image (see {@link #CS_YCCK}) and
209 * decompressing YCCK JPEG images into CMYK pixels.
211 public static final int PF_CMYK = 11;
215 * Returns the pixel size (in bytes) for the given pixel format.
217 * @param pixelFormat the pixel format (one of <code>PF_*</code>)
219 * @return the pixel size (in bytes) for the given pixel format.
221 public static int getPixelSize(int pixelFormat) {
222 checkPixelFormat(pixelFormat);
223 return PIXEL_SIZE[pixelFormat];
226 private static final int[] PIXEL_SIZE = {
227 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
232 * For the given pixel format, returns the number of bytes that the red
233 * component is offset from the start of the pixel. For instance, if a pixel
234 * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
235 * then the red component will be
236 * <code>pixel[TJ.getRedOffset(TJ.PF_BGRX)]</code>.
238 * @param pixelFormat the pixel format (one of <code>PF_*</code>)
240 * @return the red offset for the given pixel format, or -1 if the pixel
241 * format does not have a red component.
243 public static int getRedOffset(int pixelFormat) {
244 checkPixelFormat(pixelFormat);
245 return RED_OFFSET[pixelFormat];
248 private static final int[] RED_OFFSET = {
249 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
254 * For the given pixel format, returns the number of bytes that the green
255 * component is offset from the start of the pixel. For instance, if a pixel
256 * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
257 * then the green component will be
258 * <code>pixel[TJ.getGreenOffset(TJ.PF_BGRX)]</code>.
260 * @param pixelFormat the pixel format (one of <code>PF_*</code>)
262 * @return the green offset for the given pixel format, or -1 if the pixel
263 * format does not have a green component.
265 public static int getGreenOffset(int pixelFormat) {
266 checkPixelFormat(pixelFormat);
267 return GREEN_OFFSET[pixelFormat];
270 private static final int[] GREEN_OFFSET = {
271 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
276 * For the given pixel format, returns the number of bytes that the blue
277 * component is offset from the start of the pixel. For instance, if a pixel
278 * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
279 * then the blue component will be
280 * <code>pixel[TJ.getBlueOffset(TJ.PF_BGRX)]</code>.
282 * @param pixelFormat the pixel format (one of <code>PF_*</code>)
284 * @return the blue offset for the given pixel format, or -1 if the pixel
285 * format does not have a blue component.
287 public static int getBlueOffset(int pixelFormat) {
288 checkPixelFormat(pixelFormat);
289 return BLUE_OFFSET[pixelFormat];
292 private static final int[] BLUE_OFFSET = {
293 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
298 * For the given pixel format, returns the number of bytes that the alpha
299 * component is offset from the start of the pixel. For instance, if a pixel
300 * of format <code>TJ.PF_BGRA</code> is stored in <code>char pixel[]</code>,
301 * then the alpha component will be
302 * <code>pixel[TJ.getAlphaOffset(TJ.PF_BGRA)]</code>.
304 * @param pixelFormat the pixel format (one of <code>PF_*</code>)
306 * @return the alpha offset for the given pixel format, or -1 if the pixel
307 * format does not have a alpha component.
309 public static int getAlphaOffset(int pixelFormat) {
310 checkPixelFormat(pixelFormat);
311 return ALPHA_OFFSET[pixelFormat];
314 private static final int[] ALPHA_OFFSET = {
315 -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
320 * The number of JPEG colorspaces
322 public static final int NUMCS = 5;
324 * RGB colorspace. When compressing the JPEG image, the R, G, and B
325 * components in the source image are reordered into image planes, but no
326 * colorspace conversion or subsampling is performed. RGB JPEG images can be
327 * decompressed to any of the extended RGB pixel formats or grayscale, but
328 * they cannot be decompressed to YUV images.
330 public static final int CS_RGB = 0;
332 * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
333 * mathematical transformation of RGB designed solely for storage and
334 * transmission. YCbCr images must be converted to RGB before they can
335 * actually be displayed. In the YCbCr colorspace, the Y (luminance)
336 * component represents the black & white portion of the original image,
337 * and the Cb and Cr (chrominance) components represent the color portion of
338 * the original image. Originally, the analog equivalent of this
339 * transformation allowed the same signal to drive both black & white and
340 * color televisions, but JPEG images use YCbCr primarily because it allows
341 * the color data to be optionally subsampled for the purposes of reducing
342 * bandwidth or disk space. YCbCr is the most common JPEG colorspace, and
343 * YCbCr JPEG images can be compressed from and decompressed to any of the
344 * extended RGB pixel formats or grayscale, or they can be decompressed to
347 @SuppressWarnings("checkstyle:ConstantName")
348 public static final int CS_YCbCr = 1;
350 * Grayscale colorspace. The JPEG image retains only the luminance data (Y
351 * component), and any color data from the source image is discarded.
352 * Grayscale JPEG images can be compressed from and decompressed to any of
353 * the extended RGB pixel formats or grayscale, or they can be decompressed
354 * to YUV planar images.
356 public static final int CS_GRAY = 2;
358 * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
359 * components in the source image are reordered into image planes, but no
360 * colorspace conversion or subsampling is performed. CMYK JPEG images can
361 * only be decompressed to CMYK pixels.
363 public static final int CS_CMYK = 3;
365 * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
366 * rather a mathematical transformation of CMYK designed solely for storage
367 * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
368 * reversibly transformed into YCCK, and as with YCbCr, the chrominance
369 * components in the YCCK pixels can be subsampled without incurring major
370 * perceptual loss. YCCK JPEG images can only be compressed from and
371 * decompressed to CMYK pixels.
373 public static final int CS_YCCK = 4;
377 * The uncompressed source/destination image is stored in bottom-up (Windows,
378 * OpenGL) order, not top-down (X11) order.
380 public static final int FLAG_BOTTOMUP = 2;
382 @SuppressWarnings("checkstyle:JavadocVariable")
384 public static final int FLAG_FORCEMMX = 8;
385 @SuppressWarnings("checkstyle:JavadocVariable")
387 public static final int FLAG_FORCESSE = 16;
388 @SuppressWarnings("checkstyle:JavadocVariable")
390 public static final int FLAG_FORCESSE2 = 32;
391 @SuppressWarnings("checkstyle:JavadocVariable")
393 public static final int FLAG_FORCESSE3 = 128;
396 * When decompressing an image that was compressed using chrominance
397 * subsampling, use the fastest chrominance upsampling algorithm available in
398 * the underlying codec. The default is to use smooth upsampling, which
399 * creates a smooth transition between neighboring chrominance components in
400 * order to reduce upsampling artifacts in the decompressed image.
402 public static final int FLAG_FASTUPSAMPLE = 256;
404 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
405 * default if this flag is not specified is implementation-specific. For
406 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
407 * algorithm by default when compressing, because this has been shown to have
408 * only a very slight effect on accuracy, but it uses the accurate algorithm
409 * when decompressing, because this has been shown to have a larger effect.
411 public static final int FLAG_FASTDCT = 2048;
413 * Use the most accurate DCT/IDCT algorithm available in the underlying
414 * codec. The default if this flag is not specified is
415 * implementation-specific. For example, the implementation of TurboJPEG for
416 * libjpeg[-turbo] uses the fast algorithm by default when compressing,
417 * because this has been shown to have only a very slight effect on accuracy,
418 * but it uses the accurate algorithm when decompressing, because this has
419 * been shown to have a larger effect.
421 public static final int FLAG_ACCURATEDCT = 4096;
423 * Immediately discontinue the current compression/decompression/transform
424 * operation if the underlying codec throws a warning (non-fatal error). The
425 * default behavior is to allow the operation to complete unless a fatal
426 * error is encountered.
428 * NOTE: due to the design of the TurboJPEG Java API, only certain methods
429 * (specifically, {@link TJDecompressor TJDecompressor.decompress*()} methods
430 * with a void return type) will complete and leave the output image in a
431 * fully recoverable state after a non-fatal error occurs.
433 public static final int FLAG_STOPONWARNING = 8192;
435 * Use progressive entropy coding in JPEG images generated by compression and
436 * transform operations. Progressive entropy coding will generally improve
437 * compression relative to baseline entropy coding (the default), but it will
438 * reduce compression and decompression performance considerably.
440 public static final int FLAG_PROGRESSIVE = 16384;
442 * Limit the number of progressive JPEG scans that the decompression and
443 * transform operations will process. If a progressive JPEG image contains
444 * an unreasonably large number of scans, then this flag will cause the
445 * decompression and transform operations to throw an error. The primary
446 * purpose of this is to allow security-critical applications to guard
447 * against an exploit of the progressive JPEG format described in
448 * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
450 public static final int FLAG_LIMITSCANS = 32768;
454 * The number of error codes
456 public static final int NUMERR = 2;
458 * The error was non-fatal and recoverable, but the image may still be
461 * NOTE: due to the design of the TurboJPEG Java API, only certain methods
462 * (specifically, {@link TJDecompressor TJDecompressor.decompress*()} methods
463 * with a void return type) will complete and leave the output image in a
464 * fully recoverable state after a non-fatal error occurs.
466 public static final int ERR_WARNING = 0;
468 * The error was fatal and non-recoverable.
470 public static final int ERR_FATAL = 1;
474 * Returns the maximum size of the buffer (in bytes) required to hold a JPEG
475 * image with the given width, height, and level of chrominance subsampling.
477 * @param width the width (in pixels) of the JPEG image
479 * @param height the height (in pixels) of the JPEG image
481 * @param jpegSubsamp the level of chrominance subsampling to be used when
482 * generating the JPEG image (one of {@link TJ TJ.SAMP_*})
484 * @return the maximum size of the buffer (in bytes) required to hold a JPEG
485 * image with the given width, height, and level of chrominance subsampling.
487 public static native int bufSize(int width, int height, int jpegSubsamp);
490 * Returns the size of the buffer (in bytes) required to hold a YUV planar
491 * image with the given width, height, and level of chrominance subsampling.
493 * @param width the width (in pixels) of the YUV image
495 * @param pad the width of each line in each plane of the image is padded to
496 * the nearest multiple of this number of bytes (must be a power of 2.)
498 * @param height the height (in pixels) of the YUV image
500 * @param subsamp the level of chrominance subsampling used in the YUV
501 * image (one of {@link TJ TJ.SAMP_*})
503 * @return the size of the buffer (in bytes) required to hold a YUV planar
504 * image with the given width, height, and level of chrominance subsampling.
506 public static native int bufSizeYUV(int width, int pad, int height,
510 * @deprecated Use {@link #bufSizeYUV(int, int, int, int)} instead.
512 @SuppressWarnings("checkstyle:JavadocMethod")
514 public static native int bufSizeYUV(int width, int height, int subsamp);
517 * Returns the size of the buffer (in bytes) required to hold a YUV image
518 * plane with the given parameters.
520 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
523 * @param width width (in pixels) of the YUV image. NOTE: this is the width
524 * of the whole image, not the plane width.
526 * @param stride bytes per line in the image plane.
528 * @param height height (in pixels) of the YUV image. NOTE: this is the
529 * height of the whole image, not the plane height.
531 * @param subsamp the level of chrominance subsampling used in the YUV
532 * image (one of {@link TJ TJ.SAMP_*})
534 * @return the size of the buffer (in bytes) required to hold a YUV planar
535 * image with the given parameters.
537 public static native int planeSizeYUV(int componentID, int width, int stride,
538 int height, int subsamp);
541 * Returns the plane width of a YUV image plane with the given parameters.
542 * Refer to {@link YUVImage YUVImage} for a description of plane width.
544 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
547 * @param width width (in pixels) of the YUV image
549 * @param subsamp the level of chrominance subsampling used in the YUV image
550 * (one of {@link TJ TJ.SAMP_*})
552 * @return the plane width of a YUV image plane with the given parameters.
554 public static native int planeWidth(int componentID, int width, int subsamp);
557 * Returns the plane height of a YUV image plane with the given parameters.
558 * Refer to {@link YUVImage YUVImage} for a description of plane height.
560 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
563 * @param height height (in pixels) of the YUV image
565 * @param subsamp the level of chrominance subsampling used in the YUV image
566 * (one of {@link TJ TJ.SAMP_*})
568 * @return the plane height of a YUV image plane with the given parameters.
570 public static native int planeHeight(int componentID, int height,
574 * Returns a list of fractional scaling factors that the JPEG decompressor in
575 * this implementation of TurboJPEG supports.
577 * @return a list of fractional scaling factors that the JPEG decompressor in
578 * this implementation of TurboJPEG supports.
580 public static native TJScalingFactor[] getScalingFactors();
586 private static void checkPixelFormat(int pixelFormat) {
587 if (pixelFormat < 0 || pixelFormat >= NUMPF)
588 throw new IllegalArgumentException("Invalid pixel format");
591 private static void checkSubsampling(int subsamp) {
592 if (subsamp < 0 || subsamp >= NUMSAMP)
593 throw new IllegalArgumentException("Invalid subsampling type");