Changeset 6127 in josm for trunk/src/com/drew/metadata/jpeg


Ignore:
Timestamp:
2013-08-09T18:05:11+02:00 (8 years ago)
Author:
bastiK
Message:

applied #8895 - Upgrade metadata-extractor to v. 2.6.4 (patch by ebourg)

Location:
trunk/src/com/drew/metadata/jpeg
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/drew/metadata/jpeg/JpegCommentDescriptor.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
    19 import com.drew.metadata.Directory;
     23import com.drew.lang.annotations.NotNull;
     24import com.drew.lang.annotations.Nullable;
    2025import com.drew.metadata.TagDescriptor;
    2126
    2227/**
     28 * Provides human-readable string representations of tag values stored in a <code>JpegCommentDirectory</code>.
    2329 *
    2430 * @author Drew Noakes http://drewnoakes.com
    2531 */
    26 public class JpegCommentDescriptor extends TagDescriptor
     32public class JpegCommentDescriptor extends TagDescriptor<JpegCommentDirectory>
    2733{
    28     public JpegCommentDescriptor(Directory directory)
     34    public JpegCommentDescriptor(@NotNull JpegCommentDirectory directory)
    2935    {
    3036        super(directory);
    3137    }
    3238
    33     public String getDescription(int tagType)
     39    @Nullable
     40    public String getJpegCommentDescription()
    3441    {
    35         return _directory.getString(tagType);
     42        return _directory.getString(JpegCommentDirectory.TAG_JPEG_COMMENT);
    3643    }
    3744}
  • trunk/src/com/drew/metadata/jpeg/JpegCommentDirectory.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
     23import com.drew.lang.annotations.NotNull;
    1924import com.drew.metadata.Directory;
    2025
     
    2227
    2328/**
     29 * Describes tags used by a JPEG file comment.
    2430 *
    2531 * @author Drew Noakes http://drewnoakes.com
    2632 */
    27 public class JpegCommentDirectory extends Directory {
     33public 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;
    2840
    29         /** This is in bits/sample, usually 8 (12 and 16 not supported by most software). */
    30         public static final int TAG_JPEG_COMMENT = 0;
     41    @NotNull
     42    protected static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>();
    3143
    32         protected static final HashMap tagNameMap = new HashMap();
     44    static {
     45        _tagNameMap.put(TAG_JPEG_COMMENT, "Jpeg Comment");
     46    }
    3347
    34         static {
    35         tagNameMap.put(new Integer(TAG_JPEG_COMMENT), "Jpeg Comment");
    36         }
     48    public JpegCommentDirectory()
     49    {
     50        this.setDescriptor(new JpegCommentDescriptor(this));
     51    }
    3752
    38     public JpegCommentDirectory() {
    39                 this.setDescriptor(new JpegCommentDescriptor(this));
    40         }
     53    @NotNull
     54    public String getName()
     55    {
     56        return "JpegComment";
     57    }
    4158
    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    }
    4964}
  • trunk/src/com/drew/metadata/jpeg/JpegCommentReader.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
    19 import com.drew.imaging.jpeg.JpegProcessingException;
    20 import com.drew.imaging.jpeg.JpegSegmentReader;
     23import com.drew.lang.BufferBoundsException;
     24import com.drew.lang.BufferReader;
     25import com.drew.lang.annotations.NotNull;
    2126import com.drew.metadata.Metadata;
    2227import com.drew.metadata.MetadataReader;
    2328
    24 import java.io.File;
    25 import java.io.InputStream;
    26 
    2729/**
     30 * Decodes the comment stored within Jpeg files, populating a <code>Metadata</code> object with tag values in a
     31 * <code>JpegCommentDirectory</code>.
    2832 *
    2933 * @author Drew Noakes http://drewnoakes.com
     
    3236{
    3337    /**
    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 JpegProcessingException
    42     {
    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 JpegProcessingException
    51     {
    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     /**
    6938     * Performs the Jpeg data extraction, adding found values to the specified
    7039     * instance of <code>Metadata</code>.
    7140     */
    72     public Metadata extract(Metadata metadata)
     41    public void extract(@NotNull final BufferReader reader, @NotNull Metadata metadata)
    7342    {
    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");
    7649        }
    77 
    78         JpegCommentDirectory directory = (JpegCommentDirectory)metadata.getDirectory(JpegCommentDirectory.class);
    79 
    80         directory.setString(JpegCommentDirectory.TAG_JPEG_COMMENT, new String(_data));
    81 
    82         return metadata;
    8350    }
    8451}
  • trunk/src/com/drew/metadata/jpeg/JpegComponent.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
    19 import com.drew.metadata.MetadataException;
     23import com.drew.lang.annotations.Nullable;
    2024
    2125import java.io.Serializable;
    2226
    2327/**
    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
    2932 */
    3033public class JpegComponent implements Serializable
    3134{
     35    private static final long serialVersionUID = 61121257899091914L;
     36
    3237    private final int _componentId;
    3338    private final int _samplingFactorByte;
     
    4651    }
    4752
    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()
    4959    {
    5060        switch (_componentId)
     
    6171                return "Q";
    6272        }
    63 
    64         throw new MetadataException("Unsupported component id: " + _componentId);
     73        return null;
    6574    }
    6675
  • trunk/src/com/drew/metadata/jpeg/JpegDescriptor.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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/
    1420 */
    1521package com.drew.metadata.jpeg;
    1622
    17 import com.drew.metadata.Directory;
    18 import com.drew.metadata.MetadataException;
     23import com.drew.lang.annotations.NotNull;
     24import com.drew.lang.annotations.Nullable;
    1925import com.drew.metadata.TagDescriptor;
    2026
     
    2228 * Provides human-readable string versions of the tags stored in a JpegDirectory.
    2329 * Thanks to Darrell Silver (www.darrellsilver.com) for the initial version of this class.
     30 *
     31 * @author Drew Noakes http://drewnoakes.com
    2432 */
    25 public class JpegDescriptor extends TagDescriptor
     33public class JpegDescriptor extends TagDescriptor<JpegDirectory>
    2634{
    27     public JpegDescriptor(Directory directory)
     35    public JpegDescriptor(@NotNull JpegDirectory directory)
    2836    {
    2937        super(directory);
    3038    }
    3139
    32     public String getDescription(int tagType) throws MetadataException
     40    @Nullable
     41    public String getDescription(int tagType)
    3342    {
    3443        switch (tagType)
    3544        {
     45            case JpegDirectory.TAG_JPEG_COMPRESSION_TYPE:
     46                return getImageCompressionTypeDescription();
    3647            case JpegDirectory.TAG_JPEG_COMPONENT_DATA_1:
    3748                return getComponentDataDescription(0);
     
    4859            case JpegDirectory.TAG_JPEG_IMAGE_WIDTH:
    4960                return getImageWidthDescription();
     61            default:
     62                return super.getDescription(tagType);
    5063        }
    51 
    52         return _directory.getString(tagType);
    5364    }
    5465
     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
    5593    public String getImageWidthDescription()
    5694    {
    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";
    5899    }
    59100
     101    @Nullable
    60102    public String getImageHeightDescription()
    61103    {
    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";
    63108    }
    64109
     110    @Nullable
    65111    public String getDataPrecisionDescription()
    66112    {
    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";
    68117    }
    69118
    70     public String getComponentDataDescription(int componentNumber) throws MetadataException
     119    @Nullable
     120    public String getComponentDataDescription(int componentNumber)
    71121    {
    72         JpegComponent component = ((JpegDirectory)_directory).getComponent(componentNumber);
     122        JpegComponent value = _directory.getComponent(componentNumber);
    73123
    74         if (component==null)
    75             throw new MetadataException("No Jpeg component exists with number " + componentNumber);
     124        if (value==null)
     125            return null;
    76126
    77         StringBuffer sb = new StringBuffer();
    78         sb.append(component.getComponentName());
     127        StringBuilder sb = new StringBuilder();
     128        sb.append(value.getComponentName());
    79129        sb.append(" component: Quantization table ");
    80         sb.append(component.getQuantizationTableNumber());
     130        sb.append(value.getQuantizationTableNumber());
    81131        sb.append(", Sampling factors ");
    82         sb.append(component.getHorizontalSamplingFactor());
     132        sb.append(value.getHorizontalSamplingFactor());
    83133        sb.append(" horiz/");
    84         sb.append(component.getVerticalSamplingFactor());
     134        sb.append(value.getVerticalSamplingFactor());
    85135        sb.append(" vert");
    86136        return sb.toString();
  • trunk/src/com/drew/metadata/jpeg/JpegDirectory.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
     23import com.drew.lang.annotations.NotNull;
     24import com.drew.lang.annotations.Nullable;
    1925import com.drew.metadata.Directory;
    2026import com.drew.metadata.MetadataException;
     
    2430/**
    2531 * 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
    2734 */
    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;
     35public 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;
    4554
    4655    // NOTE!  Component tag type int values must increment in steps of 1
    4756
    48         /** the first of a possible 4 color components.  Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS.*/
    49         public static final int TAG_JPEG_COMPONENT_DATA_1 = 6;
    50         /** the second of a possible 4 color components.  Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS.*/
    51         public static final int TAG_JPEG_COMPONENT_DATA_2 = 7;
    52         /** the third of a possible 4 color components.  Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS.*/
    53         public static final int TAG_JPEG_COMPONENT_DATA_3 = 8;
    54         /** the fourth of a possible 4 color components.  Number of components specified in TAG_JPEG_NUMBER_OF_COMPONENTS.*/
    55         public static final int TAG_JPEG_COMPONENT_DATA_4 = 9;
     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;
    5665
    57         protected static final HashMap tagNameMap = new HashMap();
     66    @NotNull
     67    protected static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>();
    5868
    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    }
    6980
    70     public JpegDirectory() {
    71                 this.setDescriptor(new JpegDescriptor(this));
    72         }
     81    public JpegDirectory()
     82    {
     83        this.setDescriptor(new JpegDescriptor(this));
     84    }
    7385
    74         public String getName() {
    75                 return "Jpeg";
    76         }
     86    @NotNull
     87    public String getName()
     88    {
     89        return "Jpeg";
     90    }
    7791
    78         protected HashMap getTagNameMap() {
    79                 return tagNameMap;
    80         }
     92    @NotNull
     93    protected HashMap<Integer, String> getTagNameMap()
     94    {
     95        return _tagNameMap;
     96    }
    8197
    8298    /**
    83      *
    8499     * @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.
    87102     */
     103    @Nullable
    88104    public JpegComponent getComponent(int componentNumber)
    89105    {
    90106        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);
    95108    }
    96109
  • trunk/src/com/drew/metadata/jpeg/JpegReader.java

    r4231 r6127  
    11/*
    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
    63 *
    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
    97 *
    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
    149 *
    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/
    1620 */
    1721package com.drew.metadata.jpeg;
    1822
    19 import com.drew.imaging.jpeg.JpegProcessingException;
    20 import com.drew.imaging.jpeg.JpegSegmentReader;
     23import com.drew.lang.BufferBoundsException;
     24import com.drew.lang.BufferReader;
     25import com.drew.lang.annotations.NotNull;
    2126import com.drew.metadata.Metadata;
    22 import com.drew.metadata.MetadataException;
    2327import com.drew.metadata.MetadataReader;
    2428
    25 import java.io.File;
    26 import java.io.InputStream;
    27 
    2829/**
     30 * Decodes Jpeg SOF0 data, populating a <code>Metadata</code> object with tag values in a <code>JpegDirectory</code>.
    2931 *
    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
    3133 */
    3234public class JpegReader implements MetadataReader
    3335{
    3436    /**
    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     /**
    7037     * Performs the Jpeg data extraction, adding found values to the specified
    7138     * instance of <code>Metadata</code>.
    7239     */
    73     public Metadata extract(Metadata metadata)
     40    public void extract(@NotNull final BufferReader reader, @NotNull Metadata metadata)
    7441    {
    75         if (_data==null) {
    76             return metadata;
    77         }
    78 
    79         JpegDirectory directory = (JpegDirectory)metadata.getDirectory(JpegDirectory.class);
     42        JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class);
    8043
    8144        try {
    8245            // data precision
    83             int dataPrecision = get16Bits(JpegDirectory.TAG_JPEG_DATA_PRECISION);
     46            int dataPrecision = reader.getUInt8(JpegDirectory.TAG_JPEG_DATA_PRECISION);
    8447            directory.setInt(JpegDirectory.TAG_JPEG_DATA_PRECISION, dataPrecision);
    8548
    8649            // process height
    87             int height = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
     50            int height = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
    8851            directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT, height);
    8952
    9053            // process width
    91             int width = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
     54            int width = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
    9255            directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH, width);
    9356
    9457            // number of components
    95             int numberOfComponents = get16Bits(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);
     58            int numberOfComponents = reader.getUInt8(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);
    9659            directory.setInt(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS, numberOfComponents);
    9760
     
    10164            // 3 - Quantization table number
    10265            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++);
    10870                JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber);
    10971                directory.setObject(JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + i, component);
    11072            }
    11173
    112         } catch (MetadataException me) {
    113             directory.addError("MetadataException: " + me);
     74        } catch (BufferBoundsException ex) {
     75            directory.addError(ex.getMessage());
    11476        }
    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);
    14577    }
    14678}
Note: See TracChangeset for help on using the changeset viewer.