source: josm/trunk/src/com/drew/metadata/jpeg/JpegReader.java@ 8132

Last change on this file since 8132 was 8132, checked in by Don-vip, 11 years ago

fix #11162 - update to metadata-extractor 2.7.2

File size: 4.2 KB
Line 
1/*
2 * Copyright 2002-2015 Drew Noakes
3 *
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
7 *
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 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21package com.drew.metadata.jpeg;
22
23import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
24import com.drew.imaging.jpeg.JpegSegmentType;
25import com.drew.lang.SequentialByteArrayReader;
26import com.drew.lang.SequentialReader;
27import com.drew.lang.annotations.NotNull;
28import com.drew.metadata.Metadata;
29
30import java.io.IOException;
31import java.util.Arrays;
32
33/**
34 * Decodes JPEG SOFn data, populating a {@link Metadata} object with tag values in a {@link JpegDirectory}.
35 *
36 * @author Drew Noakes https://drewnoakes.com
37 * @author Darrell Silver http://www.darrellsilver.com
38 */
39public class JpegReader implements JpegSegmentMetadataReader
40{
41 @NotNull
42 public Iterable<JpegSegmentType> getSegmentTypes()
43 {
44 // NOTE that some SOFn values do not exist
45 return Arrays.asList(
46 JpegSegmentType.SOF0,
47 JpegSegmentType.SOF1,
48 JpegSegmentType.SOF2,
49 JpegSegmentType.SOF3,
50// JpegSegmentType.SOF4,
51 JpegSegmentType.SOF5,
52 JpegSegmentType.SOF6,
53 JpegSegmentType.SOF7,
54 JpegSegmentType.SOF8,
55 JpegSegmentType.SOF9,
56 JpegSegmentType.SOF10,
57 JpegSegmentType.SOF11,
58// JpegSegmentType.SOF12,
59 JpegSegmentType.SOF13,
60 JpegSegmentType.SOF14,
61 JpegSegmentType.SOF15
62 );
63 }
64
65 public boolean canProcess(@NotNull byte[] segmentBytes, @NotNull JpegSegmentType segmentType)
66 {
67 return true;
68 }
69
70 public void extract(@NotNull byte[] segmentBytes, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
71 {
72 if (metadata.containsDirectory(JpegDirectory.class)) {
73 // If this directory is already present, discontinue this operation.
74 // We only store metadata for the *first* matching SOFn segment.
75 return;
76 }
77
78 JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class);
79
80 // The value of TAG_COMPRESSION_TYPE is determined by the segment type found
81 directory.setInt(JpegDirectory.TAG_COMPRESSION_TYPE, segmentType.byteValue - JpegSegmentType.SOF0.byteValue);
82
83 SequentialReader reader = new SequentialByteArrayReader(segmentBytes);
84
85 try {
86 directory.setInt(JpegDirectory.TAG_DATA_PRECISION, reader.getUInt8());
87 directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16());
88 directory.setInt(JpegDirectory.TAG_IMAGE_WIDTH, reader.getUInt16());
89 short componentCount = reader.getUInt8();
90 directory.setInt(JpegDirectory.TAG_NUMBER_OF_COMPONENTS, componentCount);
91
92 // for each component, there are three bytes of data:
93 // 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
94 // 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal
95 // 3 - Quantization table number
96 for (int i = 0; i < (int)componentCount; i++) {
97 final int componentId = reader.getUInt8();
98 final int samplingFactorByte = reader.getUInt8();
99 final int quantizationTableNumber = reader.getUInt8();
100 final JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber);
101 directory.setObject(JpegDirectory.TAG_COMPONENT_DATA_1 + i, component);
102 }
103
104 } catch (IOException ex) {
105 directory.addError(ex.getMessage());
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.