fis.read(inputbuf);
fis.close();
- TJDecompressor tjd=new TJDecompressor();
- TJHeaderInfo tji=tjd.decompressHeader(inputbuf, inputsize);
- System.out.print("Source Image: "+tji.width+" x "+tji.height+ " pixels, ");
- switch(tji.subsamp) {
+ TJDecompressor tjd=new TJDecompressor(inputbuf);
+ int width=tjd.getWidth();
+ int height=tjd.getHeight();
+ int subsamp=tjd.getSubsamp();
+ System.out.print("Source Image: "+width+" x "+height+" pixels, ");
+ switch(subsamp) {
case TJ.SAMP444: System.out.println("4:4:4 subsampling"); break;
case TJ.SAMP422: System.out.println("4:2:2 subsampling"); break;
case TJ.SAMP420: System.out.println("4:2:0 subsampling"); break;
}
if(scalefactor!=1) {
- tji.width=(tji.width+scalefactor-1)/scalefactor;
- tji.height=(tji.height+scalefactor-1)/scalefactor;
- System.out.println("Dest. Image: "+tji.width+" x "+tji.height
+ width=(width+scalefactor-1)/scalefactor;
+ height=(height+scalefactor-1)/scalefactor;
+ System.out.println("Dest. Image: "+width+" x "+height
+" pixels");
}
-
- byte [] tmpbuf=new byte[tji.width*tji.height*3];
- tjd.decompress(inputbuf, inputsize, tmpbuf, tji.width, tji.width*3,
- tji.height, 3, TJ.BOTTOMUP);
+ byte [] tmpbuf=tjd.decompress(width, 0, height, TJ.BGR, TJ.BOTTOMUP);
tjd.close();
- TJCompressor tjc=new TJCompressor();
- byte [] outputbuf=new byte[(int)TJ.bufSize(tji.width, tji.height)];
- long outputsize=tjc.compress(tmpbuf, tji.width, tji.width*3, tji.height,
- 3, outputbuf, tji.subsamp, 95, TJ.BOTTOMUP);
+ TJCompressor tjc=new TJCompressor(tmpbuf, width, 0, height, TJ.BGR);
+ byte [] outputbuf=new byte[(int)TJ.bufSize(width, height)];
+ long outputsize=tjc.compress(outputbuf, subsamp, 95, TJ.BOTTOMUP);
tjc.close();
file=new File(argv[1]);
// Subsampling options
final public static int
+ NUMSUBOPT = 4,
SAMP444 = 0,
SAMP422 = 1,
SAMP420 = 2,
GRAYSCALE = 3;
+ // Pixel formats
+ final public static int
+ NUMPIXFORMATS = 7,
+ RGB = 0,
+ BGR = 1,
+ RGBX = 2,
+ BGRX = 3,
+ XBGR = 4,
+ XRGB = 5,
+ YUV = 6;
+
+ final public static int pixelSize[] = {
+ 3, 3, 4, 4, 4, 4, 3
+ };
+
+ public static int getPixelSize(int pixelFormat) throws Exception {
+ if(pixelFormat < 0 || pixelFormat >= NUMPIXFORMATS)
+ throw new Exception("Invalid pixel format");
+ return pixelSize[pixelFormat];
+ }
+
// Flags
final public static int
- BGR = 1,
BOTTOMUP = 2,
FORCEMMX = 8,
FORCESSE = 16,
FORCESSE2 = 32,
- ALPHAFIRST = 64,
FORCESSE3 = 128,
- FASTUPSAMPLE = 256,
- YUV = 512;
+ FASTUPSAMPLE = 256;
+
+ final private static int
+ TJ_BGR = 1,
+ TJ_ALPHAFIRST = 64,
+ TJ_YUV = 512;
+
+ final private static int flags[] = {
+ 0, TJ_BGR, 0, TJ_BGR, TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST, TJ_YUV
+ };
+
+ public static int getFlags(int pixelFormat) throws Exception {
+ if(pixelFormat < 0 || pixelFormat >= NUMPIXFORMATS)
+ throw new Exception("Invalid pixel format");
+ return flags[pixelFormat];
+ }
+
+ public native final static long bufSize(int width, int height)
+ throws Exception;
- public native final static long bufSize(int width, int height);
+ public native final static long bufSizeYUV(int width, int height,
+ int subsamp)
+ throws Exception;
};
init();
}
+ public TJCompressor(byte [] buf, int width, int pitch, int height,
+ int pixelFormat) throws Exception {
+ setBitmapBuffer(buf, width, pitch, height, pixelFormat);
+ }
+
+ public void setBitmapBuffer(byte [] buf, int width, int pitch, int height,
+ int pixelFormat) throws Exception {
+ if(handle == 0) init();
+ if(buf == null || width < 1 || height < 1 || pitch < 0 || pixelFormat < 0
+ || pixelFormat >= TJ.NUMPIXFORMATS)
+ throw new Exception("Invalid argument in setBitmapBuffer()");
+ bitmapBuf = buf;
+ bitmapWidth = width;
+ if(pitch == 0) bitmapPitch = width * TJ.getPixelSize(pixelFormat);
+ else bitmapPitch = pitch;
+ bitmapHeight = height;
+ bitmapPixelFormat = pixelFormat;
+ }
+
+ public long compress(byte [] dstBuf, int jpegSubsamp, int jpegQual,
+ int flags) throws Exception {
+ return compress(bitmapBuf, bitmapWidth, bitmapPitch, bitmapHeight,
+ TJ.getPixelSize(bitmapPixelFormat), dstBuf, jpegSubsamp, jpegQual,
+ flags | TJ.getFlags(bitmapPixelFormat));
+ }
+
public void close() throws Exception {
destroy();
}
private native void destroy() throws Exception;
// JPEG size in bytes is returned
- public native long compress(byte [] srcbuf, int width, int pitch,
- int height, int pixelsize, byte [] dstbuf, int jpegsubsamp, int jpegqual,
+ private native long compress(byte [] srcBuf, int width, int pitch,
+ int height, int pixelSize, byte [] dstbuf, int jpegSubsamp, int jpegQual,
int flags) throws Exception;
static {
}
private long handle = 0;
+ private byte [] bitmapBuf = null;
+ private int bitmapWidth = 0;
+ private int bitmapHeight = 0;
+ private int bitmapPitch = 0;
+ private int bitmapPixelFormat = -1;
};
init();
}
+ public TJDecompressor(byte [] buf) throws Exception {
+ setJPEGBuffer(buf);
+ }
+
+ public void setJPEGBuffer(byte [] buf) throws Exception {
+ if(handle == 0) init();
+ if(buf == null) throw new Exception("Invalid argument in setJPEGBuffer()");
+ jpegBuf = buf;
+ decompressHeader();
+ }
+
+ public int getWidth() throws Exception {
+ if(header.width < 1) throw new Exception("JPEG buffer not initialized");
+ return header.width;
+ }
+
+ public int getHeight() throws Exception {
+ if(header.height < 1) throw new Exception("JPEG buffer not initialized");
+ return header.height;
+ }
+
+ public int getSubsamp() throws Exception {
+ if(header.subsamp < 0) throw new Exception("JPEG buffer not initialized");
+ return header.subsamp;
+ }
+
+ public int getScaledWidth(int desired_width, int desired_height)
+ throws Exception {
+ if(header.width < 1 || header.height < 1)
+ throw new Exception("JPEG buffer not initialized");
+ return getScaledWidth(header.width, header.height, desired_width,
+ desired_height);
+ }
+
+ public int getScaledHeight(int output_width, int output_height)
+ throws Exception {
+ if(header.width < 1 || header.height < 1)
+ throw new Exception("JPEG buffer not initialized");
+ return getScaledHeight(header.width, header.height, output_width,
+ output_height);
+ }
+
+ public void decompress(byte [] dstBuf, int width, int pitch,
+ int height, int pixelFormat, int flags) throws Exception {
+ if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
+ decompress(jpegBuf, jpegBuf.length, dstBuf, width, pitch, height,
+ TJ.getPixelSize(pixelFormat), flags | TJ.getFlags(pixelFormat));
+ }
+
+ public byte [] decompress(int width, int pitch, int height,
+ int pixelFormat, int flags) throws Exception {
+ if(width < 0 || height < 0 || pitch < 0 || pixelFormat < 0
+ || pixelFormat >= TJ.NUMPIXFORMATS)
+ throw new Exception("Invalid argument in decompress()");
+ int pixelSize = TJ.getPixelSize(pixelFormat);
+ int scaledWidth = getScaledWidth(width, height);
+ int scaledHeight = getScaledHeight(width, height);
+ if(pitch == 0) pitch = scaledWidth * pixelSize;
+ long bufSize;
+ if(pixelFormat == TJ.YUV)
+ bufSize = TJ.bufSizeYUV(width, height, header.subsamp);
+ else bufSize = pitch * scaledHeight;
+ byte [] buf = new byte[(int)bufSize];
+ if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
+ decompress(jpegBuf, jpegBuf.length, buf, width, pitch, height,
+ TJ.getPixelSize(pixelFormat), flags | TJ.getFlags(pixelFormat));
+ return buf;
+ }
+
public void close() throws Exception {
destroy();
}
private native void destroy() throws Exception;
- public native TJHeaderInfo decompressHeader(byte [] srcbuf, long size)
+ private native TJHeaderInfo decompressHeader(byte [] srcBuf, long size)
throws Exception;
- public native void decompress(byte [] srcbuf, long size, byte [] dstbuf,
- int width, int pitch, int height, int pixelsize, int flags)
+ private void decompressHeader() throws Exception {
+ header = decompressHeader(jpegBuf, jpegBuf.length);
+ }
+
+ private native void decompress(byte [] srcBuf, long size, byte [] dstBuf,
+ int width, int pitch, int height, int pixelSize, int flags)
throws Exception;
+ private native int getScaledWidth(int input_width, int input_height,
+ int output_width, int output_height) throws Exception;
+
+ private native int getScaledHeight(int input_width, int input_height,
+ int output_width, int output_height) throws Exception;
+
static {
System.loadLibrary("turbojpeg");
}
private long handle = 0;
+ private byte [] jpegBuf = null;
+ TJHeaderInfo header = null;
};
#ifdef __cplusplus
extern "C" {
#endif
+#undef org_libjpegturbo_turbojpeg_TJ_NUMSUBOPT
+#define org_libjpegturbo_turbojpeg_TJ_NUMSUBOPT 4L
#undef org_libjpegturbo_turbojpeg_TJ_SAMP444
#define org_libjpegturbo_turbojpeg_TJ_SAMP444 0L
#undef org_libjpegturbo_turbojpeg_TJ_SAMP422
#define org_libjpegturbo_turbojpeg_TJ_SAMP420 2L
#undef org_libjpegturbo_turbojpeg_TJ_GRAYSCALE
#define org_libjpegturbo_turbojpeg_TJ_GRAYSCALE 3L
+#undef org_libjpegturbo_turbojpeg_TJ_NUMPIXFORMATS
+#define org_libjpegturbo_turbojpeg_TJ_NUMPIXFORMATS 7L
+#undef org_libjpegturbo_turbojpeg_TJ_RGB
+#define org_libjpegturbo_turbojpeg_TJ_RGB 0L
#undef org_libjpegturbo_turbojpeg_TJ_BGR
#define org_libjpegturbo_turbojpeg_TJ_BGR 1L
+#undef org_libjpegturbo_turbojpeg_TJ_RGBX
+#define org_libjpegturbo_turbojpeg_TJ_RGBX 2L
+#undef org_libjpegturbo_turbojpeg_TJ_BGRX
+#define org_libjpegturbo_turbojpeg_TJ_BGRX 3L
+#undef org_libjpegturbo_turbojpeg_TJ_XBGR
+#define org_libjpegturbo_turbojpeg_TJ_XBGR 4L
+#undef org_libjpegturbo_turbojpeg_TJ_XRGB
+#define org_libjpegturbo_turbojpeg_TJ_XRGB 5L
+#undef org_libjpegturbo_turbojpeg_TJ_YUV
+#define org_libjpegturbo_turbojpeg_TJ_YUV 6L
#undef org_libjpegturbo_turbojpeg_TJ_BOTTOMUP
#define org_libjpegturbo_turbojpeg_TJ_BOTTOMUP 2L
#undef org_libjpegturbo_turbojpeg_TJ_FORCEMMX
#define org_libjpegturbo_turbojpeg_TJ_FORCESSE 16L
#undef org_libjpegturbo_turbojpeg_TJ_FORCESSE2
#define org_libjpegturbo_turbojpeg_TJ_FORCESSE2 32L
-#undef org_libjpegturbo_turbojpeg_TJ_ALPHAFIRST
-#define org_libjpegturbo_turbojpeg_TJ_ALPHAFIRST 64L
#undef org_libjpegturbo_turbojpeg_TJ_FORCESSE3
#define org_libjpegturbo_turbojpeg_TJ_FORCESSE3 128L
#undef org_libjpegturbo_turbojpeg_TJ_FASTUPSAMPLE
#define org_libjpegturbo_turbojpeg_TJ_FASTUPSAMPLE 256L
-#undef org_libjpegturbo_turbojpeg_TJ_YUV
-#define org_libjpegturbo_turbojpeg_TJ_YUV 512L
+#undef org_libjpegturbo_turbojpeg_TJ_TJ_BGR
+#define org_libjpegturbo_turbojpeg_TJ_TJ_BGR 1L
+#undef org_libjpegturbo_turbojpeg_TJ_TJ_ALPHAFIRST
+#define org_libjpegturbo_turbojpeg_TJ_TJ_ALPHAFIRST 64L
+#undef org_libjpegturbo_turbojpeg_TJ_TJ_YUV
+#define org_libjpegturbo_turbojpeg_TJ_TJ_YUV 512L
/*
* Class: org_libjpegturbo_turbojpeg_TJ
* Method: bufSize
JNIEXPORT jlong JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
(JNIEnv *, jclass, jint, jint);
+/*
+ * Class: org_libjpegturbo_turbojpeg_TJ
+ * Method: bufSizeYUV
+ * Signature: (III)J
+ */
+JNIEXPORT jlong JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV
+ (JNIEnv *, jclass, jint, jint, jint);
+
#ifdef __cplusplus
}
#endif
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
(JNIEnv *, jobject, jbyteArray, jlong, jbyteArray, jint, jint, jint, jint, jint);
+/*
+ * Class: org_libjpegturbo_turbojpeg_TJDecompressor
+ * Method: getScaledWidth
+ * Signature: (IIII)I
+ */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledWidth
+ (JNIEnv *, jobject, jint, jint, jint, jint);
+
+/*
+ * Class: org_libjpegturbo_turbojpeg_TJDecompressor
+ * Method: getScaledHeight
+ * Signature: (IIII)I
+ */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledHeight
+ (JNIEnv *, jobject, jint, jint, jint, jint);
+
#ifdef __cplusplus
}
#endif
JNIEXPORT jlong JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
(JNIEnv *env, jclass cls, jint width, jint height)
{
- return TJBUFSIZE(width, height);
+ jlong retval=TJBUFSIZE(width, height);
+ if(retval==-1) _throw(tjGetErrorStr());
+
+ bailout:
+ return retval;
+}
+
+JNIEXPORT jlong JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV
+ (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
+{
+ jlong retval=TJBUFSIZEYUV(width, height, subsamp);
+ if(retval==-1) _throw(tjGetErrorStr());
+
+ bailout:
+ return retval;
}
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
return;
}
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledWidth
+ (JNIEnv *env, jobject obj, jint input_width, jint input_height,
+ jint output_width, jint output_height)
+{
+ if(tjScaledSize(input_width, input_height, &output_width, &output_height)
+ ==-1)
+ _throw(tjGetErrorStr());
+
+ bailout:
+ return output_width;
+}
+
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledHeight
+ (JNIEnv *env, jobject obj, jint input_width, jint input_height,
+ jint output_width, jint output_height)
+{
+ if(tjScaledSize(input_width, input_height, &output_width, &output_height)
+ ==-1)
+ _throw(tjGetErrorStr());
+
+ bailout:
+ return output_height;
+}
+
JNIEXPORT jobject JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader
(JNIEnv *env, jobject obj, jbyteArray src, jlong size)
{
tjDestroy;
tjGetErrorStr;
Java_org_libjpegturbo_turbojpeg_TJ_bufSize;
+ Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV;
Java_org_libjpegturbo_turbojpeg_TJCompressor_init;
Java_org_libjpegturbo_turbojpeg_TJCompressor_compress;
Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy;
Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader;
Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress;
Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy;
+ Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledHeight;
+ Java_org_libjpegturbo_turbojpeg_TJDecompressor_getScaledWidth;
local:
*;
};