Java code cleanup + Java docs
This commit is contained in:
@@ -30,40 +30,92 @@ package org.libjpegturbo.turbojpeg;
|
||||
|
||||
import java.awt.image.*;
|
||||
|
||||
/**
|
||||
* TurboJPEG decompressor
|
||||
*/
|
||||
public class TJDecompressor {
|
||||
|
||||
/**
|
||||
* Create a TurboJPEG decompresssor instance.
|
||||
*/
|
||||
public TJDecompressor() throws Exception {
|
||||
init();
|
||||
}
|
||||
|
||||
public TJDecompressor(byte[] buf) throws Exception {
|
||||
/**
|
||||
* Create a TurboJPEG decompressor instance and associate the JPEG image
|
||||
* stored in <code>jpegImage</code> with the newly-created instance.
|
||||
*
|
||||
* @param jpegImage JPEG image buffer (size of JPEG image is assumed to be
|
||||
* the length of the buffer)
|
||||
*/
|
||||
public TJDecompressor(byte[] jpegImage) throws Exception {
|
||||
init();
|
||||
setJPEGBuffer(buf, buf.length);
|
||||
setJPEGBuffer(jpegImage, jpegImage.length);
|
||||
}
|
||||
|
||||
public TJDecompressor(byte[] buf, int bufSize) throws Exception {
|
||||
/**
|
||||
* Create a TurboJPEG decompressor instance and associate the JPEG image
|
||||
* of length <code>imageSize</code> bytes stored in <code>jpegImage</code>
|
||||
* with the newly-created instance.
|
||||
*
|
||||
* @param jpegImage JPEG image buffer
|
||||
*
|
||||
* @param imageSize size of JPEG image (in bytes)
|
||||
*/
|
||||
public TJDecompressor(byte[] jpegImage, int imageSize) throws Exception {
|
||||
init();
|
||||
setJPEGBuffer(buf, bufSize);
|
||||
setJPEGBuffer(jpegImage, imageSize);
|
||||
}
|
||||
|
||||
public void setJPEGBuffer(byte[] buf, int bufSize) throws Exception {
|
||||
if(buf == null || bufSize < 1)
|
||||
/**
|
||||
* Associate a JPEG image buffer with this decompressor instance. This
|
||||
* buffer will be used as the source buffer for subsequent decompress
|
||||
* operations.
|
||||
*
|
||||
* @param jpegImage JPEG image buffer
|
||||
*
|
||||
* @param imageSize size of JPEG image (in bytes)
|
||||
*/
|
||||
public void setJPEGBuffer(byte[] jpegImage, int imageSize) throws Exception {
|
||||
if(jpegImage == null || imageSize < 1)
|
||||
throw new Exception("Invalid argument in setJPEGBuffer()");
|
||||
jpegBuf = buf;
|
||||
jpegBufSize = bufSize;
|
||||
jpegBuf = jpegImage;
|
||||
jpegBufSize = imageSize;
|
||||
decompressHeader(jpegBuf, jpegBufSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the width of the JPEG image associated with this decompressor
|
||||
* instance.
|
||||
*
|
||||
* @return the width of the JPEG image associated with this decompressor
|
||||
* instance
|
||||
*/
|
||||
public int getWidth() throws Exception {
|
||||
if(jpegWidth < 1) throw new Exception("JPEG buffer not initialized");
|
||||
return jpegWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the height of the JPEG image associated with this decompressor
|
||||
* instance.
|
||||
*
|
||||
* @return the height of the JPEG image associated with this decompressor
|
||||
* instance
|
||||
*/
|
||||
public int getHeight() throws Exception {
|
||||
if(jpegHeight < 1) throw new Exception("JPEG buffer not initialized");
|
||||
return jpegHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the level of chrominance subsampling used in the JPEG image
|
||||
* associated with this decompressor instance.
|
||||
*
|
||||
* @return the level of chrominance subsampling used in the JPEG image
|
||||
* associated with this decompressor instance
|
||||
*/
|
||||
public int getSubsamp() throws Exception {
|
||||
if(jpegSubsamp < 0) throw new Exception("JPEG buffer not initialized");
|
||||
if(jpegSubsamp >= TJ.NUMSAMPOPT)
|
||||
@@ -71,16 +123,46 @@ public class TJDecompressor {
|
||||
return jpegSubsamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JPEG image buffer associated with this decompressor instance.
|
||||
*
|
||||
* @return the JPEG image buffer associated with this decompressor instance
|
||||
*/
|
||||
public byte[] getJPEGBuf() throws Exception {
|
||||
if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
|
||||
return jpegBuf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the JPEG image (in bytes) associated with this
|
||||
* decompressor instance.
|
||||
*
|
||||
* @return the size of the JPEG image (in bytes) associated with this
|
||||
* decompressor instance
|
||||
*/
|
||||
public int getJPEGSize() throws Exception {
|
||||
if(jpegBufSize < 1) throw new Exception("JPEG buffer not initialized");
|
||||
return jpegBufSize;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the width of the largest scaled down image that the TurboJPEG
|
||||
* decompressor can generate without exceeding the desired image width and
|
||||
* height.
|
||||
*
|
||||
* @param desiredWidth desired width (in pixels) of the decompressed image.
|
||||
* If this is set to 0, then only the height will be considered when
|
||||
* determining the scaled image size.
|
||||
*
|
||||
* @param desiredHeight desired height (in pixels) of the decompressed image.
|
||||
* If this is set to 0, then only the width will be considered when
|
||||
* determining the scaled image size.
|
||||
*
|
||||
* @return the width of the largest scaled down image that the TurboJPEG
|
||||
* decompressor can generate without exceeding the desired image width and
|
||||
* height
|
||||
*/
|
||||
public int getScaledWidth(int desiredWidth, int desiredHeight)
|
||||
throws Exception {
|
||||
if(jpegWidth < 1 || jpegHeight < 1)
|
||||
@@ -102,6 +184,23 @@ public class TJDecompressor {
|
||||
return scaledWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the height of the largest scaled down image that the TurboJPEG
|
||||
* decompressor can generate without exceeding the desired image width and
|
||||
* height.
|
||||
*
|
||||
* @param desiredWidth desired width (in pixels) of the decompressed image.
|
||||
* If this is set to 0, then only the height will be considered when
|
||||
* determining the scaled image size.
|
||||
*
|
||||
* @param desiredHeight desired height (in pixels) of the decompressed image.
|
||||
* If this is set to 0, then only the width will be considered when
|
||||
* determining the scaled image size.
|
||||
*
|
||||
* @return the height of the largest scaled down image that the TurboJPEG
|
||||
* decompressor can generate without exceeding the desired image width and
|
||||
* height
|
||||
*/
|
||||
public int getScaledHeight(int desiredWidth, int desiredHeight)
|
||||
throws Exception {
|
||||
if(jpegWidth < 1 || jpegHeight < 1)
|
||||
@@ -123,6 +222,44 @@ public class TJDecompressor {
|
||||
return scaledHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and output a decompressed image to the given destination buffer.
|
||||
*
|
||||
* @param dstBuf buffer which will receive the decompressed image. This
|
||||
* buffer should normally be <code>pitch * scaledHeight</code> bytes in size,
|
||||
* where <code>scaledHeight = ceil(jpegHeight * scalingFactor)</code>, and
|
||||
* the supported scaling factors can be determined by calling {@link
|
||||
* TJ#getScalingFactors}.
|
||||
*
|
||||
* @param desiredWidth desired width (in pixels) of the decompressed image.
|
||||
* If the desired image dimensions are smaller than the dimensions of the
|
||||
* JPEG image being decompressed, then TurboJPEG will use scaling in the JPEG
|
||||
* decompressor to generate the largest possible image that will fit within
|
||||
* the desired dimensions. If desiredWidth is set to 0, then only the height
|
||||
* will be considered when determining the scaled image size.
|
||||
*
|
||||
* @param pitch bytes per line of the destination image. Normally, this
|
||||
* should be set to <code>scaledWidth * TJ.pixelSize(pixelFormat)</code> if
|
||||
* the decompressed image is unpadded, but you can use this to, for instance,
|
||||
* pad each line of the decompressed image to a 4-byte boundary. NOTE:
|
||||
* <code>scaledWidth = ceil(jpegWidth * scalingFactor)</code>. Setting this
|
||||
* parameter to 0 is the equivalent of setting it to
|
||||
* <code>scaledWidth * pixelSize</code>.
|
||||
*
|
||||
* @param desiredHeight desired height (in pixels) of the decompressed image.
|
||||
* If the desired image dimensions are smaller than the dimensions of the
|
||||
* JPEG image being decompressed, then TurboJPEG will use scaling in the JPEG
|
||||
* decompressor to generate the largest possible image that will fit within
|
||||
* the desired dimensions. If desiredHeight is set to 0, then only the
|
||||
* width will be considered when determining the scaled image size.
|
||||
*
|
||||
* @param pixelFormat Pixel format of the decompressed image (see
|
||||
* {@link TJ})
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*/
|
||||
public void decompress(byte[] dstBuf, int desiredWidth, int pitch,
|
||||
int desiredHeight, int pixelFormat, int flags) throws Exception {
|
||||
if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
|
||||
@@ -133,6 +270,27 @@ public class TJDecompressor {
|
||||
desiredHeight, pixelFormat, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and return a buffer containing the decompressed image.
|
||||
*
|
||||
* @param desiredWidth see
|
||||
* {@link #decompress(byte[], int, int, int, int, int)} for description
|
||||
*
|
||||
* @param pitch see
|
||||
* {@link #decompress(byte[], int, int, int, int, int)} for description
|
||||
*
|
||||
* @param desiredHeight see
|
||||
* {@link #decompress(byte[], int, int, int, int, int)} for description
|
||||
*
|
||||
* @param pixelFormat Pixel format of the decompressed image (see
|
||||
* {@link TJ})
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*
|
||||
* @return a buffer containing the decompressed image
|
||||
*/
|
||||
public byte[] decompress(int desiredWidth, int pitch, int desiredHeight,
|
||||
int pixelFormat, int flags) throws Exception {
|
||||
if(desiredWidth < 0 || pitch < 0 || desiredHeight < 0
|
||||
@@ -147,6 +305,24 @@ public class TJDecompressor {
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and output a YUV planar image to the given destination buffer.
|
||||
* This method performs JPEG decompression but leaves out the color
|
||||
* conversion step, so a planar YUV image is generated instead of an RGB
|
||||
* image. The padding of the planes in this image is the same as the images
|
||||
* generated by {@link TJCompressor#encodeYUV(byte[], int)}. Note that, if
|
||||
* the width or height of the image is not an even multiple of the MCU block
|
||||
* size (see {@link TJ#getMCUWidth} and {@link TJ#getMCUHeight}), then an
|
||||
* intermediate buffer copy will be performed within TurboJPEG.
|
||||
*
|
||||
* @param dstBuf buffer which will receive the YUV planar image. Use
|
||||
* {@link TJ#bufSizeYUV} to determine the appropriate size for this buffer
|
||||
* based on the image width, height, and level of chrominance subsampling.
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*/
|
||||
public void decompressToYUV(byte[] dstBuf, int flags) throws Exception {
|
||||
if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
|
||||
if(dstBuf == null || flags < 0)
|
||||
@@ -154,6 +330,17 @@ public class TJDecompressor {
|
||||
decompressToYUV(jpegBuf, jpegBufSize, dstBuf, flags);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and return a buffer containing a YUV planar image. See {@link
|
||||
* #decompressToYUV(byte[], int)} for more detail.
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*
|
||||
* @return a buffer containing a YUV planar image
|
||||
*/
|
||||
public byte[] decompressToYUV(int flags) throws Exception {
|
||||
if(flags < 0)
|
||||
throw new Exception("Invalid argument in decompressToYUV()");
|
||||
@@ -166,6 +353,17 @@ public class TJDecompressor {
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and output a decompressed image to the given
|
||||
* <code>BufferedImage</code> instance.
|
||||
*
|
||||
* @param dstImage a <code>BufferedImage</code> instance which will receive
|
||||
* the decompressed image
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*/
|
||||
public void decompress(BufferedImage dstImage, int flags) throws Exception {
|
||||
if(dstImage == null || flags < 0)
|
||||
throw new Exception("Invalid argument in decompress()");
|
||||
@@ -212,6 +410,26 @@ public class TJDecompressor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress the JPEG source image associated with this decompressor
|
||||
* instance and return a <code>BufferedImage</code> instance containing the
|
||||
* decompressed image.
|
||||
*
|
||||
* @param desiredWidth see
|
||||
* {@link #decompress(byte[], int, int, int, int, int)} for description
|
||||
*
|
||||
* @param desiredHeight see
|
||||
* {@link #decompress(byte[], int, int, int, int, int)} for description
|
||||
*
|
||||
* @param bufferedImageType the image type of the <code>BufferedImage</code>
|
||||
* instance to create (for instance, <code>BufferedImage.TYPE_INT_RGB</code>)
|
||||
*
|
||||
* @param flags the bitwise OR of one or more of the flags described in
|
||||
* {@link TJ}
|
||||
*
|
||||
* @return a <code>BufferedImage</code> instance containing the
|
||||
* decompressed image
|
||||
*/
|
||||
public BufferedImage decompress(int desiredWidth, int desiredHeight,
|
||||
int bufferedImageType, int flags) throws Exception {
|
||||
if(desiredWidth < 0 || desiredHeight < 0 || flags < 0)
|
||||
@@ -224,6 +442,9 @@ public class TJDecompressor {
|
||||
return img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the native structures associated with this decompressor instance.
|
||||
*/
|
||||
public void close() throws Exception {
|
||||
destroy();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user