Implement YUV encode/decode methods at the Java level; Remove some of the arguments from the Java API and replace with get/set methods; General API cleanup; Fix BufferedImage grayscale tests in TJUnitTest


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@451 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/java/TJExample.java b/java/TJExample.java
index 238bc2f..fc4cb6b 100644
--- a/java/TJExample.java
+++ b/java/TJExample.java
@@ -150,10 +150,8 @@
           height = (height + scaleFactor - 1)/scaleFactor;
         }
 
-        if(!outFormat.equalsIgnoreCase("jpg")) {
-          img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-          tjd.decompress(img, 0);
-        }
+        if(!outFormat.equalsIgnoreCase("jpg"))
+          img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB, 0);
         else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, 0);
         tjd.close();
       }
@@ -175,14 +173,17 @@
           + " subsampling, quality = " + outQual);
         TJCompressor tjc = new TJCompressor();
         int jpegSize;
-        byte [] jpegBuf = new byte[TJ.bufSize(width, height)];
+        byte [] jpegBuf;
 
+        tjc.setSubsamp(outSubsamp);
+        tjc.setJPEGQuality(outQual);
         if(img != null)
-          jpegSize = tjc.compress(img, jpegBuf, outSubsamp, outQual, 0);
+          jpegBuf = tjc.compress(img, 0);
         else {
           tjc.setBitmapBuffer(bmpBuf, width, 0, height, TJ.PF_BGRX);
-          jpegSize = tjc.compress(jpegBuf, outSubsamp, outQual, 0);
+          jpegBuf = tjc.compress(0);
         }
+        jpegSize = tjc.getCompressedSize();
         tjc.close();
 
         file = new File(argv[1]);
diff --git a/java/TJUnitTest.java b/java/TJUnitTest.java
index 227729a..b9b792c 100644
--- a/java/TJUnitTest.java
+++ b/java/TJUnitTest.java
@@ -57,12 +57,6 @@
 
 	private final static String _pixformatstr[]=
 		{"RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale"};
-	private final static int _roffset[]=
-		{0, 2, 0, 2, 3, 1, 0};
-	private final static int _goffset[]=
-		{1, 1, 1, 1, 2, 2, 0};
-	private final static int _boffset[]=
-		{2, 0, 2, 0, 1, 3, 0};
 	private final static int biType[]=
 		{0, BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_INT_BGR,
 			BufferedImage.TYPE_INT_RGB, 0, 0, BufferedImage.TYPE_BYTE_GRAY};
@@ -104,10 +98,13 @@
 		{(byte)255, 0, 0}
 	};
 
-	private static void initbuf(byte [] buf, int w, int h, int pf, int flags)
+	private static void initbuf(byte [] buf, int w, int pitch, int h, int pf,
+		int flags) throws Exception
 	{
-		int roffset=_roffset[pf], goffset=_goffset[pf], boffset=_boffset[pf];
-		int ps=TJ.pixelSize[pf];
+		int roffset=TJ.getRedShift(pf)/8;
+		int goffset=TJ.getGreenShift(pf)/8;
+		int boffset=TJ.getBlueShift(pf)/8;
+		int ps=TJ.getPixelSize(pf);
 		int i, _i, j;
 
 		Arrays.fill(buf, (byte)0);
@@ -118,8 +115,8 @@
 				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
 				for(j=0; j<w; j++)
 				{
-					if(((_i/8)+(j/8))%2==0) buf[w*i+j]=(byte)255;
-					else buf[w*i+j]=76;
+					if(((_i/8)+(j/8))%2==0) buf[pitch*i+j]=(byte)255;
+					else buf[pitch*i+j]=76;
 				}
 			}
 			for(_i=16; _i<h; _i++)
@@ -127,8 +124,8 @@
 				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
 				for(j=0; j<w; j++)
 				{
-					if(((_i/8)+(j/8))%2==0) buf[w*i+j]=0;
-					else buf[w*i+j]=(byte)226;
+					if(((_i/8)+(j/8))%2==0) buf[pitch*i+j]=0;
+					else buf[pitch*i+j]=(byte)226;
 				}
 			}
 			return;
@@ -138,11 +135,11 @@
 			if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
 			for(j=0; j<w; j++)
 			{
-				buf[(w*i+j)*ps+roffset]=(byte)255;
+				buf[pitch*i+j*ps+roffset]=(byte)255;
 				if(((_i/8)+(j/8))%2==0)
 				{
-					buf[(w*i+j)*ps+goffset]=(byte)255;
-					buf[(w*i+j)*ps+boffset]=(byte)255;
+					buf[pitch*i+j*ps+goffset]=(byte)255;
+					buf[pitch*i+j*ps+boffset]=(byte)255;
 				}
 			}
 		}
@@ -153,49 +150,33 @@
 			{
 				if(((_i/8)+(j/8))%2!=0)
 				{
-					buf[(w*i+j)*ps+roffset]=(byte)255;
-					buf[(w*i+j)*ps+goffset]=(byte)255;
+					buf[pitch*i+j*ps+roffset]=(byte)255;
+					buf[pitch*i+j*ps+goffset]=(byte)255;
 				}
 			}
 		}
 	}
 
-	private static void initimg(BufferedImage img, int pf, int flags)
+	private static void initintbuf(int [] buf, int w, int pitch, int h, int pf,
+		int flags) throws Exception
 	{
-		int i, _i, j, w=img.getWidth(), h=img.getHeight(), pixel;
+		int rshift=TJ.getRedShift(pf);
+		int gshift=TJ.getGreenShift(pf);
+		int bshift=TJ.getBlueShift(pf);
+		int i, _i, j;
 
-		if(pf==TJ.PF_GRAY)
-		{
-			for(_i=0; _i<16; _i++)
-			{
-				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-				for(j=0; j<w; j++)
-				{
-					if(((_i/8)+(j/8))%2==0) pixel=0xFFFFFFFF;
-					else pixel=0xFF4C4C4C;
-					img.setRGB(j, i, pixel);
-				}
-			}
-			for(_i=16; _i<h; _i++)
-			{
-				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-				for(j=0; j<w; j++)
-				{
-					if(((_i/8)+(j/8))%2==0) pixel=0xFF000000;
-					else pixel=0xFFE2E2E2;
-					img.setRGB(j, i, pixel);
-				}
-			}
-			return;
-		}
+		Arrays.fill(buf, 0);
 		for(_i=0; _i<16; _i++)
 		{
 			if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
 			for(j=0; j<w; j++)
 			{
-				pixel=0xFFFF0000;
-				if(((_i/8)+(j/8))%2==0) pixel|=0x0000FFFF;
-				img.setRGB(j, i, pixel);
+				buf[pitch*i+j]=(255 << rshift);
+				if(((_i/8)+(j/8))%2==0)
+				{
+					buf[pitch*i+j]|=(255 << gshift);
+					buf[pitch*i+j]|=(255 << bshift);
+				}
 			}
 		}
 		for(_i=16; _i<h; _i++)
@@ -203,32 +184,38 @@
 			if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
 			for(j=0; j<w; j++)
 			{
-				pixel=0;
-				if(((_i/8)+(j/8))%2!=0) pixel=0xFFFFFF00;
-				img.setRGB(j, i, pixel);
+				if(((_i/8)+(j/8))%2!=0)
+				{
+					buf[pitch*i+j]=(255 << rshift);
+					buf[pitch*i+j]|=(255 << gshift);
+				}
 			}
 		}
 	}
 
-	private static void dumpbuf(byte [] buf, int w, int h, int pf,
-		int scalefactor, int flags)
+	private static void initimg(BufferedImage img, int pf, int flags)
+		throws Exception
 	{
-		int ps=TJ.pixelSize[pf];
-		int roffset=_roffset[pf], goffset=_goffset[pf], boffset=_boffset[pf];
-		int i, j;
-
-		System.out.print("\n");
-		for(i=0; i<h; i++)
+		WritableRaster wr=img.getRaster();
+		int imgtype=img.getType();
+		if(imgtype==BufferedImage.TYPE_INT_RGB
+			|| imgtype==BufferedImage.TYPE_INT_BGR)
 		{
-			for(j=0; j<w; j++)
-			{
-				int r=buf[(w*i+j)*ps+roffset];
-				int g=buf[(w*i+j)*ps+goffset];
-				int b=buf[(w*i+j)*ps+boffset];
-				if(r<0) r+=256;  if(g<0) g+=256;  if(b<0) b+=256;
-				System.out.format("%3d/%3d/%3d ", r, g, b);
-			}
-			System.out.print("\n");
+      SinglePixelPackedSampleModel sm=
+				(SinglePixelPackedSampleModel)img.getSampleModel();
+			int pitch=sm.getScanlineStride();
+			DataBufferInt db=(DataBufferInt)wr.getDataBuffer();
+			int [] buf = db.getData();
+			initintbuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
+		}
+		else
+		{
+			ComponentSampleModel sm=
+				(ComponentSampleModel)img.getSampleModel();
+			int pitch=sm.getScanlineStride();
+			DataBufferByte db=(DataBufferByte)wr.getDataBuffer();
+			byte [] buf = db.getData();
+			initbuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
 		}
 	}
 
@@ -265,98 +252,73 @@
 		}
 	}
 
-	private static int checkbuf(byte [] buf, int w, int h, int pf, int subsamp,
-		int scalefactor, int flags) throws Exception
+	private static int checkbuf(byte [] buf, int w, int pitch, int h, int pf,
+		int subsamp, int scalefactor, int flags) throws Exception
 	{
-		int roffset=_roffset[pf], goffset=_goffset[pf], boffset=_boffset[pf];
-		int ps=TJ.pixelSize[pf];
+		int roffset=TJ.getRedShift(pf)/8;
+		int goffset=TJ.getGreenShift(pf)/8;
+		int boffset=TJ.getBlueShift(pf)/8;
+		int ps=TJ.getPixelSize(pf);
 		int i, _i, j, retval=1;
 		int halfway=16/scalefactor, blocksize=8/scalefactor;
 
 		try
 		{
-			if(subsamp==TJ.SAMP_GRAY)
+			for(_i=0; _i<halfway; _i++)
 			{
-				for(_i=0; _i<halfway; _i++)
+				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
+				for(j=0; j<w; j++)
 				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
+					byte r=buf[pitch*i+j*ps+roffset], g=buf[pitch*i+j*ps+goffset],
+						b=buf[pitch*i+j*ps+boffset];
+					if(((_i/blocksize)+(j/blocksize))%2==0)
 					{
-						byte r=buf[(w*i+j)*ps+roffset],
-							g=buf[(w*i+j)*ps+goffset],
-							b=buf[(w*i+j)*ps+boffset];
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval255(_i, j, r, "R");
-							checkval255(_i, j, g, "G");
-							checkval255(_i, j, b, "B");
-						}
-						else
+						checkval255(_i, j, r, "R");
+						checkval255(_i, j, g, "G");
+						checkval255(_i, j, b, "B");
+					}
+					else
+					{
+						if(subsamp==TJ.SAMP_GRAY)
 						{
 							checkval(_i, j, r, "R", 76);
 							checkval(_i, j, g, "G", 76);
 							checkval(_i, j, b, "B", 76);
 						}
-					}
-				}
-				for(_i=halfway; _i<h; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						byte r=buf[(w*i+j)*ps+roffset],
-							g=buf[(w*i+j)*ps+goffset],
-							b=buf[(w*i+j)*ps+boffset];
-						if(((_i/blocksize)+(j/blocksize))%2==0)
+						else
 						{
-							checkval0(_i, j, r, "R");
+							checkval255(_i, j, r, "R");
 							checkval0(_i, j, g, "G");
 							checkval0(_i, j, b, "B");
 						}
-						else
+					}
+				}
+			}
+			for(_i=halfway; _i<h; _i++)
+			{
+				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
+				for(j=0; j<w; j++)
+				{
+					byte r=buf[pitch*i+j*ps+roffset], g=buf[pitch*i+j*ps+goffset],
+						b=buf[pitch*i+j*ps+boffset];
+					if(((_i/blocksize)+(j/blocksize))%2==0)
+					{
+						checkval0(_i, j, r, "R");
+						checkval0(_i, j, g, "G");
+					}
+					else
+					{
+						if(subsamp==TJ.SAMP_GRAY)
 						{
 							checkval(_i, j, r, "R", 226);
 							checkval(_i, j, g, "G", 226);
 							checkval(_i, j, b, "B", 226);
 						}
-					}
-				}
-			}
-			else
-			{
-				for(_i=0; _i<halfway; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						checkval255(_i, j, buf[(w*i+j)*ps+roffset], "R");
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval255(_i, j, buf[(w*i+j)*ps+goffset], "G");
-							checkval255(_i, j, buf[(w*i+j)*ps+boffset], "B");
-						}
 						else
 						{
-							checkval0(_i, j, buf[(w*i+j)*ps+goffset], "G");
-							checkval0(_i, j, buf[(w*i+j)*ps+boffset], "B");
-						}
-					}
-				}
-				for(_i=halfway; _i<h; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						checkval0(_i, j, buf[(w*i+j)*ps+boffset], "B");
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval0(_i, j, buf[(w*i+j)*ps+roffset], "R");
-							checkval0(_i, j, buf[(w*i+j)*ps+goffset], "G");
-						}
-						else
-						{
-							checkval255(_i, j, buf[(w*i+j)*ps+roffset], "R");
-							checkval255(_i, j, buf[(w*i+j)*ps+goffset], "G");
+							checkval255(_i, j, r, "R");
+							checkval255(_i, j, g, "G");
+							checkval0(_i, j, b, "B");							
 						}
 					}
 				}
@@ -367,116 +329,150 @@
 			System.out.println(e);
 			retval=0;
 		}
+
+		if(retval==0)
+		{
+			System.out.print("\n");
+			for(i=0; i<h; i++)
+			{
+				for(j=0; j<w; j++)
+				{
+					int r=buf[pitch*i+j*ps+roffset];
+					int g=buf[pitch*i+j*ps+goffset];
+					int b=buf[pitch*i+j*ps+boffset];
+					if(r<0) r+=256;  if(g<0) g+=256;  if(b<0) b+=256;
+					System.out.format("%3d/%3d/%3d ", r, g, b);
+				}
+				System.out.print("\n");
+			}
+		}
+		return retval;
+	}
+
+	private static int checkintbuf(int [] buf, int w, int pitch, int h, int pf,
+		int subsamp, int scalefactor, int flags) throws Exception
+	{
+		int rshift=TJ.getRedShift(pf);
+		int gshift=TJ.getGreenShift(pf);
+		int bshift=TJ.getBlueShift(pf);
+		int i, _i, j, retval=1;
+		int halfway=16/scalefactor, blocksize=8/scalefactor;
+
+		try
+		{
+			for(_i=0; _i<halfway; _i++)
+			{
+				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
+				for(j=0; j<w; j++)
+				{
+					int r=(buf[pitch*i+j] >> rshift) & 0xFF;
+					int g=(buf[pitch*i+j] >> gshift) & 0xFF;
+					int b=(buf[pitch*i+j] >> bshift) & 0xFF;
+					if(((_i/blocksize)+(j/blocksize))%2==0)
+					{
+						checkval255(_i, j, r, "R");
+						checkval255(_i, j, g, "G");
+						checkval255(_i, j, b, "B");
+					}
+					else
+					{
+						if(subsamp==TJ.SAMP_GRAY)
+						{
+							checkval(_i, j, r, "R", 76);
+							checkval(_i, j, g, "G", 76);
+							checkval(_i, j, b, "B", 76);
+						}
+						else
+						{
+							checkval255(_i, j, r, "R");
+							checkval0(_i, j, g, "G");
+							checkval0(_i, j, b, "B");
+						}
+					}
+				}
+			}
+			for(_i=halfway; _i<h; _i++)
+			{
+				if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
+				for(j=0; j<w; j++)
+				{
+					int r=(buf[pitch*i+j] >> rshift) & 0xFF;
+					int g=(buf[pitch*i+j] >> gshift) & 0xFF;
+					int b=(buf[pitch*i+j] >> bshift) & 0xFF;
+					if(((_i/blocksize)+(j/blocksize))%2==0)
+					{
+						checkval0(_i, j, r, "R");
+						checkval0(_i, j, g, "G");
+					}
+					else
+					{
+						if(subsamp==TJ.SAMP_GRAY)
+						{
+							checkval(_i, j, r, "R", 226);
+							checkval(_i, j, g, "G", 226);
+							checkval(_i, j, b, "B", 226);
+						}
+						else
+						{
+							checkval255(_i, j, r, "R");
+							checkval255(_i, j, g, "G");
+							checkval0(_i, j, b, "B");
+						}
+					}
+				}
+			}
+		}
+		catch(Exception e)
+		{
+			System.out.println(e);
+			retval=0;
+		}
+
+		if(retval==0)
+		{
+			System.out.print("\n");
+			for(i=0; i<h; i++)
+			{
+				for(j=0; j<w; j++)
+				{
+					int r=(buf[pitch*i+j] >> rshift) & 0xFF;
+					int g=(buf[pitch*i+j] >> gshift) & 0xFF;
+					int b=(buf[pitch*i+j] >> bshift) & 0xFF;
+					if(r<0) r+=256;  if(g<0) g+=256;  if(b<0) b+=256;
+					System.out.format("%3d/%3d/%3d ", r, g, b);
+				}
+				System.out.print("\n");
+			}
+		}
 		return retval;
 	}
 
 	private static int checkimg(BufferedImage img, int pf,
 		int subsamp, int scalefactor, int flags) throws Exception
 	{
-		int i, _i, j, retval=1, w=img.getWidth(), h=img.getHeight();
-		int halfway=16/scalefactor, blocksize=8/scalefactor;
-
-		try
+		WritableRaster wr=img.getRaster();
+		int imgtype=img.getType();
+		if(imgtype==BufferedImage.TYPE_INT_RGB
+			|| imgtype==BufferedImage.TYPE_INT_BGR)
 		{
-			if(subsamp==TJ.SAMP_GRAY)
-			{
-				for(_i=0; _i<halfway; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						int r=(img.getRGB(j, i)>>16) & 0xFF;
-						int g=(img.getRGB(j, i)>>8) & 0xFF;
-						int b=img.getRGB(j, i) & 0xFF;
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval255(_i, j, r, "R");
-							checkval255(_i, j, g, "G");
-							checkval255(_i, j, b, "B");
-						}
-						else
-						{
-							checkval(_i, j, r, "R", 76);
-							checkval(_i, j, g, "G", 76);
-							checkval(_i, j, b, "B", 76);
-						}
-					}
-				}
-				for(_i=halfway; _i<h; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						int r=(img.getRGB(j, i)>>16) & 0xFF;
-						int g=(img.getRGB(j, i)>>8) & 0xFF;
-						int b=img.getRGB(j, i) & 0xFF;
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval0(_i, j, r, "R");
-							checkval0(_i, j, g, "G");
-							checkval0(_i, j, b, "B");
-						}
-						else
-						{
-							checkval(_i, j, r, "R", 226);
-							checkval(_i, j, g, "G", 226);
-							checkval(_i, j, b, "B", 226);
-						}
-					}
-				}
-			}
-			else
-			{
-				for(_i=0; _i<halfway; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						int r=(img.getRGB(j, i)>>16) & 0xFF;
-						int g=(img.getRGB(j, i)>>8) & 0xFF;
-						int b=img.getRGB(j, i) & 0xFF;
-						checkval255(_i, j, r, "R");
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval255(_i, j, g, "G");
-							checkval255(_i, j, b, "B");
-						}
-						else
-						{
-							checkval0(_i, j, g, "G");
-							checkval0(_i, j, b, "B");
-						}
-					}
-				}
-				for(_i=halfway; _i<h; _i++)
-				{
-					if((flags&TJ.BOTTOMUP)!=0) i=h-_i-1;  else i=_i;
-					for(j=0; j<w; j++)
-					{
-						int r=(img.getRGB(j, i)>>16) & 0xFF;
-						int g=(img.getRGB(j, i)>>8) & 0xFF;
-						int b=img.getRGB(j, i) & 0xFF;
-						checkval0(_i, j, b, "B");
-						if(((_i/blocksize)+(j/blocksize))%2==0)
-						{
-							checkval0(_i, j, r, "R");
-							checkval0(_i, j, g, "G");
-						}
-						else
-						{
-							checkval255(_i, j, r, "R");
-							checkval255(_i, j, g, "G");
-						}
-					}
-				}
-			}
+      SinglePixelPackedSampleModel sm=
+				(SinglePixelPackedSampleModel)img.getSampleModel();
+			int pitch=sm.getScanlineStride();
+			DataBufferInt db=(DataBufferInt)wr.getDataBuffer();
+			int [] buf = db.getData();
+			return checkintbuf(buf, img.getWidth(), pitch, img.getHeight(), pf,
+				subsamp, scalefactor, flags);
 		}
-		catch(Exception e)
+		else
 		{
-			System.out.println(e);
-			retval=0;
+			ComponentSampleModel sm=
+				(ComponentSampleModel)img.getSampleModel();
+			int pitch=sm.getScanlineStride();
+			DataBufferByte db=(DataBufferByte)wr.getDataBuffer();
+			byte [] buf = db.getData();
+			return checkbuf(buf, img.getWidth(), pitch, img.getHeight(), pf, subsamp,
+				scalefactor, flags);
 		}
-		return retval;
 	}
 
 	private static int PAD(int v, int p)
@@ -616,9 +612,7 @@
 	{
 		String tempstr;  byte [] bmpbuf=null;  BufferedImage img=null;
 		String pixformat;  double t;
-		int size=0, ps=TJ.pixelSize[pf];
-
-		if(yuv==YUVENCODE) flags|=TJ.YUV;
+		int size=0, ps=TJ.getPixelSize(pf);
 
 		pixformat=_pixformatstr[pf];
 
@@ -642,18 +636,25 @@
 		else
 		{
 			bmpbuf=new byte[w*h*ps+1];
-			initbuf(bmpbuf, w, h, pf, flags);
+			initbuf(bmpbuf, w, w*ps, h, pf, flags);
 		}
 		Arrays.fill(jpegbuf, (byte)0);
 
 		t=gettime();
+		tjc.setSubsamp(subsamp);
+		tjc.setJPEGQuality(qual);
 		if(bi)
-			size=tjc.compress(img, jpegbuf, subsamp, qual, flags);
+		{
+			if(yuv==YUVENCODE) tjc.encodeYUV(img, jpegbuf, flags);
+			else tjc.compress(img, jpegbuf, flags);
+		}
 		else
 		{
 			tjc.setBitmapBuffer(bmpbuf, w, 0, h, pf);
-			size=tjc.compress(jpegbuf, subsamp, qual, flags);
+			if(yuv==YUVENCODE) tjc.encodeYUV(jpegbuf, flags);
+			else tjc.compress(jpegbuf, flags);
 		}
+		size=tjc.getCompressedSize();
 		t=gettime()-t;
 
 		if(yuv==YUVENCODE)
@@ -689,8 +690,7 @@
 		int temp1, temp2;
 		BufferedImage img=null;  byte [] bmpbuf=null;
 
-		if(yuv==YUVDECODE) flags|=TJ.YUV;
-		else if(yuv==YUVENCODE) return;
+		if(yuv==YUVENCODE) return;
 
 		pixformat=_pixformatstr[pf];
 		System.out.print("JPEG -> ");
@@ -716,13 +716,12 @@
 		if(temp1!=scaledw || temp2!=scaledh)
 			throw new Exception("Scaled size mismatch");
 
-		if(bi)
-		{
-			img=new BufferedImage(scaledw, scaledh, biType[pf]);
-			tjd.decompress(img, flags);
-		}
+		if(yuv==YUVDECODE) bmpbuf=tjd.decompressToYUV(flags);
 		else
-			bmpbuf=tjd.decompress(scaledw, 0, scaledh, pf, flags);
+		{
+			if(bi) img=tjd.decompress(scaledw, scaledh, biType[pf], flags);
+			else bmpbuf=tjd.decompress(scaledw, 0, scaledh, pf, flags);
+		}
 		t=gettime()-t;
 
 		if(bi)
@@ -743,14 +742,12 @@
 		else
 		{
 			if((bi && checkimg(img, pf, subsamp, scalefactor, flags)==1)
-				|| (!bi && checkbuf(bmpbuf, scaledw, scaledh, pf, subsamp, scalefactor,
-				flags)==1))
+				|| (!bi && checkbuf(bmpbuf, scaledw, scaledw*TJ.getPixelSize(pf),
+					scaledh, pf, subsamp, scalefactor, flags)==1))
 				System.out.print("Passed.");
 			else
 			{
 				System.out.print("FAILED!");  exitstatus=-1;
-				if(bmpbuf!=null)
-					dumpbuf(bmpbuf, scaledw, scaledh, pf, scalefactor, flags);
 			}
 		}
 		System.out.format("  %.6f ms\n", t*1000.);
@@ -778,8 +775,10 @@
 	{
 		TJCompressor tjc=null;  TJDecompressor tjd=null;
 		int size;  int pfstart, pfend;
+		byte [] jpegbuf;
 
-		byte [] jpegbuf=new byte[TJ.bufSize(w, h)];
+		if(yuv==YUVENCODE) jpegbuf=new byte[TJ.bufSizeYUV(w, h, subsamp)];
+		else jpegbuf=new byte[TJ.bufSize(w, h)];
 
 		try
 		{
@@ -821,7 +820,7 @@
 	private static void dotest1() throws Exception
 	{
 		int i, j, i2;  byte [] bmpbuf, jpgbuf;
-		TJCompressor tjc=null;  int size;
+		TJCompressor tjc=null;
 
 		try
 		{
@@ -843,7 +842,9 @@
 						bmpbuf[i2*4+2]=pixels[i2%9][0];
 					}
 					tjc.setBitmapBuffer(bmpbuf, i, 0, j, TJ.PF_BGRX);
-					size=tjc.compress(jpgbuf, TJ.SAMP_444, 100, 0);
+					tjc.setSubsamp(TJ.SAMP_444);
+					tjc.setJPEGQuality(100);
+					tjc.compress(jpgbuf, 0);
 
 					bmpbuf=new byte[j*i*4];
 					jpgbuf=new byte[TJ.bufSize(j, i)];
@@ -853,7 +854,7 @@
 						else bmpbuf[i2*4]=bmpbuf[i2*4+1]=bmpbuf[i2*4+2]=0;
 					}
 					tjc.setBitmapBuffer(bmpbuf, j, 0, i, TJ.PF_BGRX);
-					size=tjc.compress(jpgbuf, TJ.SAMP_444, 100, 0);
+					tjc.compress(jpgbuf, 0);
 				}
 			}
 			System.out.println("Done.      ");
@@ -879,23 +880,21 @@
 					usage();
 				if(argv[i].equalsIgnoreCase("-bi")) bi=true;
 			}
-			if(bi && doyuv)
-				throw new Exception("-bi and -yuv cannot be used together.");
 			if(doyuv) yuv=YUVENCODE;
 			dotest(35, 39, bi? _3byteFormatsBI:_3byteFormats, TJ.SAMP_444, "test");
 			dotest(39, 41, bi? _4byteFormatsBI:_4byteFormats, TJ.SAMP_444, "test");
 			if(doyuv)
 			{
-				dotest(41, 35, _3byteFormats, TJ.SAMP_422, "test");
-				dotest(35, 39, _4byteFormats, TJ.SAMP_422, "test");
-				dotest(39, 41, _3byteFormats, TJ.SAMP_420, "test");
-				dotest(41, 35, _4byteFormats, TJ.SAMP_420, "test");
+				dotest(41, 35, bi? _3byteFormatsBI:_3byteFormats, TJ.SAMP_422, "test");
+				dotest(35, 39, bi? _4byteFormatsBI:_4byteFormats, TJ.SAMP_422, "test");
+				dotest(39, 41, bi? _3byteFormatsBI:_3byteFormats, TJ.SAMP_420, "test");
+				dotest(41, 35, bi? _4byteFormatsBI:_4byteFormats, TJ.SAMP_420, "test");
 			}
 			dotest(35, 39, _onlyGray, TJ.SAMP_GRAY, "test");
 			dotest(39, 41, bi? _3byteFormatsBI:_3byteFormats, TJ.SAMP_GRAY, "test");
 			dotest(41, 35, bi? _4byteFormatsBI:_4byteFormats, TJ.SAMP_GRAY, "test");
 			if(!doyuv && !bi) dotest1();
-			if(doyuv)
+			if(doyuv && !bi)
 			{
 				yuv=YUVDECODE;
 				dotest(48, 48, _onlyRGB, TJ.SAMP_444, "test");
@@ -904,8 +903,10 @@
 				dotest(39, 41, _onlyRGB, TJ.SAMP_422, "test");
 				dotest(48, 48, _onlyRGB, TJ.SAMP_420, "test");
 				dotest(41, 35, _onlyRGB, TJ.SAMP_420, "test");
+				dotest(48, 48, _onlyRGB, TJ.SAMP_GRAY, "test");
+				dotest(35, 39, _onlyRGB, TJ.SAMP_GRAY, "test");
 				dotest(48, 48, _onlyGray, TJ.SAMP_GRAY, "test");
-				dotest(35, 39, _onlyGray, TJ.SAMP_GRAY, "test");
+				dotest(39, 41, _onlyGray, TJ.SAMP_GRAY, "test");
 			}
 		}
 		catch(Exception e)
diff --git a/java/org/libjpegturbo/turbojpeg/TJ.java b/java/org/libjpegturbo/turbojpeg/TJ.java
index 9951fa2..332426a 100644
--- a/java/org/libjpegturbo/turbojpeg/TJ.java
+++ b/java/org/libjpegturbo/turbojpeg/TJ.java
@@ -49,7 +49,7 @@
     PF_XRGB  = 5,
     PF_GRAY  = 6;
 
-  final public static int pixelSize[] = {
+  final private static int pixelSize[] = {
     3, 3, 4, 4, 4, 4, 1
   };
 
@@ -59,6 +59,36 @@
     return pixelSize[pixelFormat];
   }
 
+  final private static int redShift[] = {
+    0, 16, 0, 16, 24, 8, 0
+  };
+
+  final public static int getRedShift(int pixelFormat) throws Exception {
+    if(pixelFormat < 0 || pixelFormat >= NUMPFOPT)
+      throw new Exception("Invalid pixel format");
+    return redShift[pixelFormat];
+  }
+
+  final private static int greenShift[] = {
+    8, 8, 8, 8, 16, 16, 0
+  };
+
+  final public static int getGreenShift(int pixelFormat) throws Exception {
+    if(pixelFormat < 0 || pixelFormat >= NUMPFOPT)
+      throw new Exception("Invalid pixel format");
+    return greenShift[pixelFormat];
+  }
+
+  final private static int blueShift[] = {
+    16, 0, 16, 0, 8, 24, 0
+  };
+
+  final public static int getBlueShift(int pixelFormat) throws Exception {
+    if(pixelFormat < 0 || pixelFormat >= NUMPFOPT)
+      throw new Exception("Invalid pixel format");
+    return blueShift[pixelFormat];
+  }
+
   // Flags
   final public static int
     BOTTOMUP     = 2,
@@ -66,22 +96,7 @@
     FORCESSE     = 16,
     FORCESSE2    = 32,
     FORCESSE3    = 128,
-    FASTUPSAMPLE = 256,
-    YUV          = 512;
-
-  final private static int
-    TJ_BGR        = 1,
-    TJ_ALPHAFIRST = 64;
-
-  final private static int flags[] = {
-    0, TJ_BGR, 0, TJ_BGR, TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST, 0
-  };
-
-  final public static int getFlags(int pixelFormat) throws Exception {
-    if(pixelFormat < 0 || pixelFormat >= NUMPFOPT)
-      throw new Exception("Invalid pixel format");
-    return flags[pixelFormat];
-  }
+    FASTUPSAMPLE = 256;
 
   public native final static int bufSize(int width, int height)
     throws Exception;
diff --git a/java/org/libjpegturbo/turbojpeg/TJCompressor.java b/java/org/libjpegturbo/turbojpeg/TJCompressor.java
index 0fcffac..d0c1910 100644
--- a/java/org/libjpegturbo/turbojpeg/TJCompressor.java
+++ b/java/org/libjpegturbo/turbojpeg/TJCompressor.java
@@ -55,18 +55,40 @@
     bitmapPixelFormat = pixelFormat;
   }
 
-  public int 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 setSubsamp(int newSubsamp) throws Exception {
+    if(newSubsamp < 0 || newSubsamp >= TJ.NUMSAMPOPT)
+      throw new Exception ("Invalid argument in setSubsamp()");
+    subsamp = newSubsamp;
   }
 
-  public int compress(BufferedImage srcImage, byte [] dstBuf, int jpegSubsamp,
-    int jpegQual, int flags) throws Exception {
-    if(srcImage == null || flags < 0)
-      throw new Exception("Invalid argument in decompress()");
-    flags &= ~(TJ.YUV);
+  public void setJPEGQuality(int quality) throws Exception {
+    if(quality < 1 || quality > 100)
+      throw new Exception ("Invalid argument in setJPEGQuality()");
+    jpegQuality = quality;
+  }
+
+  public void compress(byte [] dstBuf, int flags) throws Exception {
+    if(dstBuf == null || flags < 0)
+      throw new Exception("Invalid argument in compress()");
+    if(bitmapBuf == null) throw new Exception("Bitmap buffer not initialized");
+    if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
+    compressedSize = compress(bitmapBuf, bitmapWidth, bitmapPitch,
+      bitmapHeight, bitmapPixelFormat, dstBuf, subsamp, jpegQuality, flags);
+  }
+
+  public byte [] compress(int flags) throws Exception {
+    if(bitmapWidth < 1 || bitmapHeight < 1)
+      throw new Exception("Bitmap buffer not initialized");
+    byte [] buf = new byte[TJ.bufSize(bitmapWidth, bitmapHeight)];
+    compress(buf, flags);
+    return buf;
+  }
+
+  public void compress(BufferedImage srcImage, byte [] dstBuf, int flags)
+    throws Exception {
+    if(srcImage == null || dstBuf == null || flags < 0)
+      throw new Exception("Invalid argument in compress()");
     int width = srcImage.getWidth();
     int height = srcImage.getHeight();
     int pixelFormat;  boolean intPixels=false;
@@ -83,29 +105,117 @@
         throw new Exception("Unsupported BufferedImage format");
     }
     WritableRaster wr = srcImage.getRaster();
+    if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
     if(intPixels) {
       SinglePixelPackedSampleModel sm =
         (SinglePixelPackedSampleModel)srcImage.getSampleModel();
       int pitch = sm.getScanlineStride();
       DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
       int [] buf = db.getData();
-      return compress(buf, width, pitch, height, dstBuf, jpegSubsamp, jpegQual,
-        flags | TJ.getFlags(pixelFormat));
+      compressedSize = compress(buf, width, pitch, height, pixelFormat, dstBuf,
+        subsamp, jpegQuality, flags);
     }
     else {
       ComponentSampleModel sm =
         (ComponentSampleModel)srcImage.getSampleModel();
       int pixelSize = sm.getPixelStride();
-      if(pixelSize != TJ.pixelSize[pixelFormat])
+      if(pixelSize != TJ.getPixelSize(pixelFormat))
         throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
       int pitch = sm.getScanlineStride();
       DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
       byte [] buf = db.getData();
-      return compress(buf, width, pitch, height, TJ.getPixelSize(pixelFormat),
-        dstBuf, jpegSubsamp, jpegQual, flags | TJ.getFlags(pixelFormat));
+      compressedSize = compress(buf, width, pitch, height, pixelFormat, dstBuf,
+        subsamp, jpegQuality, flags);
     }
   }
 
+  public byte [] compress(BufferedImage srcImage, int flags) throws Exception {
+    int width = srcImage.getWidth();
+    int height = srcImage.getHeight();
+    byte [] buf = new byte[TJ.bufSize(width, height)];
+    compress(srcImage, buf, flags);
+    return buf;
+  }
+
+  public void encodeYUV(byte [] dstBuf, int flags) throws Exception {
+    if(dstBuf == null || flags < 0)
+      throw new Exception("Invalid argument in compress()");
+    if(bitmapBuf == null) throw new Exception("Bitmap buffer not initialized");
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
+    encodeYUV(bitmapBuf, bitmapWidth, bitmapPitch, bitmapHeight,
+      bitmapPixelFormat, dstBuf, subsamp, flags);
+    compressedSize = TJ.bufSizeYUV(bitmapWidth, bitmapHeight, subsamp);
+  }
+
+  public byte [] encodeYUV(int flags) throws Exception {
+    if(bitmapWidth < 1 || bitmapHeight < 1)
+      throw new Exception("Bitmap buffer not initialized");
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
+    byte [] buf = new byte[TJ.bufSizeYUV(bitmapWidth, bitmapHeight, subsamp)];
+    encodeYUV(buf, flags);
+    return buf;
+  }
+
+  public void encodeYUV(BufferedImage srcImage, byte [] dstBuf, int flags)
+    throws Exception {
+    if(srcImage == null || dstBuf == null || flags < 0)
+      throw new Exception("Invalid argument in encodeYUV()");
+    int width = srcImage.getWidth();
+    int height = srcImage.getHeight();
+    int pixelFormat;  boolean intPixels=false;
+    switch(srcImage.getType()) {
+      case BufferedImage.TYPE_3BYTE_BGR:
+        pixelFormat=TJ.PF_BGR;  break;
+      case BufferedImage.TYPE_BYTE_GRAY:
+        pixelFormat=TJ.PF_GRAY;  break;
+      case BufferedImage.TYPE_INT_BGR:
+        pixelFormat=TJ.PF_RGBX;  intPixels=true;  break;
+      case BufferedImage.TYPE_INT_RGB:
+        pixelFormat=TJ.PF_BGRX;  intPixels=true;  break;
+      default:
+        throw new Exception("Unsupported BufferedImage format");
+    }
+    WritableRaster wr = srcImage.getRaster();
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
+    if(intPixels) {
+      SinglePixelPackedSampleModel sm =
+        (SinglePixelPackedSampleModel)srcImage.getSampleModel();
+      int pitch = sm.getScanlineStride();
+      DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
+      int [] buf = db.getData();
+      encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
+        flags);
+    }
+    else {
+      ComponentSampleModel sm =
+        (ComponentSampleModel)srcImage.getSampleModel();
+      int pixelSize = sm.getPixelStride();
+      if(pixelSize != TJ.getPixelSize(pixelFormat))
+        throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
+      int pitch = sm.getScanlineStride();
+      DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
+      byte [] buf = db.getData();
+      encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
+        flags);
+    }
+    compressedSize = TJ.bufSizeYUV(width, height, subsamp);
+  }
+
+  public byte [] encodeYUV(BufferedImage srcImage, int flags)
+    throws Exception {
+    if(subsamp < 0) throw new Exception("Subsampling level not set");
+    int width = srcImage.getWidth();
+    int height = srcImage.getHeight();
+    byte [] buf = new byte[TJ.bufSizeYUV(width, height, subsamp)];
+    encodeYUV(srcImage, buf, flags);
+    return buf;
+  }
+
+  public int getCompressedSize() {
+    return compressedSize;
+  }
+
   public void close() throws Exception {
     destroy();
   }
@@ -126,11 +236,19 @@
 
   // JPEG size in bytes is returned
   private native int compress(byte [] srcBuf, int width, int pitch,
-    int height, int pixelSize, byte [] dstbuf, int jpegSubsamp, int jpegQual,
+    int height, int pixelFormat, byte [] dstbuf, int jpegSubsamp, int jpegQual,
     int flags) throws Exception;
 
   private native int compress(int [] srcBuf, int width, int pitch,
-    int height, byte [] dstbuf, int jpegSubsamp, int jpegQual, int flags)
+    int height, int pixelFormat, byte [] dstbuf, int jpegSubsamp, int jpegQual,
+    int flags) throws Exception;
+
+  private native void encodeYUV(byte [] srcBuf, int width, int pitch,
+    int height, int pixelFormat, byte [] dstbuf, int subsamp, int flags)
+    throws Exception;
+
+  private native void encodeYUV(int [] srcBuf, int width, int pitch,
+    int height, int pixelFormat, byte [] dstbuf, int subsamp, int flags)
     throws Exception;
 
   static {
@@ -143,4 +261,7 @@
   private int bitmapHeight = 0;
   private int bitmapPitch = 0;
   private int bitmapPixelFormat = -1;
+  private int subsamp = -1;
+  private int jpegQuality = -1;
+  private int compressedSize = 0;
 };
diff --git a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
index 9cd2fe6..fdc1e4e 100644
--- a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
+++ b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
@@ -74,6 +74,8 @@
     throws Exception {
     if(jpegWidth < 1 || jpegHeight < 1)
       throw new Exception("JPEG buffer not initialized");
+    if(desiredWidth < 0 || desiredHeight < 0)
+      throw new Exception("Invalid argument in getScaledWidth()");
     return getScaledWidth(jpegWidth, jpegHeight, desiredWidth,
       desiredHeight);
   }
@@ -82,6 +84,8 @@
     throws Exception {
     if(jpegWidth < 1 || jpegHeight < 1)
       throw new Exception("JPEG buffer not initialized");
+    if(desiredWidth < 0 || desiredHeight < 0)
+      throw new Exception("Invalid argument in getScaledHeight()");
     return getScaledHeight(jpegWidth, jpegHeight, desiredWidth,
       desiredHeight);
   }
@@ -89,35 +93,49 @@
   public void decompress(byte [] dstBuf, int desiredWidth, int pitch,
     int desiredHeight, int pixelFormat, int flags) throws Exception {
     if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
+    if(dstBuf == null || desiredWidth < 0 || pitch < 0 || desiredHeight < 0
+      || pixelFormat < 0 || pixelFormat >= TJ.NUMPFOPT || flags < 0)
+      throw new Exception("Invalid argument in decompress()");
     decompress(jpegBuf, jpegBufSize, dstBuf, desiredWidth, pitch,
-      desiredHeight, TJ.getPixelSize(pixelFormat),
-      flags | TJ.getFlags(pixelFormat));
+      desiredHeight, pixelFormat, flags);
   }
 
   public byte [] decompress(int desiredWidth, int pitch, int desiredHeight,
     int pixelFormat, int flags) throws Exception {
-    if(desiredWidth < 0 || desiredHeight < 0 || pitch < 0 || pixelFormat < 0
-      || pixelFormat >= TJ.NUMPFOPT || flags < 0)
+    if(desiredWidth < 0 || pitch < 0 || desiredHeight < 0
+      || pixelFormat < 0 || pixelFormat >= TJ.NUMPFOPT || flags < 0)
       throw new Exception("Invalid argument in decompress()");
     int pixelSize = TJ.getPixelSize(pixelFormat);
     int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
     int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
     if(pitch == 0) pitch = scaledWidth * pixelSize;
-    int bufSize;
-    if((flags&TJ.YUV)!=0)
-      bufSize = TJ.bufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp);
-    else bufSize = pitch * scaledHeight;
-    byte [] buf = new byte[bufSize];
+    byte [] buf = new byte[pitch * scaledHeight];
+    decompress(buf, desiredWidth, pitch, desiredHeight, pixelFormat, flags);
+    return buf;
+  }
+
+  public void decompressToYUV(byte [] dstBuf, int flags) throws Exception {
     if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
-    decompress(jpegBuf, jpegBufSize, buf, desiredWidth, pitch, desiredHeight,
-      TJ.getPixelSize(pixelFormat), flags | TJ.getFlags(pixelFormat));
+    if(dstBuf == null || flags < 0)
+      throw new Exception("Invalid argument in decompressToYUV()");
+    decompressToYUV(jpegBuf, jpegBufSize, dstBuf, flags);
+  }
+
+  public byte [] decompressToYUV(int flags) throws Exception {
+    if(flags < 0)
+      throw new Exception("Invalid argument in decompressToYUV()");
+    if(jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0)
+      throw new Exception("JPEG buffer not initialized");
+    if(jpegSubsamp >= TJ.NUMSAMPOPT)
+      throw new Exception("JPEG header information is invalid");
+    byte [] buf = new byte[TJ.bufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)];
+    decompressToYUV(buf, flags);
     return buf;
   }
 
   public void decompress(BufferedImage dstImage, int flags) throws Exception {
     if(dstImage == null || flags < 0)
       throw new Exception("Invalid argument in decompress()");
-    flags &= ~(TJ.YUV);
     int desiredWidth = dstImage.getWidth();
     int desiredHeight = dstImage.getHeight();
     int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
@@ -144,14 +162,15 @@
       int pitch = sm.getScanlineStride();
       DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
       int [] buf = db.getData();
+      if(jpegBuf == null) throw new Exception("JPEG buffer not initialized");
       decompress(jpegBuf, jpegBufSize, buf, scaledWidth, pitch, scaledHeight,
-        flags | TJ.getFlags(pixelFormat));
+        pixelFormat, flags);
     }
     else {
       ComponentSampleModel sm =
         (ComponentSampleModel)dstImage.getSampleModel();
       int pixelSize = sm.getPixelStride();
-      if(pixelSize != TJ.pixelSize[pixelFormat])
+      if(pixelSize != TJ.getPixelSize(pixelFormat))
         throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
       int pitch = sm.getScanlineStride();
       DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
@@ -160,6 +179,18 @@
     }
   }
 
+  public BufferedImage decompress(int desiredWidth, int desiredHeight,
+    int bufferedImageType, int flags) throws Exception {
+    if(desiredWidth < 0 || desiredHeight < 0 || flags < 0)
+      throw new Exception("Invalid argument in decompress()");
+    int scaledWidth = getScaledWidth(desiredWidth, desiredHeight);
+    int scaledHeight = getScaledHeight(desiredWidth, desiredHeight);
+    BufferedImage img = new BufferedImage(scaledWidth, scaledHeight,
+      bufferedImageType);
+    decompress(img, flags);
+    return img;
+  }
+
   public void close() throws Exception {
     destroy();
   }
@@ -182,11 +213,15 @@
     throws Exception;
 
   private native void decompress(byte [] srcBuf, int size, byte [] dstBuf,
-    int desiredWidth, int pitch, int desiredHeight, int pixelSize, int flags)
+    int desiredWidth, int pitch, int desiredHeight, int pixelFormat, int flags)
     throws Exception;
 
   private native void decompress(byte [] srcBuf, int size, int [] dstBuf,
-    int desiredWidth, int pitch, int desiredHeight, int flags)
+    int desiredWidth, int pitch, int desiredHeight, int pixelFormat, int flags)
+    throws Exception;
+
+  private native void decompressToYUV(byte [] srcBuf, int size, byte [] dstBuf,
+    int flags)
     throws Exception;
 
   private native int getScaledWidth(int jpegWidth, int jpegHeight,
diff --git a/java/org_libjpegturbo_turbojpeg_TJ.h b/java/org_libjpegturbo_turbojpeg_TJ.h
index a1fe82d..b2be5d8 100644
--- a/java/org_libjpegturbo_turbojpeg_TJ.h
+++ b/java/org_libjpegturbo_turbojpeg_TJ.h
@@ -45,12 +45,6 @@
 #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
 /*
  * Class:     org_libjpegturbo_turbojpeg_TJ
  * Method:    bufSize
diff --git a/java/org_libjpegturbo_turbojpeg_TJCompressor.h b/java/org_libjpegturbo_turbojpeg_TJCompressor.h
index e7129af..59f81e3 100644
--- a/java/org_libjpegturbo_turbojpeg_TJCompressor.h
+++ b/java/org_libjpegturbo_turbojpeg_TJCompressor.h
@@ -34,10 +34,26 @@
 /*
  * Class:     org_libjpegturbo_turbojpeg_TJCompressor
  * Method:    compress
- * Signature: ([IIII[BIII)I
+ * Signature: ([IIIII[BIII)I
  */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIII_3BIII
-  (JNIEnv *, jobject, jintArray, jint, jint, jint, jbyteArray, jint, jint, jint);
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
+  (JNIEnv *, jobject, jintArray, jint, jint, jint, jint, jbyteArray, jint, jint, jint);
+
+/*
+ * Class:     org_libjpegturbo_turbojpeg_TJCompressor
+ * Method:    encodeYUV
+ * Signature: ([BIIII[BII)V
+ */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
+  (JNIEnv *, jobject, jbyteArray, jint, jint, jint, jint, jbyteArray, jint, jint);
+
+/*
+ * Class:     org_libjpegturbo_turbojpeg_TJCompressor
+ * Method:    encodeYUV
+ * Signature: ([IIIII[BII)V
+ */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
+  (JNIEnv *, jobject, jintArray, jint, jint, jint, jint, jbyteArray, jint, jint);
 
 #ifdef __cplusplus
 }
diff --git a/java/org_libjpegturbo_turbojpeg_TJDecompressor.h b/java/org_libjpegturbo_turbojpeg_TJDecompressor.h
index 8f65388..6951906 100644
--- a/java/org_libjpegturbo_turbojpeg_TJDecompressor.h
+++ b/java/org_libjpegturbo_turbojpeg_TJDecompressor.h
@@ -42,10 +42,18 @@
 /*
  * Class:     org_libjpegturbo_turbojpeg_TJDecompressor
  * Method:    decompress
- * Signature: ([BI[IIIII)V
+ * Signature: ([BI[IIIIII)V
  */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIII
-  (JNIEnv *, jobject, jbyteArray, jint, jintArray, jint, jint, jint, jint);
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
+  (JNIEnv *, jobject, jbyteArray, jint, jintArray, jint, jint, jint, jint, jint);
+
+/*
+ * Class:     org_libjpegturbo_turbojpeg_TJDecompressor
+ * Method:    decompressToYUV
+ * Signature: ([BI[BI)V
+ */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV
+  (JNIEnv *, jobject, jbyteArray, jint, jbyteArray, jint);
 
 /*
  * Class:     org_libjpegturbo_turbojpeg_TJDecompressor