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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.