org.libjpegturbo.turbojpeg

## Class TJCompressor

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

-     public class TJCompressor
      extends java.lang.Object

  TurboJPEG compressor

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private java.nio.ByteOrder` | `byteOrder`
    `private int` | `compressedSize`
    `private long` | `handle`
    `private int` | `jpegQuality`
    `private static java.lang.String` | `NO_ASSOC_ERROR`
    `private byte[]` | `srcBuf`
    `private int` | `srcHeight`
    `private int` | `srcPitch`
    `private int` | `srcPixelFormat`
    `private int` | `srcWidth`
    `private int` | `subsamp`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TJCompressor()` Create a TurboJPEG compressor instance.
    `TJCompressor(byte[] srcImage, 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

    All Methods Instance Methods Concrete 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)` Compress the uncompressed source image stored in `srcImage` and output a JPEG image to the given destination buffer.
    `byte[]` | `compress(java.awt.image.BufferedImage srcImage, int flags)` Compress the uncompressed source image stored in `srcImage` and return a buffer containing a JPEG image.
    `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.
    `private int` | `compress(byte[] srcBuf, int width, int pitch, int height, int pixelFormat, byte[] dstbuf, int jpegSubsamp, int jpegQual, int flags)`
    `byte[]` | `compress(int flags)` Compress the uncompressed source image associated with this compressor instance and return a buffer containing a JPEG image.
    `private int` | `compress(int[] srcBuf, int width, int pitch, int height, int pixelFormat, byte[] dstbuf, int jpegSubsamp, int jpegQual, int flags)`
    `private void` | `destroy()`
    `void` | `encodeYUV(java.awt.image.BufferedImage srcImage, byte[] dstBuf, int flags)` Encode the uncompressed source image stored in `srcImage` and output a YUV planar image to the given destination buffer.
    `byte[]` | `encodeYUV(java.awt.image.BufferedImage srcImage, int flags)` Encode the uncompressed source image stored in `srcImage` and return a buffer containing a YUV planar image.
    `void` | `encodeYUV(byte[] dstBuf, int flags)` Encode the uncompressed source image associated with this compressor instance and output a YUV planar image to the given destination buffer.
    `private void` | `encodeYUV(byte[] srcBuf, int width, int pitch, int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)`
    `byte[]` | `encodeYUV(int flags)` Encode the uncompressed source image associated with this compressor instance and return a buffer containing a YUV planar image.
    `private void` | `encodeYUV(int[] srcBuf, int width, int pitch, int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)`
    `protected void` | `finalize()`
    `int` | `getCompressedSize()` Returns the size of the image (in bytes) generated by the most recent compress/encode operation.
    `private void` | `init()`
    `void` | `setJPEGQuality(int quality)` Set the JPEG image quality level for subsequent compress operations.
    `void` | `setSourceImage(byte[] srcImage, int width, int pitch, int height, int pixelFormat)` Associate an uncompressed 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`

- - ### Field Detail

    - #### NO_ASSOC_ERROR

          private static final java.lang.String NO_ASSOC_ERROR

      See Also:
        Constant Field Values

    - #### handle

          private long handle

    - #### srcBuf

          private byte[] srcBuf

    - #### srcWidth

          private int srcWidth

    - #### srcHeight

          private int srcHeight

    - #### srcPitch

          private int srcPitch

    - #### srcPixelFormat

          private int srcPixelFormat

    - #### subsamp

          private int subsamp

    - #### jpegQuality

          private int jpegQuality

    - #### compressedSize

          private int compressedSize

    - #### byteOrder

          private java.nio.ByteOrder byteOrder

  - ### Constructor Detail

    - #### TJCompressor

          public TJCompressor()
                       throws java.lang.Exception

      Create a TurboJPEG compressor instance.
      Throws:
        `java.lang.Exception`

    - #### TJCompressor

          public TJCompressor(byte[] srcImage,
                              int width,
                              int pitch,
                              int height,
                              int pixelFormat)
                       throws java.lang.Exception

      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)` for description
        `width` - see `setSourceImage(byte[], int, int, int, int)` for description
        `pitch` - see `setSourceImage(byte[], int, int, int, int)` for description
        `height` - see `setSourceImage(byte[], int, int, int, int)` for description
        `pixelFormat` - see `setSourceImage(byte[], int, int, int, int)` for description
      Throws:
        `java.lang.Exception`

  - ### Method Detail

    - #### setSourceImage

          public void setSourceImage(byte[] srcImage,
                                     int width,
                                     int pitch,
                                     int height,
                                     int pixelFormat)
                              throws java.lang.Exception

      Associate an uncompressed source image with this compressor instance.
      Parameters:
        `srcImage` - image buffer containing RGB or grayscale pixels to be compressed
        `width` - width (in pixels) of the source image
        `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 4-byte boundaries, as is the case for Windows bitmaps. 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 source image
        `pixelFormat` - pixel format of the source image (one of `TJ.PF_*`)
      Throws:
        `java.lang.Exception`

    - #### setSubsamp

          public void setSubsamp(int newSubsamp)
                          throws java.lang.Exception

      Set the level of chrominance subsampling for subsequent compress/encode operations.
      Parameters:
        `newSubsamp` - the new level of chrominance subsampling (one of `TJ.SAMP_*`)
      Throws:
        `java.lang.Exception`

    - #### setJPEGQuality

          public void setJPEGQuality(int quality)
                              throws java.lang.Exception

      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)
      Throws:
        `java.lang.Exception`

    - #### compress

          public void compress(byte[] dstBuf,
                               int flags)
                        throws java.lang.Exception

      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 image width and height.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `java.lang.Exception`

    - #### compress

          public byte[] compress(int flags)
                          throws java.lang.Exception

      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:
        `java.lang.Exception`

    - #### compress

          public void compress(java.awt.image.BufferedImage srcImage,
                               byte[] dstBuf,
                               int flags)
                        throws java.lang.Exception

      Compress the uncompressed source image stored in `srcImage` and output a JPEG image to the given destination buffer.
      Parameters:
        `srcImage` - a `BufferedImage` instance containing RGB or grayscale pixels to be compressed
        `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 image width and height.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `java.lang.Exception`

    - #### compress

          public byte[] compress(java.awt.image.BufferedImage srcImage,
                                 int flags)
                          throws java.lang.Exception

      Compress the uncompressed source image stored in `srcImage` and return a buffer containing a JPEG image.
      Parameters:
        `srcImage` - a `BufferedImage` instance containing RGB or grayscale pixels to be compressed
        `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:
        `java.lang.Exception`

    - #### encodeYUV

          public void encodeYUV(byte[] dstBuf,
                                int flags)
                         throws java.lang.Exception

      Encode the uncompressed source image associated with this compressor instance and output a YUV planar image to the given destination buffer. This method uses the accelerated color conversion routines in TurboJPEG's underlying codec to produce a planar YUV image that is suitable for direct video display. Specifically, if the chrominance components are subsampled along the horizontal dimension, then the width of the luminance plane is padded to 2 in the output image (same goes for the height of the luminance plane, if the chrominance components are subsampled along the vertical dimension.) Also, each line of each plane in the output image is padded to 4 bytes. Although this will work with any subsampling option, it is really only useful in combination with `TJ.SAMP_420`, which produces an image compatible with the I420 (AKA "YUV420P") format.
      Parameters:
        `dstBuf` - buffer that will receive the YUV planar image. Use `TJ.bufSizeYUV(int, int, int)` to determine the appropriate size for this buffer based on the image width, height, and level of chrominance subsampling.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `java.lang.Exception`

    - #### encodeYUV

          public byte[] encodeYUV(int flags)
                           throws java.lang.Exception

      Encode the uncompressed source image associated with this compressor instance and return a buffer containing a YUV planar image. See `encodeYUV(byte[], int)` for more detail.
      Parameters:
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Returns:
        a buffer containing a YUV planar image
      Throws:
        `java.lang.Exception`

    - #### encodeYUV

          public void encodeYUV(java.awt.image.BufferedImage srcImage,
                                byte[] dstBuf,
                                int flags)
                         throws java.lang.Exception

      Encode the uncompressed source image stored in `srcImage` and output a YUV planar image to the given destination buffer. See `encodeYUV(byte[], int)` for more detail.
      Parameters:
        `srcImage` - a `BufferedImage` instance containing RGB or grayscale pixels to be encoded
        `dstBuf` - buffer that will receive the YUV planar image. Use `TJ.bufSizeYUV(int, int, int)` to determine the appropriate size for this buffer based on the image width, height, and level of chrominance subsampling.
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Throws:
        `java.lang.Exception`

    - #### encodeYUV

          public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
                                  int flags)
                           throws java.lang.Exception

      Encode the uncompressed source image stored in `srcImage` and return a buffer containing a YUV planar image. See `encodeYUV(byte[], int)` for more detail.
      Parameters:
        `srcImage` - a `BufferedImage` instance containing RGB or grayscale pixels to be encoded
        `flags` - the bitwise OR of one or more of `TJ.FLAG_*`
      Returns:
        a buffer containing a YUV planar image
      Throws:
        `java.lang.Exception`

    - #### getCompressedSize

          public int getCompressedSize()

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

    - #### close

          public void close()
                     throws java.lang.Exception

      Free the native structures associated with this compressor instance.
      Throws:
        `java.lang.Exception`

    - #### finalize

          protected void finalize()
                           throws java.lang.Throwable

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

    - #### init

          private void init()
                     throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### destroy

          private void destroy()
                        throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### compress

          private int compress(byte[] srcBuf,
                               int width,
                               int pitch,
                               int height,
                               int pixelFormat,
                               byte[] dstbuf,
                               int jpegSubsamp,
                               int jpegQual,
                               int flags)
                        throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### compress

          private int compress(int[] srcBuf,
                               int width,
                               int pitch,
                               int height,
                               int pixelFormat,
                               byte[] dstbuf,
                               int jpegSubsamp,
                               int jpegQual,
                               int flags)
                        throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### encodeYUV

          private void encodeYUV(byte[] srcBuf,
                                 int width,
                                 int pitch,
                                 int height,
                                 int pixelFormat,
                                 byte[] dstbuf,
                                 int subsamp,
                                 int flags)
                          throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### encodeYUV

          private void encodeYUV(int[] srcBuf,
                                 int width,
                                 int pitch,
                                 int height,
                                 int pixelFormat,
                                 byte[] dstbuf,
                                 int subsamp,
                                 int flags)
                          throws java.lang.Exception

      Throws:
        `java.lang.Exception`

Copyright © 2026 Open Microscopy Environment