org.libjpegturbo.turbojpeg

## Class TJCompressor

- java.lang.Object
- - org.libjpegturbo.turbojpeg.TJCompressor

- All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

      public class TJCompressor
      extends java.lang.Object
      implements java.io.Closeable

  TurboJPEG compressor

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TJCompressor()` Create a TurboJPEG compressor instance.
    `TJCompressor(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height)` Create a TurboJPEG compressor instance and associate the uncompressed source image stored in `srcImage` with the newly created instance.
    `TJCompressor(byte[] srcImage, int width, int pitch, int height, int pixelFormat)` Deprecated. Use `TJCompressor(byte[], int, int, int, int, int, int)` instead.
    `TJCompressor(byte[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)` Create a TurboJPEG compressor instance and associate the uncompressed source image stored in `srcImage` with the newly created instance.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Free the native structures associated with this compressor instance.
    `void` | `compress(java.awt.image.BufferedImage srcImage, byte[] dstBuf, int flags)` Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `compress(byte[], int)` instead.
    `byte[]` | `compress(java.awt.image.BufferedImage srcImage, int flags)` Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `compress(int)` instead.
    `void` | `compress(byte[] dstBuf, int flags)` Compress the uncompressed source image associated with this compressor instance and output a JPEG image to the given destination buffer.
    `byte[]` | `compress(int flags)` Compress the uncompressed source image associated with this compressor instance and return a buffer containing a JPEG image.
    `void` | `encodeYUV(java.awt.image.BufferedImage srcImage, byte[] dstBuf, int flags)` Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `encodeYUV(byte[], int)` instead.
    `byte[]` | `encodeYUV(java.awt.image.BufferedImage srcImage, int flags)` Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `encodeYUV(int, int)` instead.
    `void` | `encodeYUV(byte[] dstBuf, int flags)` Deprecated. Use `encodeYUV(YUVImage, int)` instead.
    `byte[]` | `encodeYUV(int flags)` Deprecated. Use `encodeYUV(int, int)` instead.
    `YUVImage` | `encodeYUV(int[] strides, int flags)` Encode the uncompressed source image associated with this compressor instance into separate Y, U (Cb), and V (Cr) image planes and return a `YUVImage` instance containing the encoded image planes.
    `YUVImage` | `encodeYUV(int pad, int flags)` Encode the uncompressed source image associated with this compressor instance into a unified YUV planar image buffer and return a `YUVImage` instance containing the encoded image.
    `void` | `encodeYUV(YUVImage dstImage, int flags)` Encode the uncompressed source image associated with this compressor instance into a YUV planar image and store it in the given `YUVImage` instance.
    `protected void` | `finalize()`
    `int` | `getCompressedSize()` Returns the size of the image (in bytes) generated by the most recent compress operation.
    `void` | `setJPEGQuality(int quality)` Set the JPEG image quality level for subsequent compress operations.
    `void` | `setSourceImage(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height)` Associate an uncompressed RGB or grayscale source image with this compressor instance.
    `void` | `setSourceImage(byte[] srcImage, int width, int pitch, int height, int pixelFormat)` Deprecated. Use `setSourceImage(byte[], int, int, int, int, int, int)` instead.
    `void` | `setSourceImage(byte[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)` Associate an uncompressed RGB, grayscale, or CMYK source image with this compressor instance.
    `void` | `setSourceImage(YUVImage srcImage)` Associate an uncompressed YUV planar source image with this compressor instance.
    `void` | `setSubsamp(int newSubsamp)` Set the level of chrominance subsampling for subsequent compress/encode operations.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TJCompressor

          public TJCompressor()
                       throws TJException

      Create a TurboJPEG compressor instance.
      Throws:
        `TJException`

    - #### TJCompressor

          public TJCompressor(byte[] srcImage,
                      int x,
                      int y,
                      int width,
                      int pitch,
                      int height,
                      int pixelFormat)
                       throws TJException

      Create a TurboJPEG compressor instance and associate the uncompressed source image stored in `srcImage` with the newly created instance.
      Parameters:
        `srcImage` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `x` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `y` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `width` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `pitch` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `height` - see `setSourceImage(byte[], int, int, int, int, int, int)` for description
        `pixelFormat` - pixel format of the source image (one of `TJ.PF_*`)
      Throws:
        `TJException`

    - #### TJCompressor

          @Deprecated
          public TJCompressor(byte[] srcImage,
                                 int width,
                                 int pitch,
                                 int height,
                                 int pixelFormat)
                       throws TJException

      Deprecated. Use `TJCompressor(byte[], int, int, int, int, int, int)` instead.
      Throws:
        `TJException`

    - #### TJCompressor

          public TJCompressor(java.awt.image.BufferedImage srcImage,
                      int x,
                      int y,
                      int width,
                      int height)
                       throws TJException

      Create a TurboJPEG compressor instance and associate the uncompressed source image stored in `srcImage` with the newly created instance.
      Parameters:
        `srcImage` - see `setSourceImage(BufferedImage, int, int, int, int)` for description
        `x` - see `setSourceImage(BufferedImage, int, int, int, int)` for description
        `y` - see `setSourceImage(BufferedImage, int, int, int, int)` for description
        `width` - see `setSourceImage(BufferedImage, int, int, int, int)` for description
        `height` - see `setSourceImage(BufferedImage, int, int, int, int)` for description
      Throws:
        `TJException`

  - ### Method Detail

    - #### setSourceImage

          public void setSourceImage(byte[] srcImage,
                            int x,
                            int y,
                            int width,
                            int pitch,
                            int height,
                            int pixelFormat)
                              throws TJException

      Associate an uncompressed RGB, grayscale, or CMYK source image with this compressor instance.
      Parameters:
        `srcImage` - image buffer containing RGB, grayscale, or CMYK pixels to be compressed or encoded. This buffer is not modified.
        `x` - x offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `y` - y offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `width` - width (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `pitch` - bytes per line of the source image. Normally, this should be `width * TJ.pixelSize(pixelFormat)` if the source image is unpadded, but you can use this parameter to, for instance, specify that the scanlines in the source image are padded to a 4-byte boundary 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 lines, etc. Setting this parameter to 0 is the equivalent of setting it to `width * TJ.pixelSize(pixelFormat)`.
        `height` - height (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `pixelFormat` - pixel format of the source image (one of `TJ.PF_*`)
      Throws:
        `TJException`

    - #### setSourceImage

          @Deprecated
          public void setSourceImage(byte[] srcImage,
                                       int width,
                                       int pitch,
                                       int height,
                                       int pixelFormat)
                              throws TJException

      Deprecated. Use `setSourceImage(byte[], int, int, int, int, int, int)` instead.
      Throws:
        `TJException`

    - #### setSourceImage

          public void setSourceImage(java.awt.image.BufferedImage srcImage,
                            int x,
                            int y,
                            int width,
                            int height)
                              throws TJException

      Associate an uncompressed RGB or grayscale source image with this compressor instance.
      Parameters:
        `srcImage` - a `BufferedImage` instance containing RGB or grayscale pixels to be compressed or encoded. This image is not modified.
        `x` - x offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `y` - y offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded
        `width` - width (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded (0 = use the width of the source image)
        `height` - height (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encoded (0 = use the height of the source image)
      Throws:
        `TJException`

    - #### setSourceImage

          public void setSourceImage(YUVImage srcImage)
                              throws TJException

      Associate an uncompressed YUV planar source image with this compressor instance.
      Parameters:
        `srcImage` - YUV planar image to be compressed. This image is not modified.
      Throws:
        `TJException`

    - #### setSubsamp

          public void setSubsamp(int newSubsamp)

      Set the level of chrominance subsampling for subsequent compress/encode operations. When pixels are converted from RGB to YCbCr (see `TJ.CS_YCbCr`) or from CMYK to YCCK (see `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".

      NOTE: This method has no effect when compressing a JPEG image from a YUV planar source. In that case, the level of chrominance subsampling in the JPEG image is determined by the source. Further, this method has no effect when encoding to a pre-allocated `YUVImage` instance. In that case, the level of chrominance subsampling is determined by the destination.

      Parameters:
        `newSubsamp` - the level of chrominance subsampling to use in subsequent compress/encode oeprations (one of `TJ.SAMP_*`)

    - #### setJPEGQuality

          public void setJPEGQuality(int quality)

      Set the JPEG image quality level for subsequent compress operations.
      Parameters:
        `quality` - the new JPEG image quality level (1 to 100, 1 = worst, 100 = best)

    - #### compress

          public void compress(byte[] dstBuf,
                      int flags)
                        throws TJException

      Compress the uncompressed source image associated with this compressor instance and output a JPEG image to the given destination buffer.
      Parameters:
        `dstBuf` - buffer that will receive the JPEG image. Use `TJ.bufSize(int, int, int)` to determine the maximum size for this buffer based on the source image's width and height and the desired level of chrominance subsampling.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `TJException`

    - #### compress

          public byte[] compress(int flags)
                          throws TJException

      Compress the uncompressed source image associated with this compressor instance and return a buffer containing a JPEG image.
      Parameters:
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Returns:
        a buffer containing a JPEG image. The length of this buffer will not be equal to the size of the JPEG image. Use `getCompressedSize()` to obtain the size of the JPEG image.
      Throws:
        `TJException`

    - #### compress

          @Deprecated
          public void compress(java.awt.image.BufferedImage srcImage,
                                 byte[] dstBuf,
                                 int flags)
                        throws TJException

      Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `compress(byte[], int)` instead.
      Throws:
        `TJException`

    - #### compress

          @Deprecated
          public byte[] compress(java.awt.image.BufferedImage srcImage,
                                   int flags)
                          throws TJException

      Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `compress(int)` instead.
      Throws:
        `TJException`

    - #### encodeYUV

          public void encodeYUV(YUVImage dstImage,
                       int flags)
                         throws TJException

      Encode the uncompressed source image associated with this compressor instance into a YUV planar image and store it in the given `YUVImage` instance. This method uses the accelerated color conversion routines in TurboJPEG's underlying codec but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images to YUV is not supported.
      Parameters:
        `dstImage` - `YUVImage` instance that will receive the YUV planar image
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `TJException`

    - #### encodeYUV

          @Deprecated
          public void encodeYUV(byte[] dstBuf,
                                  int flags)
                         throws TJException

      Deprecated. Use `encodeYUV(YUVImage, int)` instead.
      Throws:
        `TJException`

    - #### encodeYUV

          public YUVImage encodeYUV(int pad,
                           int flags)
                             throws TJException

      Encode the uncompressed source image associated with this compressor instance into a unified YUV planar image buffer and return a `YUVImage` instance containing the encoded image. This method uses the accelerated color conversion routines in TurboJPEG's underlying codec but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images to YUV is not supported.
      Parameters:
        `pad` - the width of each line in each plane of the YUV image will be padded to the nearest multiple of this number of bytes (must be a power of 2.)
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Returns:
        a YUV planar image.
      Throws:
        `TJException`

    - #### encodeYUV

          public YUVImage encodeYUV(int[] strides,
                           int flags)
                             throws TJException

      Encode the uncompressed source image associated with this compressor instance into separate Y, U (Cb), and V (Cr) image planes and return a `YUVImage` instance containing the encoded image planes. This method uses the accelerated color conversion routines in TurboJPEG's underlying codec but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images to YUV is not supported.
      Parameters:
        `strides` - an array of integers, each specifying the number of bytes per line in the corresponding plane of the output image. Setting the stride for any plane to 0 is the same as setting it to the component width of the plane. If `strides` is null, then the strides for all planes will be set to their respective component widths. You can adjust the strides in order to add an arbitrary amount of line padding to each plane.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Returns:
        a YUV planar image.
      Throws:
        `TJException`

    - #### encodeYUV

          @Deprecated
          public byte[] encodeYUV(int flags)
                           throws TJException

      Deprecated. Use `encodeYUV(int, int)` instead.
      Throws:
        `TJException`

    - #### encodeYUV

          @Deprecated
          public void encodeYUV(java.awt.image.BufferedImage srcImage,
                                  byte[] dstBuf,
                                  int flags)
                         throws TJException

      Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `encodeYUV(byte[], int)` instead.
      Throws:
        `TJException`

    - #### encodeYUV

          @Deprecated
          public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
                                    int flags)
                           throws TJException

      Deprecated. Use `setSourceImage(BufferedImage, int, int, int, int)` and `encodeYUV(int, int)` instead.
      Throws:
        `TJException`

    - #### getCompressedSize

          public int getCompressedSize()

      Returns the size of the image (in bytes) generated by the most recent compress operation.
      Returns:
        the size of the image (in bytes) generated by the most recent compress operation.

    - #### close

          public void close()
                     throws TJException

      Free the native structures associated with this compressor instance.
      Specified by:
        `close` in interface `java.io.Closeable`
      Specified by:
        `close` in interface `java.lang.AutoCloseable`
      Throws:
        `TJException`

    - #### finalize

          protected void finalize()
                           throws java.lang.Throwable

      Overrides:
        `finalize` in class `java.lang.Object`
      Throws:
        `java.lang.Throwable`