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 | */
|
---|
21 | package com.drew.metadata.jpeg;
|
---|
22 |
|
---|
23 | import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
|
---|
24 | import com.drew.imaging.jpeg.JpegSegmentType;
|
---|
25 | import com.drew.lang.SequentialByteArrayReader;
|
---|
26 | import com.drew.lang.SequentialReader;
|
---|
27 | import com.drew.lang.annotations.NotNull;
|
---|
28 | import com.drew.metadata.ErrorDirectory;
|
---|
29 | import com.drew.metadata.Metadata;
|
---|
30 |
|
---|
31 | import java.io.IOException;
|
---|
32 | import java.util.Arrays;
|
---|
33 | import 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 | */
|
---|
40 | public 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 | }
|
---|