Frames | No Frames |
1: /* JPEGImageInputStream.java -- 2: Copyright (C) 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package gnu.javax.imageio.jpeg; 39: 40: import java.io.EOFException; 41: import java.io.IOException; 42: import javax.imageio.*; 43: import javax.imageio.spi.*; 44: import javax.imageio.metadata.*; 45: import javax.imageio.stream.ImageInputStream; 46: import javax.imageio.stream.ImageInputStreamImpl; 47: 48: import java.util.Iterator; 49: import java.awt.image.BufferedImage; 50: 51: public class JPEGImageInputStream 52: extends ImageInputStreamImpl 53: { 54: private ImageInputStream in; 55: 56: byte marker; 57: 58: public JPEGImageInputStream(ImageInputStream in) 59: { 60: super(); 61: 62: this.in = in; 63: } 64: 65: public int read() 66: throws IOException 67: { 68: setBitOffset(0); 69: return in.read(); 70: } 71: 72: public int read(byte[] data, int offset, int len) 73: throws IOException 74: { 75: setBitOffset(0); 76: return in.read(data, offset, len); 77: } 78: 79: /** 80: * Pull a byte from the stream, this checks to see if the byte is 0xff 81: * and if the next byte isn't 0x00 (stuffed byte) it errors out. If it's 82: * 0x00 then it simply ignores the byte. 83: * 84: * @return the next byte in the buffer 85: * 86: * @throws IOException TODO 87: * @throws BitStreamException TODO 88: */ 89: private byte pullByte() throws IOException, JPEGMarkerFoundException 90: { 91: byte mybyte = readByte(); 92: // FIXME: handle multiple 0xff in a row 93: if(mybyte==(byte)(0xff)) 94: { 95: byte secondbyte = readByte(); 96: if(secondbyte != (byte)(0x00)) 97: { 98: marker = secondbyte; 99: throw new JPEGMarkerFoundException(); 100: } 101: } 102: return mybyte; 103: } 104: 105: /** 106: * This returns the marker that was last encountered. This should only be 107: * used if removeBit() throws a MarkerTagFound exception. 108: * 109: * @return marker as byte 110: */ 111: public byte getMarker() 112: { 113: return marker; 114: } 115: 116: /** 117: * Removes a bit from the buffer. (Removes from the top of a queue). This 118: * also checks for markers and throws MarkerTagFound exception if it does. 119: * If MarkerTagFound is thrown you can use getMarker() method to get the 120: * marker that caused the throw. 121: * 122: * @param l specifies how many bits you want to remove and add to the 123: * integer 124: * @return the amount of bits specified by l as an integer 125: * 126: * @throws IOException TODO 127: * @throws JPEGMarkerFoundException 128: * @throws BitStreamException TODO 129: */ 130: public int readBit() 131: throws IOException, JPEGMarkerFoundException 132: { 133: checkClosed(); 134: 135: // Calc new bit offset here, readByte resets it. 136: int newOffset = (bitOffset + 1) & 0x7; 137: 138: byte data = pullByte(); 139: 140: if (bitOffset != 0) 141: { 142: seek(getStreamPosition() - 1); 143: data = (byte) (data >> (8 - newOffset)); 144: } 145: 146: bitOffset = newOffset; 147: return data & 0x1; 148: } 149: 150: 151: /** 152: * This method skips over the the data and finds the next position 153: * in the bit sequence with a X'FF' X'??' sequence. Multiple X'FF 154: * bytes in sequence are considered padding and interpreted as one 155: * X'FF byte. 156: * 157: * @return the next marker byte in the stream 158: * @throws IOException if the end of the stream is reached 159: * unexpectedly 160: */ 161: public byte findNextMarker() 162: throws IOException 163: { 164: boolean marked0xff = false; 165: byte byteinfo = JPEGMarker.X00; 166: 167: setBitOffset(0); 168: while (true) 169: { 170: byteinfo = readByte(); 171: if (!marked0xff) 172: { 173: if (byteinfo == JPEGMarker.XFF) 174: marked0xff = true; 175: } 176: else 177: { 178: if (byteinfo == JPEGMarker.XFF) 179: // Ignore the value 0xff when it is immediately 180: // followed by another 0xff byte. 181: continue; 182: else if (byteinfo == JPEGMarker.X00) 183: // The sequence 0xff 0x00 is used to encode the 184: // actual value 0xff. So restart our search for a 185: // marker. 186: marked0xff = false; 187: else 188: // One or more 0xff values were follwed by a 189: // non-0x00, non-0xff value so return this as the 190: // marker byte. 191: return byteinfo; 192: } 193: } 194: } 195: }