Debian Codegraph @

src/com/drew/imaging/heif/HeifReader.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 package com.drew.imaging.heif;
22
23 import com.drew.lang.SequentialReader;
24 import com.drew.lang.StreamReader;
25 import com.drew.metadata.heif.HeifBoxTypes;
26 import com.drew.metadata.heif.HeifContainerTypes;
27 import com.drew.metadata.heif.HeifDirectory;
28 import com.drew.metadata.heif.boxes.Box;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.Set;
35
36 public class HeifReader
37 {
38 private static final Set<String> ACCEPTABLE_PRE_META_BOX_TYPES =
39 new HashSet<String>(Arrays.asList(HeifBoxTypes.BOX_FILE_TYPE, HeifContainerTypes.BOX_METADATA));
40
41 public void extract(InputStream inputStream, HeifHandler<?> handler)
42 {
43 // We need to read through the input stream to find the meta box which will tell us what handler to use
44
45 // The meta box is not necessarily the first box, so we need to mark the input stream (if we can)
46 // so we can re-read the stream with the proper handler if necessary
47
48 try {
49 boolean markSupported = false;
50 if (inputStream.markSupported()) {
51 markSupported = true;
52 inputStream.mark(inputStream.available() + 1); // +1 since we're going to read past the end of the stream by 1 byte
53 }
54
55 StreamReader reader = new StreamReader(inputStream);
56 reader.setMotorolaByteOrder(true);
57
58 processTopLevelBoxes(inputStream, reader, -1, handler, markSupported);
59 } catch (IOException e) {
60 // Any errors should have been added to the directory
61 }
62 }
63
64 private void processTopLevelBoxes(InputStream inputStream,
65 SequentialReader reader,
66 long atomEnd,
67 HeifHandler<?> handler,
68 boolean markSupported) throws IOException
69 {
70 boolean foundMetaBox = false;
71 boolean needToReset = false;
72 try {
73 while (atomEnd == -1 || reader.getPosition() < atomEnd) {
74
75 Box box = new Box(reader);
76
77 if (!foundMetaBox && !ACCEPTABLE_PRE_META_BOX_TYPES.contains(box.type)) {
78 // If we hit a box that needs a more specific handler (like mdat) without yet hitting the meta box,
79 // we'll need to reset the stream and use the correct handler once we find it
80 needToReset = true;
81 }
82
83 if (HeifContainerTypes.BOX_METADATA.equalsIgnoreCase(box.type)) {
84 foundMetaBox = true;
85 }
86
87 handler = processBox(reader, box, handler);
88 }
89 } catch (IOException e) {
90 // Currently, reader relies on IOException to end
91 }
92
93 if (needToReset && markSupported) {
94 inputStream.reset();
95 reader = new StreamReader(inputStream);
96 processBoxes(reader, -1, handler);
97 } else if (needToReset) {
98 HeifDirectory heifDirectory = handler.metadata.getFirstDirectoryOfType(HeifDirectory.class);
99 if (heifDirectory != null) {
100 heifDirectory.addError("Unable to extract Exif data because inputStream was not resettable and 'meta' was not first box");
101 }
102 }
103 }
104
105 private HeifHandler<?> processBoxes(SequentialReader reader, long atomEnd, HeifHandler<?> handler)
106 {
107 try {
108 while (atomEnd == -1 || reader.getPosition() < atomEnd) {
109
110 Box box = new Box(reader);
111
112 handler = processBox(reader, box, handler);
113 }
114 } catch (IOException e) {
115 // Currently, reader relies on IOException to end
116 }
117 return handler;
118 }
119
120 private HeifHandler<?> processBox(SequentialReader reader, Box box, HeifHandler<?> handler) throws IOException
121 {
122 if (handler.shouldAcceptContainer(box)) {
123 handler.processContainer(box, reader);
124 handler = processBoxes(reader, box.size + reader.getPosition() - 8, handler);
125 } else if (handler.shouldAcceptBox(box)) {
126 handler = handler.processBox(box, reader.getBytes((int) box.size - 8));
127 } else if (box.size > 1) {
128 reader.skip(box.size - 8);
129 }
130 return handler;
131 }
132 }