source: josm/trunk/src/com/drew/imaging/jpeg/JpegSegmentReader.java@ 4231

Last change on this file since 4231 was 4231, checked in by stoecker, 13 years ago

add signpost and metadata extractor code to repository directly

File size: 11.2 KB
Line 
1/*
2 * JpegSegmentReader.java
3 *
4 * This class written by Drew Noakes, in accordance with the Jpeg specification.
5 *
6 * This is public domain software - that is, you can do whatever you want
7 * with it, and include it software that is licensed under the GNU or the
8 * BSD license, or whatever other licence you choose, including proprietary
9 * closed source licenses. I do ask that you leave this header in tact.
10 *
11 * If you make modifications to this code that you think would benefit the
12 * wider community, please send me a copy and I'll post it on my site.
13 *
14 * If you make use of this code, I'd appreciate hearing about it.
15 * drew@drewnoakes.com
16 * Latest version of this software kept at
17 * http://drewnoakes.com/
18 *
19 * Created by dnoakes on 04-Nov-2002 00:54:00 using IntelliJ IDEA
20 */
21package com.drew.imaging.jpeg;
22
23import java.io.*;
24
25/**
26 * Performs read functions of Jpeg files, returning specific file segments.
27 * TODO add a findAvailableSegments() method
28 * TODO add more segment identifiers
29 * TODO add a getSegmentDescription() method, returning for example 'App1 application data segment, commonly containing Exif data'
30 * @author Drew Noakes http://drewnoakes.com
31 */
32public class JpegSegmentReader
33{
34 // Jpeg data can be sourced from either a file, byte[] or InputStream
35
36 /** Jpeg file */
37 private final File _file;
38 /** Jpeg data as byte array */
39 private final byte[] _data;
40 /** Jpeg data as an InputStream */
41 private final InputStream _stream;
42
43 private JpegSegmentData _segmentData;
44
45 /**
46 * Private, because this segment crashes my algorithm, and searching for
47 * it doesn't work (yet).
48 */
49 private static final byte SEGMENT_SOS = (byte)0xDA;
50
51 /**
52 * Private, because one wouldn't search for it.
53 */
54 private static final byte MARKER_EOI = (byte)0xD9;
55
56 /** APP0 Jpeg segment identifier -- Jfif data. */
57 public static final byte SEGMENT_APP0 = (byte)0xE0;
58 /** APP1 Jpeg segment identifier -- where Exif data is kept. */
59 public static final byte SEGMENT_APP1 = (byte)0xE1;
60 /** APP2 Jpeg segment identifier. */
61 public static final byte SEGMENT_APP2 = (byte)0xE2;
62 /** APP3 Jpeg segment identifier. */
63 public static final byte SEGMENT_APP3 = (byte)0xE3;
64 /** APP4 Jpeg segment identifier. */
65 public static final byte SEGMENT_APP4 = (byte)0xE4;
66 /** APP5 Jpeg segment identifier. */
67 public static final byte SEGMENT_APP5 = (byte)0xE5;
68 /** APP6 Jpeg segment identifier. */
69 public static final byte SEGMENT_APP6 = (byte)0xE6;
70 /** APP7 Jpeg segment identifier. */
71 public static final byte SEGMENT_APP7 = (byte)0xE7;
72 /** APP8 Jpeg segment identifier. */
73 public static final byte SEGMENT_APP8 = (byte)0xE8;
74 /** APP9 Jpeg segment identifier. */
75 public static final byte SEGMENT_APP9 = (byte)0xE9;
76 /** APPA Jpeg segment identifier -- can hold Unicode comments. */
77 public static final byte SEGMENT_APPA = (byte)0xEA;
78 /** APPB Jpeg segment identifier. */
79 public static final byte SEGMENT_APPB = (byte)0xEB;
80 /** APPC Jpeg segment identifier. */
81 public static final byte SEGMENT_APPC = (byte)0xEC;
82 /** APPD Jpeg segment identifier -- IPTC data in here. */
83 public static final byte SEGMENT_APPD = (byte)0xED;
84 /** APPE Jpeg segment identifier. */
85 public static final byte SEGMENT_APPE = (byte)0xEE;
86 /** APPF Jpeg segment identifier. */
87 public static final byte SEGMENT_APPF = (byte)0xEF;
88 /** Start Of Image segment identifier. */
89 public static final byte SEGMENT_SOI = (byte)0xD8;
90 /** Define Quantization Table segment identifier. */
91 public static final byte SEGMENT_DQT = (byte)0xDB;
92 /** Define Huffman Table segment identifier. */
93 public static final byte SEGMENT_DHT = (byte)0xC4;
94 /** Start-of-Frame Zero segment identifier. */
95 public static final byte SEGMENT_SOF0 = (byte)0xC0;
96 /** Jpeg comment segment identifier. */
97 public static final byte SEGMENT_COM = (byte)0xFE;
98
99 /**
100 * Creates a JpegSegmentReader for a specific file.
101 * @param file the Jpeg file to read segments from
102 */
103 public JpegSegmentReader(File file) throws JpegProcessingException
104 {
105 _file = file;
106 _data = null;
107 _stream = null;
108
109 readSegments();
110 }
111
112 /**
113 * Creates a JpegSegmentReader for a byte array.
114 * @param fileContents the byte array containing Jpeg data
115 */
116 public JpegSegmentReader(byte[] fileContents) throws JpegProcessingException
117 {
118 _file = null;
119 _data = fileContents;
120 _stream = null;
121
122 readSegments();
123 }
124
125 public JpegSegmentReader(InputStream in) throws JpegProcessingException
126 {
127 _stream = in;
128 _file = null;
129 _data = null;
130
131 readSegments();
132 }
133
134 public JpegSegmentReader(JpegSegmentData segmentData)
135 {
136 _file = null;
137 _data = null;
138 _stream = null;
139
140 _segmentData = segmentData;
141 }
142
143 /**
144 * Reads the first instance of a given Jpeg segment, returning the contents as
145 * a byte array.
146 * @param segmentMarker the byte identifier for the desired segment
147 * @return the byte array if found, else null
148 * @throws JpegProcessingException for any problems processing the Jpeg data,
149 * including inner IOExceptions
150 */
151 public byte[] readSegment(byte segmentMarker) throws JpegProcessingException
152 {
153 return readSegment(segmentMarker, 0);
154 }
155
156 /**
157 * Reads the first instance of a given Jpeg segment, returning the contents as
158 * a byte array.
159 * @param segmentMarker the byte identifier for the desired segment
160 * @param occurrence the occurrence of the specified segment within the jpeg file
161 * @return the byte array if found, else null
162 */
163 public byte[] readSegment(byte segmentMarker, int occurrence)
164 {
165 return _segmentData.getSegment(segmentMarker, occurrence);
166 }
167
168 public final int getSegmentCount(byte segmentMarker)
169 {
170 return _segmentData.getSegmentCount(segmentMarker);
171 }
172
173 public final JpegSegmentData getSegmentData()
174 {
175 return _segmentData;
176 }
177
178 private void readSegments() throws JpegProcessingException
179 {
180 _segmentData = new JpegSegmentData();
181
182 BufferedInputStream inStream = getJpegInputStream();
183 try {
184 int offset = 0;
185 // first two bytes should be jpeg magic number
186 if (!isValidJpegHeaderBytes(inStream)) {
187 throw new JpegProcessingException("not a jpeg file");
188 }
189 offset += 2;
190 do {
191 // next byte is 0xFF
192 byte segmentIdentifier = (byte)(inStream.read() & 0xFF);
193 if ((segmentIdentifier & 0xFF) != 0xFF) {
194 throw new JpegProcessingException("expected jpeg segment start identifier 0xFF at offset " + offset + ", not 0x" + Integer.toHexString(segmentIdentifier & 0xFF));
195 }
196 offset++;
197 // next byte is <segment-marker>
198 byte thisSegmentMarker = (byte)(inStream.read() & 0xFF);
199 offset++;
200 // next 2-bytes are <segment-size>: [high-byte] [low-byte]
201 byte[] segmentLengthBytes = new byte[2];
202 inStream.read(segmentLengthBytes, 0, 2);
203 offset += 2;
204 int segmentLength = ((segmentLengthBytes[0] << 8) & 0xFF00) | (segmentLengthBytes[1] & 0xFF);
205 // segment length includes size bytes, so subtract two
206 segmentLength -= 2;
207 if (segmentLength > inStream.available())
208 throw new JpegProcessingException("segment size would extend beyond file stream length");
209 else if (segmentLength < 0)
210 throw new JpegProcessingException("segment size would be less than zero");
211 byte[] segmentBytes = new byte[segmentLength];
212 inStream.read(segmentBytes, 0, segmentLength);
213 offset += segmentLength;
214 if ((thisSegmentMarker & 0xFF) == (SEGMENT_SOS & 0xFF)) {
215 // The 'Start-Of-Scan' segment's length doesn't include the image data, instead would
216 // have to search for the two bytes: 0xFF 0xD9 (EOI).
217 // It comes last so simply return at this point
218 return;
219 } else if ((thisSegmentMarker & 0xFF) == (MARKER_EOI & 0xFF)) {
220 // the 'End-Of-Image' segment -- this should never be found in this fashion
221 return;
222 } else {
223 _segmentData.addSegment(thisSegmentMarker, segmentBytes);
224 }
225 // didn't find the one we're looking for, loop through to the next segment
226 } while (true);
227 } catch (IOException ioe) {
228 //throw new JpegProcessingException("IOException processing Jpeg file", ioe);
229 throw new JpegProcessingException("IOException processing Jpeg file: " + ioe.getMessage(), ioe);
230 } finally {
231 try {
232 if (inStream != null) {
233 inStream.close();
234 }
235 } catch (IOException ioe) {
236 //throw new JpegProcessingException("IOException processing Jpeg file", ioe);
237 throw new JpegProcessingException("IOException processing Jpeg file: " + ioe.getMessage(), ioe);
238 }
239 }
240 }
241
242 /**
243 * Private helper method to create a BufferedInputStream of Jpeg data from whichever
244 * data source was specified upon construction of this instance.
245 * @return a a BufferedInputStream of Jpeg data
246 * @throws JpegProcessingException for any problems obtaining the stream
247 */
248 private BufferedInputStream getJpegInputStream() throws JpegProcessingException
249 {
250 if (_stream!=null) {
251 if (_stream instanceof BufferedInputStream) {
252 return (BufferedInputStream) _stream;
253 } else {
254 return new BufferedInputStream(_stream);
255 }
256 }
257 InputStream inputStream;
258 if (_data == null) {
259 try {
260 inputStream = new FileInputStream(_file);
261 } catch (FileNotFoundException e) {
262 throw new JpegProcessingException("Jpeg file does not exist", e);
263 }
264 } else {
265 inputStream = new ByteArrayInputStream(_data);
266 }
267 return new BufferedInputStream(inputStream);
268 }
269
270 /**
271 * Helper method that validates the Jpeg file's magic number.
272 * @param fileStream the InputStream to read bytes from, which must be positioned
273 * at its start (i.e. no bytes read yet)
274 * @return true if the magic number is Jpeg (0xFFD8)
275 * @throws IOException for any problem in reading the file
276 */
277 private boolean isValidJpegHeaderBytes(InputStream fileStream) throws IOException
278 {
279 byte[] header = new byte[2];
280 fileStream.read(header, 0, 2);
281 return (header[0] & 0xFF) == 0xFF && (header[1] & 0xFF) == 0xD8;
282 }
283}
Note: See TracBrowser for help on using the repository browser.