Streamline Java wrapper

This commit is contained in:
DRC
2011-02-22 10:27:31 +00:00
parent 892b81cb44
commit 1a9a4d13c7
8 changed files with 262 additions and 30 deletions

View File

@@ -32,22 +32,61 @@ final public class TJ {
// 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;
public native final static long bufSize(int width, int height);
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 bufSizeYUV(int width, int height,
int subsamp)
throws Exception;
};

View File

@@ -34,6 +34,32 @@ public class TJCompressor {
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();
}
@@ -53,8 +79,8 @@ public class TJCompressor {
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 {
@@ -62,4 +88,9 @@ public class TJCompressor {
}
private long handle = 0;
private byte [] bitmapBuf = null;
private int bitmapWidth = 0;
private int bitmapHeight = 0;
private int bitmapPitch = 0;
private int bitmapPixelFormat = -1;
};

View File

@@ -34,6 +34,75 @@ public class TJDecompressor {
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();
}
@@ -52,16 +121,28 @@ public class TJDecompressor {
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;
};