From 739edeb8a647d428514da522d730a955ff0c1c5c Mon Sep 17 00:00:00 2001 From: DRC Date: Tue, 21 Jul 2015 09:34:02 -0500 Subject: [PATCH] Further exception cleanup Use a new checked exception type (TJException) when passing through errors from the underlying C library. This gives the application a choice of catching all exceptions or just those from TurboJPEG. Throw IllegalArgumentException at the JNI level when arguments to the JNI function are incorrect, and when one of the TurboJPEG "utility" functions returns an error (because, per the C API specification, those functions will only return an error if one of their arguments is out of range.) Remove "throws Exception" from the signature of any methods that no longer pass through an error from the TurboJPEG C library. Credit Viktor for the new code Code formatting tweaks --- ChangeLog.txt | 3 +- java/CMakeLists.txt | 1 + java/Makefile.am | 2 + java/TJExample.java | 4 +- java/doc/allclasses-frame.html | 1 + java/doc/allclasses-noframe.html | 1 + java/doc/index-all.html | 10 + java/doc/org/libjpegturbo/turbojpeg/TJ.html | 47 +-- .../libjpegturbo/turbojpeg/TJCompressor.html | 80 ++--- .../turbojpeg/TJCustomFilter.html | 4 +- .../turbojpeg/TJDecompressor.html | 97 +++--- .../libjpegturbo/turbojpeg/TJException.html | 287 ++++++++++++++++++ .../turbojpeg/TJScalingFactor.html | 4 +- .../libjpegturbo/turbojpeg/TJTransform.html | 14 +- .../libjpegturbo/turbojpeg/TJTransformer.html | 20 +- .../org/libjpegturbo/turbojpeg/YUVImage.html | 49 +-- .../libjpegturbo/turbojpeg/package-frame.html | 4 + .../turbojpeg/package-summary.html | 15 + .../libjpegturbo/turbojpeg/package-tree.html | 13 + java/doc/overview-tree.html | 13 + java/doc/serialized-form.html | 9 + java/org/libjpegturbo/turbojpeg/TJ.java | 22 +- .../libjpegturbo/turbojpeg/TJCompressor.java | 71 +++-- .../turbojpeg/TJCustomFilter.java | 2 +- .../turbojpeg/TJDecompressor.java | 75 ++--- .../libjpegturbo/turbojpeg/TJException.java | 53 ++++ .../turbojpeg/TJScalingFactor.java | 1 + .../libjpegturbo/turbojpeg/TJTransform.java | 4 +- .../libjpegturbo/turbojpeg/TJTransformer.java | 15 +- java/org/libjpegturbo/turbojpeg/YUVImage.java | 19 +- turbojpeg-jni.c | 28 +- 31 files changed, 663 insertions(+), 305 deletions(-) create mode 100644 java/doc/org/libjpegturbo/turbojpeg/TJException.html create mode 100644 java/org/libjpegturbo/turbojpeg/TJException.java diff --git a/ChangeLog.txt b/ChangeLog.txt index e1069f5..65e64de 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -17,7 +17,8 @@ try-with-resources statement. [4] The TurboJPEG Java classes now throw unchecked idiomatic exceptions (IllegalArgumentException, IllegalStateException) for unrecoverable errors -caused by incorrect API usage. +caused by incorrect API usage, and those classes throw a new checked exception +type (TJException) for errors that are passed through from the C library. 1.4.1 diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt index f9f9e57..0af8ae1 100644 --- a/java/CMakeLists.txt +++ b/java/CMakeLists.txt @@ -5,6 +5,7 @@ set(JAVA_CLASSNAMES org/libjpegturbo/turbojpeg/TJ org/libjpegturbo/turbojpeg/TJCompressor org/libjpegturbo/turbojpeg/TJCustomFilter org/libjpegturbo/turbojpeg/TJDecompressor + org/libjpegturbo/turbojpeg/TJException org/libjpegturbo/turbojpeg/TJScalingFactor org/libjpegturbo/turbojpeg/TJTransform org/libjpegturbo/turbojpeg/TJTransformer diff --git a/java/Makefile.am b/java/Makefile.am index 23e3412..475dd3f 100644 --- a/java/Makefile.am +++ b/java/Makefile.am @@ -10,6 +10,7 @@ JAVASOURCES = org/libjpegturbo/turbojpeg/TJ.java \ org/libjpegturbo/turbojpeg/TJCompressor.java \ org/libjpegturbo/turbojpeg/TJCustomFilter.java \ org/libjpegturbo/turbojpeg/TJDecompressor.java \ + org/libjpegturbo/turbojpeg/TJException.java \ org/libjpegturbo/turbojpeg/TJScalingFactor.java \ org/libjpegturbo/turbojpeg/TJTransform.java \ org/libjpegturbo/turbojpeg/TJTransformer.java \ @@ -31,6 +32,7 @@ JAVA_CLASSES = org/libjpegturbo/turbojpeg/TJ.class \ org/libjpegturbo/turbojpeg/TJCompressor.class \ org/libjpegturbo/turbojpeg/TJCustomFilter.class \ org/libjpegturbo/turbojpeg/TJDecompressor.class \ + org/libjpegturbo/turbojpeg/TJException.class \ org/libjpegturbo/turbojpeg/TJLoader.class \ org/libjpegturbo/turbojpeg/TJScalingFactor.class \ org/libjpegturbo/turbojpeg/TJTransform.class \ diff --git a/java/TJExample.java b/java/TJExample.java index 7562114..da09807 100644 --- a/java/TJExample.java +++ b/java/TJExample.java @@ -1,5 +1,5 @@ /* - * Copyright (C)2011-2012, 2014 D. R. Commander. All Rights Reserved. + * Copyright (C)2011-2012, 2014-2015 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -351,7 +351,7 @@ public class TJExample implements TJCustomFilter { public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion, Rectangle planeRegion, int componentIndex, int transformIndex, TJTransform transform) - throws Exception { + throws TJException { for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) { coeffBuffer.put(i, (short)(-coeffBuffer.get(i))); } diff --git a/java/doc/allclasses-frame.html b/java/doc/allclasses-frame.html index 509ea50..fecac06 100644 --- a/java/doc/allclasses-frame.html +++ b/java/doc/allclasses-frame.html @@ -13,6 +13,7 @@
  • TJCompressor
  • TJCustomFilter
  • TJDecompressor
  • +
  • TJException
  • TJScalingFactor
  • TJTransform
  • TJTransformer
  • diff --git a/java/doc/allclasses-noframe.html b/java/doc/allclasses-noframe.html index 3eac18f..1f7fd3c 100644 --- a/java/doc/allclasses-noframe.html +++ b/java/doc/allclasses-noframe.html @@ -13,6 +13,7 @@
  • TJCompressor
  • TJCustomFilter
  • TJDecompressor
  • +
  • TJException
  • TJScalingFactor
  • TJTransform
  • TJTransformer
  • diff --git a/java/doc/index-all.html b/java/doc/index-all.html index 1af78be..a02d9c4 100644 --- a/java/doc/index-all.html +++ b/java/doc/index-all.html @@ -821,6 +821,16 @@ source image stored in yuvImage with the newly created instance. +
    TJException - Exception in org.libjpegturbo.turbojpeg
    +
     
    +
    TJException() - Constructor for exception org.libjpegturbo.turbojpeg.TJException
    +
     
    +
    TJException(String, Throwable) - Constructor for exception org.libjpegturbo.turbojpeg.TJException
    +
     
    +
    TJException(String) - Constructor for exception org.libjpegturbo.turbojpeg.TJException
    +
     
    +
    TJException(Throwable) - Constructor for exception org.libjpegturbo.turbojpeg.TJException
    +
     
    TJScalingFactor - Class in org.libjpegturbo.turbojpeg
    Fractional scaling factor
    diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJ.html b/java/doc/org/libjpegturbo/turbojpeg/TJ.html index 3ff9b89..ffef657 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJ.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJ.html @@ -1075,16 +1075,13 @@ public static final int FLAG_FORCESSE3

    bufSize

    public static int bufSize(int width,
               int height,
    -          int jpegSubsamp)
    -                   throws java.lang.Exception
    + int jpegSubsamp)
    Returns the maximum size of the buffer (in bytes) required to hold a JPEG image with the given width, height, and level of chrominance subsampling.
    Parameters:
    width - the width (in pixels) of the JPEG image
    height - the height (in pixels) of the JPEG image
    jpegSubsamp - the level of chrominance subsampling to be used when generating the JPEG image (one of TJ.SAMP_*)
    Returns:
    the maximum size of the buffer (in bytes) required to hold a JPEG - image with the given width, height, and level of chrominance subsampling.
    -
    Throws:
    -
    java.lang.Exception
    + image with the given width, height, and level of chrominance subsampling.
    @@ -1096,17 +1093,14 @@ public static final int FLAG_FORCESSE3
    public static int bufSizeYUV(int width,
                  int pad,
                  int height,
    -             int subsamp)
    -                      throws java.lang.Exception
    + int subsamp)
    Returns the size of the buffer (in bytes) required to hold a YUV planar image with the given width, height, and level of chrominance subsampling.
    Parameters:
    width - the width (in pixels) of the YUV image
    pad - the width of each line in each plane of the image is padded to the nearest multiple of this number of bytes (must be a power of 2.)
    height - the height (in pixels) of the YUV image
    subsamp - the level of chrominance subsampling used in the YUV image (one of TJ.SAMP_*)
    Returns:
    the size of the buffer (in bytes) required to hold a YUV planar - image with the given width, height, and level of chrominance subsampling.
    -
    Throws:
    -
    java.lang.Exception
    + image with the given width, height, and level of chrominance subsampling. @@ -1118,11 +1112,8 @@ public static final int FLAG_FORCESSE3
    @Deprecated
     public static int bufSizeYUV(int width,
                             int height,
    -                        int subsamp)
    -                      throws java.lang.Exception
    + int subsamp)
    Deprecated. Use bufSizeYUV(int, int, int, int) instead.
    -
    Throws:
    -
    java.lang.Exception
    @@ -1135,8 +1126,7 @@ public static int bufSizeYUV(int width, int width, int stride, int height, - int subsamp) - throws java.lang.Exception + int subsamp)
    Returns the size of the buffer (in bytes) required to hold a YUV image plane with the given parameters.
    Parameters:
    componentID - ID number of the image plane (0 = Y, 1 = U/Cb, @@ -1145,9 +1135,7 @@ public static int bufSizeYUV(int width, height of the whole image, not the plane height.
    subsamp - the level of chrominance subsampling used in the YUV image (one of TJ.SAMP_*)
    Returns:
    the size of the buffer (in bytes) required to hold a YUV planar - image with the given parameters.
    -
    Throws:
    -
    java.lang.Exception
    + image with the given parameters. @@ -1158,16 +1146,13 @@ public static int bufSizeYUV(int width,

    planeWidth

    public static int planeWidth(int componentID,
                  int width,
    -             int subsamp)
    -                      throws java.lang.Exception
    + int subsamp)
    Returns the plane width of a YUV image plane with the given parameters. Refer to YUVImage for a description of plane width.
    Parameters:
    componentID - ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
    width - width (in pixels) of the YUV image
    subsamp - the level of chrominance subsampling used in the YUV image (one of TJ.SAMP_*)
    -
    Returns:
    the plane width of a YUV image plane with the given parameters.
    -
    Throws:
    -
    java.lang.Exception
    +
    Returns:
    the plane width of a YUV image plane with the given parameters.
    @@ -1178,16 +1163,13 @@ public static int bufSizeYUV(int width,

    planeHeight

    public static int planeHeight(int componentID,
                   int height,
    -              int subsamp)
    -                       throws java.lang.Exception
    + int subsamp)
    Returns the plane height of a YUV image plane with the given parameters. Refer to YUVImage for a description of plane height.
    Parameters:
    componentID - ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
    height - height (in pixels) of the YUV image
    subsamp - the level of chrominance subsampling used in the YUV image (one of TJ.SAMP_*)
    -
    Returns:
    the plane height of a YUV image plane with the given parameters.
    -
    Throws:
    -
    java.lang.Exception
    +
    Returns:
    the plane height of a YUV image plane with the given parameters.
    @@ -1196,14 +1178,11 @@ public static int bufSizeYUV(int width, diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html index 9fe4f99..29f12b7 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html @@ -381,10 +381,10 @@ implements java.io.Closeable
  • TJCompressor

    public TJCompressor()
    -             throws java.lang.Exception
    + throws TJException
    Create a TurboJPEG compressor instance.
    Throws:
    -
    java.lang.Exception
    +
    TJException
  • @@ -400,14 +400,14 @@ implements java.io.Closeable int pitch, int height, int pixelFormat) - throws java.lang.Exception + throws TJException
    Create a TurboJPEG compressor instance and associate the uncompressed source image stored in srcImage with the newly created instance.
    Parameters:
    srcImage - see setSourceImage(byte[], int, int, int, int, int, int) for description
    x - see setSourceImage(byte[], int, int, int, int, int, int) for description
    y - see setSourceImage(byte[], int, int, int, int, int, int) for description
    width - see setSourceImage(byte[], int, int, int, int, int, int) for description
    pitch - see setSourceImage(byte[], int, int, int, int, int, int) for description
    height - see setSourceImage(byte[], int, int, int, int, int, int) for description
    pixelFormat - pixel format of the source image (one of TJ.PF_*)
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -422,11 +422,11 @@ public TJCompressor(byte[] srcImage, int pitch, int height, int pixelFormat) - throws java.lang.Exception + throws TJException
    Deprecated. Use TJCompressor(byte[], int, int, int, int, int, int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -440,7 +440,7 @@ public TJCompressor(byte[] srcImage, int y, int width, int height) - throws java.lang.Exception + throws TJException
    Create a TurboJPEG compressor instance and associate the uncompressed source image stored in srcImage with the newly created instance.
    @@ -451,7 +451,7 @@ public TJCompressor(byte[] srcImage, setSourceImage(BufferedImage, int, int, int, int) for description
    height - see setSourceImage(BufferedImage, int, int, int, int) for description
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -475,7 +475,7 @@ public TJCompressor(byte[] srcImage, int pitch, int height, int pixelFormat) - throws java.lang.Exception + throws TJException
    Associate an uncompressed RGB, grayscale, or CMYK source image with this compressor instance.
    Parameters:
    srcImage - image buffer containing RGB, grayscale, or CMYK pixels to @@ -493,7 +493,7 @@ public TJCompressor(byte[] srcImage, which the JPEG or YUV image should be compressed/encoded
    pixelFormat - pixel format of the source image (one of TJ.PF_*)
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -508,11 +508,11 @@ public void setSourceImage(byte[] srcImage, int pitch, int height, int pixelFormat) - throws java.lang.Exception + throws TJException
    Deprecated. Use setSourceImage(byte[], int, int, int, int, int, int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -526,7 +526,7 @@ public void setSourceImage(byte[] srcImage, int y, int width, int height) - throws java.lang.Exception + throws TJException
    Associate an uncompressed RGB or grayscale source image with this compressor instance.
    Parameters:
    srcImage - a BufferedImage instance containing RGB or @@ -538,7 +538,7 @@ public void setSourceImage(byte[] srcImage, which the JPEG or YUV image should be compressed/encoded (0 = use the height of the source image)
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -548,13 +548,13 @@ public void setSourceImage(byte[] srcImage,
  • setSourceImage

    public void setSourceImage(YUVImage srcImage)
    -                    throws java.lang.Exception
    + throws TJException
    Associate an uncompressed YUV planar source image with this compressor instance.
    Parameters:
    srcImage - YUV planar image to be compressed. This image is not modified.
    Throws:
    -
    java.lang.Exception
    +
    TJException
  • @@ -604,7 +604,7 @@ public void setSourceImage(byte[] srcImage,

    compress

    public void compress(byte[] dstBuf,
                 int flags)
    -              throws java.lang.Exception
    + throws
    TJException
    Compress the uncompressed source image associated with this compressor instance and output a JPEG image to the given destination buffer.
    Parameters:
    dstBuf - buffer that will receive the JPEG image. Use @@ -613,7 +613,7 @@ public void setSourceImage(byte[] srcImage, subsampling.
    flags - the bitwise OR of one or more of TJ.FLAG_*
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -623,7 +623,7 @@ public void setSourceImage(byte[] srcImage,
  • compress

    public byte[] compress(int flags)
    -                throws java.lang.Exception
    + throws
    TJException
    Compress the uncompressed source image associated with this compressor instance and return a buffer containing a JPEG image.
    Parameters:
    flags - the bitwise OR of one or more of @@ -631,7 +631,7 @@ public void setSourceImage(byte[] srcImage,
    Returns:
    a buffer containing a JPEG image. The length of this buffer will not be equal to the size of the JPEG image. Use getCompressedSize() to obtain the size of the JPEG image.
    Throws:
    -
    java.lang.Exception
    +
    TJException
  • @@ -644,12 +644,12 @@ public void setSourceImage(byte[] srcImage, public void compress(java.awt.image.BufferedImage srcImage, byte[] dstBuf, int flags) - throws java.lang.Exception + throws TJException
    Deprecated. Use setSourceImage(BufferedImage, int, int, int, int) and compress(byte[], int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -661,12 +661,12 @@ public void compress(java.awt.image.BufferedImage srcImage,
    @Deprecated
     public byte[] compress(java.awt.image.BufferedImage srcImage,
                              int flags)
    -                throws java.lang.Exception
    + throws
    TJException
    Deprecated. Use setSourceImage(BufferedImage, int, int, int, int) and compress(int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -677,7 +677,7 @@ public byte[] compress(java.awt.image.BufferedImage srcImage,

    encodeYUV

    public void encodeYUV(YUVImage dstImage,
                  int flags)
    -               throws java.lang.Exception
    + throws TJException
    Encode the uncompressed source image associated with this compressor instance into a YUV planar image and store it in the given YUVImage instance. This method uses the accelerated color @@ -688,7 +688,7 @@ public byte[] compress(java.awt.image.BufferedImage srcImage, image
    flags - the bitwise OR of one or more of TJ.FLAG_*
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -700,10 +700,10 @@ public byte[] compress(java.awt.image.BufferedImage srcImage,
    @Deprecated
     public void encodeYUV(byte[] dstBuf,
                             int flags)
    -               throws java.lang.Exception
    + throws
    TJException
    Deprecated. Use encodeYUV(YUVImage, int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -714,7 +714,7 @@ public void encodeYUV(byte[] dstBuf,

    encodeYUV

    public YUVImage encodeYUV(int pad,
                      int flags)
    -                   throws java.lang.Exception
    + throws TJException
    Encode the uncompressed source image associated with this compressor instance into a unified YUV planar image buffer and return a YUVImage instance containing the encoded image. This method @@ -727,7 +727,7 @@ public void encodeYUV(byte[] dstBuf, TJ.FLAG_*
    Returns:
    a YUV planar image.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -738,7 +738,7 @@ public void encodeYUV(byte[] dstBuf,

    encodeYUV

    public YUVImage encodeYUV(int[] strides,
                      int flags)
    -                   throws java.lang.Exception
    + throws TJException
    Encode the uncompressed source image associated with this compressor instance into separate Y, U (Cb), and V (Cr) image planes and return a YUVImage instance containing the encoded image planes. This @@ -755,7 +755,7 @@ public void encodeYUV(byte[] dstBuf, TJ.FLAG_*
    Returns:
    a YUV planar image.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -766,10 +766,10 @@ public void encodeYUV(byte[] dstBuf,

    encodeYUV

    @Deprecated
     public byte[] encodeYUV(int flags)
    -                 throws java.lang.Exception
    + throws
    TJException
    Deprecated. Use encodeYUV(int, int) instead.
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -782,12 +782,12 @@ public byte[] encodeYUV(int flags) public void encodeYUV(java.awt.image.BufferedImage srcImage, byte[] dstBuf, int flags) - throws java.lang.Exception + throws TJException
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -799,12 +799,12 @@ public void encodeYUV(java.awt.image.BufferedImage srcImage,
    @Deprecated
     public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
                               int flags)
    -                 throws java.lang.Exception
    + throws
    TJException
    Throws:
    -
    java.lang.Exception
    +
    TJException
    @@ -827,7 +827,7 @@ public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
  • close

    public void close()
    -           throws java.io.IOException
    + throws
    TJException
    Free the native structures associated with this compressor instance.
    Specified by:
    @@ -835,7 +835,7 @@ public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
    Specified by:
    close in interface java.lang.AutoCloseable
    Throws:
    -
    java.io.IOException
    +
    TJException
  • diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJCustomFilter.html b/java/doc/org/libjpegturbo/turbojpeg/TJCustomFilter.html index c2b6e61..6bd6fd2 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJCustomFilter.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJCustomFilter.html @@ -144,7 +144,7 @@ int componentID, int transformID, TJTransform transform) - throws java.lang.Exception + throws TJException
    A callback function that can be used to modify the DCT coefficients after they are losslessly transformed but before they are transcoded to a new JPEG image. This allows for custom filters or other transformations to be @@ -165,7 +165,7 @@ transform in the transforms array that was passed to TJTransformer.transform().
    transform - a TJTransform instance that specifies the parameters and/or cropping region for this transform
    Throws:
    -
    java.lang.Exception
    +
    TJException
    diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html index 6cec2cc..a914de9 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html @@ -33,7 +33,7 @@