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.IOException; 41 import javax.imageio.stream.ImageInputStream; 42 import javax.imageio.stream.ImageInputStreamImpl; 43 44 public class JPEGImageInputStream 45 extends ImageInputStreamImpl 46 { 47 private ImageInputStream in; 48 49 byte marker; 50 JPEGImageInputStream(ImageInputStream in)51 public JPEGImageInputStream(ImageInputStream in) 52 { 53 super(); 54 55 this.in = in; 56 } 57 read()58 public int read() 59 throws IOException 60 { 61 setBitOffset(0); 62 return in.read(); 63 } 64 read(byte[] data, int offset, int len)65 public int read(byte[] data, int offset, int len) 66 throws IOException 67 { 68 setBitOffset(0); 69 return in.read(data, offset, len); 70 } 71 72 /** 73 * Pull a byte from the stream, this checks to see if the byte is 0xff 74 * and if the next byte isn't 0x00 (stuffed byte) it errors out. If it's 75 * 0x00 then it simply ignores the byte. 76 * 77 * @return the next byte in the buffer 78 * 79 * @throws IOException TODO 80 * @throws BitStreamException TODO 81 */ pullByte()82 private byte pullByte() throws IOException, JPEGMarkerFoundException 83 { 84 byte mybyte = readByte(); 85 // FIXME: handle multiple 0xff in a row 86 if(mybyte==(byte)(0xff)) 87 { 88 byte secondbyte = readByte(); 89 if(secondbyte != (byte)(0x00)) 90 { 91 marker = secondbyte; 92 throw new JPEGMarkerFoundException(); 93 } 94 } 95 return mybyte; 96 } 97 98 /** 99 * This returns the marker that was last encountered. This should only be 100 * used if removeBit() throws a MarkerTagFound exception. 101 * 102 * @return marker as byte 103 */ getMarker()104 public byte getMarker() 105 { 106 return marker; 107 } 108 109 /** 110 * Removes a bit from the buffer. (Removes from the top of a queue). This 111 * also checks for markers and throws MarkerTagFound exception if it does. 112 * If MarkerTagFound is thrown you can use getMarker() method to get the 113 * marker that caused the throw. 114 * 115 * @param l specifies how many bits you want to remove and add to the 116 * integer 117 * @return the amount of bits specified by l as an integer 118 * 119 * @throws IOException TODO 120 * @throws JPEGMarkerFoundException 121 * @throws BitStreamException TODO 122 */ readBit()123 public int readBit() 124 throws IOException, JPEGMarkerFoundException 125 { 126 checkClosed(); 127 128 // Calc new bit offset here, readByte resets it. 129 int newOffset = (bitOffset + 1) & 0x7; 130 131 byte data = pullByte(); 132 133 if (bitOffset != 0) 134 { 135 seek(getStreamPosition() - 1); 136 data = (byte) (data >> (8 - newOffset)); 137 } 138 139 bitOffset = newOffset; 140 return data & 0x1; 141 } 142 143 144 /** 145 * This method skips over the the data and finds the next position 146 * in the bit sequence with a X'FF' X'??' sequence. Multiple X'FF 147 * bytes in sequence are considered padding and interpreted as one 148 * X'FF byte. 149 * 150 * @return the next marker byte in the stream 151 * @throws IOException if the end of the stream is reached 152 * unexpectedly 153 */ findNextMarker()154 public byte findNextMarker() 155 throws IOException 156 { 157 boolean marked0xff = false; 158 byte byteinfo = JPEGMarker.X00; 159 160 setBitOffset(0); 161 while (true) 162 { 163 byteinfo = readByte(); 164 if (!marked0xff) 165 { 166 if (byteinfo == JPEGMarker.XFF) 167 marked0xff = true; 168 } 169 else 170 { 171 if (byteinfo == JPEGMarker.XFF) 172 // Ignore the value 0xff when it is immediately 173 // followed by another 0xff byte. 174 continue; 175 else if (byteinfo == JPEGMarker.X00) 176 // The sequence 0xff 0x00 is used to encode the 177 // actual value 0xff. So restart our search for a 178 // marker. 179 marked0xff = false; 180 else 181 // One or more 0xff values were follwed by a 182 // non-0x00, non-0xff value so return this as the 183 // marker byte. 184 return byteinfo; 185 } 186 } 187 } 188 } 189