Changeset 6127 in josm for trunk/src/com/drew/metadata/jpeg
- Timestamp:
- 2013-08-09T18:05:11+02:00 (12 years ago)
- Location:
- trunk/src/com/drew/metadata/jpeg
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/jpeg/JpegCommentDescriptor.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 Oct 10, 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.metadata.Directory; 23 import com.drew.lang.annotations.NotNull; 24 import com.drew.lang.annotations.Nullable; 20 25 import com.drew.metadata.TagDescriptor; 21 26 22 27 /** 28 * Provides human-readable string representations of tag values stored in a <code>JpegCommentDirectory</code>. 23 29 * 24 30 * @author Drew Noakes http://drewnoakes.com 25 31 */ 26 public class JpegCommentDescriptor extends TagDescriptor 32 public class JpegCommentDescriptor extends TagDescriptor<JpegCommentDirectory> 27 33 { 28 public JpegCommentDescriptor(Directory directory) 34 public JpegCommentDescriptor(@NotNull JpegCommentDirectory directory) 29 35 { 30 36 super(directory); 31 37 } 32 38 33 public String getDescription(int tagType) 39 @Nullable 40 public String getJpegCommentDescription() 34 41 { 35 return _directory.getString( tagType);42 return _directory.getString(JpegCommentDirectory.TAG_JPEG_COMMENT); 36 43 } 37 44 } -
trunk/src/com/drew/metadata/jpeg/JpegCommentDirectory.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 Oct 10, 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 23 import com.drew.lang.annotations.NotNull; 19 24 import com.drew.metadata.Directory; 20 25 … … 22 27 23 28 /** 29 * Describes tags used by a JPEG file comment. 24 30 * 25 31 * @author Drew Noakes http://drewnoakes.com 26 32 */ 27 public class JpegCommentDirectory extends Directory { 33 public class JpegCommentDirectory extends Directory 34 { 35 /** 36 * This value does not apply to a particular standard. Rather, this value has been fabricated to maintain 37 * consistency with other directory types. 38 */ 39 public static final int TAG_JPEG_COMMENT = 0; 28 40 29 /** This is in bits/sample, usually 8 (12 and 16 not supported by most software). */ 30 publicstatic finalint TAG_JPEG_COMMENT = 0;41 @NotNull 42 protected static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>(); 31 43 32 protected static final HashMap tagNameMap = new HashMap(); 44 static { 45 _tagNameMap.put(TAG_JPEG_COMMENT, "Jpeg Comment"); 46 } 33 47 34 static { 35 tagNameMap.put(new Integer(TAG_JPEG_COMMENT), "Jpeg Comment"); 36 } 48 public JpegCommentDirectory() 49 { 50 this.setDescriptor(new JpegCommentDescriptor(this)); 51 } 37 52 38 public JpegCommentDirectory() { 39 this.setDescriptor(new JpegCommentDescriptor(this)); 40 } 53 @NotNull 54 public String getName() 55 { 56 return "JpegComment"; 57 } 41 58 42 public String getName() { 43 return "JpegComment"; 44 } 45 46 protected HashMap getTagNameMap() { 47 return tagNameMap; 48 } 59 @NotNull 60 protected HashMap<Integer, String> getTagNameMap() 61 { 62 return _tagNameMap; 63 } 49 64 } -
trunk/src/com/drew/metadata/jpeg/JpegCommentReader.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 Oct 10, 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 27 import com.drew.metadata.MetadataReader; 23 28 24 import java.io.File;25 import java.io.InputStream;26 27 29 /** 30 * Decodes the comment stored within Jpeg files, populating a <code>Metadata</code> object with tag values in a 31 * <code>JpegCommentDirectory</code>. 28 32 * 29 33 * @author Drew Noakes http://drewnoakes.com … … 32 36 { 33 37 /** 34 * The COM data segment.35 */36 private final byte[] _data;37 38 /**39 * Creates a new JpegReader for the specified Jpeg jpegFile.40 */41 public JpegCommentReader(File jpegFile) throws JpegProcessingException42 {43 this(new JpegSegmentReader(jpegFile).readSegment(JpegSegmentReader.SEGMENT_COM));44 }45 46 /** Creates a JpegCommentReader for a JPEG stream.47 *48 * @param is JPEG stream. Stream will be closed.49 */50 public JpegCommentReader(InputStream is) throws JpegProcessingException51 {52 this(new JpegSegmentReader(is).readSegment(JpegSegmentReader.SEGMENT_APPD));53 }54 55 public JpegCommentReader(byte[] data)56 {57 _data = data;58 }59 60 /**61 * Performs the Jpeg data extraction, returning a new instance of <code>Metadata</code>.62 */63 public Metadata extract()64 {65 return extract(new Metadata());66 }67 68 /**69 38 * Performs the Jpeg data extraction, adding found values to the specified 70 39 * instance of <code>Metadata</code>. 71 40 */ 72 public Metadataextract(Metadata metadata)41 public void extract(@NotNull final BufferReader reader, @NotNull Metadata metadata) 73 42 { 74 if (_data==null) { 75 return metadata; 43 JpegCommentDirectory directory = metadata.getOrCreateDirectory(JpegCommentDirectory.class); 44 45 try { 46 directory.setString(JpegCommentDirectory.TAG_JPEG_COMMENT, reader.getString(0, (int)reader.getLength())); 47 } catch (BufferBoundsException e) { 48 directory.addError("Exception reading JPEG comment string"); 76 49 } 77 78 JpegCommentDirectory directory = (JpegCommentDirectory)metadata.getDirectory(JpegCommentDirectory.class);79 80 directory.setString(JpegCommentDirectory.TAG_JPEG_COMMENT, new String(_data));81 82 return metadata;83 50 } 84 51 } -
trunk/src/com/drew/metadata/jpeg/JpegComponent.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 Oct 9, 17:04:07 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. metadata.MetadataException;23 import com.drew.lang.annotations.Nullable; 20 24 21 25 import java.io.Serializable; 22 26 23 27 /** 24 * Created by IntelliJ IDEA. 25 * User: dnoakes 26 * Date: 09-Oct-2003 27 * Time: 17:04:07 28 * To change this template use Options | File Templates. 28 * Stores information about a Jpeg image component such as the component id, horiz/vert sampling factor and 29 * quantization table number. 30 * 31 * @author Drew Noakes http://drewnoakes.com 29 32 */ 30 33 public class JpegComponent implements Serializable 31 34 { 35 private static final long serialVersionUID = 61121257899091914L; 36 32 37 private final int _componentId; 33 38 private final int _samplingFactorByte; … … 46 51 } 47 52 48 public String getComponentName() throws MetadataException 53 /** 54 * Returns the component name (one of: Y, Cb, Cr, I, or Q) 55 * @return the component name 56 */ 57 @Nullable 58 public String getComponentName() 49 59 { 50 60 switch (_componentId) … … 61 71 return "Q"; 62 72 } 63 64 throw new MetadataException("Unsupported component id: " + _componentId); 73 return null; 65 74 } 66 75 -
trunk/src/com/drew/metadata/jpeg/JpegDescriptor.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 9 * 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/ 14 20 */ 15 21 package com.drew.metadata.jpeg; 16 22 17 import com.drew. metadata.Directory;18 import com.drew. metadata.MetadataException;23 import com.drew.lang.annotations.NotNull; 24 import com.drew.lang.annotations.Nullable; 19 25 import com.drew.metadata.TagDescriptor; 20 26 … … 22 28 * Provides human-readable string versions of the tags stored in a JpegDirectory. 23 29 * Thanks to Darrell Silver (www.darrellsilver.com) for the initial version of this class. 30 * 31 * @author Drew Noakes http://drewnoakes.com 24 32 */ 25 public class JpegDescriptor extends TagDescriptor 33 public class JpegDescriptor extends TagDescriptor<JpegDirectory> 26 34 { 27 public JpegDescriptor(Directory directory) 35 public JpegDescriptor(@NotNull JpegDirectory directory) 28 36 { 29 37 super(directory); 30 38 } 31 39 32 public String getDescription(int tagType) throws MetadataException 40 @Nullable 41 public String getDescription(int tagType) 33 42 { 34 43 switch (tagType) 35 44 { 45 case JpegDirectory.TAG_JPEG_COMPRESSION_TYPE: 46 return getImageCompressionTypeDescription(); 36 47 case JpegDirectory.TAG_JPEG_COMPONENT_DATA_1: 37 48 return getComponentDataDescription(0); … … 48 59 case JpegDirectory.TAG_JPEG_IMAGE_WIDTH: 49 60 return getImageWidthDescription(); 61 default: 62 return super.getDescription(tagType); 50 63 } 51 52 return _directory.getString(tagType);53 64 } 54 65 66 @Nullable 67 public String getImageCompressionTypeDescription() 68 { 69 Integer value = _directory.getInteger(JpegDirectory.TAG_JPEG_COMPRESSION_TYPE); 70 if (value==null) 71 return null; 72 // Note there is no 2 or 12 73 switch (value) { 74 case 0: return "Baseline"; 75 case 1: return "Extended sequential, Huffman"; 76 case 2: return "Progressive, Huffman"; 77 case 3: return "Lossless, Huffman"; 78 case 5: return "Differential sequential, Huffman"; 79 case 6: return "Differential progressive, Huffman"; 80 case 7: return "Differential lossless, Huffman"; 81 case 8: return "Reserved for JPEG extensions"; 82 case 9: return "Extended sequential, arithmetic"; 83 case 10: return "Progressive, arithmetic"; 84 case 11: return "Lossless, arithmetic"; 85 case 13: return "Differential sequential, arithmetic"; 86 case 14: return "Differential progressive, arithmetic"; 87 case 15: return "Differential lossless, arithmetic"; 88 default: 89 return "Unknown type: "+ value; 90 } 91 } 92 @Nullable 55 93 public String getImageWidthDescription() 56 94 { 57 return _directory.getString(JpegDirectory.TAG_JPEG_IMAGE_WIDTH) + " pixels"; 95 final String value = _directory.getString(JpegDirectory.TAG_JPEG_IMAGE_WIDTH); 96 if (value==null) 97 return null; 98 return value + " pixels"; 58 99 } 59 100 101 @Nullable 60 102 public String getImageHeightDescription() 61 103 { 62 return _directory.getString(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT) + " pixels"; 104 final String value = _directory.getString(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT); 105 if (value==null) 106 return null; 107 return value + " pixels"; 63 108 } 64 109 110 @Nullable 65 111 public String getDataPrecisionDescription() 66 112 { 67 return _directory.getString(JpegDirectory.TAG_JPEG_DATA_PRECISION) + " bits"; 113 final String value = _directory.getString(JpegDirectory.TAG_JPEG_DATA_PRECISION); 114 if (value==null) 115 return null; 116 return value + " bits"; 68 117 } 69 118 70 public String getComponentDataDescription(int componentNumber) throws MetadataException 119 @Nullable 120 public String getComponentDataDescription(int componentNumber) 71 121 { 72 JpegComponent component = ((JpegDirectory)_directory).getComponent(componentNumber);122 JpegComponent value = _directory.getComponent(componentNumber); 73 123 74 if ( component==null)75 throw new MetadataException("No Jpeg component exists with number " + componentNumber);124 if (value==null) 125 return null; 76 126 77 StringBu ffer sb = new StringBuffer();78 sb.append( component.getComponentName());127 StringBuilder sb = new StringBuilder(); 128 sb.append(value.getComponentName()); 79 129 sb.append(" component: Quantization table "); 80 sb.append( component.getQuantizationTableNumber());130 sb.append(value.getQuantizationTableNumber()); 81 131 sb.append(", Sampling factors "); 82 sb.append( component.getHorizontalSamplingFactor());132 sb.append(value.getHorizontalSamplingFactor()); 83 133 sb.append(" horiz/"); 84 sb.append( component.getVerticalSamplingFactor());134 sb.append(value.getVerticalSamplingFactor()); 85 135 sb.append(" vert"); 86 136 return sb.toString(); -
trunk/src/com/drew/metadata/jpeg/JpegDirectory.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 on Aug 2, 2003. 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 23 import com.drew.lang.annotations.NotNull; 24 import com.drew.lang.annotations.Nullable; 19 25 import com.drew.metadata.Directory; 20 26 import com.drew.metadata.MetadataException; … … 24 30 /** 25 31 * Directory of tags and values for the SOF0 Jpeg segment. This segment holds basic metadata about the image. 26 * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes 32 * 33 * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes http://drewnoakes.com 27 34 */ 28 public class JpegDirectory extends Directory { 29 30 /** This is in bits/sample, usually 8 (12 and 16 not supported by most software). */ 31 public static final int TAG_JPEG_DATA_PRECISION = 0; 32 /** The image's height. Necessary for decoding the image, so it should always be there. */ 33 public static final int TAG_JPEG_IMAGE_HEIGHT = 1; 34 /** The image's width. Necessary for decoding the image, so it should always be there. */ 35 public static final int TAG_JPEG_IMAGE_WIDTH = 3; 36 /** Usually 1 = grey scaled, 3 = color YcbCr or YIQ, 4 = color CMYK 37 * Each component TAG_COMPONENT_DATA_[1-4], has the following meaning: 38 * component Id(1byte)(1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q), 39 * sampling factors (1byte) (bit 0-3 vertical., 4-7 horizontal.), 40 * quantization table number (1 byte). 41 * <p> 42 * This info is from http://www.funducode.com/freec/Fileformats/format3/format3b.htm 43 */ 44 public static final int TAG_JPEG_NUMBER_OF_COMPONENTS = 5; 35 public class JpegDirectory extends Directory 36 { 37 public static final int TAG_JPEG_COMPRESSION_TYPE = -3; 38 /** This is in bits/sample, usually 8 (12 and 16 not supported by most software). */ 39 public static final int TAG_JPEG_DATA_PRECISION = 0; 40 /** The image's height. Necessary for decoding the image, so it should always be there. */ 41 public static final int TAG_JPEG_IMAGE_HEIGHT = 1; 42 /** The image's width. Necessary for decoding the image, so it should always be there. */ 43 public static final int TAG_JPEG_IMAGE_WIDTH = 3; 44 /** 45 * Usually 1 = grey scaled, 3 = color YcbCr or YIQ, 4 = color CMYK 46 * Each component TAG_COMPONENT_DATA_[1-4], has the following meaning: 47 * component Id(1byte)(1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q), 48 * sampling factors (1byte) (bit 0-3 vertical., 4-7 horizontal.), 49 * quantization table number (1 byte). 50 * <p/> 51 * This info is from http://www.funducode.com/freec/Fileformats/format3/format3b.htm 52 */ 53 public static final int TAG_JPEG_NUMBER_OF_COMPONENTS = 5; 45 54 46 55 // NOTE! Component tag type int values must increment in steps of 1 47 56 48 49 50 51 52 53 54 55 57 /** the first of a possible 4 color components. Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS. */ 58 public static final int TAG_JPEG_COMPONENT_DATA_1 = 6; 59 /** the second of a possible 4 color components. Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS. */ 60 public static final int TAG_JPEG_COMPONENT_DATA_2 = 7; 61 /** the third of a possible 4 color components. Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS. */ 62 public static final int TAG_JPEG_COMPONENT_DATA_3 = 8; 63 /** the fourth of a possible 4 color components. Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS. */ 64 public static final int TAG_JPEG_COMPONENT_DATA_4 = 9; 56 65 57 protected static final HashMap tagNameMap = new HashMap(); 66 @NotNull 67 protected static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>(); 58 68 59 static { 60 tagNameMap.put(new Integer(TAG_JPEG_DATA_PRECISION), "Data Precision"); 61 tagNameMap.put(new Integer(TAG_JPEG_IMAGE_WIDTH), "Image Width"); 62 tagNameMap.put(new Integer(TAG_JPEG_IMAGE_HEIGHT), "Image Height"); 63 tagNameMap.put(new Integer(TAG_JPEG_NUMBER_OF_COMPONENTS), "Number of Components"); 64 tagNameMap.put(new Integer(TAG_JPEG_COMPONENT_DATA_1), "Component 1"); 65 tagNameMap.put(new Integer(TAG_JPEG_COMPONENT_DATA_2), "Component 2"); 66 tagNameMap.put(new Integer(TAG_JPEG_COMPONENT_DATA_3), "Component 3"); 67 tagNameMap.put(new Integer(TAG_JPEG_COMPONENT_DATA_4), "Component 4"); 68 } 69 static { 70 _tagNameMap.put(TAG_JPEG_COMPRESSION_TYPE, "Compression Type"); 71 _tagNameMap.put(TAG_JPEG_DATA_PRECISION, "Data Precision"); 72 _tagNameMap.put(TAG_JPEG_IMAGE_WIDTH, "Image Width"); 73 _tagNameMap.put(TAG_JPEG_IMAGE_HEIGHT, "Image Height"); 74 _tagNameMap.put(TAG_JPEG_NUMBER_OF_COMPONENTS, "Number of Components"); 75 _tagNameMap.put(TAG_JPEG_COMPONENT_DATA_1, "Component 1"); 76 _tagNameMap.put(TAG_JPEG_COMPONENT_DATA_2, "Component 2"); 77 _tagNameMap.put(TAG_JPEG_COMPONENT_DATA_3, "Component 3"); 78 _tagNameMap.put(TAG_JPEG_COMPONENT_DATA_4, "Component 4"); 79 } 69 80 70 public JpegDirectory() { 71 this.setDescriptor(new JpegDescriptor(this)); 72 } 81 public JpegDirectory() 82 { 83 this.setDescriptor(new JpegDescriptor(this)); 84 } 73 85 74 public String getName() { 75 return "Jpeg"; 76 } 86 @NotNull 87 public String getName() 88 { 89 return "Jpeg"; 90 } 77 91 78 protected HashMap getTagNameMap() { 79 return tagNameMap; 80 } 92 @NotNull 93 protected HashMap<Integer, String> getTagNameMap() 94 { 95 return _tagNameMap; 96 } 81 97 82 98 /** 83 *84 99 * @param componentNumber The zero-based index of the component. This number is normally between 0 and 3. 85 * Use getNumberOfComponents for bounds-checking. 86 * @return 100 * Use getNumberOfComponents for bounds-checking. 101 * @return the JpegComponent having the specified number. 87 102 */ 103 @Nullable 88 104 public JpegComponent getComponent(int componentNumber) 89 105 { 90 106 int tagType = JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + componentNumber; 91 92 JpegComponent component = (JpegComponent)getObject(tagType); 93 94 return component; 107 return (JpegComponent)getObject(tagType); 95 108 } 96 109 -
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 Metadataextract(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 ( MetadataExceptionme) {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.