Code formatting tweaks

git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/branches/1.3.x@968 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
DRC
2013-04-27 12:36:07 +00:00
parent aba6c404c3
commit 08fcac01e6
10 changed files with 940 additions and 880 deletions

View File

@@ -31,38 +31,38 @@ package org.libjpegturbo.turbojpeg;
/**
* TurboJPEG utility class (cannot be instantiated)
*/
final public class TJ {
public final class TJ {
/**
* The number of chrominance subsampling options
*/
final public static int NUMSAMP = 5;
public static final int NUMSAMP = 5;
/**
* 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG
* or YUV image will contain one chrominance component for every pixel in the
* source image.
*/
final public static int SAMP_444 = 0;
public static final int SAMP_444 = 0;
/**
* 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
* chrominance component for every 2x1 block of pixels in the source image.
*/
final public static int SAMP_422 = 1;
public static final int SAMP_422 = 1;
/**
* 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
* chrominance component for every 2x2 block of pixels in the source image.
*/
final public static int SAMP_420 = 2;
public static final int SAMP_420 = 2;
/**
* Grayscale. The JPEG or YUV image will contain no chrominance components.
*/
final public static int SAMP_GRAY = 3;
public static final int SAMP_GRAY = 3;
/**
* 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
* chrominance component for every 1x2 block of pixels in the source image.
*/
final public static int SAMP_440 = 4;
public static final int SAMP_440 = 4;
/**
@@ -75,12 +75,12 @@ final public class TJ {
* @return the MCU block width for the given level of chrominance subsampling
*/
public static int getMCUWidth(int subsamp) throws Exception {
if(subsamp < 0 || subsamp >= NUMSAMP)
if (subsamp < 0 || subsamp >= NUMSAMP)
throw new Exception("Invalid subsampling type");
return mcuWidth[subsamp];
}
final private static int mcuWidth[] = {
private static final int[] mcuWidth = {
8, 16, 16, 8, 8
};
@@ -96,12 +96,12 @@ final public class TJ {
* subsampling
*/
public static int getMCUHeight(int subsamp) throws Exception {
if(subsamp < 0 || subsamp >= NUMSAMP)
if (subsamp < 0 || subsamp >= NUMSAMP)
throw new Exception("Invalid subsampling type");
return mcuHeight[subsamp];
}
final private static int mcuHeight[] = {
private static final int[] mcuHeight = {
8, 8, 16, 8, 16
};
@@ -109,76 +109,76 @@ final public class TJ {
/**
* The number of pixel formats
*/
final public static int NUMPF = 11;
public static final int NUMPF = 11;
/**
* RGB pixel format. The red, green, and blue components in the image are
* stored in 3-byte pixels in the order R, G, B from lowest to highest byte
* address within each pixel.
*/
final public static int PF_RGB = 0;
public static final int PF_RGB = 0;
/**
* BGR pixel format. The red, green, and blue components in the image are
* stored in 3-byte pixels in the order B, G, R from lowest to highest byte
* address within each pixel.
*/
final public static int PF_BGR = 1;
public static final int PF_BGR = 1;
/**
* RGBX pixel format. The red, green, and blue components in the image are
* stored in 4-byte pixels in the order R, G, B from lowest to highest byte
* address within each pixel. The X component is ignored when compressing
* and undefined when decompressing.
*/
final public static int PF_RGBX = 2;
public static final int PF_RGBX = 2;
/**
* BGRX pixel format. The red, green, and blue components in the image are
* stored in 4-byte pixels in the order B, G, R from lowest to highest byte
* address within each pixel. The X component is ignored when compressing
* and undefined when decompressing.
*/
final public static int PF_BGRX = 3;
public static final int PF_BGRX = 3;
/**
* XBGR pixel format. The red, green, and blue components in the image are
* stored in 4-byte pixels in the order R, G, B from highest to lowest byte
* address within each pixel. The X component is ignored when compressing
* and undefined when decompressing.
*/
final public static int PF_XBGR = 4;
public static final int PF_XBGR = 4;
/**
* XRGB pixel format. The red, green, and blue components in the image are
* stored in 4-byte pixels in the order B, G, R from highest to lowest byte
* address within each pixel. The X component is ignored when compressing
* and undefined when decompressing.
*/
final public static int PF_XRGB = 5;
public static final int PF_XRGB = 5;
/**
* Grayscale pixel format. Each 1-byte pixel represents a luminance
* (brightness) level from 0 to 255.
*/
final public static int PF_GRAY = 6;
public static final int PF_GRAY = 6;
/**
* RGBA pixel format. This is the same as {@link #PF_RGBX}, except that when
* decompressing, the X byte is guaranteed to be 0xFF, which can be
* interpreted as an opaque alpha channel.
*/
final public static int PF_RGBA = 7;
public static final int PF_RGBA = 7;
/**
* BGRA pixel format. This is the same as {@link #PF_BGRX}, except that when
* decompressing, the X byte is guaranteed to be 0xFF, which can be
* interpreted as an opaque alpha channel.
*/
final public static int PF_BGRA = 8;
public static final int PF_BGRA = 8;
/**
* ABGR pixel format. This is the same as {@link #PF_XBGR}, except that when
* decompressing, the X byte is guaranteed to be 0xFF, which can be
* interpreted as an opaque alpha channel.
*/
final public static int PF_ABGR = 9;
public static final int PF_ABGR = 9;
/**
* ARGB pixel format. This is the same as {@link #PF_XRGB}, except that when
* decompressing, the X byte is guaranteed to be 0xFF, which can be
* interpreted as an opaque alpha channel.
*/
final public static int PF_ARGB = 10;
public static final int PF_ARGB = 10;
/**
@@ -189,12 +189,12 @@ final public class TJ {
* @return the pixel size (in bytes) for the given pixel format
*/
public static int getPixelSize(int pixelFormat) throws Exception {
if(pixelFormat < 0 || pixelFormat >= NUMPF)
if (pixelFormat < 0 || pixelFormat >= NUMPF)
throw new Exception("Invalid pixel format");
return pixelSize[pixelFormat];
}
final private static int pixelSize[] = {
private static final int[] pixelSize = {
3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4
};
@@ -211,12 +211,12 @@ final public class TJ {
* @return the red offset for the given pixel format
*/
public static int getRedOffset(int pixelFormat) throws Exception {
if(pixelFormat < 0 || pixelFormat >= NUMPF)
if (pixelFormat < 0 || pixelFormat >= NUMPF)
throw new Exception("Invalid pixel format");
return redOffset[pixelFormat];
}
final private static int redOffset[] = {
private static final int[] redOffset = {
0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1
};
@@ -233,12 +233,12 @@ final public class TJ {
* @return the green offset for the given pixel format
*/
public static int getGreenOffset(int pixelFormat) throws Exception {
if(pixelFormat < 0 || pixelFormat >= NUMPF)
if (pixelFormat < 0 || pixelFormat >= NUMPF)
throw new Exception("Invalid pixel format");
return greenOffset[pixelFormat];
}
final private static int greenOffset[] = {
private static final int[] greenOffset = {
1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2
};
@@ -255,12 +255,12 @@ final public class TJ {
* @return the blue offset for the given pixel format
*/
public static int getBlueOffset(int pixelFormat) throws Exception {
if(pixelFormat < 0 || pixelFormat >= NUMPF)
if (pixelFormat < 0 || pixelFormat >= NUMPF)
throw new Exception("Invalid pixel format");
return blueOffset[pixelFormat];
}
final private static int blueOffset[] = {
private static final int[] blueOffset = {
2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3
};
@@ -269,27 +269,27 @@ final public class TJ {
* The uncompressed source/destination image is stored in bottom-up (Windows,
* OpenGL) order, not top-down (X11) order.
*/
final public static int FLAG_BOTTOMUP = 2;
public static final int FLAG_BOTTOMUP = 2;
/**
* Turn off CPU auto-detection and force TurboJPEG to use MMX code
* (if the underlying codec supports it.)
*/
final public static int FLAG_FORCEMMX = 8;
public static final int FLAG_FORCEMMX = 8;
/**
* Turn off CPU auto-detection and force TurboJPEG to use SSE code
* (if the underlying codec supports it.)
*/
final public static int FLAG_FORCESSE = 16;
public static final int FLAG_FORCESSE = 16;
/**
* Turn off CPU auto-detection and force TurboJPEG to use SSE2 code
* (if the underlying codec supports it.)
*/
final public static int FLAG_FORCESSE2 = 32;
public static final int FLAG_FORCESSE2 = 32;
/**
* Turn off CPU auto-detection and force TurboJPEG to use SSE3 code
* (if the underlying codec supports it.)
*/
final public static int FLAG_FORCESSE3 = 128;
public static final int FLAG_FORCESSE3 = 128;
/**
* When decompressing an image that was compressed using chrominance
* subsampling, use the fastest chrominance upsampling algorithm available in
@@ -297,7 +297,7 @@ final public class TJ {
* creates a smooth transition between neighboring chrominance components in
* order to reduce upsampling artifacts in the decompressed image.
*/
final public static int FLAG_FASTUPSAMPLE = 256;
public static final int FLAG_FASTUPSAMPLE = 256;
/**
* Use the fastest DCT/IDCT algorithm available in the underlying codec. The
* default if this flag is not specified is implementation-specific. The
@@ -306,7 +306,7 @@ final public class TJ {
* effect on accuracy, but it uses the accurate algorithm when decompressing,
* because this has been shown to have a larger effect.
*/
final public static int FLAG_FASTDCT = 2048;
public static final int FLAG_FASTDCT = 2048;
/**
* Use the most accurate DCT/IDCT algorithm available in the underlying
* codec. The default if this flag is not specified is
@@ -316,7 +316,7 @@ final public class TJ {
* accurate algorithm when decompressing, because this has been shown to have
* a larger effect.
*/
final public static int FLAG_ACCURATEDCT = 4096;
public static final int FLAG_ACCURATEDCT = 4096;
/**
@@ -333,7 +333,7 @@ final public class TJ {
* @return the maximum size of the buffer (in bytes) required to hold a JPEG
* image with the given width, height, and level of chrominance subsampling
*/
public native static int bufSize(int width, int height, int jpegSubsamp)
public static native int bufSize(int width, int height, int jpegSubsamp)
throws Exception;
/**
@@ -350,7 +350,7 @@ final public class TJ {
* @return the size of the buffer (in bytes) required to hold a YUV planar
* image with the given width, height, and level of chrominance subsampling
*/
public native static int bufSizeYUV(int width, int height,
public static native int bufSizeYUV(int width, int height,
int subsamp)
throws Exception;
@@ -361,7 +361,7 @@ final public class TJ {
* @return a list of fractional scaling factors that the JPEG decompressor in
* this implementation of TurboJPEG supports
*/
public native static TJScalingFactor[] getScalingFactors()
public static native TJScalingFactor[] getScalingFactors()
throws Exception;
static {

View File

@@ -36,7 +36,7 @@ import java.nio.*;
*/
public class TJCompressor {
private final static String NO_ASSOC_ERROR =
private static final String NO_ASSOC_ERROR =
"No source image is associated with this instance";
/**
@@ -63,7 +63,7 @@ public class TJCompressor {
* {@link TJ TJ.PF_*})
*/
public TJCompressor(byte[] srcImage, int width, int pitch, int height,
int pixelFormat) throws Exception {
int pixelFormat) throws Exception {
setSourceImage(srcImage, width, pitch, height, pixelFormat);
}
@@ -88,7 +88,7 @@ public class TJCompressor {
* {@link TJ TJ.PF_*})
*/
public TJCompressor(byte[] srcImage, int x, int y, int width, int pitch,
int height, int pixelFormat) throws Exception {
int height, int pixelFormat) throws Exception {
setSourceImage(srcImage, x, y, width, pitch, height, pixelFormat);
}
@@ -123,15 +123,18 @@ public class TJCompressor {
* {@link TJ TJ.PF_*})
*/
public void setSourceImage(byte[] srcImage, int x, int y, int width,
int pitch, int height, int pixelFormat) throws Exception {
if(handle == 0) init();
if(srcImage == null || x < 0 || y < 0 || width < 1 || height < 1
|| pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
int pitch, int height, int pixelFormat)
throws Exception {
if (handle == 0) init();
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
throw new Exception("Invalid argument in setSourceImage()");
srcBuf = srcImage;
srcWidth = width;
if(pitch == 0) srcPitch = width * TJ.getPixelSize(pixelFormat);
else srcPitch = pitch;
if (pitch == 0)
srcPitch = width * TJ.getPixelSize(pixelFormat);
else
srcPitch = pitch;
srcHeight = height;
srcPixelFormat = pixelFormat;
srcX = x;
@@ -143,7 +146,7 @@ public class TJCompressor {
* {@link #setSourceImage(byte[], int, int, int, int, int, int)} instead.
*/
public void setSourceImage(byte[] srcImage, int width, int pitch,
int height, int pixelFormat) throws Exception {
int height, int pixelFormat) throws Exception {
setSourceImage(srcImage, 0, 0, width, pitch, height, pixelFormat);
srcX = srcY = -1;
}
@@ -157,7 +160,7 @@ public class TJCompressor {
* {@link TJ TJ.SAMP_*})
*/
public void setSubsamp(int newSubsamp) throws Exception {
if(newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
if (newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
throw new Exception("Invalid argument in setSubsamp()");
subsamp = newSubsamp;
}
@@ -169,7 +172,7 @@ public class TJCompressor {
* 100 = best)
*/
public void setJPEGQuality(int quality) throws Exception {
if(quality < 1 || quality > 100)
if (quality < 1 || quality > 100)
throw new Exception("Invalid argument in setJPEGQuality()");
jpegQuality = quality;
}
@@ -185,17 +188,22 @@ public class TJCompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void compress(byte[] dstBuf, int flags) throws Exception {
if(dstBuf == null || flags < 0)
if (dstBuf == null || flags < 0)
throw new Exception("Invalid argument in compress()");
if(srcBuf == null) throw new Exception(NO_ASSOC_ERROR);
if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
if(subsamp < 0) throw new Exception("Subsampling level not set");
if (srcBuf == null)
throw new Exception(NO_ASSOC_ERROR);
if (jpegQuality < 0)
throw new Exception("JPEG Quality not set");
if (subsamp < 0)
throw new Exception("Subsampling level not set");
if (srcX >= 0 && srcY >= 0)
compressedSize = compress(srcBuf, srcX, srcY, srcWidth, srcPitch,
srcHeight, srcPixelFormat, dstBuf, subsamp, jpegQuality, flags);
srcHeight, srcPixelFormat, dstBuf, subsamp,
jpegQuality, flags);
else
compressedSize = compress(srcBuf, srcWidth, srcPitch,
srcHeight, srcPixelFormat, dstBuf, subsamp, jpegQuality, flags);
compressedSize = compress(srcBuf, srcWidth, srcPitch, srcHeight,
srcPixelFormat, dstBuf, subsamp, jpegQuality,
flags);
}
/**
@@ -209,7 +217,7 @@ public class TJCompressor {
* #getCompressedSize} to obtain the size of the JPEG image.
*/
public byte[] compress(int flags) throws Exception {
if(srcWidth < 1 || srcHeight < 1)
if (srcWidth < 1 || srcHeight < 1)
throw new Exception(NO_ASSOC_ERROR);
byte[] buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)];
compress(buf, flags);
@@ -230,13 +238,14 @@ public class TJCompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void compress(BufferedImage srcImage, byte[] dstBuf, int flags)
throws Exception {
if(srcImage == null || dstBuf == null || flags < 0)
throws Exception {
if (srcImage == null || dstBuf == null || flags < 0)
throw new Exception("Invalid argument in compress()");
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int pixelFormat; boolean intPixels = false;
if(byteOrder == null)
int pixelFormat;
boolean intPixels = false;
if (byteOrder == null)
byteOrder = ByteOrder.nativeOrder();
switch(srcImage.getType()) {
case BufferedImage.TYPE_3BYTE_BGR:
@@ -247,7 +256,7 @@ public class TJCompressor {
case BufferedImage.TYPE_BYTE_GRAY:
pixelFormat = TJ.PF_GRAY; break;
case BufferedImage.TYPE_INT_BGR:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XBGR;
else
pixelFormat = TJ.PF_RGBX;
@@ -255,7 +264,7 @@ public class TJCompressor {
case BufferedImage.TYPE_INT_RGB:
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XRGB;
else
pixelFormat = TJ.PF_BGRX;
@@ -264,9 +273,11 @@ public class TJCompressor {
throw new Exception("Unsupported BufferedImage format");
}
WritableRaster wr = srcImage.getRaster();
if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
if(subsamp < 0) throw new Exception("Subsampling level not set");
if(intPixels) {
if (jpegQuality < 0)
throw new Exception("JPEG Quality not set");
if (subsamp < 0)
throw new Exception("Subsampling level not set");
if (intPixels) {
SinglePixelPackedSampleModel sm =
(SinglePixelPackedSampleModel)srcImage.getSampleModel();
int pitch = sm.getScanlineStride();
@@ -274,26 +285,27 @@ public class TJCompressor {
int[] buf = db.getData();
if (srcX >= 0 && srcY >= 0)
compressedSize = compress(buf, srcX, srcY, width, pitch, height,
pixelFormat, dstBuf, subsamp, jpegQuality, flags);
pixelFormat, dstBuf, subsamp, jpegQuality,
flags);
else
compressedSize = compress(buf, width, pitch, height, pixelFormat,
dstBuf, subsamp, jpegQuality, flags);
}
else {
dstBuf, subsamp, jpegQuality, flags);
} else {
ComponentSampleModel sm =
(ComponentSampleModel)srcImage.getSampleModel();
int pixelSize = sm.getPixelStride();
if(pixelSize != TJ.getPixelSize(pixelFormat))
if (pixelSize != TJ.getPixelSize(pixelFormat))
throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
int pitch = sm.getScanlineStride();
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
byte[] buf = db.getData();
if (srcX >= 0 && srcY >= 0)
compressedSize = compress(buf, srcX, srcY, width, pitch, height,
pixelFormat, dstBuf, subsamp, jpegQuality, flags);
pixelFormat, dstBuf, subsamp, jpegQuality,
flags);
else
compressedSize = compress(buf, width, pitch, height, pixelFormat,
dstBuf, subsamp, jpegQuality, flags);
dstBuf, subsamp, jpegQuality, flags);
}
}
@@ -340,12 +352,14 @@ public class TJCompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void encodeYUV(byte[] dstBuf, int flags) throws Exception {
if(dstBuf == null || flags < 0)
if (dstBuf == null || flags < 0)
throw new Exception("Invalid argument in compress()");
if(srcBuf == null) throw new Exception(NO_ASSOC_ERROR);
if(subsamp < 0) throw new Exception("Subsampling level not set");
if (srcBuf == null)
throw new Exception(NO_ASSOC_ERROR);
if (subsamp < 0)
throw new Exception("Subsampling level not set");
encodeYUV(srcBuf, srcWidth, srcPitch, srcHeight,
srcPixelFormat, dstBuf, subsamp, flags);
srcPixelFormat, dstBuf, subsamp, flags);
compressedSize = TJ.bufSizeYUV(srcWidth, srcHeight, subsamp);
}
@@ -359,9 +373,10 @@ public class TJCompressor {
* @return a buffer containing a YUV planar image
*/
public byte[] encodeYUV(int flags) throws Exception {
if(srcWidth < 1 || srcHeight < 1)
if (srcWidth < 1 || srcHeight < 1)
throw new Exception(NO_ASSOC_ERROR);
if(subsamp < 0) throw new Exception("Subsampling level not set");
if (subsamp < 0)
throw new Exception("Subsampling level not set");
byte[] buf = new byte[TJ.bufSizeYUV(srcWidth, srcHeight, subsamp)];
encodeYUV(buf, flags);
return buf;
@@ -383,12 +398,12 @@ public class TJCompressor {
*/
public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags)
throws Exception {
if(srcImage == null || dstBuf == null || flags < 0)
if (srcImage == null || dstBuf == null || flags < 0)
throw new Exception("Invalid argument in encodeYUV()");
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int pixelFormat; boolean intPixels = false;
if(byteOrder == null)
if (byteOrder == null)
byteOrder = ByteOrder.nativeOrder();
switch(srcImage.getType()) {
case BufferedImage.TYPE_3BYTE_BGR:
@@ -399,7 +414,7 @@ public class TJCompressor {
case BufferedImage.TYPE_BYTE_GRAY:
pixelFormat = TJ.PF_GRAY; break;
case BufferedImage.TYPE_INT_BGR:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XBGR;
else
pixelFormat = TJ.PF_RGBX;
@@ -407,7 +422,7 @@ public class TJCompressor {
case BufferedImage.TYPE_INT_RGB:
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XRGB;
else
pixelFormat = TJ.PF_BGRX;
@@ -416,27 +431,26 @@ public class TJCompressor {
throw new Exception("Unsupported BufferedImage format");
}
WritableRaster wr = srcImage.getRaster();
if(subsamp < 0) throw new Exception("Subsampling level not set");
if(intPixels) {
if (subsamp < 0) throw new Exception("Subsampling level not set");
if (intPixels) {
SinglePixelPackedSampleModel sm =
(SinglePixelPackedSampleModel)srcImage.getSampleModel();
int pitch = sm.getScanlineStride();
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
int[] buf = db.getData();
encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
flags);
}
else {
flags);
} else {
ComponentSampleModel sm =
(ComponentSampleModel)srcImage.getSampleModel();
int pixelSize = sm.getPixelStride();
if(pixelSize != TJ.getPixelSize(pixelFormat))
if (pixelSize != TJ.getPixelSize(pixelFormat))
throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
int pitch = sm.getScanlineStride();
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
byte[] buf = db.getData();
encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
flags);
flags);
}
compressedSize = TJ.bufSizeYUV(width, height, subsamp);
}
@@ -453,9 +467,9 @@ public class TJCompressor {
*
* @return a buffer containing a YUV planar image
*/
public byte[] encodeYUV(BufferedImage srcImage, int flags)
throws Exception {
if(subsamp < 0) throw new Exception("Subsampling level not set");
public byte[] encodeYUV(BufferedImage srcImage, int flags) throws Exception {
if (subsamp < 0)
throw new Exception("Subsampling level not set");
int width = srcImage.getWidth();
int height = srcImage.getHeight();
byte[] buf = new byte[TJ.bufSizeYUV(width, height, subsamp)];
@@ -484,9 +498,8 @@ public class TJCompressor {
protected void finalize() throws Throwable {
try {
close();
}
catch(Exception e) {}
finally {
} catch(Exception e) {
} finally {
super.finalize();
}
};

View File

@@ -69,8 +69,8 @@ public interface TJCustomFilter {
* @param transform a {@link TJTransform} instance that specifies the
* parameters and/or cropping region for this transform
*/
public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion,
Rectangle planeRegion, int componentID, int transformID,
TJTransform transform)
void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion,
Rectangle planeRegion, int componentID, int transformID,
TJTransform transform)
throws Exception;
}

View File

@@ -36,7 +36,7 @@ import java.nio.*;
*/
public class TJDecompressor {
private final static String NO_ASSOC_ERROR =
private static final String NO_ASSOC_ERROR =
"No JPEG image is associated with this instance";
/**
@@ -82,7 +82,7 @@ public class TJDecompressor {
* @param imageSize size of the JPEG image (in bytes)
*/
public void setJPEGImage(byte[] jpegImage, int imageSize) throws Exception {
if(jpegImage == null || imageSize < 1)
if (jpegImage == null || imageSize < 1)
throw new Exception("Invalid argument in setJPEGImage()");
jpegBuf = jpegImage;
jpegBufSize = imageSize;
@@ -97,7 +97,8 @@ public class TJDecompressor {
* instance
*/
public int getWidth() throws Exception {
if(jpegWidth < 1) throw new Exception(NO_ASSOC_ERROR);
if (jpegWidth < 1)
throw new Exception(NO_ASSOC_ERROR);
return jpegWidth;
}
@@ -109,7 +110,8 @@ public class TJDecompressor {
* instance
*/
public int getHeight() throws Exception {
if(jpegHeight < 1) throw new Exception(NO_ASSOC_ERROR);
if (jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR);
return jpegHeight;
}
@@ -121,8 +123,9 @@ public class TJDecompressor {
* associated with this decompressor instance
*/
public int getSubsamp() throws Exception {
if(jpegSubsamp < 0) throw new Exception(NO_ASSOC_ERROR);
if(jpegSubsamp >= TJ.NUMSAMP)
if (jpegSubsamp < 0)
throw new Exception(NO_ASSOC_ERROR);
if (jpegSubsamp >= TJ.NUMSAMP)
throw new Exception("JPEG header information is invalid");
return jpegSubsamp;
}
@@ -133,7 +136,8 @@ public class TJDecompressor {
* @return the JPEG image buffer associated with this decompressor instance
*/
public byte[] getJPEGBuf() throws Exception {
if(jpegBuf == null) throw new Exception(NO_ASSOC_ERROR);
if (jpegBuf == null)
throw new Exception(NO_ASSOC_ERROR);
return jpegBuf;
}
@@ -145,7 +149,8 @@ public class TJDecompressor {
* decompressor instance
*/
public int getJPEGSize() throws Exception {
if(jpegBufSize < 1) throw new Exception(NO_ASSOC_ERROR);
if (jpegBufSize < 1)
throw new Exception(NO_ASSOC_ERROR);
return jpegBufSize;
}
@@ -170,22 +175,24 @@ public class TJDecompressor {
* height
*/
public int getScaledWidth(int desiredWidth, int desiredHeight)
throws Exception {
if(jpegWidth < 1 || jpegHeight < 1)
throws Exception {
if (jpegWidth < 1 || jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR);
if(desiredWidth < 0 || desiredHeight < 0)
if (desiredWidth < 0 || desiredHeight < 0)
throw new Exception("Invalid argument in getScaledWidth()");
TJScalingFactor sf[] = TJ.getScalingFactors();
if(desiredWidth == 0) desiredWidth = jpegWidth;
if(desiredHeight == 0) desiredHeight = jpegHeight;
TJScalingFactor[] sf = TJ.getScalingFactors();
if (desiredWidth == 0)
desiredWidth = jpegWidth;
if (desiredHeight == 0)
desiredHeight = jpegHeight;
int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for(int i = 0; i < sf.length; i++) {
for (int i = 0; i < sf.length; i++) {
scaledWidth = sf[i].getScaled(jpegWidth);
scaledHeight = sf[i].getScaled(jpegHeight);
if(scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break;
}
if(scaledWidth > desiredWidth || scaledHeight > desiredHeight)
if (scaledWidth > desiredWidth || scaledHeight > desiredHeight)
throw new Exception("Could not scale down to desired image dimensions");
return scaledWidth;
}
@@ -210,22 +217,24 @@ public class TJDecompressor {
* height
*/
public int getScaledHeight(int desiredWidth, int desiredHeight)
throws Exception {
if(jpegWidth < 1 || jpegHeight < 1)
throws Exception {
if (jpegWidth < 1 || jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR);
if(desiredWidth < 0 || desiredHeight < 0)
if (desiredWidth < 0 || desiredHeight < 0)
throw new Exception("Invalid argument in getScaledHeight()");
TJScalingFactor sf[] = TJ.getScalingFactors();
if(desiredWidth == 0) desiredWidth = jpegWidth;
if(desiredHeight == 0) desiredHeight = jpegHeight;
TJScalingFactor[] sf = TJ.getScalingFactors();
if (desiredWidth == 0)
desiredWidth = jpegWidth;
if (desiredHeight == 0)
desiredHeight = jpegHeight;
int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for(int i = 0; i < sf.length; i++) {
for (int i = 0; i < sf.length; i++) {
scaledWidth = sf[i].getScaled(jpegWidth);
scaledHeight = sf[i].getScaled(jpegHeight);
if(scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break;
}
if(scaledWidth > desiredWidth || scaledHeight > desiredHeight)
if (scaledWidth > desiredWidth || scaledHeight > desiredHeight)
throw new Exception("Could not scale down to desired image dimensions");
return scaledHeight;
}
@@ -284,15 +293,16 @@ public class TJDecompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void decompress(byte[] dstBuf, int x, int y, int desiredWidth,
int pitch, int desiredHeight, int pixelFormat, int flags)
throws Exception {
if(jpegBuf == null) throw new Exception(NO_ASSOC_ERROR);
if(dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || pitch < 0
|| desiredHeight < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF
|| flags < 0)
int pitch, int desiredHeight, int pixelFormat,
int flags) throws Exception {
if (jpegBuf == null)
throw new Exception(NO_ASSOC_ERROR);
if (dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || pitch < 0 ||
desiredHeight < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF ||
flags < 0)
throw new Exception("Invalid argument in decompress()");
decompress(jpegBuf, jpegBufSize, dstBuf, x, y, desiredWidth, pitch,
desiredHeight, pixelFormat, flags);
desiredHeight, pixelFormat, flags);
}
/**
@@ -300,10 +310,10 @@ public class TJDecompressor {
* {@link #decompress(byte[], int, int, int, int, int, int, int)} instead.
*/
public void decompress(byte[] dstBuf, int desiredWidth, int pitch,
int desiredHeight, int pixelFormat, int flags)
throws Exception {
int desiredHeight, int pixelFormat, int flags)
throws Exception {
decompress(dstBuf, 0, 0, desiredWidth, pitch, desiredHeight, pixelFormat,
flags);
flags);
}
/**
@@ -330,14 +340,15 @@ public class TJDecompressor {
* @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
|| pixelFormat < 0 || pixelFormat >= TJ.NUMPF || flags < 0)
int pixelFormat, int flags) throws Exception {
if (desiredWidth < 0 || pitch < 0 || desiredHeight < 0 ||
pixelFormat < 0 || pixelFormat >= TJ.NUMPF || flags < 0)
throw new Exception("Invalid argument in decompress()");
int pixelSize = TJ.getPixelSize(pixelFormat);
int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
if(pitch == 0) pitch = scaledWidth * pixelSize;
if (pitch == 0)
pitch = scaledWidth * pixelSize;
byte[] buf = new byte[pitch * scaledHeight];
decompress(buf, desiredWidth, pitch, desiredHeight, pixelFormat, flags);
return buf;
@@ -361,13 +372,14 @@ public class TJDecompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void decompressToYUV(byte[] dstBuf, int flags) throws Exception {
if(jpegBuf == null) throw new Exception(NO_ASSOC_ERROR);
if(dstBuf == null || flags < 0)
if (jpegBuf == null)
throw new Exception(NO_ASSOC_ERROR);
if (dstBuf == null || flags < 0)
throw new Exception("Invalid argument in decompressToYUV()");
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
@@ -378,11 +390,11 @@ public class TJDecompressor {
* @return a buffer containing a YUV planar image
*/
public byte[] decompressToYUV(int flags) throws Exception {
if(flags < 0)
if (flags < 0)
throw new Exception("Invalid argument in decompressToYUV()");
if(jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0)
if (jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0)
throw new Exception(NO_ASSOC_ERROR);
if(jpegSubsamp >= TJ.NUMSAMP)
if (jpegSubsamp >= TJ.NUMSAMP)
throw new Exception("JPEG header information is invalid");
byte[] buf = new byte[TJ.bufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)];
decompressToYUV(buf, flags);
@@ -440,15 +452,16 @@ public class TJDecompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void decompress(int[] dstBuf, int x, int y, int desiredWidth,
int stride, int desiredHeight, int pixelFormat, int flags)
throws Exception {
if(jpegBuf == null) throw new Exception(NO_ASSOC_ERROR);
if(dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || stride < 0
|| desiredHeight < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF
|| flags < 0)
int stride, int desiredHeight, int pixelFormat,
int flags) throws Exception {
if (jpegBuf == null)
throw new Exception(NO_ASSOC_ERROR);
if (dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || stride < 0 ||
desiredHeight < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF ||
flags < 0)
throw new Exception("Invalid argument in decompress()");
decompress(jpegBuf, jpegBufSize, dstBuf, x, y, desiredWidth, stride,
desiredHeight, pixelFormat, flags);
desiredHeight, pixelFormat, flags);
}
/**
@@ -462,16 +475,16 @@ public class TJDecompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void decompress(BufferedImage dstImage, int flags) throws Exception {
if(dstImage == null || flags < 0)
if (dstImage == null || flags < 0)
throw new Exception("Invalid argument in decompress()");
int desiredWidth = dstImage.getWidth();
int desiredHeight = dstImage.getHeight();
int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
if(scaledWidth != desiredWidth || scaledHeight != desiredHeight)
if (scaledWidth != desiredWidth || scaledHeight != desiredHeight)
throw new Exception("BufferedImage dimensions do not match a scaled image size that TurboJPEG is capable of generating.");
int pixelFormat; boolean intPixels = false;
if(byteOrder == null)
if (byteOrder == null)
byteOrder = ByteOrder.nativeOrder();
switch(dstImage.getType()) {
case BufferedImage.TYPE_3BYTE_BGR:
@@ -482,20 +495,20 @@ public class TJDecompressor {
case BufferedImage.TYPE_BYTE_GRAY:
pixelFormat = TJ.PF_GRAY; break;
case BufferedImage.TYPE_INT_BGR:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XBGR;
else
pixelFormat = TJ.PF_RGBX;
intPixels = true; break;
case BufferedImage.TYPE_INT_RGB:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_XRGB;
else
pixelFormat = TJ.PF_BGRX;
intPixels = true; break;
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
if(byteOrder == ByteOrder.BIG_ENDIAN)
if (byteOrder == ByteOrder.BIG_ENDIAN)
pixelFormat = TJ.PF_ARGB;
else
pixelFormat = TJ.PF_BGRA;
@@ -504,21 +517,21 @@ public class TJDecompressor {
throw new Exception("Unsupported BufferedImage format");
}
WritableRaster wr = dstImage.getRaster();
if(intPixels) {
if (intPixels) {
SinglePixelPackedSampleModel sm =
(SinglePixelPackedSampleModel)dstImage.getSampleModel();
int pitch = sm.getScanlineStride();
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
int[] buf = db.getData();
if(jpegBuf == null) throw new Exception(NO_ASSOC_ERROR);
if (jpegBuf == null)
throw new Exception(NO_ASSOC_ERROR);
decompress(jpegBuf, jpegBufSize, buf, scaledWidth, pitch, scaledHeight,
pixelFormat, flags);
}
else {
pixelFormat, flags);
} else {
ComponentSampleModel sm =
(ComponentSampleModel)dstImage.getSampleModel();
int pixelSize = sm.getPixelStride();
if(pixelSize != TJ.getPixelSize(pixelFormat))
if (pixelSize != TJ.getPixelSize(pixelFormat))
throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
int pitch = sm.getScanlineStride();
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
@@ -550,13 +563,14 @@ public class TJDecompressor {
* decompressed image
*/
public BufferedImage decompress(int desiredWidth, int desiredHeight,
int bufferedImageType, int flags) throws Exception {
if(desiredWidth < 0 || desiredHeight < 0 || flags < 0)
int bufferedImageType, int flags)
throws Exception {
if (desiredWidth < 0 || desiredHeight < 0 || flags < 0)
throw new Exception("Invalid argument in decompress()");
int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
BufferedImage img = new BufferedImage(scaledWidth, scaledHeight,
bufferedImageType);
bufferedImageType);
decompress(img, flags);
return img;
}
@@ -571,9 +585,8 @@ public class TJDecompressor {
protected void finalize() throws Throwable {
try {
close();
}
catch(Exception e) {}
finally {
} catch(Exception e) {
} finally {
super.finalize();
}
};

View File

@@ -34,7 +34,7 @@ package org.libjpegturbo.turbojpeg;
public class TJScalingFactor {
public TJScalingFactor(int num, int denom) throws Exception {
if(num < 1 || denom < 1)
if (num < 1 || denom < 1)
throw new Exception("Numerator and denominator must be >= 1");
this.num = num;
this.denom = denom;

View File

@@ -40,54 +40,54 @@ public class TJTransform extends Rectangle {
/**
* The number of lossless transform operations
*/
final public static int NUMOP = 8;
public static final int NUMOP = 8;
/**
* Do not transform the position of the image pixels.
*/
final public static int OP_NONE = 0;
public static final int OP_NONE = 0;
/**
* Flip (mirror) image horizontally. This transform is imperfect if there
* are any partial MCU blocks on the right edge.
* @see #OPT_PERFECT
*/
final public static int OP_HFLIP = 1;
public static final int OP_HFLIP = 1;
/**
* Flip (mirror) image vertically. This transform is imperfect if there are
* any partial MCU blocks on the bottom edge.
* @see #OPT_PERFECT
*/
final public static int OP_VFLIP = 2;
public static final int OP_VFLIP = 2;
/**
* Transpose image (flip/mirror along upper left to lower right axis). This
* transform is always perfect.
* @see #OPT_PERFECT
*/
final public static int OP_TRANSPOSE = 3;
public static final int OP_TRANSPOSE = 3;
/**
* Transverse transpose image (flip/mirror along upper right to lower left
* axis). This transform is imperfect if there are any partial MCU blocks in
* the image.
* @see #OPT_PERFECT
*/
final public static int OP_TRANSVERSE = 4;
public static final int OP_TRANSVERSE = 4;
/**
* Rotate image clockwise by 90 degrees. This transform is imperfect if
* there are any partial MCU blocks on the bottom edge.
* @see #OPT_PERFECT
*/
final public static int OP_ROT90 = 5;
public static final int OP_ROT90 = 5;
/**
* Rotate image 180 degrees. This transform is imperfect if there are any
* partial MCU blocks in the image.
* @see #OPT_PERFECT
*/
final public static int OP_ROT180 = 6;
public static final int OP_ROT180 = 6;
/**
* Rotate image counter-clockwise by 90 degrees. This transform is imperfect
* if there are any partial MCU blocks on the right edge.
* @see #OPT_PERFECT
*/
final public static int OP_ROT270 = 7;
public static final int OP_ROT270 = 7;
/**
@@ -103,21 +103,21 @@ public class TJTransform extends Rectangle {
* partial MCU blocks that cannot be transformed will be left in place, which
* will create odd-looking strips on the right or bottom edge of the image.
*/
final public static int OPT_PERFECT = 1;
public static final int OPT_PERFECT = 1;
/**
* This option will discard any partial MCU blocks that cannot be
* transformed.
*/
final public static int OPT_TRIM = 2;
public static final int OPT_TRIM = 2;
/**
* This option will enable lossless cropping.
*/
final public static int OPT_CROP = 4;
public static final int OPT_CROP = 4;
/**
* This option will discard the color data in the input image and produce
* a grayscale output image.
*/
final public static int OPT_GRAY = 8;
public static final int OPT_GRAY = 8;
/**
* This option will prevent {@link TJTransformer#transform
* TJTransformer.transform()} from outputting a JPEG image for this
@@ -125,9 +125,9 @@ public class TJTransform extends Rectangle {
* filter to capture the transformed DCT coefficients without transcoding
* them.
*/
final public static int OPT_NOOUTPUT = 16;
public static final int OPT_NOOUTPUT = 16;
/**
* Create a new lossless transform instance.
*/
@@ -160,9 +160,11 @@ public class TJTransform extends Rectangle {
* TJCustomFilter} interface, or null if no custom filter is needed
*/
public TJTransform(int x, int y, int w, int h, int op, int options,
TJCustomFilter cf) throws Exception {
TJCustomFilter cf) throws Exception {
super(x, y, w, h);
this.op = op; this.options = options; this.cf = cf;
this.op = op;
this.options = options;
this.cf = cf;
}
/**
@@ -182,9 +184,11 @@ public class TJTransform extends Rectangle {
* TJCustomFilter} interface, or null if no custom filter is needed
*/
public TJTransform(Rectangle r, int op, int options,
TJCustomFilter cf) throws Exception {
TJCustomFilter cf) throws Exception {
super(r);
this.op = op; this.options = options; this.cf = cf;
this.op = op;
this.options = options;
this.cf = cf;
}
/**

View File

@@ -93,12 +93,13 @@ public class TJTransformer extends TJDecompressor {
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
*/
public void transform(byte[][] dstBufs, TJTransform[] transforms,
int flags) throws Exception {
if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
int flags) throws Exception {
if (jpegBuf == null)
throw new Exception("JPEG buffer not initialized");
transformedSizes = transform(jpegBuf, jpegBufSize, dstBufs, transforms,
flags);
flags);
}
/**
* Losslessly transform the JPEG image associated with this transformer
* instance and return an array of {@link TJDecompressor} instances, each of
@@ -116,23 +117,23 @@ public class TJTransformer extends TJDecompressor {
public TJDecompressor[] transform(TJTransform[] transforms, int flags)
throws Exception {
byte[][] dstBufs = new byte[transforms.length][];
if(jpegWidth < 1 || jpegHeight < 1)
if (jpegWidth < 1 || jpegHeight < 1)
throw new Exception("JPEG buffer not initialized");
for(int i = 0; i < transforms.length; i++) {
for (int i = 0; i < transforms.length; i++) {
int w = jpegWidth, h = jpegHeight;
if((transforms[i].options & TJTransform.OPT_CROP) != 0) {
if(transforms[i].width != 0) w = transforms[i].width;
if(transforms[i].height != 0) h = transforms[i].height;
if ((transforms[i].options & TJTransform.OPT_CROP) != 0) {
if (transforms[i].width != 0) w = transforms[i].width;
if (transforms[i].height != 0) h = transforms[i].height;
}
dstBufs[i] = new byte[TJ.bufSize(w, h, jpegSubsamp)];
}
TJDecompressor[] tjd = new TJDecompressor[transforms.length];
transform(dstBufs, transforms, flags);
for(int i = 0; i < transforms.length; i++)
for (int i = 0; i < transforms.length; i++)
tjd[i] = new TJDecompressor(dstBufs[i], transformedSizes[i]);
return tjd;
}
/**
* Returns an array containing the sizes of the transformed JPEG images from
* the most recent call to {@link #transform transform()}.
@@ -141,7 +142,7 @@ public class TJTransformer extends TJDecompressor {
* the most recent call to {@link #transform transform()}
*/
public int[] getTransformedSizes() throws Exception {
if(transformedSizes == null)
if (transformedSizes == null)
throw new Exception("No image has been transformed yet");
return transformedSizes;
}