Restore backward compatibility between libjpeg-turbo 1.3.x JAR and the new JNI library

git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1349 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
DRC
2014-08-15 14:01:21 +00:00
parent 927a10db05
commit 26dd86bd89
2 changed files with 47 additions and 45 deletions

View File

@@ -37,7 +37,7 @@ import java.nio.*;
public class TJDecompressor { public class TJDecompressor {
private static final String NO_ASSOC_ERROR = private static final String NO_ASSOC_ERROR =
"No source image is associated with this instance"; "No JPEG image is associated with this instance";
/** /**
* Create a TurboJPEG decompresssor instance. * Create a TurboJPEG decompresssor instance.
@@ -139,9 +139,9 @@ public class TJDecompressor {
public int getWidth() throws Exception { public int getWidth() throws Exception {
if (yuvImage != null) if (yuvImage != null)
return yuvImage.getWidth(); return yuvImage.getWidth();
if (srcWidth < 1) if (jpegWidth < 1)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
return srcWidth; return jpegWidth;
} }
/** /**
@@ -154,9 +154,9 @@ public class TJDecompressor {
public int getHeight() throws Exception { public int getHeight() throws Exception {
if (yuvImage != null) if (yuvImage != null)
return yuvImage.getHeight(); return yuvImage.getHeight();
if (srcHeight < 1) if (jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
return srcHeight; return jpegHeight;
} }
/** /**
@@ -170,11 +170,11 @@ public class TJDecompressor {
public int getSubsamp() throws Exception { public int getSubsamp() throws Exception {
if (yuvImage != null) if (yuvImage != null)
return yuvImage.getSubsamp(); return yuvImage.getSubsamp();
if (srcSubsamp < 0) if (jpegSubsamp < 0)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
if (srcSubsamp >= TJ.NUMSAMP) if (jpegSubsamp >= TJ.NUMSAMP)
throw new Exception("JPEG header information is invalid"); throw new Exception("JPEG header information is invalid");
return srcSubsamp; return jpegSubsamp;
} }
/** /**
@@ -188,11 +188,11 @@ public class TJDecompressor {
public int getColorspace() throws Exception { public int getColorspace() throws Exception {
if (yuvImage != null) if (yuvImage != null)
return TJ.CS_YCbCr; return TJ.CS_YCbCr;
if (srcColorspace < 0) if (jpegColorspace < 0)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
if (srcColorspace >= TJ.NUMCS) if (jpegColorspace >= TJ.NUMCS)
throw new Exception("JPEG header information is invalid"); throw new Exception("JPEG header information is invalid");
return srcColorspace; return jpegColorspace;
} }
/** /**
@@ -265,19 +265,19 @@ public class TJDecompressor {
*/ */
public int getScaledWidth(int desiredWidth, int desiredHeight) public int getScaledWidth(int desiredWidth, int desiredHeight)
throws Exception { throws Exception {
if (srcWidth < 1 || srcHeight < 1) if (jpegWidth < 1 || jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
if (desiredWidth < 0 || desiredHeight < 0) if (desiredWidth < 0 || desiredHeight < 0)
throw new Exception("Invalid argument in getScaledWidth()"); throw new Exception("Invalid argument in getScaledWidth()");
TJScalingFactor[] sf = TJ.getScalingFactors(); TJScalingFactor[] sf = TJ.getScalingFactors();
if (desiredWidth == 0) if (desiredWidth == 0)
desiredWidth = srcWidth; desiredWidth = jpegWidth;
if (desiredHeight == 0) if (desiredHeight == 0)
desiredHeight = srcHeight; desiredHeight = jpegHeight;
int scaledWidth = srcWidth, scaledHeight = srcHeight; int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for (int i = 0; i < sf.length; i++) { for (int i = 0; i < sf.length; i++) {
scaledWidth = sf[i].getScaled(srcWidth); scaledWidth = sf[i].getScaled(jpegWidth);
scaledHeight = sf[i].getScaled(srcHeight); scaledHeight = sf[i].getScaled(jpegHeight);
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight) if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break; break;
} }
@@ -307,19 +307,19 @@ public class TJDecompressor {
*/ */
public int getScaledHeight(int desiredWidth, int desiredHeight) public int getScaledHeight(int desiredWidth, int desiredHeight)
throws Exception { throws Exception {
if (srcWidth < 1 || srcHeight < 1) if (jpegWidth < 1 || jpegHeight < 1)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
if (desiredWidth < 0 || desiredHeight < 0) if (desiredWidth < 0 || desiredHeight < 0)
throw new Exception("Invalid argument in getScaledHeight()"); throw new Exception("Invalid argument in getScaledHeight()");
TJScalingFactor[] sf = TJ.getScalingFactors(); TJScalingFactor[] sf = TJ.getScalingFactors();
if (desiredWidth == 0) if (desiredWidth == 0)
desiredWidth = srcWidth; desiredWidth = jpegWidth;
if (desiredHeight == 0) if (desiredHeight == 0)
desiredHeight = srcHeight; desiredHeight = jpegHeight;
int scaledWidth = srcWidth, scaledHeight = srcHeight; int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for (int i = 0; i < sf.length; i++) { for (int i = 0; i < sf.length; i++) {
scaledWidth = sf[i].getScaled(srcWidth); scaledWidth = sf[i].getScaled(jpegWidth);
scaledHeight = sf[i].getScaled(srcHeight); scaledHeight = sf[i].getScaled(jpegHeight);
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight) if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break; break;
} }
@@ -497,7 +497,7 @@ public class TJDecompressor {
if (scaledWidth != dstImage.getWidth() || if (scaledWidth != dstImage.getWidth() ||
scaledHeight != dstImage.getHeight()) 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 Exception("YUVImage dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating.");
if (srcSubsamp != dstImage.getSubsamp()) if (jpegSubsamp != dstImage.getSubsamp())
throw new Exception("YUVImage subsampling level does not match that of the JPEG image"); throw new Exception("YUVImage subsampling level does not match that of the JPEG image");
decompressToYUV(jpegBuf, jpegBufSize, dstImage.getBuf(), decompressToYUV(jpegBuf, jpegBufSize, dstImage.getBuf(),
@@ -510,8 +510,8 @@ public class TJDecompressor {
*/ */
@Deprecated @Deprecated
public void decompressToYUV(byte[] dstBuf, int flags) throws Exception { public void decompressToYUV(byte[] dstBuf, int flags) throws Exception {
YUVImage dstImage = new YUVImage(dstBuf, srcWidth, 4, srcHeight, YUVImage dstImage = new YUVImage(dstBuf, jpegWidth, 4, jpegHeight,
srcSubsamp); jpegSubsamp);
decompressToYUV(dstImage, flags); decompressToYUV(dstImage, flags);
} }
@@ -553,9 +553,9 @@ public class TJDecompressor {
int flags) throws Exception { int flags) throws Exception {
if (flags < 0) if (flags < 0)
throw new Exception("Invalid argument in decompressToYUV()"); throw new Exception("Invalid argument in decompressToYUV()");
if (srcWidth < 1 || srcHeight < 1 || srcSubsamp < 0) if (jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0)
throw new Exception(NO_ASSOC_ERROR); throw new Exception(NO_ASSOC_ERROR);
if (srcSubsamp >= TJ.NUMSAMP) if (jpegSubsamp >= TJ.NUMSAMP)
throw new Exception("JPEG header information is invalid"); throw new Exception("JPEG header information is invalid");
if (yuvImage != null) if (yuvImage != null)
throw new Exception("Source image is the wrong type"); throw new Exception("Source image is the wrong type");
@@ -563,7 +563,7 @@ public class TJDecompressor {
int scaledWidth = getScaledWidth(desiredWidth, desiredHeight); int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = getScaledHeight(desiredWidth, desiredHeight); int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
YUVImage yuvImage = new YUVImage(scaledWidth, pad, scaledHeight, YUVImage yuvImage = new YUVImage(scaledWidth, pad, scaledHeight,
srcSubsamp); jpegSubsamp);
decompressToYUV(yuvImage, flags); decompressToYUV(yuvImage, flags);
return yuvImage; return yuvImage;
} }
@@ -573,7 +573,7 @@ public class TJDecompressor {
*/ */
@Deprecated @Deprecated
public byte[] decompressToYUV(int flags) throws Exception { public byte[] decompressToYUV(int flags) throws Exception {
YUVImage dstImage = new YUVImage(srcWidth, 4, srcHeight, srcSubsamp); YUVImage dstImage = new YUVImage(jpegWidth, 4, jpegHeight, jpegSubsamp);
decompressToYUV(dstImage, flags); decompressToYUV(dstImage, flags);
return dstImage.getBuf(); return dstImage.getBuf();
} }
@@ -855,9 +855,9 @@ public class TJDecompressor {
protected byte[] jpegBuf = null; protected byte[] jpegBuf = null;
protected int jpegBufSize = 0; protected int jpegBufSize = 0;
protected YUVImage yuvImage = null; protected YUVImage yuvImage = null;
protected int srcWidth = 0; protected int jpegWidth = 0;
protected int srcHeight = 0; protected int jpegHeight = 0;
protected int srcSubsamp = -1; protected int jpegSubsamp = -1;
protected int srcColorspace = -1; protected int jpegColorspace = -1;
private ByteOrder byteOrder = null; private ByteOrder byteOrder = null;
}; };

View File

@@ -394,13 +394,15 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
(*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL; (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL;
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcSubsamp", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
(*env)->SetIntField(env, obj, _fid, jpegSubsamp); (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcColorspace", "I")); if((_fid=(*env)->GetFieldID(env, _cls, "jpegColorspace", "I"))==0)
(*env)->SetIntField(env, obj, _fid, jpegColorspace); (*env)->ExceptionClear(env);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcWidth", "I")); else
(*env)->SetIntField(env, obj, _fid, jpegColorspace);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
(*env)->SetIntField(env, obj, _fid, width); (*env)->SetIntField(env, obj, _fid, width);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcHeight", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
(*env)->SetIntField(env, obj, _fid, height); (*env)->SetIntField(env, obj, _fid, height);
bailout: bailout:
@@ -507,11 +509,11 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
if((*env)->GetArrayLength(env, src)<jpegSize) if((*env)->GetArrayLength(env, src)<jpegSize)
_throw("Source buffer is not large enough"); _throw("Source buffer is not large enough");
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcSubsamp", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcWidth", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcHeight", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
yuvSize=(jsize)tjBufSizeYUV2(desiredWidth==0? jpegWidth:desiredWidth, yuvSize=(jsize)tjBufSizeYUV2(desiredWidth==0? jpegWidth:desiredWidth,
@@ -702,11 +704,11 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
_throw("Source buffer is not large enough"); _throw("Source buffer is not large enough");
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcWidth", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcHeight", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
bailif0(_fid=(*env)->GetFieldID(env, _cls, "srcSubsamp", "I")); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
n=(*env)->GetArrayLength(env, dstobjs); n=(*env)->GetArrayLength(env, dstobjs);