source: josm/trunk/src/com/drew/metadata/jpeg/JpegDnlReader.java@ 13500

Last change on this file since 13500 was 13061, checked in by Don-vip, 6 years ago

fix #15505 - update to metadata-extractor 2.10.1

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1/*
2 * Copyright 2002-2017 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.ErrorDirectory;
29import com.drew.metadata.Metadata;
30
31import java.io.IOException;
32import java.util.Arrays;
33import java.util.Collections;
34
35/**
36 * Decodes JPEG DNL data, adjusting the image height with information missing from the JPEG SOFx segment.
37 *
38 * @author Nadahar
39 */
40public class JpegDnlReader implements JpegSegmentMetadataReader
41{
42 @NotNull
43 public Iterable<JpegSegmentType> getSegmentTypes()
44 {
45 return Collections.singletonList(JpegSegmentType.DNL);
46 }
47
48 public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
49 {
50 for (byte[] segmentBytes : segments) {
51 extract(segmentBytes, metadata, segmentType);
52 }
53 }
54
55 public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType)
56 {
57 JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
58 if (directory == null) {
59 ErrorDirectory errorDirectory = new ErrorDirectory();
60 metadata.addDirectory(errorDirectory);
61 errorDirectory.addError("DNL segment found without SOFx - illegal JPEG format");
62 return;
63 }
64
65 SequentialReader reader = new SequentialByteArrayReader(segmentBytes);
66
67 try {
68 // Only set height from DNL if it's not already defined
69 Integer i = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT);
70 if (i == null || i == 0) {
71 directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16());
72 }
73 } catch (IOException ex) {
74 directory.addError(ex.getMessage());
75 }
76 }
77}
Note: See TracBrowser for help on using the repository browser.