Package 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.CloseableTurboJPEG compressor
-
-
Constructor Summary
Constructors Constructor Description TJCompressor()Create a TurboJPEG compressor instance.TJCompressor(byte[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)Create a TurboJPEG compressor instance and associate the packed-pixel source image, stored insrcImagewith 2 to 8 bits of data precision per sample, with the newly created instance.TJCompressor(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height)Create a TurboJPEG compressor instance and associate the 8-bit-per-sample packed-pixel source image stored insrcImagewith the newly created instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Free the native structures associated with this compressor instance.byte[]compress()Compress the packed-pixel or planar YUV source image associated with this compressor instance and return a buffer containing a JPEG image.voidcompress(byte[] dstBuf)Compress the packed-pixel or planar YUV source image associated with this compressor instance and output a JPEG image to the given destination buffer.YUVImageencodeYUV(int align)Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into an 8-bit-per-sample unified planar YUV image and return aYUVImageinstance containing the encoded image.YUVImageencodeYUV(int[] strides)Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into separate 8-bit-per-sample Y, U (Cb), and V (Cr) image planes and return aYUVImageinstance containing the encoded image planes.voidencodeYUV(YUVImage dstImage)Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into an 8-bit-per-sample planar YUV image and store it in the givenYUVImageinstance.protected voidfinalize()intget(int param)Get the value of a compression parameter.intgetCompressedSize()Returns the size of the image (in bytes) generated by the most recent compression operation.intgetHeight()Returns the height of the source image (packed-pixel or YUV) associated with this compressor instance.intgetPitch()Returns the pitch (samples per row) of the packed-pixel source image associated with this compressor instance.intgetPixelFormat()Returns the pixel format of the packed-pixel source image associated with this compressor instance.java.lang.ObjectgetSourceBuf()Returns the buffer containing the packed-pixel image associated with this compressor instance.intgetWidth()Returns the width of the source image (packed-pixel or YUV) associated with this compressor instance.voidloadSourceImage(java.lang.String fileName, int align, int pixelFormat)Load a packed-pixel RGB or grayscale source image with 2 to 16 bits of data precision per sample from disk into memory and associate it with this compressor instance.voidset(int param, int value)Set the value of a compression parameter.voidsetICCProfile(byte[] iccBuf)Embed an ICC (International Color Consortium) color management profile in JPEG images generated by subsequent compression operations.voidsetSourceImage(byte[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)Associate a packed-pixel RGB, grayscale, or CMYK source image with 2 to 8 bits of data precision per sample with this compressor instance.voidsetSourceImage(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height)Associate an 8-bit-per-sample packed-pixel RGB or grayscale source image with this compressor instance.voidsetSourceImage(YUVImage srcImage)Associate an 8-bit-per-sample planar YUV source image with this compressor instance.voidsetSourceImage12(short[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)Associate a packed-pixel RGB, grayscale, or CMYK source image with 9 to 12 bits of data precision per sample with this compressor instance.voidsetSourceImage16(short[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat)Associate a packed-pixel RGB, grayscale, or CMYK source image with 13 to 16 bits of data precision per sample with this compressor instance.
-
-
-
Constructor Detail
-
TJCompressor
public TJCompressor() throws TJExceptionCreate a TurboJPEG compressor instance.- Throws:
TJException
-
TJCompressor
public TJCompressor(byte[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat) throws TJExceptionCreate a TurboJPEG compressor instance and associate the packed-pixel source image, stored insrcImagewith 2 to 8 bits of data precision per sample, with the newly created instance.- Parameters:
srcImage- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptionx- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptiony- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptionwidth- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptionpitch- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptionheight- seesetSourceImage(byte[], int, int, int, int, int, int)for descriptionpixelFormat- pixel format of the source image (one ofTJ.PF_*)- Throws:
TJException
-
TJCompressor
public TJCompressor(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height) throws TJExceptionCreate a TurboJPEG compressor instance and associate the 8-bit-per-sample packed-pixel source image stored insrcImagewith the newly created instance.- Parameters:
srcImage- seesetSourceImage(BufferedImage, int, int, int, int)for descriptionx- seesetSourceImage(BufferedImage, int, int, int, int)for descriptiony- seesetSourceImage(BufferedImage, int, int, int, int)for descriptionwidth- seesetSourceImage(BufferedImage, int, int, int, int)for descriptionheight- seesetSourceImage(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 TJExceptionAssociate a packed-pixel RGB, grayscale, or CMYK source image with 2 to 8 bits of data precision per sample with this compressor instance. Note that packed-pixel source images with 2 to 7 bits of data precision per sample can only be compressed into lossless JPEG images.- Parameters:
srcImage- buffer containing a packed-pixel RGB, grayscale, or CMYK source image to be compressed or encoded. The data precision of the source image (from 2 to 8 bits per sample) can be specified usingTJ.PARAM_PRECISIONand defaults to 8 ifTJ.PARAM_PRECISIONis unset or out of range. 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/encodedy- y offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encodedwidth- width (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encodedpitch- bytes per row in the source image. Normally this should bewidth *, if the source image is unpadded. (Setting this parameter to 0 is the equivalent of setting it toTJ.getPixelSize(pixelFormat)width *.) However, you can also use this parameter to specify the row alignment/padding of the source image, to skip rows, or to compress/encode a JPEG or YUV image from a specific region of a larger source image.TJ.getPixelSize(pixelFormat)height- height (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encodedpixelFormat- pixel format of the source image (one ofTJ.PF_*)- Throws:
TJException
-
setSourceImage12
public void setSourceImage12(short[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat) throws TJExceptionAssociate a packed-pixel RGB, grayscale, or CMYK source image with 9 to 12 bits of data precision per sample with this compressor instance. Note that packed-pixel source images with 9 to 11 bits of data precision per sample can only be compressed into lossless JPEG images.- Parameters:
srcImage- buffer containing a packed-pixel RGB, grayscale, or CMYK source image to be compressed. The data precision of the source image (from 9 to 12 bits per sample) can be specified usingTJ.PARAM_PRECISIONand defaults to 12 ifTJ.PARAM_PRECISIONis unset or out of range. This buffer is not modified.x- x offset (in pixels) of the region in the source image from which the JPEG image should be compressedy- y offset (in pixels) of the region in the source image from which the JPEG image should be compressedwidth- width (in pixels) of the region in the source image from which the JPEG image should be compressedpitch- samples per row in the source image. Normally this should bewidth *, if the source image is unpadded. (Setting this parameter to 0 is the equivalent of setting it toTJ.getPixelSize(pixelFormat)width *.) However, you can also use this parameter to specify the row alignment/padding of the source image, to skip rows, or to compress a JPEG image from a specific region of a larger source image.TJ.getPixelSize(pixelFormat)height- height (in pixels) of the region in the source image from which the JPEG image should be compressedpixelFormat- pixel format of the source image (one ofTJ.PF_*)- Throws:
TJException
-
setSourceImage16
public void setSourceImage16(short[] srcImage, int x, int y, int width, int pitch, int height, int pixelFormat) throws TJExceptionAssociate a packed-pixel RGB, grayscale, or CMYK source image with 13 to 16 bits of data precision per sample with this compressor instance. Note that packed-pixel source images with 13 to 16 bits of data precision per sample can only be compressed into lossless JPEG images.- Parameters:
srcImage- buffer containing a packed-pixel RGB, grayscale, or CMYK source image to be compressed. The data precision of the source image (from 13 to 16 bits per sample) can be specified usingTJ.PARAM_PRECISIONand defaults to 16 ifTJ.PARAM_PRECISIONis unset or out of range. This buffer is not modified.x- x offset (in pixels) of the region in the source image from which the JPEG image should be compressedy- y offset (in pixels) of the region in the source image from which the JPEG image should be compressedwidth- width (in pixels) of the region in the source image from which the JPEG image should be compressedpitch- samples per row in the source image. Normally this should bewidth *, if the source image is unpadded. (Setting this parameter to 0 is the equivalent of setting it toTJ.getPixelSize(pixelFormat)width *.) However, you can also use this parameter to specify the row alignment/padding of the source image, to skip rows, or to compress a JPEG image from a specific region of a larger source image.TJ.getPixelSize(pixelFormat)height- height (in pixels) of the region in the source image from which the JPEG image should be compressedpixelFormat- pixel format of the source image (one ofTJ.PF_*)- Throws:
TJException
-
setSourceImage
public void setSourceImage(java.awt.image.BufferedImage srcImage, int x, int y, int width, int height) throws TJExceptionAssociate an 8-bit-per-sample packed-pixel RGB or grayscale source image with this compressor instance.- Parameters:
srcImage- aBufferedImageinstance containing a packed-pixel RGB or grayscale source image 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/encodedy- y offset (in pixels) of the region in the source image from which the JPEG or YUV image should be compressed/encodedwidth- 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 8-bit-per-sample planar YUV source image with this compressor instance. This method setsTJ.PARAM_SUBSAMPto the chrominance subsampling level of the source image.- Parameters:
srcImage- planar YUV source image to be compressed. This image is not modified.- Throws:
TJException
-
loadSourceImage
public void loadSourceImage(java.lang.String fileName, int align, int pixelFormat) throws TJExceptionLoad a packed-pixel RGB or grayscale source image with 2 to 16 bits of data precision per sample from disk into memory and associate it with this compressor instance. Note that packed-pixel source images with 2 to 7, 9 to 11, or 13 to 16 bits of data precision per sample can only be compressed into lossless JPEG images.getWidth(),getPitch(),getHeight(),getPixelFormat(), andgetSourceBuf()can be used to obtain the source image's dimensions, pixel format, and buffer after it is loaded.- Parameters:
fileName- name of a file containing a packed-pixel image in Windows BMP or PBMPLUS (PPM/PGM) format. Windows BMP files require 8-bit-per-sample data precision. When loading a PBMPLUS file, the target data precision (from 2 to 16 bits per sample) can be specified usingTJ.PARAM_PRECISIONand defaults to 8 ifTJ.PARAM_PRECISIONis unset. If the data precision of the PBMPLUS file does not match the target data precision, then upconverting or downconverting will be performed.align- row alignment (in samples) of the packed-pixel buffer into which the source image will be loaded (must be a power of 2.) Setting this parameter to n will cause all rows in the buffer to be padded to the nearest multiple of n samples (1 = unpadded.)pixelFormat- pixel format of the packed-pixel buffer into which the source image will be loaded. The behavior of this method varies depending on the value ofpixelFormat:-
TJ.PF_UNKNOWN: The packed-pixel buffer created by this method will use the most optimal pixel format for the file type. UsegetPixelFormat()to obtain the ID of that pixel format. -
TJ.PF_GRAY: Only PGM files and 8-bit-per-pixel BMP files with a grayscale colormap can be loaded. -
TJ.PF_CMYK: The RGB or grayscale pixels stored in the file will be converted using a quick & dirty algorithm that is suitable only for testing purposes. (Proper conversion between CMYK and other formats requires a color management system.) - Other
pixel formats: The packed-pixel buffer will use the specified pixel format, and pixel format conversion will be performed if necessary.
-
- Throws:
TJException
-
getWidth
public int getWidth()
Returns the width of the source image (packed-pixel or YUV) associated with this compressor instance.- Returns:
- the width of the source image (packed-pixel or YUV) associated with this compressor instance.
-
getPitch
public int getPitch()
Returns the pitch (samples per row) of the packed-pixel source image associated with this compressor instance. If the source image is aBufferedImageinstance with integer pixels, then the stride (pixels per row) is returned instead.- Returns:
- the pitch (samples per row) of the packed-pixel source image associated with this compressor instance.
-
getHeight
public int getHeight()
Returns the height of the source image (packed-pixel or YUV) associated with this compressor instance.- Returns:
- the height of the source image (packed-pixel or YUV) associated with this compressor instance.
-
getPixelFormat
public int getPixelFormat()
Returns the pixel format of the packed-pixel source image associated with this compressor instance. The pixel format is one ofTJ.PF_*.- Returns:
- the pixel format of the packed-pixel source image associated with this compressor instance.
-
getSourceBuf
public java.lang.Object getSourceBuf()
Returns the buffer containing the packed-pixel image associated with this compressor instance. The buffer is abytearray if the source image has 2 to 8 bits of data precision per sample, ashortarray if the source image has 9 to 16 bits of data precision per sample, and either abyteor anintarray (depending on the image type) if the source image is aBufferedImageinstance.- Returns:
- the buffer containing the packed-pixel image associated with this compressor instance.
-
set
public void set(int param, int value)Set the value of a compression parameter.- Parameters:
param- one ofTJ.PARAM_*value- value of the compression parameter (refer toparameter documentation)
-
get
public int get(int param)
Get the value of a compression parameter.- Parameters:
param- one ofTJ.PARAM_*- Returns:
- the value of the specified compression parameter, or -1 if the value is unknown.
-
setICCProfile
public void setICCProfile(byte[] iccBuf) throws TJExceptionEmbed an ICC (International Color Consortium) color management profile in JPEG images generated by subsequent compression operations.- Parameters:
iccBuf- buffer containing an ICC profile. (The size of the ICC profile is assumed to be the length of the array.) A copy is made of the ICC profile, so this buffer can be reused as soon as this method returns. Setting this parameter to null removes any ICC profile that was previously associated with this compressor instance.- Throws:
TJException
-
compress
public void compress(byte[] dstBuf) throws TJExceptionCompress the packed-pixel or planar YUV 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. UseTJ.bufSize()to determine the maximum size for this buffer based on the source image's width and height and the desired level of chrominance subsampling (seeTJ.PARAM_SUBSAMP), then add the size of the ICC profile (if any) that was previously associated with this compressor instance (seesetICCProfile().)- Throws:
TJException
-
compress
public byte[] compress() throws TJExceptionCompress the packed-pixel or planar YUV source image associated with this compressor instance and return a buffer containing a JPEG image.- 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
-
encodeYUV
public void encodeYUV(YUVImage dstImage) throws TJException
Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into an 8-bit-per-sample planar YUV image and store it in the givenYUVImageinstance. This method performs color conversion (which is accelerated in the libjpeg-turbo implementation) but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images into YUV images is not supported. This method setsTJ.PARAM_SUBSAMPto the chrominance subsampling level of the destination image.- Parameters:
dstImage-YUVImageinstance that will receive the planar YUV image- Throws:
TJException
-
encodeYUV
public YUVImage encodeYUV(int align) throws TJException
Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into an 8-bit-per-sample unified planar YUV image and return aYUVImageinstance containing the encoded image. This method performs color conversion (which is accelerated in the libjpeg-turbo implementation) but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images into YUV images is not supported.- Parameters:
align- row alignment (in bytes) of the YUV image (must be a power of 2.) Setting this parameter to n will cause each row in each plane of the YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)- Returns:
- a
YUVImageinstance containing the unified planar YUV encoded image - Throws:
TJException
-
encodeYUV
public YUVImage encodeYUV(int[] strides) throws TJException
Encode the 8-bit-per-sample packed-pixel source image associated with this compressor instance into separate 8-bit-per-sample Y, U (Cb), and V (Cr) image planes and return aYUVImageinstance containing the encoded image planes. This method performs color conversion (which is accelerated in the libjpeg-turbo implementation) but does not execute any of the other steps in the JPEG compression process. Encoding CMYK source images into YUV images is not supported.- Parameters:
strides- an array of integers, each specifying the number of bytes per row in the corresponding plane of the YUV source image. Setting the stride for any plane to 0 is the same as setting it to the plane width (seeYUVImage.) Ifstridesis null, then the strides for all planes will be set to their respective plane widths. You can adjust the strides in order to add an arbitrary amount of row padding to each plane.- Returns:
- a
YUVImageinstance containing the encoded image planes - Throws:
TJException
-
getCompressedSize
public int getCompressedSize()
Returns the size of the image (in bytes) generated by the most recent compression operation.- Returns:
- the size of the image (in bytes) generated by the most recent compression operation.
-
close
public void close() throws TJExceptionFree the native structures associated with this compressor instance.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
TJException
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
-