Changeset 6127 in josm for trunk/src/com/drew/metadata/jpeg/JpegReader.java
- Timestamp:
- 2013-08-09T18:05:11+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/jpeg/JpegReader.java
r4231 r6127 1 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. 2 * Copyright 2002-2012 Drew Noakes 6 3 * 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. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 9 7 * 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/ 8 * http://www.apache.org/licenses/LICENSE-2.0 14 9 * 15 * Created by dnoakes on Aug 2, 2003 using IntelliJ IDEA. 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * More information about this project is available at: 17 * 18 * http://drewnoakes.com/code/exif/ 19 * http://code.google.com/p/metadata-extractor/ 16 20 */ 17 21 package com.drew.metadata.jpeg; 18 22 19 import com.drew.imaging.jpeg.JpegProcessingException; 20 import com.drew.imaging.jpeg.JpegSegmentReader; 23 import com.drew.lang.BufferBoundsException; 24 import com.drew.lang.BufferReader; 25 import com.drew.lang.annotations.NotNull; 21 26 import com.drew.metadata.Metadata; 22 import com.drew.metadata.MetadataException;23 27 import com.drew.metadata.MetadataReader; 24 28 25 import java.io.File;26 import java.io.InputStream;27 28 29 /** 30 * Decodes Jpeg SOF0 data, populating a <code>Metadata</code> object with tag values in a <code>JpegDirectory</code>. 29 31 * 30 * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes 32 * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes http://drewnoakes.com 31 33 */ 32 34 public class JpegReader implements MetadataReader 33 35 { 34 36 /** 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 JpegProcessingException43 {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 JpegProcessingException52 {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 37 * Performs the Jpeg data extraction, adding found values to the specified 71 38 * instance of <code>Metadata</code>. 72 39 */ 73 public Metadata extract(Metadata metadata)40 public void extract(@NotNull final BufferReader reader, @NotNull Metadata metadata) 74 41 { 75 if (_data==null) { 76 return metadata; 77 } 78 79 JpegDirectory directory = (JpegDirectory)metadata.getDirectory(JpegDirectory.class); 42 JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class); 80 43 81 44 try { 82 45 // data precision 83 int dataPrecision = get16Bits(JpegDirectory.TAG_JPEG_DATA_PRECISION);46 int dataPrecision = reader.getUInt8(JpegDirectory.TAG_JPEG_DATA_PRECISION); 84 47 directory.setInt(JpegDirectory.TAG_JPEG_DATA_PRECISION, dataPrecision); 85 48 86 49 // process height 87 int height = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);50 int height = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT); 88 51 directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT, height); 89 52 90 53 // process width 91 int width = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);54 int width = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_WIDTH); 92 55 directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH, width); 93 56 94 57 // number of components 95 int numberOfComponents = get16Bits(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);58 int numberOfComponents = reader.getUInt8(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS); 96 59 directory.setInt(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS, numberOfComponents); 97 60 … … 101 64 // 3 - Quantization table number 102 65 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++); 66 for (int i = 0; i < numberOfComponents; i++) { 67 int componentId = reader.getUInt8(offset++); 68 int samplingFactorByte = reader.getUInt8(offset++); 69 int quantizationTableNumber = reader.getUInt8(offset++); 108 70 JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber); 109 71 directory.setObject(JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + i, component); 110 72 } 111 73 112 } catch ( MetadataException me) {113 directory.addError( "MetadataException: " + me);74 } catch (BufferBoundsException ex) { 75 directory.addError(ex.getMessage()); 114 76 } 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 byte122 * @return the 32 bit int value, between 0x0000 and 0xFFFF123 */124 private int get32Bits(int offset) throws MetadataException125 {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 byte136 * @return the 16 bit int value, between 0x00 and 0xFF137 */138 private int get16Bits(int offset) throws MetadataException139 {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 77 } 146 78 }
Note:
See TracChangeset
for help on using the changeset viewer.