Home | History | Annotate | Download | only in turbojpeg
      1 /*
      2  * Copyright (C)2011-2013 D. R. Commander.  All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are met:
      6  *
      7  * - Redistributions of source code must retain the above copyright notice,
      8  *   this list of conditions and the following disclaimer.
      9  * - Redistributions in binary form must reproduce the above copyright notice,
     10  *   this list of conditions and the following disclaimer in the documentation
     11  *   and/or other materials provided with the distribution.
     12  * - Neither the name of the libjpeg-turbo Project nor the names of its
     13  *   contributors may be used to endorse or promote products derived from this
     14  *   software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
     17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
     20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 package org.libjpegturbo.turbojpeg;
     30 
     31 /**
     32  * TurboJPEG utility class (cannot be instantiated)
     33  */
     34 public final class TJ {
     35 
     36 
     37   /**
     38    * The number of chrominance subsampling options
     39    */
     40   public static final int NUMSAMP   = 6;
     41   /**
     42    * 4:4:4 chrominance subsampling (no chrominance subsampling).  The JPEG
     43    * or YUV image will contain one chrominance component for every pixel in the
     44    * source image.
     45    */
     46   public static final int SAMP_444  = 0;
     47   /**
     48    * 4:2:2 chrominance subsampling.  The JPEG or YUV image will contain one
     49    * chrominance component for every 2x1 block of pixels in the source image.
     50    */
     51   public static final int SAMP_422  = 1;
     52   /**
     53    * 4:2:0 chrominance subsampling.  The JPEG or YUV image will contain one
     54    * chrominance component for every 2x2 block of pixels in the source image.
     55    */
     56   public static final int SAMP_420  = 2;
     57   /**
     58    * Grayscale.  The JPEG or YUV image will contain no chrominance components.
     59    */
     60   public static final int SAMP_GRAY = 3;
     61   /**
     62    * 4:4:0 chrominance subsampling.  The JPEG or YUV image will contain one
     63    * chrominance component for every 1x2 block of pixels in the source image.
     64    * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
     65    */
     66   public static final int SAMP_440  = 4;
     67   /**
     68    * 4:1:1 chrominance subsampling.  The JPEG or YUV image will contain one
     69    * chrominance component for every 4x1 block of pixels in the source image.
     70    * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
     71    * same size as those compressed with 4:2:0 subsampling, and in the
     72    * aggregate, both subsampling methods produce approximately the same
     73    * perceptual quality.  However, 4:1:1 is better able to reproduce sharp
     74    * horizontal features.  Note that 4:1:1 subsampling is not fully accelerated
     75    * in libjpeg-turbo.
     76    */
     77   public static final int SAMP_411  = 5;
     78 
     79 
     80   /**
     81    * Returns the MCU block width for the given level of chrominance
     82    * subsampling.
     83    *
     84    * @param subsamp the level of chrominance subsampling (one of
     85    * <code>SAMP_*</code>)
     86    *
     87    * @return the MCU block width for the given level of chrominance
     88    * subsampling.
     89    */
     90   public static int getMCUWidth(int subsamp) throws Exception {
     91     if (subsamp < 0 || subsamp >= NUMSAMP)
     92       throw new Exception("Invalid subsampling type");
     93     return mcuWidth[subsamp];
     94   }
     95 
     96   private static final int[] mcuWidth = {
     97     8, 16, 16, 8, 8, 32
     98   };
     99 
    100 
    101   /**
    102    * Returns the MCU block height for the given level of chrominance
    103    * subsampling.
    104    *
    105    * @param subsamp the level of chrominance subsampling (one of
    106    * <code>SAMP_*</code>)
    107    *
    108    * @return the MCU block height for the given level of chrominance
    109    * subsampling.
    110    */
    111   public static int getMCUHeight(int subsamp) throws Exception {
    112     if (subsamp < 0 || subsamp >= NUMSAMP)
    113       throw new Exception("Invalid subsampling type");
    114     return mcuHeight[subsamp];
    115   }
    116 
    117   private static final int[] mcuHeight = {
    118     8, 8, 16, 8, 16, 8
    119   };
    120 
    121 
    122   /**
    123    * The number of pixel formats
    124    */
    125   public static final int NUMPF   = 12;
    126   /**
    127    * RGB pixel format.  The red, green, and blue components in the image are
    128    * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
    129    * address within each pixel.
    130    */
    131   public static final int PF_RGB  = 0;
    132   /**
    133    * BGR pixel format.  The red, green, and blue components in the image are
    134    * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
    135    * address within each pixel.
    136    */
    137   public static final int PF_BGR  = 1;
    138   /**
    139    * RGBX pixel format.  The red, green, and blue components in the image are
    140    * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
    141    * address within each pixel.  The X component is ignored when compressing
    142    * and undefined when decompressing.
    143    */
    144   public static final int PF_RGBX = 2;
    145   /**
    146    * BGRX pixel format.  The red, green, and blue components in the image are
    147    * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
    148    * address within each pixel.  The X component is ignored when compressing
    149    * and undefined when decompressing.
    150    */
    151   public static final int PF_BGRX = 3;
    152   /**
    153    * XBGR pixel format.  The red, green, and blue components in the image are
    154    * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
    155    * address within each pixel.  The X component is ignored when compressing
    156    * and undefined when decompressing.
    157    */
    158   public static final int PF_XBGR = 4;
    159   /**
    160    * XRGB pixel format.  The red, green, and blue components in the image are
    161    * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
    162    * address within each pixel.  The X component is ignored when compressing
    163    * and undefined when decompressing.
    164    */
    165   public static final int PF_XRGB = 5;
    166   /**
    167    * Grayscale pixel format.  Each 1-byte pixel represents a luminance
    168    * (brightness) level from 0 to 255.
    169    */
    170   public static final int PF_GRAY = 6;
    171   /**
    172    * RGBA pixel format.  This is the same as {@link #PF_RGBX}, except that when
    173    * decompressing, the X byte is guaranteed to be 0xFF, which can be
    174    * interpreted as an opaque alpha channel.
    175    */
    176   public static final int PF_RGBA = 7;
    177   /**
    178    * BGRA pixel format.  This is the same as {@link #PF_BGRX}, except that when
    179    * decompressing, the X byte is guaranteed to be 0xFF, which can be
    180    * interpreted as an opaque alpha channel.
    181    */
    182   public static final int PF_BGRA = 8;
    183   /**
    184    * ABGR pixel format.  This is the same as {@link #PF_XBGR}, except that when
    185    * decompressing, the X byte is guaranteed to be 0xFF, which can be
    186    * interpreted as an opaque alpha channel.
    187    */
    188   public static final int PF_ABGR = 9;
    189   /**
    190    * ARGB pixel format.  This is the same as {@link #PF_XRGB}, except that when
    191    * decompressing, the X byte is guaranteed to be 0xFF, which can be
    192    * interpreted as an opaque alpha channel.
    193    */
    194   public static final int PF_ARGB = 10;
    195   /**
    196    * CMYK pixel format.  Unlike RGB, which is an additive color model used
    197    * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
    198    * color model used primarily for printing.  In the CMYK color model, the
    199    * value of each color component typically corresponds to an amount of cyan,
    200    * magenta, yellow, or black ink that is applied to a white background.  In
    201    * order to convert between CMYK and RGB, it is necessary to use a color
    202    * management system (CMS.)  A CMS will attempt to map colors within the
    203    * printer's gamut to perceptually similar colors in the display's gamut and
    204    * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
    205    * be defined with a simple formula.  Thus, such a conversion is out of scope
    206    * for a codec library.  However, the TurboJPEG API allows for compressing
    207    * CMYK pixels into a YCCK JPEG image (see {@link #CS_YCCK}) and
    208    * decompressing YCCK JPEG images into CMYK pixels.
    209    */
    210   public static final int PF_CMYK = 11;
    211 
    212 
    213   /**
    214    * Returns the pixel size (in bytes) for the given pixel format.
    215    *
    216    * @param pixelFormat the pixel format (one of <code>PF_*</code>)
    217    *
    218    * @return the pixel size (in bytes) for the given pixel format.
    219    */
    220   public static int getPixelSize(int pixelFormat) throws Exception {
    221     if (pixelFormat < 0 || pixelFormat >= NUMPF)
    222       throw new Exception("Invalid pixel format");
    223     return pixelSize[pixelFormat];
    224   }
    225 
    226   private static final int[] pixelSize = {
    227     3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
    228   };
    229 
    230 
    231   /**
    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>.
    237    *
    238    * @param pixelFormat the pixel format (one of <code>PF_*</code>)
    239    *
    240    * @return the red offset for the given pixel format.
    241    */
    242   public static int getRedOffset(int pixelFormat) throws Exception {
    243     if (pixelFormat < 0 || pixelFormat >= NUMPF)
    244       throw new Exception("Invalid pixel format");
    245     return redOffset[pixelFormat];
    246   }
    247 
    248   private static final int[] redOffset = {
    249     0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1, -1
    250   };
    251 
    252 
    253   /**
    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>.
    259    *
    260    * @param pixelFormat the pixel format (one of <code>PF_*</code>)
    261    *
    262    * @return the green offset for the given pixel format.
    263    */
    264   public static int getGreenOffset(int pixelFormat) throws Exception {
    265     if (pixelFormat < 0 || pixelFormat >= NUMPF)
    266       throw new Exception("Invalid pixel format");
    267     return greenOffset[pixelFormat];
    268   }
    269 
    270   private static final int[] greenOffset = {
    271     1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2, -1
    272   };
    273 
    274 
    275   /**
    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>.
    281    *
    282    * @param pixelFormat the pixel format (one of <code>PF_*</code>)
    283    *
    284    * @return the blue offset for the given pixel format.
    285    */
    286   public static int getBlueOffset(int pixelFormat) throws Exception {
    287     if (pixelFormat < 0 || pixelFormat >= NUMPF)
    288       throw new Exception("Invalid pixel format");
    289     return blueOffset[pixelFormat];
    290   }
    291 
    292   private static final int[] blueOffset = {
    293     2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3, -1
    294   };
    295 
    296 
    297   /**
    298    * The number of JPEG colorspaces
    299    */
    300   public static final int NUMCS = 5;
    301   /**
    302    * RGB colorspace.  When compressing the JPEG image, the R, G, and B
    303    * components in the source image are reordered into image planes, but no
    304    * colorspace conversion or subsampling is performed.  RGB JPEG images can be
    305    * decompressed to any of the extended RGB pixel formats or grayscale, but
    306    * they cannot be decompressed to YUV images.
    307    */
    308   public static final int CS_RGB = 0;
    309   /**
    310    * YCbCr colorspace.  YCbCr is not an absolute colorspace but rather a
    311    * mathematical transformation of RGB designed solely for storage and
    312    * transmission.  YCbCr images must be converted to RGB before they can
    313    * actually be displayed.  In the YCbCr colorspace, the Y (luminance)
    314    * component represents the black & white portion of the original image, and
    315    * the Cb and Cr (chrominance) components represent the color portion of the
    316    * original image.  Originally, the analog equivalent of this transformation
    317    * allowed the same signal to drive both black & white and color televisions,
    318    * but JPEG images use YCbCr primarily because it allows the color data to be
    319    * optionally subsampled for the purposes of reducing bandwidth or disk
    320    * space.  YCbCr is the most common JPEG colorspace, and YCbCr JPEG images
    321    * can be compressed from and decompressed to any of the extended RGB pixel
    322    * formats or grayscale, or they can be decompressed to YUV planar images.
    323    */
    324   public static final int CS_YCbCr = 1;
    325   /**
    326    * Grayscale colorspace.  The JPEG image retains only the luminance data (Y
    327    * component), and any color data from the source image is discarded.
    328    * Grayscale JPEG images can be compressed from and decompressed to any of
    329    * the extended RGB pixel formats or grayscale, or they can be decompressed
    330    * to YUV planar images.
    331    */
    332   public static final int CS_GRAY = 2;
    333   /**
    334    * CMYK colorspace.  When compressing the JPEG image, the C, M, Y, and K
    335    * components in the source image are reordered into image planes, but no
    336    * colorspace conversion or subsampling is performed.  CMYK JPEG images can
    337    * only be decompressed to CMYK pixels.
    338    */
    339   public static final int CS_CMYK = 3;
    340   /**
    341    * YCCK colorspace.  YCCK (AKA "YCbCrK") is not an absolute colorspace but
    342    * rather a mathematical transformation of CMYK designed solely for storage
    343    * and transmission.  It is to CMYK as YCbCr is to RGB.  CMYK pixels can be
    344    * reversibly transformed into YCCK, and as with YCbCr, the chrominance
    345    * components in the YCCK pixels can be subsampled without incurring major
    346    * perceptual loss.  YCCK JPEG images can only be compressed from and
    347    * decompressed to CMYK pixels.
    348    */
    349   public static final int CS_YCCK = 4;
    350 
    351 
    352   /**
    353    * The uncompressed source/destination image is stored in bottom-up (Windows,
    354    * OpenGL) order, not top-down (X11) order.
    355    */
    356   public static final int FLAG_BOTTOMUP     = 2;
    357 
    358   @Deprecated
    359   public static final int FLAG_FORCEMMX     = 8;
    360   @Deprecated
    361   public static final int FLAG_FORCESSE     = 16;
    362   @Deprecated
    363   public static final int FLAG_FORCESSE2    = 32;
    364   @Deprecated
    365   public static final int FLAG_FORCESSE3    = 128;
    366 
    367   /**
    368    * When decompressing an image that was compressed using chrominance
    369    * subsampling, use the fastest chrominance upsampling algorithm available in
    370    * the underlying codec.  The default is to use smooth upsampling, which
    371    * creates a smooth transition between neighboring chrominance components in
    372    * order to reduce upsampling artifacts in the decompressed image.
    373    */
    374   public static final int FLAG_FASTUPSAMPLE = 256;
    375   /**
    376    * Use the fastest DCT/IDCT algorithm available in the underlying codec.  The
    377    * default if this flag is not specified is implementation-specific.  For
    378    * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
    379    * algorithm by default when compressing, because this has been shown to have
    380    * only a very slight effect on accuracy, but it uses the accurate algorithm
    381    * when decompressing, because this has been shown to have a larger effect.
    382    */
    383   public static final int FLAG_FASTDCT      =  2048;
    384   /**
    385    * Use the most accurate DCT/IDCT algorithm available in the underlying
    386    * codec.  The default if this flag is not specified is
    387    * implementation-specific.  For example, the implementation of TurboJPEG for
    388    * libjpeg[-turbo] uses the fast algorithm by default when compressing,
    389    * because this has been shown to have only a very slight effect on accuracy,
    390    * but it uses the accurate algorithm when decompressing, because this has
    391    * been shown to have a larger effect.
    392    */
    393   public static final int FLAG_ACCURATEDCT  =  4096;
    394 
    395 
    396   /**
    397    * Returns the maximum size of the buffer (in bytes) required to hold a JPEG
    398    * image with the given width, height, and level of chrominance subsampling.
    399    *
    400    * @param width the width (in pixels) of the JPEG image
    401    *
    402    * @param height the height (in pixels) of the JPEG image
    403    *
    404    * @param jpegSubsamp the level of chrominance subsampling to be used when
    405    * generating the JPEG image (one of {@link TJ TJ.SAMP_*})
    406    *
    407    * @return the maximum size of the buffer (in bytes) required to hold a JPEG
    408    * image with the given width, height, and level of chrominance subsampling.
    409    */
    410   public static native int bufSize(int width, int height, int jpegSubsamp)
    411     throws Exception;
    412 
    413   /**
    414    * Returns the size of the buffer (in bytes) required to hold a YUV planar
    415    * image with the given width, height, and level of chrominance subsampling.
    416    *
    417    * @param width the width (in pixels) of the YUV image
    418    *
    419    * @param pad the width of each line in each plane of the image is padded to
    420    * the nearest multiple of this number of bytes (must be a power of 2.)
    421    *
    422    * @param height the height (in pixels) of the YUV image
    423    *
    424    * @param subsamp the level of chrominance subsampling used in the YUV
    425    * image (one of {@link TJ TJ.SAMP_*})
    426    *
    427    * @return the size of the buffer (in bytes) required to hold a YUV planar
    428    * image with the given width, height, and level of chrominance subsampling.
    429    */
    430   public static native int bufSizeYUV(int width, int pad, int height,
    431                                       int subsamp)
    432     throws Exception;
    433 
    434   /**
    435    * @deprecated Use {@link #bufSizeYUV(int, int, int, int)} instead.
    436    */
    437   @Deprecated
    438   public static native int bufSizeYUV(int width, int height, int subsamp)
    439     throws Exception;
    440 
    441   /**
    442    * Returns the size of the buffer (in bytes) required to hold a YUV image
    443    * plane with the given parameters.
    444    *
    445    * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
    446    * 2 = V/Cr)
    447    *
    448    * @param width width (in pixels) of the YUV image.  NOTE: this is the width
    449    * of the whole image, not the plane width.
    450    *
    451    * @param stride bytes per line in the image plane.
    452    *
    453    * @param height height (in pixels) of the YUV image.  NOTE: this is the
    454    * height of the whole image, not the plane height.
    455    *
    456    * @param subsamp the level of chrominance subsampling used in the YUV
    457    * image (one of {@link TJ TJ.SAMP_*})
    458    *
    459    * @return the size of the buffer (in bytes) required to hold a YUV planar
    460    * image with the given parameters.
    461    */
    462   public static native int planeSizeYUV(int componentID, int width, int stride,
    463                                         int height, int subsamp)
    464     throws Exception;
    465 
    466   /**
    467    * Returns the plane width of a YUV image plane with the given parameters.
    468    * Refer to {@link YUVImage YUVImage} for a description of plane width.
    469    *
    470    * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
    471    * 2 = V/Cr)
    472    *
    473    * @param width width (in pixels) of the YUV image
    474    *
    475    * @param subsamp the level of chrominance subsampling used in the YUV image
    476    * (one of {@link TJ TJ.SAMP_*})
    477    *
    478    * @return the plane width of a YUV image plane with the given parameters.
    479    */
    480   public static native int planeWidth(int componentID, int width, int subsamp)
    481     throws Exception;
    482 
    483   /**
    484    * Returns the plane height of a YUV image plane with the given parameters.
    485    * Refer to {@link YUVImage YUVImage} for a description of plane height.
    486    *
    487    * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
    488    * 2 = V/Cr)
    489    *
    490    * @param height height (in pixels) of the YUV image
    491    *
    492    * @param subsamp the level of chrominance subsampling used in the YUV image
    493    * (one of {@link TJ TJ.SAMP_*})
    494    *
    495    * @return the plane height of a YUV image plane with the given parameters.
    496    */
    497   public static native int planeHeight(int componentID, int height,
    498                                        int subsamp)
    499     throws Exception;
    500 
    501   /**
    502    * Returns a list of fractional scaling factors that the JPEG decompressor in
    503    * this implementation of TurboJPEG supports.
    504    *
    505    * @return a list of fractional scaling factors that the JPEG decompressor in
    506    * this implementation of TurboJPEG supports.
    507    */
    508   public static native TJScalingFactor[] getScalingFactors()
    509     throws Exception;
    510 
    511   static {
    512     TJLoader.load();
    513   }
    514 };
    515