Throw idiomatic unchecked exceptions from the Java classes and JNI wrapper if there is an unrecoverable error caused by incorrect API usage (such as illegal arguments, etc.), and throw Errors if there is an unrecoverable error at the C level (such as a failed malloc() call.)

Change the behavior of the bailif0() macro in the JNI wrapper so that it doesn't throw an exception for an unexpected NULL condition.  In fact, in all cases, the underlying JNI API function (such as GetFieldID(), etc.) will throw an Error on its own whenever it returns NULL, so our custom exceptions were never being thrown in that case anyhow.  All we need to do is just detect the error and bail out of the C code.

This also corrects a couple of formatting issues (semicolons aren't needed at the end of class definitions, and @Override should be specified for the methods we're overriding from super-classes, so the compiler can sanity-check that we're actually overriding a method and not declaring a new one.)


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1595 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
DRC
2015-07-14 20:42:52 +00:00
parent 1a4778f8f0
commit b3817dab86
16 changed files with 315 additions and 375 deletions

View File

@@ -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
=====

View File

@@ -983,16 +983,13 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getMCUWidth</h4>
<pre>public static&nbsp;int&nbsp;getMCUWidth(int&nbsp;subsamp)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getMCUWidth(int&nbsp;subsamp)</pre>
<div class="block">Returns the MCU block width for the given level of chrominance
subsampling.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>subsamp</code> - the level of chrominance subsampling (one of
<code>SAMP_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the MCU block width for the given level of chrominance
subsampling.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
subsampling.</dd></dl>
</li>
</ul>
<a name="getMCUHeight(int)">
@@ -1001,16 +998,13 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getMCUHeight</h4>
<pre>public static&nbsp;int&nbsp;getMCUHeight(int&nbsp;subsamp)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getMCUHeight(int&nbsp;subsamp)</pre>
<div class="block">Returns the MCU block height for the given level of chrominance
subsampling.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>subsamp</code> - the level of chrominance subsampling (one of
<code>SAMP_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the MCU block height for the given level of chrominance
subsampling.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
subsampling.</dd></dl>
</li>
</ul>
<a name="getPixelSize(int)">
@@ -1019,13 +1013,10 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getPixelSize</h4>
<pre>public static&nbsp;int&nbsp;getPixelSize(int&nbsp;pixelFormat)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getPixelSize(int&nbsp;pixelFormat)</pre>
<div class="block">Returns the pixel size (in bytes) for the given pixel format.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>pixelFormat</code> - the pixel format (one of <code>PF_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the pixel size (in bytes) for the given pixel format.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the pixel size (in bytes) for the given pixel format.</dd></dl>
</li>
</ul>
<a name="getRedOffset(int)">
@@ -1034,17 +1025,14 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getRedOffset</h4>
<pre>public static&nbsp;int&nbsp;getRedOffset(int&nbsp;pixelFormat)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getRedOffset(int&nbsp;pixelFormat)</pre>
<div class="block">For the given pixel format, returns the number of bytes that the red
component is offset from the start of the pixel. For instance, if a pixel
of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
then the red component will be
<code>pixel[TJ.getRedOffset(TJ.PF_BGRX)]</code>.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>pixelFormat</code> - the pixel format (one of <code>PF_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the red offset for the given pixel format.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the red offset for the given pixel format.</dd></dl>
</li>
</ul>
<a name="getGreenOffset(int)">
@@ -1053,17 +1041,14 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getGreenOffset</h4>
<pre>public static&nbsp;int&nbsp;getGreenOffset(int&nbsp;pixelFormat)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getGreenOffset(int&nbsp;pixelFormat)</pre>
<div class="block">For the given pixel format, returns the number of bytes that the green
component is offset from the start of the pixel. For instance, if a pixel
of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
then the green component will be
<code>pixel[TJ.getGreenOffset(TJ.PF_BGRX)]</code>.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>pixelFormat</code> - the pixel format (one of <code>PF_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the green offset for the given pixel format.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the green offset for the given pixel format.</dd></dl>
</li>
</ul>
<a name="getBlueOffset(int)">
@@ -1072,17 +1057,14 @@ public static final&nbsp;int FLAG_FORCESSE3</pre>
<ul class="blockList">
<li class="blockList">
<h4>getBlueOffset</h4>
<pre>public static&nbsp;int&nbsp;getBlueOffset(int&nbsp;pixelFormat)
throws java.lang.Exception</pre>
<pre>public static&nbsp;int&nbsp;getBlueOffset(int&nbsp;pixelFormat)</pre>
<div class="block">For the given pixel format, returns the number of bytes that the blue
component is offset from the start of the pixel. For instance, if a pixel
of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
then the blue component will be
<code>pixel[TJ.getBlueOffset(TJ.PF_BGRX)]</code>.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>pixelFormat</code> - the pixel format (one of <code>PF_*</code>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the blue offset for the given pixel format.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the blue offset for the given pixel format.</dd></dl>
</li>
</ul>
<a name="bufSize(int, int, int)">

View File

@@ -563,8 +563,7 @@ public&nbsp;void&nbsp;setSourceImage(byte[]&nbsp;srcImage,
<ul class="blockList">
<li class="blockList">
<h4>setSubsamp</h4>
<pre>public&nbsp;void&nbsp;setSubsamp(int&nbsp;newSubsamp)
throws java.lang.Exception</pre>
<pre>public&nbsp;void&nbsp;setSubsamp(int&nbsp;newSubsamp)</pre>
<div class="block">Set the level of chrominance subsampling for subsequent compress/encode
operations. When pixels are converted from RGB to YCbCr (see
<a href="../../../org/libjpegturbo/turbojpeg/TJ.html#CS_YCbCr"><code>TJ.CS_YCbCr</code></a>) or from CMYK to YCCK (see <a href="../../../org/libjpegturbo/turbojpeg/TJ.html#CS_YCCK"><code>TJ.CS_YCCK</code></a>) as part
@@ -582,9 +581,7 @@ public&nbsp;void&nbsp;setSourceImage(byte[]&nbsp;srcImage,
destination.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>newSubsamp</code> - the level of chrominance subsampling to use in
subsequent compress/encode oeprations (one of
<a href="../../../org/libjpegturbo/turbojpeg/TJ.html#SAMP_444"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<a href="../../../org/libjpegturbo/turbojpeg/TJ.html#SAMP_444"><code>TJ.SAMP_*</code></a>)</dd></dl>
</li>
</ul>
<a name="setJPEGQuality(int)">
@@ -593,13 +590,10 @@ public&nbsp;void&nbsp;setSourceImage(byte[]&nbsp;srcImage,
<ul class="blockList">
<li class="blockList">
<h4>setJPEGQuality</h4>
<pre>public&nbsp;void&nbsp;setJPEGQuality(int&nbsp;quality)
throws java.lang.Exception</pre>
<pre>public&nbsp;void&nbsp;setJPEGQuality(int&nbsp;quality)</pre>
<div class="block">Set the JPEG image quality level for subsequent compress operations.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>quality</code> - the new JPEG image quality level (1 to 100, 1 = worst,
100 = best)</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
100 = best)</dd></dl>
</li>
</ul>
<a name="compress(byte[], int)">

View File

@@ -653,14 +653,11 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getWidth</h4>
<pre>public&nbsp;int&nbsp;getWidth()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getWidth()</pre>
<div class="block">Returns the width of the source image (JPEG or YUV) associated with this
decompressor instance.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the width of the source image (JPEG or YUV) associated with this
decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
decompressor instance.</dd></dl>
</li>
</ul>
<a name="getHeight()">
@@ -669,14 +666,11 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getHeight</h4>
<pre>public&nbsp;int&nbsp;getHeight()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getHeight()</pre>
<div class="block">Returns the height of the source image (JPEG or YUV) associated with this
decompressor instance.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the height of the source image (JPEG or YUV) associated with this
decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
decompressor instance.</dd></dl>
</li>
</ul>
<a name="getSubsamp()">
@@ -685,15 +679,12 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getSubsamp</h4>
<pre>public&nbsp;int&nbsp;getSubsamp()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getSubsamp()</pre>
<div class="block">Returns the level of chrominance subsampling used in the source image
(JPEG or YUV) associated with this decompressor instance. See
<a href="../../../org/libjpegturbo/turbojpeg/TJ.html#SAMP_444"><code>TJ.SAMP_*</code></a>.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the level of chrominance subsampling used in the source image
(JPEG or YUV) associated with this decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
(JPEG or YUV) associated with this decompressor instance.</dd></dl>
</li>
</ul>
<a name="getColorspace()">
@@ -702,15 +693,12 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getColorspace</h4>
<pre>public&nbsp;int&nbsp;getColorspace()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getColorspace()</pre>
<div class="block">Returns the colorspace used in the source image (JPEG or YUV) associated
with this decompressor instance. See <a href="../../../org/libjpegturbo/turbojpeg/TJ.html#CS_RGB"><code>TJ.CS_*</code></a>. If the
source image is YUV, then this always returns <a href="../../../org/libjpegturbo/turbojpeg/TJ.html#CS_YCbCr"><code>TJ.CS_YCbCr</code></a>.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the colorspace used in the source image (JPEG or YUV) associated
with this decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
with this decompressor instance.</dd></dl>
</li>
</ul>
<a name="getJPEGBuf()">
@@ -719,12 +707,9 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getJPEGBuf</h4>
<pre>public&nbsp;byte[]&nbsp;getJPEGBuf()
throws java.lang.Exception</pre>
<pre>public&nbsp;byte[]&nbsp;getJPEGBuf()</pre>
<div class="block">Returns the JPEG image buffer associated with this decompressor instance.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the JPEG image buffer associated with this decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the JPEG image buffer associated with this decompressor instance.</dd></dl>
</li>
</ul>
<a name="getJPEGSize()">
@@ -733,14 +718,11 @@ public&nbsp;void&nbsp;setJPEGImage(byte[]&nbsp;jpegImage,
<ul class="blockList">
<li class="blockList">
<h4>getJPEGSize</h4>
<pre>public&nbsp;int&nbsp;getJPEGSize()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getJPEGSize()</pre>
<div class="block">Returns the size of the JPEG image (in bytes) associated with this
decompressor instance.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the size of the JPEG image (in bytes) associated with this
decompressor instance.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
decompressor instance.</dd></dl>
</li>
</ul>
<a name="getScaledWidth(int, int)">

View File

@@ -192,10 +192,7 @@ extends java.lang.Object</pre>
<li class="blockList">
<h4>TJScalingFactor</h4>
<pre>public&nbsp;TJScalingFactor(int&nbsp;num,
int&nbsp;denom)
throws java.lang.Exception</pre>
<dl><dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
int&nbsp;denom)</pre>
</li>
</ul>
</li>

View File

@@ -341,14 +341,11 @@ extends <a href="../../../org/libjpegturbo/turbojpeg/TJDecompressor.html" title=
<ul class="blockListLast">
<li class="blockList">
<h4>getTransformedSizes</h4>
<pre>public&nbsp;int[]&nbsp;getTransformedSizes()
throws java.lang.Exception</pre>
<pre>public&nbsp;int[]&nbsp;getTransformedSizes()</pre>
<div class="block">Returns an array containing the sizes of the transformed JPEG images
generated by the most recent transform operation.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>an array containing the sizes of the transformed JPEG images
generated by the most recent transform operation.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
generated by the most recent transform operation.</dd></dl>
</li>
</ul>
</li>

View File

@@ -610,12 +610,9 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getWidth</h4>
<pre>public&nbsp;int&nbsp;getWidth()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getWidth()</pre>
<div class="block">Returns the width of the YUV image (or subregion.)</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the width of the YUV image (or subregion)</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the width of the YUV image (or subregion)</dd></dl>
</li>
</ul>
<a name="getHeight()">
@@ -624,12 +621,9 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getHeight</h4>
<pre>public&nbsp;int&nbsp;getHeight()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getHeight()</pre>
<div class="block">Returns the height of the YUV image (or subregion.)</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the height of the YUV image (or subregion)</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the height of the YUV image (or subregion)</dd></dl>
</li>
</ul>
<a name="getPad()">
@@ -638,13 +632,10 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getPad</h4>
<pre>public&nbsp;int&nbsp;getPad()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getPad()</pre>
<div class="block">Returns the line padding used in the YUV image buffer (if this image is
stored in a unified buffer rather than separate image planes.)</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the line padding used in the YUV image buffer</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the line padding used in the YUV image buffer</dd></dl>
</li>
</ul>
<a name="getStrides()">
@@ -653,12 +644,9 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getStrides</h4>
<pre>public&nbsp;int[]&nbsp;getStrides()
throws java.lang.Exception</pre>
<pre>public&nbsp;int[]&nbsp;getStrides()</pre>
<div class="block">Returns the number of bytes per line of each plane in the YUV image.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the number of bytes per line of each plane in the YUV image</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the number of bytes per line of each plane in the YUV image</dd></dl>
</li>
</ul>
<a name="getOffsets()">
@@ -667,14 +655,11 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getOffsets</h4>
<pre>public&nbsp;int[]&nbsp;getOffsets()
throws java.lang.Exception</pre>
<pre>public&nbsp;int[]&nbsp;getOffsets()</pre>
<div class="block">Returns the offsets (in bytes) of each plane within the planes of a larger
YUV image.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the offsets (in bytes) of each plane within the planes of a larger
YUV image</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
YUV image</dd></dl>
</li>
</ul>
<a name="getSubsamp()">
@@ -683,13 +668,10 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getSubsamp</h4>
<pre>public&nbsp;int&nbsp;getSubsamp()
throws java.lang.Exception</pre>
<pre>public&nbsp;int&nbsp;getSubsamp()</pre>
<div class="block">Returns the level of chrominance subsampling used in the YUV image. See
<a href="../../../org/libjpegturbo/turbojpeg/TJ.html#SAMP_444"><code>TJ.SAMP_*</code></a>.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the level of chrominance subsampling used in the YUV image</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the level of chrominance subsampling used in the YUV image</dd></dl>
</li>
</ul>
<a name="getPlanes()">
@@ -698,13 +680,10 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getPlanes</h4>
<pre>public&nbsp;byte[][]&nbsp;getPlanes()
throws java.lang.Exception</pre>
<pre>public&nbsp;byte[][]&nbsp;getPlanes()</pre>
<div class="block">Returns the YUV image planes. If the image is stored in a unified buffer,
then all image planes will point to that buffer.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the YUV image planes</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the YUV image planes</dd></dl>
</li>
</ul>
<a name="getBuf()">
@@ -713,13 +692,10 @@ extends java.lang.Object</pre>
<ul class="blockList">
<li class="blockList">
<h4>getBuf</h4>
<pre>public&nbsp;byte[]&nbsp;getBuf()
throws java.lang.Exception</pre>
<pre>public&nbsp;byte[]&nbsp;getBuf()</pre>
<div class="block">Returns the YUV image buffer (if this image is stored in a unified
buffer rather than separate image planes.)</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>the YUV image buffer</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dl><dt><span class="strong">Returns:</span></dt><dd>the YUV image buffer</dd></dl>
</li>
</ul>
<a name="getSize()">

View File

@@ -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");
}
}

View File

@@ -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;
};
}

View File

@@ -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;
};
}

View File

@@ -32,4 +32,4 @@ final class TJLoader {
static void load() {
System.loadLibrary("@TURBOJPEG_DLL_NAME@");
}
};
}

View File

@@ -56,4 +56,4 @@ final class TJLoader {
}
}
}
};
}

View File

@@ -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 {
* <code>other</code> 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;
};
}

View File

@@ -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;
};
}

View File

@@ -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,9 +282,9 @@ 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][];
@@ -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;
};
}

View File

@@ -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)*srcElementSize<arraySize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
jpegSize=tjBufSize(width, height, jpegSubsamp);
if((*env)->GetArrayLength(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)<nc)
_throw("Planes array is too small for the subsampling type");
_throwarg("Planes array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
_throw("Offsets array is too small for the subsampling type");
_throwarg("Offsets array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jSrcStrides)<nc)
_throw("Strides array is too small for the subsampling type");
_throwarg("Strides array is too small for the subsampling type");
jpegSize=tjBufSize(width, height, subsamp);
if((*env)->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])<srcOffsets[i]+planeSize)
_throw("Source plane is not large enough");
_throwarg("Source plane is not large enough");
bailif0(srcPlanes[i]=(*env)->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)<nc)
_throw("Planes array is too small for the subsampling type");
_throwarg("Planes array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jDstOffsets)<nc)
_throw("Offsets array is too small for the subsampling type");
_throwarg("Offsets array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jDstStrides)<nc)
_throw("Strides array is too small for the subsampling type");
_throwarg("Strides array is too small for the subsampling type");
actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(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])<dstOffsets[i]+planeSize)
_throw("Destination plane is not large enough");
_throwarg("Destination plane is not large enough");
bailif0(dstPlanes[i]=(*env)->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)*srcElementSize<arraySize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
if((*env)->GetArrayLength(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)<jpegSize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
bailif0(jpegBuf=(*env)->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)<jpegSize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
_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));
@@ -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)<jpegSize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
bailif0(_fid=(*env)->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; i<nsf; i++)
{
scaledWidth=TJSCALED(jpegWidth, sf[i]);
@@ -770,16 +769,16 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp);
if(planeSize<0 || pw<0)
_throw(tjGetErrorStr());
_throwtj();
if(dstOffsets[i]<0)
_throw("Invalid argument in decompressToYUV()");
_throwarg("Invalid argument in decompressToYUV()");
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])<dstOffsets[i]+planeSize)
_throw("Destination plane is not large enough");
_throwarg("Destination plane is not large enough");
bailif0(dstPlanes[i]=(*env)->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)<jpegSize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
bailif0(_fid=(*env)->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)<nc)
_throw("Planes array is too small for the subsampling type");
_throwarg("Planes array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
_throw("Offsets array is too small for the subsampling type");
_throwarg("Offsets array is too small for the subsampling type");
if((*env)->GetArrayLength(env, jSrcStrides)<nc)
_throw("Strides array is too small for the subsampling type");
_throwarg("Strides array is too small for the subsampling type");
actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
_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));
@@ -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])<srcOffsets[i]+planeSize)
_throw("Source plane is not large enough");
_throwarg("Source plane is not large enough");
bailif0(srcPlanes[i]=(*env)->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)<jpegSize)
_throw("Source buffer is not large enough");
_throwarg("Source buffer is not large enough");
bailif0(_fid=(*env)->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; i<n; i++)
{
dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
@@ -1117,7 +1116,7 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
<tjBufSize(w, h, jpegSubsamp))
_throw("Destination buffer is not large enough");
_throwarg("Destination buffer is not large enough");
}
bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
for(i=0; i<n; i++)
@@ -1125,7 +1124,7 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
flags|TJFLAG_NOREALLOC)==-1)
_throw(tjGetErrorStr());
_throwtj();
for(i=0; i<n; i++)
{