diff --git a/ChangeLog.txt b/ChangeLog.txt index f0c804ef..e1069f5e 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -15,6 +15,10 @@ to partially decode a JPEG image. See libjpeg.txt for more details. implement the Closeable interface, so those classes can be used with a try-with-resources statement. +[4] The TurboJPEG Java classes now throw unchecked idiomatic exceptions +(IllegalArgumentException, IllegalStateException) for unrecoverable errors +caused by incorrect API usage. + 1.4.1 ===== diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJ.html b/java/doc/org/libjpegturbo/turbojpeg/TJ.html index f8342f24..3ff9b89d 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJ.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJ.html @@ -983,16 +983,13 @@ public static final int FLAG_FORCESSE3 @@ -1001,16 +998,13 @@ public static final int FLAG_FORCESSE3 @@ -1019,13 +1013,10 @@ public static final int FLAG_FORCESSE3 @@ -1034,17 +1025,14 @@ public static final int FLAG_FORCESSE3 @@ -1053,17 +1041,14 @@ public static final int FLAG_FORCESSE3 @@ -1072,17 +1057,14 @@ public static final int FLAG_FORCESSE3 diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html index 88f56443..9fe4f995 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html @@ -563,8 +563,7 @@ public void setSourceImage(byte[] srcImage, @@ -593,13 +590,10 @@ public void setSourceImage(byte[] srcImage, diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html index 73859d57..6cec2cc2 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html @@ -653,14 +653,11 @@ public void setJPEGImage(byte[] jpegImage, @@ -669,14 +666,11 @@ public void setJPEGImage(byte[] jpegImage, @@ -685,15 +679,12 @@ public void setJPEGImage(byte[] jpegImage, @@ -702,15 +693,12 @@ public void setJPEGImage(byte[] jpegImage, @@ -719,12 +707,9 @@ public void setJPEGImage(byte[] jpegImage, @@ -733,14 +718,11 @@ public void setJPEGImage(byte[] jpegImage, diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJScalingFactor.html b/java/doc/org/libjpegturbo/turbojpeg/TJScalingFactor.html index c28c00c0..d6be7271 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJScalingFactor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJScalingFactor.html @@ -192,10 +192,7 @@ extends java.lang.Object
  • TJScalingFactor

    public TJScalingFactor(int num,
    -               int denom)
    -                throws java.lang.Exception
    -
    Throws:
    -
    java.lang.Exception
    + int denom)
  • diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html b/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html index 8366afda..4d3a030c 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html @@ -341,14 +341,11 @@ extends
  • getTransformedSizes

    -
    public int[] getTransformedSizes()
    -                          throws java.lang.Exception
    +
    public int[] getTransformedSizes()
    Returns an array containing the sizes of the transformed JPEG images generated by the most recent transform operation.
    Returns:
    an array containing the sizes of the transformed JPEG images - generated by the most recent transform operation.
    -
    Throws:
    -
    java.lang.Exception
    + generated by the most recent transform operation.
  • diff --git a/java/doc/org/libjpegturbo/turbojpeg/YUVImage.html b/java/doc/org/libjpegturbo/turbojpeg/YUVImage.html index 0a3e0a51..3efa218f 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/YUVImage.html +++ b/java/doc/org/libjpegturbo/turbojpeg/YUVImage.html @@ -610,12 +610,9 @@ extends java.lang.Object
    @@ -624,12 +621,9 @@ extends java.lang.Object @@ -638,13 +632,10 @@ extends java.lang.Object @@ -653,12 +644,9 @@ extends java.lang.Object @@ -667,14 +655,11 @@ extends java.lang.Object @@ -683,13 +668,10 @@ extends java.lang.Object @@ -698,13 +680,10 @@ extends java.lang.Object @@ -713,13 +692,10 @@ extends java.lang.Object diff --git a/java/org/libjpegturbo/turbojpeg/TJ.java b/java/org/libjpegturbo/turbojpeg/TJ.java index 644a1978..5d355700 100644 --- a/java/org/libjpegturbo/turbojpeg/TJ.java +++ b/java/org/libjpegturbo/turbojpeg/TJ.java @@ -87,9 +87,8 @@ public final 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) - throw new Exception("Invalid subsampling type"); + public static int getMCUWidth(int subsamp) { + checkSubsampling(subsamp); return mcuWidth[subsamp]; } @@ -108,9 +107,8 @@ public final class TJ { * @return the MCU block height for the given level of chrominance * subsampling. */ - public static int getMCUHeight(int subsamp) throws Exception { - if (subsamp < 0 || subsamp >= NUMSAMP) - throw new Exception("Invalid subsampling type"); + public static int getMCUHeight(int subsamp) { + checkSubsampling(subsamp); return mcuHeight[subsamp]; } @@ -217,9 +215,8 @@ public final 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) - throw new Exception("Invalid pixel format"); + public static int getPixelSize(int pixelFormat) { + checkPixelFormat(pixelFormat); return pixelSize[pixelFormat]; } @@ -239,9 +236,8 @@ public final class TJ { * * @return the red offset for the given pixel format. */ - public static int getRedOffset(int pixelFormat) throws Exception { - if (pixelFormat < 0 || pixelFormat >= NUMPF) - throw new Exception("Invalid pixel format"); + public static int getRedOffset(int pixelFormat) { + checkPixelFormat(pixelFormat); return redOffset[pixelFormat]; } @@ -261,9 +257,8 @@ public final class TJ { * * @return the green offset for the given pixel format. */ - public static int getGreenOffset(int pixelFormat) throws Exception { - if (pixelFormat < 0 || pixelFormat >= NUMPF) - throw new Exception("Invalid pixel format"); + public static int getGreenOffset(int pixelFormat) { + checkPixelFormat(pixelFormat); return greenOffset[pixelFormat]; } @@ -283,9 +278,8 @@ public final class TJ { * * @return the blue offset for the given pixel format. */ - public static int getBlueOffset(int pixelFormat) throws Exception { - if (pixelFormat < 0 || pixelFormat >= NUMPF) - throw new Exception("Invalid pixel format"); + public static int getBlueOffset(int pixelFormat) { + checkPixelFormat(pixelFormat); return blueOffset[pixelFormat]; } @@ -511,4 +505,15 @@ public final class TJ { static { TJLoader.load(); } -}; + + private static void checkPixelFormat(int pixelFormat) { + if (pixelFormat < 0 || pixelFormat >= NUMPF) + throw new IllegalArgumentException("Invalid pixel format"); + } + + private static void checkSubsampling(int subsamp) { + if (subsamp < 0 || subsamp >= NUMSAMP) + throw new IllegalArgumentException("Invalid subsampling type"); + } + +} diff --git a/java/org/libjpegturbo/turbojpeg/TJCompressor.java b/java/org/libjpegturbo/turbojpeg/TJCompressor.java index 37fd6047..b7df7212 100644 --- a/java/org/libjpegturbo/turbojpeg/TJCompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJCompressor.java @@ -144,7 +144,7 @@ public class TJCompressor implements Closeable { 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()"); + throw new IllegalArgumentException("Invalid argument in setSourceImage()"); srcBuf = srcImage; srcWidth = width; if (pitch == 0) @@ -195,13 +195,13 @@ public class TJCompressor implements Closeable { int height) throws Exception { if (handle == 0) init(); if (srcImage == null || x < 0 || y < 0 || width < 0 || height < 0) - throw new Exception("Invalid argument in setSourceImage()"); + throw new IllegalArgumentException("Invalid argument in setSourceImage()"); srcX = x; srcY = y; srcWidth = (width == 0) ? srcImage.getWidth(): width; srcHeight = (height == 0) ? srcImage.getHeight() : height; if (x + width > srcImage.getWidth() || y + height > srcImage.getHeight()) - throw new Exception("Compression region exceeds the bounds of the source image"); + throw new IllegalArgumentException("Compression region exceeds the bounds of the source image"); int pixelFormat; boolean intPixels = false; @@ -230,7 +230,7 @@ public class TJCompressor implements Closeable { pixelFormat = TJ.PF_BGRX; intPixels = true; break; default: - throw new Exception("Unsupported BufferedImage format"); + throw new IllegalArgumentException("Unsupported BufferedImage format"); } srcPixelFormat = pixelFormat; @@ -247,7 +247,7 @@ public class TJCompressor implements Closeable { (ComponentSampleModel)srcImage.getSampleModel(); int pixelSize = sm.getPixelStride(); if (pixelSize != TJ.getPixelSize(pixelFormat)) - throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage"); + throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage"); srcPitch = sm.getScanlineStride(); DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); srcBuf = db.getData(); @@ -266,7 +266,7 @@ public class TJCompressor implements Closeable { public void setSourceImage(YUVImage srcImage) throws Exception { if (handle == 0) init(); if (srcImage == null) - throw new Exception("Invalid argument in setSourceImage()"); + throw new IllegalArgumentException("Invalid argument in setSourceImage()"); srcYUVImage = srcImage; srcBuf = null; srcBufInt = null; @@ -293,9 +293,9 @@ public class TJCompressor implements Closeable { * subsequent compress/encode oeprations (one of * {@link TJ#SAMP_444 TJ.SAMP_*}) */ - public void setSubsamp(int newSubsamp) throws Exception { + public void setSubsamp(int newSubsamp) { if (newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP) - throw new Exception("Invalid argument in setSubsamp()"); + throw new IllegalArgumentException("Invalid argument in setSubsamp()"); subsamp = newSubsamp; } @@ -305,9 +305,9 @@ public class TJCompressor implements Closeable { * @param quality the new JPEG image quality level (1 to 100, 1 = worst, * 100 = best) */ - public void setJPEGQuality(int quality) throws Exception { + public void setJPEGQuality(int quality) { if (quality < 1 || quality > 100) - throw new Exception("Invalid argument in setJPEGQuality()"); + throw new IllegalArgumentException("Invalid argument in setJPEGQuality()"); jpegQuality = quality; } @@ -325,13 +325,13 @@ public class TJCompressor implements Closeable { */ public void compress(byte[] dstBuf, int flags) throws Exception { if (dstBuf == null || flags < 0) - throw new Exception("Invalid argument in compress()"); + throw new IllegalArgumentException("Invalid argument in compress()"); if (srcBuf == null && srcBufInt == null && srcYUVImage == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (jpegQuality < 0) - throw new Exception("JPEG Quality not set"); + throw new IllegalStateException("JPEG Quality not set"); if (subsamp < 0 && srcYUVImage == null) - throw new Exception("Subsampling level not set"); + throw new IllegalStateException("Subsampling level not set"); if (srcYUVImage != null) compressedSize = compressFromYUV(srcYUVImage.getPlanes(), @@ -374,8 +374,7 @@ public class TJCompressor implements Closeable { * #getCompressedSize} to obtain the size of the JPEG image. */ public byte[] compress(int flags) throws Exception { - if (srcWidth < 1 || srcHeight < 1) - throw new Exception(NO_ASSOC_ERROR); + checkSourceImage(); byte[] buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)]; compress(buf, flags); return buf; @@ -420,15 +419,14 @@ public class TJCompressor implements Closeable { */ public void encodeYUV(YUVImage dstImage, int flags) throws Exception { if (dstImage == null || flags < 0) - throw new Exception("Invalid argument in encodeYUV()"); + throw new IllegalArgumentException("Invalid argument in encodeYUV()"); if (srcBuf == null && srcBufInt == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (srcYUVImage != null) - throw new Exception("Source image is not correct type"); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); + throw new IllegalStateException("Source image is not correct type"); + checkSubsampling(); if (srcWidth != dstImage.getWidth() || srcHeight != dstImage.getHeight()) - throw new Exception("Destination image is the wrong size"); + throw new IllegalStateException("Destination image is the wrong size"); if (srcBufInt != null) { encodeYUV(srcBufInt, srcX, srcY, srcWidth, srcStride, srcHeight, @@ -448,11 +446,9 @@ public class TJCompressor implements Closeable { @Deprecated public void encodeYUV(byte[] dstBuf, int flags) throws Exception { if(dstBuf == null) - throw new Exception("Invalid argument in encodeYUV()"); - if (srcWidth < 1 || srcHeight < 1) - throw new Exception(NO_ASSOC_ERROR); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); + throw new IllegalArgumentException("Invalid argument in encodeYUV()"); + checkSourceImage(); + checkSubsampling(); YUVImage yuvImage = new YUVImage(dstBuf, srcWidth, 4, srcHeight, subsamp); encodeYUV(yuvImage, flags); } @@ -475,12 +471,10 @@ public class TJCompressor implements Closeable { * @return a YUV planar image. */ public YUVImage encodeYUV(int pad, int flags) throws Exception { - if (srcWidth < 1 || srcHeight < 1) - throw new Exception(NO_ASSOC_ERROR); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); + checkSourceImage(); + checkSubsampling(); if(pad < 1 || ((pad & (pad - 1)) != 0)) - throw new Exception("Invalid argument in encodeYUV()"); + throw new IllegalStateException("Invalid argument in encodeYUV()"); YUVImage yuvImage = new YUVImage(srcWidth, pad, srcHeight, subsamp); encodeYUV(yuvImage, flags); return yuvImage; @@ -508,10 +502,8 @@ public class TJCompressor implements Closeable { * @return a YUV planar image. */ public YUVImage encodeYUV(int[] strides, int flags) throws Exception { - if (srcWidth < 1 || srcHeight < 1) - throw new Exception(NO_ASSOC_ERROR); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); + checkSourceImage(); + checkSubsampling(); YUVImage yuvImage = new YUVImage(srcWidth, strides, srcHeight, subsamp); encodeYUV(yuvImage, flags); return yuvImage; @@ -522,10 +514,8 @@ public class TJCompressor implements Closeable { */ @Deprecated public byte[] encodeYUV(int flags) throws Exception { - if (srcWidth < 1 || srcHeight < 1) - throw new Exception(NO_ASSOC_ERROR); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); + checkSourceImage(); + checkSubsampling(); YUVImage yuvImage = new YUVImage(srcWidth, 4, srcHeight, subsamp); encodeYUV(yuvImage, flags); return yuvImage.getBuf(); @@ -568,11 +558,13 @@ public class TJCompressor implements Closeable { /** * Free the native structures associated with this compressor instance. */ + @Override public void close() throws IOException { if (handle != 0) destroy(); } + @Override protected void finalize() throws Throwable { try { close(); @@ -630,6 +622,16 @@ public class TJCompressor implements Closeable { TJLoader.load(); } + private void checkSourceImage() { + if (srcWidth < 1 || srcHeight < 1) + throw new IllegalStateException(NO_ASSOC_ERROR); + } + + private void checkSubsampling() { + if (subsamp < 0) + throw new IllegalStateException("Subsampling level not set"); + } + private long handle = 0; private byte[] srcBuf = null; private int[] srcBufInt = null; @@ -646,4 +648,4 @@ public class TJCompressor implements Closeable { private int compressedSize = 0; private int yuvPad = 4; private ByteOrder byteOrder = null; -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java index 70022479..7ca656b3 100644 --- a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java @@ -98,7 +98,7 @@ public class TJDecompressor implements Closeable { public void setSourceImage(byte[] jpegImage, int imageSize) throws Exception { if (jpegImage == null || imageSize < 1) - throw new Exception("Invalid argument in setSourceImage()"); + throw new IllegalArgumentException("Invalid argument in setSourceImage()"); jpegBuf = jpegImage; jpegBufSize = imageSize; decompressHeader(jpegBuf, jpegBufSize); @@ -123,7 +123,7 @@ public class TJDecompressor implements Closeable { */ public void setSourceImage(YUVImage srcImage) throws Exception { if (srcImage == null) - throw new Exception("Invalid argument in setSourceImage()"); + throw new IllegalArgumentException("Invalid argument in setSourceImage()"); yuvImage = srcImage; jpegBuf = null; jpegBufSize = 0; @@ -137,11 +137,11 @@ public class TJDecompressor implements Closeable { * @return the width of the source image (JPEG or YUV) associated with this * decompressor instance. */ - public int getWidth() throws Exception { + public int getWidth() { if (yuvImage != null) return yuvImage.getWidth(); if (jpegWidth < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return jpegWidth; } @@ -152,11 +152,11 @@ public class TJDecompressor implements Closeable { * @return the height of the source image (JPEG or YUV) associated with this * decompressor instance. */ - public int getHeight() throws Exception { + public int getHeight() { if (yuvImage != null) return yuvImage.getHeight(); if (jpegHeight < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return jpegHeight; } @@ -168,13 +168,13 @@ public class TJDecompressor implements Closeable { * @return the level of chrominance subsampling used in the source image * (JPEG or YUV) associated with this decompressor instance. */ - public int getSubsamp() throws Exception { + public int getSubsamp() { if (yuvImage != null) return yuvImage.getSubsamp(); if (jpegSubsamp < 0) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (jpegSubsamp >= TJ.NUMSAMP) - throw new Exception("JPEG header information is invalid"); + throw new IllegalStateException("JPEG header information is invalid"); return jpegSubsamp; } @@ -186,13 +186,13 @@ public class TJDecompressor implements Closeable { * @return the colorspace used in the source image (JPEG or YUV) associated * with this decompressor instance. */ - public int getColorspace() throws Exception { + public int getColorspace() { if (yuvImage != null) return TJ.CS_YCbCr; if (jpegColorspace < 0) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (jpegColorspace >= TJ.NUMCS) - throw new Exception("JPEG header information is invalid"); + throw new IllegalStateException("JPEG header information is invalid"); return jpegColorspace; } @@ -201,9 +201,9 @@ public class TJDecompressor implements Closeable { * * @return the JPEG image buffer associated with this decompressor instance. */ - public byte[] getJPEGBuf() throws Exception { + public byte[] getJPEGBuf() { if (jpegBuf == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return jpegBuf; } @@ -214,9 +214,9 @@ public class TJDecompressor implements Closeable { * @return the size of the JPEG image (in bytes) associated with this * decompressor instance. */ - public int getJPEGSize() throws Exception { + public int getJPEGSize() { if (jpegBufSize < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return jpegBufSize; } @@ -242,9 +242,9 @@ public class TJDecompressor implements Closeable { public int getScaledWidth(int desiredWidth, int desiredHeight) throws Exception { if (jpegWidth < 1 || jpegHeight < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (desiredWidth < 0 || desiredHeight < 0) - throw new Exception("Invalid argument in getScaledWidth()"); + throw new IllegalArgumentException("Invalid argument in getScaledWidth()"); TJScalingFactor[] sf = TJ.getScalingFactors(); if (desiredWidth == 0) desiredWidth = jpegWidth; @@ -258,7 +258,7 @@ public class TJDecompressor implements Closeable { break; } if (scaledWidth > desiredWidth || scaledHeight > desiredHeight) - throw new Exception("Could not scale down to desired image dimensions"); + throw new IllegalArgumentException("Could not scale down to desired image dimensions"); return scaledWidth; } @@ -284,9 +284,9 @@ public class TJDecompressor implements Closeable { public int getScaledHeight(int desiredWidth, int desiredHeight) throws Exception { if (jpegWidth < 1 || jpegHeight < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (desiredWidth < 0 || desiredHeight < 0) - throw new Exception("Invalid argument in getScaledHeight()"); + throw new IllegalArgumentException("Invalid argument in getScaledHeight()"); TJScalingFactor[] sf = TJ.getScalingFactors(); if (desiredWidth == 0) desiredWidth = jpegWidth; @@ -300,7 +300,7 @@ public class TJDecompressor implements Closeable { break; } if (scaledWidth > desiredWidth || scaledHeight > desiredHeight) - throw new Exception("Could not scale down to desired image dimensions"); + throw new IllegalArgumentException("Could not scale down to desired image dimensions"); return scaledHeight; } @@ -372,11 +372,11 @@ public class TJDecompressor implements Closeable { int pitch, int desiredHeight, int pixelFormat, int flags) throws Exception { if (jpegBuf == null && yuvImage == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (dstBuf == null || x < 0 || y < 0 || pitch < 0 || (yuvImage != null && (desiredWidth < 0 || desiredHeight < 0)) || pixelFormat < 0 || pixelFormat >= TJ.NUMPF || flags < 0) - throw new Exception("Invalid argument in decompress()"); + throw new IllegalArgumentException("Invalid argument in decompress()"); if (yuvImage != null) decodeYUV(yuvImage.getPlanes(), yuvImage.getOffsets(), yuvImage.getStrides(), yuvImage.getSubsamp(), dstBuf, x, y, @@ -433,7 +433,7 @@ public class TJDecompressor implements Closeable { if (pitch < 0 || (yuvImage == null && (desiredWidth < 0 || desiredHeight < 0)) || pixelFormat < 0 || pixelFormat >= TJ.NUMPF || flags < 0) - throw new Exception("Invalid argument in decompress()"); + throw new IllegalArgumentException("Invalid argument in decompress()"); int pixelSize = TJ.getPixelSize(pixelFormat); int scaledWidth = getScaledWidth(desiredWidth, desiredHeight); int scaledHeight = getScaledHeight(desiredWidth, desiredHeight); @@ -464,18 +464,18 @@ public class TJDecompressor implements Closeable { */ public void decompressToYUV(YUVImage dstImage, int flags) throws Exception { if (jpegBuf == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (dstImage == null || flags < 0) - throw new Exception("Invalid argument in decompressToYUV()"); + throw new IllegalArgumentException("Invalid argument in decompressToYUV()"); int scaledWidth = getScaledWidth(dstImage.getWidth(), dstImage.getHeight()); int scaledHeight = getScaledHeight(dstImage.getWidth(), dstImage.getHeight()); if (scaledWidth != dstImage.getWidth() || scaledHeight != dstImage.getHeight()) - throw new Exception("YUVImage dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating."); + throw new IllegalArgumentException("YUVImage dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating."); if (jpegSubsamp != dstImage.getSubsamp()) - throw new Exception("YUVImage subsampling level does not match that of the JPEG image"); + throw new IllegalArgumentException("YUVImage subsampling level does not match that of the JPEG image"); decompressToYUV(jpegBuf, jpegBufSize, dstImage.getPlanes(), dstImage.getOffsets(), dstImage.getWidth(), @@ -534,13 +534,13 @@ public class TJDecompressor implements Closeable { int desiredHeight, int flags) throws Exception { if (flags < 0) - throw new Exception("Invalid argument in decompressToYUV()"); + throw new IllegalArgumentException("Invalid argument in decompressToYUV()"); if (jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (jpegSubsamp >= TJ.NUMSAMP) - throw new Exception("JPEG header information is invalid"); + throw new IllegalStateException("JPEG header information is invalid"); if (yuvImage != null) - throw new Exception("Source image is the wrong type"); + throw new IllegalStateException("Source image is the wrong type"); int scaledWidth = getScaledWidth(desiredWidth, desiredHeight); int scaledHeight = getScaledHeight(desiredWidth, desiredHeight); @@ -587,13 +587,13 @@ public class TJDecompressor implements Closeable { public YUVImage decompressToYUV(int desiredWidth, int pad, int desiredHeight, int flags) throws Exception { if (flags < 0) - throw new Exception("Invalid argument in decompressToYUV()"); + throw new IllegalArgumentException("Invalid argument in decompressToYUV()"); if (jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (jpegSubsamp >= TJ.NUMSAMP) - throw new Exception("JPEG header information is invalid"); + throw new IllegalStateException("JPEG header information is invalid"); if (yuvImage != null) - throw new Exception("Source image is the wrong type"); + throw new IllegalStateException("Source image is the wrong type"); int scaledWidth = getScaledWidth(desiredWidth, desiredHeight); int scaledHeight = getScaledHeight(desiredWidth, desiredHeight); @@ -679,11 +679,11 @@ public class TJDecompressor implements Closeable { int stride, int desiredHeight, int pixelFormat, int flags) throws Exception { if (jpegBuf == null && yuvImage == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (dstBuf == null || x < 0 || y < 0 || stride < 0 || (yuvImage != null && (desiredWidth < 0 || desiredHeight < 0)) || pixelFormat < 0 || pixelFormat >= TJ.NUMPF || flags < 0) - throw new Exception("Invalid argument in decompress()"); + throw new IllegalArgumentException("Invalid argument in decompress()"); if (yuvImage != null) decodeYUV(yuvImage.getPlanes(), yuvImage.getOffsets(), yuvImage.getStrides(), yuvImage.getSubsamp(), dstBuf, x, y, @@ -712,7 +712,7 @@ public class TJDecompressor implements Closeable { */ public void decompress(BufferedImage dstImage, int flags) throws Exception { if (dstImage == null || flags < 0) - throw new Exception("Invalid argument in decompress()"); + throw new IllegalArgumentException("Invalid argument in decompress()"); int desiredWidth = dstImage.getWidth(); int desiredHeight = dstImage.getHeight(); int scaledWidth, scaledHeight; @@ -720,14 +720,14 @@ public class TJDecompressor implements Closeable { if (yuvImage != null) { if (desiredWidth != yuvImage.getWidth() || desiredHeight != yuvImage.getHeight()) - throw new Exception("BufferedImage dimensions do not match the dimensions of the source image."); + throw new IllegalArgumentException("BufferedImage dimensions do not match the dimensions of the source image."); scaledWidth = yuvImage.getWidth(); scaledHeight = yuvImage.getHeight(); } else { scaledWidth = getScaledWidth(desiredWidth, desiredHeight); scaledHeight = getScaledHeight(desiredWidth, desiredHeight); if (scaledWidth != desiredWidth || scaledHeight != desiredHeight) - throw new Exception("BufferedImage dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating."); + throw new IllegalArgumentException("BufferedImage dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating."); } int pixelFormat; boolean intPixels = false; if (byteOrder == null) @@ -760,7 +760,7 @@ public class TJDecompressor implements Closeable { pixelFormat = TJ.PF_BGRA; intPixels = true; break; default: - throw new Exception("Unsupported BufferedImage format"); + throw new IllegalArgumentException("Unsupported BufferedImage format"); } WritableRaster wr = dstImage.getRaster(); if (intPixels) { @@ -776,7 +776,7 @@ public class TJDecompressor implements Closeable { pixelFormat, flags); else { if (jpegBuf == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); decompress(jpegBuf, jpegBufSize, buf, 0, 0, scaledWidth, stride, scaledHeight, pixelFormat, flags); } @@ -785,7 +785,7 @@ public class TJDecompressor implements Closeable { (ComponentSampleModel)dstImage.getSampleModel(); int pixelSize = sm.getPixelStride(); if (pixelSize != TJ.getPixelSize(pixelFormat)) - throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage"); + throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage"); int pitch = sm.getScanlineStride(); DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); byte[] buf = db.getData(); @@ -822,7 +822,7 @@ public class TJDecompressor implements Closeable { throws Exception { if ((yuvImage == null && (desiredWidth < 0 || desiredHeight < 0)) || flags < 0) - throw new Exception("Invalid argument in decompress()"); + throw new IllegalArgumentException("Invalid argument in decompress()"); int scaledWidth = getScaledWidth(desiredWidth, desiredHeight); int scaledHeight = getScaledHeight(desiredWidth, desiredHeight); BufferedImage img = new BufferedImage(scaledWidth, scaledHeight, @@ -834,11 +834,13 @@ public class TJDecompressor implements Closeable { /** * Free the native structures associated with this decompressor instance. */ + @Override public void close() throws IOException { if (handle != 0) destroy(); } + @Override protected void finalize() throws Throwable { try { close(); @@ -899,4 +901,4 @@ public class TJDecompressor implements Closeable { protected int jpegSubsamp = -1; protected int jpegColorspace = -1; private ByteOrder byteOrder = null; -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/TJLoader.java.in b/java/org/libjpegturbo/turbojpeg/TJLoader.java.in index 22353a5d..83977806 100644 --- a/java/org/libjpegturbo/turbojpeg/TJLoader.java.in +++ b/java/org/libjpegturbo/turbojpeg/TJLoader.java.in @@ -32,4 +32,4 @@ final class TJLoader { static void load() { System.loadLibrary("@TURBOJPEG_DLL_NAME@"); } -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/TJLoader.java.tmpl b/java/org/libjpegturbo/turbojpeg/TJLoader.java.tmpl index a4f1c876..5ef31182 100644 --- a/java/org/libjpegturbo/turbojpeg/TJLoader.java.tmpl +++ b/java/org/libjpegturbo/turbojpeg/TJLoader.java.tmpl @@ -56,4 +56,4 @@ final class TJLoader { } } } -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/TJScalingFactor.java b/java/org/libjpegturbo/turbojpeg/TJScalingFactor.java index e00fdf7d..069d15a6 100644 --- a/java/org/libjpegturbo/turbojpeg/TJScalingFactor.java +++ b/java/org/libjpegturbo/turbojpeg/TJScalingFactor.java @@ -33,9 +33,9 @@ package org.libjpegturbo.turbojpeg; */ public class TJScalingFactor { - public TJScalingFactor(int num, int denom) throws Exception { + public TJScalingFactor(int num, int denom) { if (num < 1 || denom < 1) - throw new Exception("Numerator and denominator must be >= 1"); + throw new IllegalArgumentException("Numerator and denominator must be >= 1"); this.num = num; this.denom = denom; } @@ -77,7 +77,7 @@ public class TJScalingFactor { * other have the same numerator and denominator. */ public boolean equals(TJScalingFactor other) { - return (this.num == other.num && this.denom == other.denom); + return this.num == other.num && this.denom == other.denom; } /** @@ -88,7 +88,7 @@ public class TJScalingFactor { * 1/1. */ public boolean isOne() { - return (num == 1 && denom == 1); + return num == 1 && denom == 1; } /** @@ -100,4 +100,4 @@ public class TJScalingFactor { * Denominator */ private int denom = 1; -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/TJTransformer.java b/java/org/libjpegturbo/turbojpeg/TJTransformer.java index 2e173445..6d3d81d3 100644 --- a/java/org/libjpegturbo/turbojpeg/TJTransformer.java +++ b/java/org/libjpegturbo/turbojpeg/TJTransformer.java @@ -96,7 +96,7 @@ public class TJTransformer extends TJDecompressor { public void transform(byte[][] dstBufs, TJTransform[] transforms, int flags) throws Exception { if (jpegBuf == null) - throw new Exception("JPEG buffer not initialized"); + throw new IllegalStateException("JPEG buffer not initialized"); transformedSizes = transform(jpegBuf, jpegBufSize, dstBufs, transforms, flags); } @@ -120,7 +120,7 @@ public class TJTransformer extends TJDecompressor { throws Exception { byte[][] dstBufs = new byte[transforms.length][]; if (jpegWidth < 1 || jpegHeight < 1) - throw new Exception("JPEG buffer not initialized"); + throw new IllegalStateException("JPEG buffer not initialized"); for (int i = 0; i < transforms.length; i++) { int w = jpegWidth, h = jpegHeight; if ((transforms[i].options & TJTransform.OPT_CROP) != 0) { @@ -143,9 +143,9 @@ public class TJTransformer extends TJDecompressor { * @return an array containing the sizes of the transformed JPEG images * generated by the most recent transform operation. */ - public int[] getTransformedSizes() throws Exception { + public int[] getTransformedSizes() { if (transformedSizes == null) - throw new Exception("No image has been transformed yet"); + throw new IllegalStateException("No image has been transformed yet"); return transformedSizes; } @@ -159,4 +159,4 @@ public class TJTransformer extends TJDecompressor { } private int[] transformedSizes = null; -}; +} diff --git a/java/org/libjpegturbo/turbojpeg/YUVImage.java b/java/org/libjpegturbo/turbojpeg/YUVImage.java index 2d790e93..44a81c70 100644 --- a/java/org/libjpegturbo/turbojpeg/YUVImage.java +++ b/java/org/libjpegturbo/turbojpeg/YUVImage.java @@ -218,12 +218,12 @@ public class YUVImage { int height, int subsamp, boolean alloc) throws Exception { if ((planes == null && !alloc) || width < 1 || height < 1 || subsamp < 0 || subsamp >= TJ.NUMSAMP) - throw new Exception("Invalid argument in YUVImage::setBuf()"); + throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()"); int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3); if (planes.length != nc || (offsets != null && offsets.length != nc) || (strides != null && strides.length != nc)) - throw new Exception("YUVImage::setBuf(): planes, offsets, or strides array is the wrong size"); + throw new IllegalArgumentException("YUVImage::setBuf(): planes, offsets, or strides array is the wrong size"); if (offsets == null) offsets = new int[nc]; @@ -239,15 +239,15 @@ public class YUVImage { strides[i] = pw; if (alloc) { if (strides[i] < pw) - throw new Exception("Stride must be >= plane width when allocating a new YUV image"); + throw new IllegalArgumentException("Stride must be >= plane width when allocating a new YUV image"); planes[i] = new byte[strides[i] * ph]; } if (planes[i] == null || offsets[i] < 0) - throw new Exception("Invalid argument in YUVImage::setBuf()"); + throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()"); if (strides[i] < 0 && offsets[i] - planeSize + pw < 0) - throw new Exception("Stride for plane " + i + " would cause memory to be accessed below plane boundary"); + throw new IllegalArgumentException("Stride for plane " + i + " would cause memory to be accessed below plane boundary"); if (planes[i].length < offsets[i] + planeSize) - throw new Exception("Image plane " + i + " is not large enough"); + throw new IllegalArgumentException("Image plane " + i + " is not large enough"); } yuvPlanes = planes; @@ -282,10 +282,10 @@ public class YUVImage { int subsamp) throws Exception { if (yuvImage == null || width < 1 || pad < 1 || ((pad & (pad - 1)) != 0) || height < 1 || subsamp < 0 || subsamp >= TJ.NUMSAMP) - throw new Exception("Invalid argument in YUVImage::setBuf()"); + throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()"); if (yuvImage.length < TJ.bufSizeYUV(width, pad, height, subsamp)) - throw new Exception("YUV image buffer is not large enough"); - + throw new IllegalArgumentException("YUV image buffer is not large enough"); + int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3); byte[][] planes = new byte[nc][]; int[] strides = new int[nc]; @@ -311,9 +311,9 @@ public class YUVImage { * * @return the width of the YUV image (or subregion) */ - public int getWidth() throws Exception { + public int getWidth() { if (yuvWidth < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvWidth; } @@ -322,9 +322,9 @@ public class YUVImage { * * @return the height of the YUV image (or subregion) */ - public int getHeight() throws Exception { + public int getHeight() { if (yuvHeight < 1) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvHeight; } @@ -334,11 +334,11 @@ public class YUVImage { * * @return the line padding used in the YUV image buffer */ - public int getPad() throws Exception { + public int getPad() { if (yuvPlanes == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); if (yuvPad < 1 || ((yuvPad & (yuvPad - 1)) != 0)) - throw new Exception("Image is not stored in a unified buffer"); + throw new IllegalStateException("Image is not stored in a unified buffer"); return yuvPad; } @@ -347,9 +347,9 @@ public class YUVImage { * * @return the number of bytes per line of each plane in the YUV image */ - public int[] getStrides() throws Exception { + public int[] getStrides() { if (yuvStrides == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvStrides; } @@ -360,9 +360,9 @@ public class YUVImage { * @return the offsets (in bytes) of each plane within the planes of a larger * YUV image */ - public int[] getOffsets() throws Exception { + public int[] getOffsets() { if (yuvOffsets == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvOffsets; } @@ -372,9 +372,9 @@ public class YUVImage { * * @return the level of chrominance subsampling used in the YUV image */ - public int getSubsamp() throws Exception { + public int getSubsamp() { if (yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvSubsamp; } @@ -384,9 +384,9 @@ public class YUVImage { * * @return the YUV image planes */ - public byte[][] getPlanes() throws Exception { + public byte[][] getPlanes() { if (yuvPlanes == null) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); return yuvPlanes; } @@ -396,13 +396,13 @@ public class YUVImage { * * @return the YUV image buffer */ - public byte[] getBuf() throws Exception { + public byte[] getBuf() { if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3); for (int i = 1; i < nc; i++) { if (yuvPlanes[i] != yuvPlanes[0]) - throw new Exception("Image is not stored in a unified buffer"); + throw new IllegalStateException("Image is not stored in a unified buffer"); } return yuvPlanes[0]; } @@ -415,13 +415,13 @@ public class YUVImage { */ public int getSize() throws Exception { if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP) - throw new Exception(NO_ASSOC_ERROR); + throw new IllegalStateException(NO_ASSOC_ERROR); int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3); if (yuvPad < 1) - throw new Exception("Image is not stored in a unified buffer"); + throw new IllegalStateException("Image is not stored in a unified buffer"); for (int i = 1; i < nc; i++) { if (yuvPlanes[i] != yuvPlanes[0]) - throw new Exception("Image is not stored in a unified buffer"); + throw new IllegalStateException("Image is not stored in a unified buffer"); } return TJ.bufSizeYUV(yuvWidth, yuvPad, yuvHeight, yuvSubsamp); } @@ -438,4 +438,4 @@ public class YUVImage { protected int yuvWidth = 0; protected int yuvHeight = 0; protected int yuvSubsamp = -1; -}; +} diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c index 04e2dc3a..0d0b80c5 100644 --- a/turbojpeg-jni.c +++ b/turbojpeg-jni.c @@ -39,30 +39,29 @@ #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) -#define _throw(msg) { \ - jclass _exccls=(*env)->FindClass(env, "java/lang/Exception"); \ - if(!_exccls) goto bailout; \ +#define _throw(msg, exceptionClass) { \ + jclass _exccls=(*env)->FindClass(env, exceptionClass); \ + if(!_exccls || (*env)->ExceptionCheck(env)) goto bailout; \ (*env)->ThrowNew(env, _exccls, msg); \ goto bailout; \ } -#define _throwio(msg) { \ - jclass _exccls=(*env)->FindClass(env, "java/io/IOException"); \ - if(!_exccls) goto bailout; \ - (*env)->ThrowNew(env, _exccls, msg); \ - goto bailout; \ -} +#define _throwio(msg) _throw(msg, "java/io/IOException") -#define bailif0(f) {if(!(f)) { \ - char temps[80]; \ - snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \ - _throw(temps); \ +#define _throwtj() _throw(tjGetErrorStr(), "java/lang/Exception") + +#define _throwarg(msg) _throw(msg, "java/lang/IllegalArgumentException") + +#define _throwmem() _throw("Memory allocation failure", "java/lang/OutOfMemoryError"); + +#define bailif0(f) {if(!(f) || (*env)->ExceptionCheck(env)) { \ + goto bailout; \ }} #define gethandle() \ jclass _cls=(*env)->GetObjectClass(env, obj); \ jfieldID _fid; \ - if(!_cls) goto bailout; \ + if(!_cls || (*env)->ExceptionCheck(env)) goto bailout; \ bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \ handle=(tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid); \ @@ -108,7 +107,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp) { jint retval=(jint)tjBufSize(width, height, jpegSubsamp); - if(retval==-1) _throw(tjGetErrorStr()); + if(retval==-1) _throwtj(); bailout: return retval; @@ -119,7 +118,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp) { jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp); - if(retval==-1) _throw(tjGetErrorStr()); + if(retval==-1) _throwtj(); bailout: return retval; @@ -140,7 +139,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII { jint retval=(jint)tjPlaneSizeYUV(componentID, width, stride, height, subsamp); - if(retval==-1) _throw(tjGetErrorStr()); + if(retval==-1) _throwtj(); bailout: return retval; @@ -151,7 +150,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III (JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp) { jint retval=(jint)tjPlaneWidth(componentID, width, subsamp); - if(retval==-1) _throw(tjGetErrorStr()); + if(retval==-1) _throwtj(); bailout: return retval; @@ -162,7 +161,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III (JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp) { jint retval=(jint)tjPlaneHeight(componentID, height, subsamp); - if(retval==-1) _throw(tjGetErrorStr()); + if(retval==-1) _throwtj(); bailout: return retval; @@ -177,7 +176,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init tjhandle handle; if((handle=tjInitCompress())==NULL) - _throw(tjGetErrorStr()); + _throwtj(); bailif0(cls=(*env)->GetObjectClass(env, obj)); bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); @@ -201,17 +200,17 @@ static jint TJCompressor_compress if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) - _throw("Invalid argument in compress()"); + _throwarg("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; if((*env)->GetArrayLength(env, src)*srcElementSizeGetArrayLength(env, dst)<(jsize)jpegSize) - _throw("Destination buffer is not large enough"); + _throwarg("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); @@ -221,7 +220,7 @@ static jint TJCompressor_compress if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); @@ -256,9 +255,9 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 jint jpegQual, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in compress()"); + _throwarg("Invalid argument in compress()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when compressing from an integer buffer."); + _throwarg("Pixel format must be 32-bit when compressing from an integer buffer."); return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags); @@ -274,9 +273,9 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in compress()"); + _throwarg("Invalid argument in compress()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when compressing from an integer buffer."); + _throwarg("Pixel format must be 32-bit when compressing from an integer buffer."); return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width, stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags); @@ -301,20 +300,20 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFrom gethandle(); if(subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) - _throw("Invalid argument in compressFromYUV()"); + _throwarg("Invalid argument in compressFromYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, srcobjs)GetArrayLength(env, jSrcOffsets)GetArrayLength(env, jSrcStrides)GetArrayLength(env, dst)<(jsize)jpegSize) - _throw("Destination buffer is not large enough"); + _throwarg("Destination buffer is not large enough"); bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0)); bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); @@ -324,16 +323,16 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFrom int pw=tjPlaneWidth(i, width, subsamp); if(planeSize<0 || pw<0) - _throw(tjGetErrorStr()); + _throwtj(); if(srcOffsets[i]<0) - _throw("Invalid argument in compressFromYUV()"); + _throwarg("Invalid argument in compressFromYUV()"); if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) - _throw("Negative plane stride would cause memory to be accessed below plane boundary"); + _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); if((*env)->GetArrayLength(env, jSrcPlanes[i])GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0)); @@ -345,7 +344,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFrom if(tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height, subsamp, &jpegBuf, &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); @@ -378,22 +377,22 @@ static void TJCompressor_encodeYUV if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0 || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) - _throw("Invalid argument in encodeYUV()"); + _throwarg("Invalid argument in encodeYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, dstobjs)GetArrayLength(env, jDstOffsets)GetArrayLength(env, jDstStrides)GetArrayLength(env, src)*srcElementSizeGetPrimitiveArrayCritical(env, jDstOffsets, 0)); bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0)); @@ -403,16 +402,16 @@ static void TJCompressor_encodeYUV int pw=tjPlaneWidth(i, width, subsamp); if(planeSize<0 || pw<0) - _throw(tjGetErrorStr()); + _throwtj(); if(dstOffsets[i]<0) - _throw("Invalid argument in encodeYUV()"); + _throwarg("Invalid argument in encodeYUV()"); if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0) - _throw("Negative plane stride would cause memory to be accessed below plane boundary"); + _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); if((*env)->GetArrayLength(env, jDstPlanes[i])GetPrimitiveArrayCritical(env, jDstPlanes[i], 0)); @@ -422,7 +421,7 @@ static void TJCompressor_encodeYUV if(tjEncodeYUVPlanes(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, dstPlanes, dstStrides, subsamp, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); @@ -456,9 +455,9 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in encodeYUV()"); + _throwarg("Invalid argument in encodeYUV()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when encoding from an integer buffer."); + _throwarg("Pixel format must be 32-bit when encoding from an integer buffer."); TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf, dstobjs, jDstOffsets, jDstStrides, @@ -480,23 +479,23 @@ JNIEXPORT void JNICALL TJCompressor_encodeYUV_12 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) - _throw("Invalid argument in encodeYUV()"); + _throwarg("Invalid argument in encodeYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; if((*env)->GetArrayLength(env, src)*srcElementSizeGetArrayLength(env, dst) <(jsize)tjBufSizeYUV(width, height, subsamp)) - _throw("Destination buffer is not large enough"); + _throwarg("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); @@ -519,9 +518,9 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in encodeYUV()"); + _throwarg("Invalid argument in encodeYUV()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when encoding from an integer buffer."); + _throwarg("Pixel format must be 32-bit when encoding from an integer buffer."); TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width, stride*sizeof(jint), height, pf, dst, subsamp, flags); @@ -553,7 +552,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init jfieldID fid; tjhandle handle; - if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr()); + if((handle=tjInitDecompress())==NULL) _throwtj(); bailif0(cls=(*env)->GetObjectClass(env, obj)); bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); @@ -573,7 +572,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFact jobjectArray sfjava=NULL; if((sf=tjGetScalingFactors(&n))==NULL || n==0) - _throw(tjGetErrorStr()); + _throwtj(); bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor")); bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0)); @@ -603,13 +602,13 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress gethandle(); if((*env)->GetArrayLength(env, src)GetPrimitiveArrayCritical(env, src, 0)); if(tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize, &width, &height, &jpegSubsamp, &jpegColorspace)==-1) - _throw(tjGetErrorStr()); + _throwtj(); (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL; @@ -641,16 +640,16 @@ static void TJDecompressor_decompress gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in decompress()"); + _throwarg("Invalid argument in decompress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, src)GetArrayLength(env, dst)*dstElementSizeGetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); @@ -658,7 +657,7 @@ static void TJDecompressor_decompress if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); @@ -690,9 +689,9 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in decompress()"); + _throwarg("Invalid argument in decompress()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); + _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer."); TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf, flags); @@ -707,9 +706,9 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress jint width, jint stride, jint height, jint pf, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in decompress()"); + _throwarg("Invalid argument in decompress()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); + _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer."); TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0, width, stride*sizeof(jint), height, pf, flags); @@ -737,7 +736,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress gethandle(); if((*env)->GetArrayLength(env, src)GetFieldID(env, _cls, "jpegSubsamp", "I")); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); @@ -752,7 +751,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress if(height==0) height=jpegHeight; sf=tjGetScalingFactors(&nsf); if(!sf || nsf<1) - _throw(tjGetErrorStr()); + _throwtj(); for(i=0; iGetObjectArrayElement(env, dstobjs, i)); if((*env)->GetArrayLength(env, jDstPlanes[i])GetPrimitiveArrayCritical(env, jDstPlanes[i], 0)); @@ -789,7 +788,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress if(tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize, dstPlanes, desiredWidth, dstStrides, desiredHeight, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); @@ -818,7 +817,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress gethandle(); if((*env)->GetArrayLength(env, src)GetFieldID(env, _cls, "jpegSubsamp", "I")); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); @@ -827,14 +826,14 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); if((*env)->GetArrayLength(env, dst) <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)) - _throw("Destination buffer is not large enough"); + _throwarg("Destination buffer is not large enough"); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); @@ -858,22 +857,22 @@ static void TJDecompressor_decodeYUV if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) - _throw("Invalid argument in decodeYUV()"); + _throwarg("Invalid argument in decodeYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) - _throw("Mismatch between Java and C API"); + _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, srcobjs)GetArrayLength(env, jSrcOffsets)GetArrayLength(env, jSrcStrides)GetArrayLength(env, dst)*dstElementSizeGetPrimitiveArrayCritical(env, jSrcOffsets, 0)); bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); @@ -883,16 +882,16 @@ static void TJDecompressor_decodeYUV int pw=tjPlaneWidth(i, width, subsamp); if(planeSize<0 || pw<0) - _throw(tjGetErrorStr()); + _throwtj(); if(srcOffsets[i]<0) - _throw("Invalid argument in decodeYUV()"); + _throwarg("Invalid argument in decodeYUV()"); if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) - _throw("Negative plane stride would cause memory to be accessed below plane boundary"); + _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); if((*env)->GetArrayLength(env, jSrcPlanes[i])GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0)); @@ -903,7 +902,7 @@ static void TJDecompressor_decodeYUV if(tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp, &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, flags)==-1) - _throw(tjGetErrorStr()); + _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); @@ -937,9 +936,9 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV_ jint width, jint stride, jint height, jint pf, jint flags) { if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) - _throw("Invalid argument in decodeYUV()"); + _throwarg("Invalid argument in decodeYUV()"); if(tjPixelSize[pf]!=sizeof(jint)) - _throw("Pixel format must be 32-bit when decoding to an integer buffer."); + _throwarg("Pixel format must be 32-bit when decoding to an integer buffer."); TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides, subsamp, dst, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf, @@ -957,7 +956,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init jfieldID fid; tjhandle handle; - if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); + if((handle=tjInitTransform())==NULL) _throwtj(); bailif0(cls=(*env)->GetObjectClass(env, obj)); bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); @@ -1047,7 +1046,7 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf gethandle(); if((*env)->GetArrayLength(env, jsrcBuf)GetFieldID(env, _cls, "jpegWidth", "I")); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); @@ -1057,19 +1056,19 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf n=(*env)->GetArrayLength(env, dstobjs); if(n!=(*env)->GetArrayLength(env, tobjs)) - _throw("Mismatch between size of transforms array and destination buffers array"); + _throwarg("Mismatch between size of transforms array and destination buffers array"); if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) - _throw("Memory allocation failure"); + _throwmem(); if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) - _throw("Memory allocation failure"); + _throwmem(); if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) - _throw("Memory allocation failure"); + _throwmem(); if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) - _throw("Memory allocation failure"); + _throwmem(); if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n)) ==NULL) - _throw("Memory allocation failure"); + _throwmem(); for(i=0; iGetObjectArrayElement(env, dstobjs, i)); if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) GetPrimitiveArrayCritical(env, jsrcBuf, 0)); for(i=0; i