Debian Codegraph @

src/com/drew/metadata/adobe/AdobeJpegReader.java

raw symbols SCIP
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 */
21
22 package com.drew.metadata.adobe;
23
24 import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
25 import com.drew.imaging.jpeg.JpegSegmentType;
26 import com.drew.lang.SequentialByteArrayReader;
27 import com.drew.lang.SequentialReader;
28 import com.drew.lang.annotations.NotNull;
29 import com.drew.metadata.Directory;
30 import com.drew.metadata.Metadata;
31
32 import java.io.IOException;
33 import java.util.Collections;
34
35 /**
36 * Decodes Adobe formatted data stored in JPEG files, normally in the APPE (App14) segment.
37 *
38 * @author Philip
39 * @author Drew Noakes https://drewnoakes.com
40 */
41 @SuppressWarnings("WeakerAccess")
42 public class AdobeJpegReader implements JpegSegmentMetadataReader
43 {
44 public static final String PREAMBLE = "Adobe";
45
46 @NotNull
47 public Iterable<JpegSegmentType> getSegmentTypes()
48 {
49 return Collections.singletonList(JpegSegmentType.APPE);
50 }
51
52 public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
53 {
54 for (byte[] bytes : segments) {
55 if (bytes.length == 12 && PREAMBLE.equalsIgnoreCase(new String(bytes, 0, PREAMBLE.length())))
56 extract(new SequentialByteArrayReader(bytes), metadata);
57 }
58 }
59
60 public void extract(@NotNull SequentialReader reader, @NotNull Metadata metadata)
61 {
62 Directory directory = new AdobeJpegDirectory();
63 metadata.addDirectory(directory);
64
65 try {
66 reader.setMotorolaByteOrder(false);
67
68 if (!reader.getString(PREAMBLE.length()).equals(PREAMBLE)) {
69 directory.addError("Invalid Adobe JPEG data header.");
70 return;
71 }
72
73 directory.setInt(AdobeJpegDirectory.TAG_DCT_ENCODE_VERSION, reader.getUInt16());
74 directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS0, reader.getUInt16());
75 directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS1, reader.getUInt16());
76 directory.setInt(AdobeJpegDirectory.TAG_COLOR_TRANSFORM, reader.getInt8());
77 } catch (IOException ex) {
78 directory.addError("IO exception processing data: " + ex.getMessage());
79 }
80 }
81 }