1 | /*
|
---|
2 | * This is public domain software - that is, you can do whatever you want
|
---|
3 | * with it, and include it software that is licensed under the GNU or the
|
---|
4 | * BSD license, or whatever other licence you choose, including proprietary
|
---|
5 | * closed source licenses. I do ask that you leave this header in tact.
|
---|
6 | *
|
---|
7 | * If you make modifications to this code that you think would benefit the
|
---|
8 | * wider community, please send me a copy and I'll post it on my site.
|
---|
9 | *
|
---|
10 | * If you make use of this code, I'd appreciate hearing about it.
|
---|
11 | * drew@drewnoakes.com
|
---|
12 | * Latest version of this software kept at
|
---|
13 | * http://drewnoakes.com/
|
---|
14 | *
|
---|
15 | * Created by dnoakes on Aug 2, 2003 using IntelliJ IDEA.
|
---|
16 | */
|
---|
17 | package com.drew.metadata.jpeg;
|
---|
18 |
|
---|
19 | import com.drew.imaging.jpeg.JpegProcessingException;
|
---|
20 | import com.drew.imaging.jpeg.JpegSegmentReader;
|
---|
21 | import com.drew.metadata.Metadata;
|
---|
22 | import com.drew.metadata.MetadataException;
|
---|
23 | import com.drew.metadata.MetadataReader;
|
---|
24 |
|
---|
25 | import java.io.File;
|
---|
26 | import java.io.InputStream;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | *
|
---|
30 | * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes
|
---|
31 | */
|
---|
32 | public class JpegReader implements MetadataReader
|
---|
33 | {
|
---|
34 | /**
|
---|
35 | * The SOF0 data segment.
|
---|
36 | */
|
---|
37 | private final byte[] _data;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates a new JpegReader for the specified Jpeg jpegFile.
|
---|
41 | */
|
---|
42 | public JpegReader(File jpegFile) throws JpegProcessingException
|
---|
43 | {
|
---|
44 | this(new JpegSegmentReader(jpegFile).readSegment(JpegSegmentReader.SEGMENT_SOF0));
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** Creates a JpegReader for a JPEG stream.
|
---|
48 | *
|
---|
49 | * @param is JPEG stream. Stream will be closed.
|
---|
50 | */
|
---|
51 | public JpegReader(InputStream is) throws JpegProcessingException
|
---|
52 | {
|
---|
53 | this(new JpegSegmentReader(is).readSegment(JpegSegmentReader.SEGMENT_APPD));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public JpegReader(byte[] data)
|
---|
57 | {
|
---|
58 | _data = data;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Performs the Jpeg data extraction, returning a new instance of <code>Metadata</code>.
|
---|
63 | */
|
---|
64 | public Metadata extract()
|
---|
65 | {
|
---|
66 | return extract(new Metadata());
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Performs the Jpeg data extraction, adding found values to the specified
|
---|
71 | * instance of <code>Metadata</code>.
|
---|
72 | */
|
---|
73 | public Metadata extract(Metadata metadata)
|
---|
74 | {
|
---|
75 | if (_data==null) {
|
---|
76 | return metadata;
|
---|
77 | }
|
---|
78 |
|
---|
79 | JpegDirectory directory = (JpegDirectory)metadata.getDirectory(JpegDirectory.class);
|
---|
80 |
|
---|
81 | try {
|
---|
82 | // data precision
|
---|
83 | int dataPrecision = get16Bits(JpegDirectory.TAG_JPEG_DATA_PRECISION);
|
---|
84 | directory.setInt(JpegDirectory.TAG_JPEG_DATA_PRECISION, dataPrecision);
|
---|
85 |
|
---|
86 | // process height
|
---|
87 | int height = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
|
---|
88 | directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT, height);
|
---|
89 |
|
---|
90 | // process width
|
---|
91 | int width = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
|
---|
92 | directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH, width);
|
---|
93 |
|
---|
94 | // number of components
|
---|
95 | int numberOfComponents = get16Bits(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);
|
---|
96 | directory.setInt(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS, numberOfComponents);
|
---|
97 |
|
---|
98 | // for each component, there are three bytes of data:
|
---|
99 | // 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
|
---|
100 | // 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal
|
---|
101 | // 3 - Quantization table number
|
---|
102 | int offset = 6;
|
---|
103 | for (int i=0; i<numberOfComponents; i++)
|
---|
104 | {
|
---|
105 | int componentId = get16Bits(offset++);
|
---|
106 | int samplingFactorByte = get16Bits(offset++);
|
---|
107 | int quantizationTableNumber = get16Bits(offset++);
|
---|
108 | JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber);
|
---|
109 | directory.setObject(JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + i, component);
|
---|
110 | }
|
---|
111 |
|
---|
112 | } catch (MetadataException me) {
|
---|
113 | directory.addError("MetadataException: " + me);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return metadata;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Returns an int calculated from two bytes of data at the specified offset (MSB, LSB).
|
---|
121 | * @param offset position within the data buffer to read first byte
|
---|
122 | * @return the 32 bit int value, between 0x0000 and 0xFFFF
|
---|
123 | */
|
---|
124 | private int get32Bits(int offset) throws MetadataException
|
---|
125 | {
|
---|
126 | if (offset+1>=_data.length) {
|
---|
127 | throw new MetadataException("Attempt to read bytes from outside Jpeg segment data buffer");
|
---|
128 | }
|
---|
129 |
|
---|
130 | return ((_data[offset] & 255) << 8) | (_data[offset + 1] & 255);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Returns an int calculated from one byte of data at the specified offset.
|
---|
135 | * @param offset position within the data buffer to read byte
|
---|
136 | * @return the 16 bit int value, between 0x00 and 0xFF
|
---|
137 | */
|
---|
138 | private int get16Bits(int offset) throws MetadataException
|
---|
139 | {
|
---|
140 | if (offset>=_data.length) {
|
---|
141 | throw new MetadataException("Attempt to read bytes from outside Jpeg segment data buffer");
|
---|
142 | }
|
---|
143 |
|
---|
144 | return (_data[offset] & 255);
|
---|
145 | }
|
---|
146 | }
|
---|