From ef2a1bf80f8afe9d9249a196d1842de0b74251a5 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 20 Feb 2014 17:47:41 +0100 Subject: Bug 980: Use Bitsream class for JPEGDecoder --- .../jogamp/opengl/util/jpeg/JPEGDecoder.java | 101 +++++++-------------- 1 file changed, 32 insertions(+), 69 deletions(-) (limited to 'src/jogl/classes/jogamp') diff --git a/src/jogl/classes/jogamp/opengl/util/jpeg/JPEGDecoder.java b/src/jogl/classes/jogamp/opengl/util/jpeg/JPEGDecoder.java index d5b01ef54..946ea8ff5 100644 --- a/src/jogl/classes/jogamp/opengl/util/jpeg/JPEGDecoder.java +++ b/src/jogl/classes/jogamp/opengl/util/jpeg/JPEGDecoder.java @@ -56,7 +56,6 @@ package jogamp.opengl.util.jpeg; -import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -65,6 +64,7 @@ import java.util.Arrays; import jogamp.opengl.Debug; import com.jogamp.common.util.ArrayHashSet; +import com.jogamp.common.util.Bitstream; import com.jogamp.common.util.VersionNumber; import com.jogamp.opengl.util.texture.TextureData; import com.jogamp.opengl.util.texture.TextureData.ColorSpace; @@ -446,9 +446,7 @@ public class JPEGDecoder { return "JPEG[size "+width+"x"+height+", compOut "+compOuts+", "+jfifS+", "+exifS+", "+adobeS+"]"; } - private BufferedInputStream istream; - private int _ipos = 0; - private int _iposSave = 0; + private final Bitstream bstream = new Bitstream(new Bitstream.ByteInputStream(null), false /* outputMode */); private int width = 0; private int height = 0; @@ -463,50 +461,20 @@ public class JPEGDecoder { public final int getWidth() { return width; } public final int getHeight() { return height; } - private final void resetInput(InputStream is) { - if( is instanceof BufferedInputStream ) { - istream = (BufferedInputStream) is; - } else { - istream = new BufferedInputStream(is); + private final void setStream(InputStream is) { + try { + bstream.setStream(is, false /* outputMode */); + } catch (Exception e) { + throw new RuntimeException(e); // should not happen, no flush() } - _ipos = 0; } - private final void markStream(int readLimit) { - istream.mark(readLimit); - _iposSave = _ipos; - } - private final void rewindStream() throws IOException { - if(DEBUG_IN) { System.err.println("JPG.rewindStream: "+_ipos+" -> "+_iposSave); } - istream.reset(); - _ipos = _iposSave; - _iposSave = 0; - } private final int readUint8() throws IOException { - final int r = istream.read(); - if( -1 < r ) { - if(DEBUG_IN) { System.err.println("u8["+_ipos+"]: "+toHexString(r)); } - _ipos++; - } else if(DEBUG_IN) { - System.err.println("u8["+_ipos+"]: EOS"); - } - return r; + return bstream.readInt8(true /* msbFirst */); } private final int readUint16() throws IOException { - final int hi = istream.read(); - if( -1 < hi ) { - _ipos++; - final int lo = istream.read(); - if( -1 < lo ) { - _ipos++; - final int r = hi << 8 | lo ; - if(DEBUG_IN) { System.err.println("u16["+(_ipos-2)+"]: "+toHexString(r)); } - return r; - } - } - if(DEBUG_IN) { System.err.println("u16["+_ipos+"]: EOS"); } - return -1; + return bstream.readInt16(true /* msbFirst */, true /* bigEndian */); } private final int readNumber() throws IOException { @@ -531,14 +499,14 @@ public class JPEGDecoder { for(int i=0; i 0) { - bitsCount--; - return (bitsData >> bitsCount) & 1; - } - bitsData = readUint8(); - if( -1 == bitsData ) { - return -1; + private final int readBit() throws MarkerException, IOException { + final int bit = bstream.readBit(true /* msbFirst */); + if( Bitstream.EOS == bit || 7 != bstream.getBitCount() ) { + return bit; } - if (bitsData == 0xFF) { // marker prefix - final int nextByte = readUint8(); // marker signature + // new byte read, i.e. bitCount == 7 + final int bitsData = bstream.getBitBuffer(); // peek for marker + if ( 0xFF == bitsData ) { // marker prefix + final int nextByte = bstream.getStream().read(); // snoop marker signature, will be dropped! if( -1 == nextByte ) { throw new CodecException("marked prefix 0xFF, then EOF"); } if (0 != nextByte) { final int marker = (bitsData << 8) | nextByte; - throw new MarkerException(marker, "Marker at readBit file pos " + _ipos); + throw new MarkerException(marker, "Marker at readBit pos " + bstream); } // unstuff 0 } - bitsCount = 7; - return bitsData >>> 7; + return bit; } private int decodeHuffman(BinObj tree) throws IOException { @@ -1205,7 +1168,7 @@ public class JPEGDecoder { return 0x000000FF & node.getValue(); } } - throw new CodecException("EOF reached at "+_ipos); + throw new CodecException("EOF reached at "+bstream); } private int receive(int length) throws IOException { int n = 0; -- cgit v1.2.3