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

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

see #17848 - update to metadata-extractor 2.12.0

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