1 | /*
|
---|
2 | * Copyright 2002-2015 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.tiff;
|
---|
22 |
|
---|
23 | import com.drew.imaging.tiff.TiffHandler;
|
---|
24 | import com.drew.lang.Rational;
|
---|
25 | import com.drew.lang.annotations.NotNull;
|
---|
26 | import com.drew.metadata.Directory;
|
---|
27 | import com.drew.metadata.Metadata;
|
---|
28 |
|
---|
29 | import java.util.Stack;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Adapter between the {@link TiffHandler} interface and the {@link Metadata}/{@link Directory} object model.
|
---|
33 | *
|
---|
34 | * @author Drew Noakes https://drewnoakes.com
|
---|
35 | */
|
---|
36 | public abstract class DirectoryTiffHandler implements TiffHandler
|
---|
37 | {
|
---|
38 | private final Stack<Directory> _directoryStack = new Stack<Directory>();
|
---|
39 |
|
---|
40 | protected Directory _currentDirectory;
|
---|
41 | protected final Metadata _metadata;
|
---|
42 |
|
---|
43 | protected DirectoryTiffHandler(Metadata metadata, Class<? extends Directory> initialDirectoryClass)
|
---|
44 | {
|
---|
45 | _metadata = metadata;
|
---|
46 | try {
|
---|
47 | _currentDirectory = initialDirectoryClass.newInstance();
|
---|
48 | } catch (InstantiationException e) {
|
---|
49 | throw new RuntimeException(e);
|
---|
50 | } catch (IllegalAccessException e) {
|
---|
51 | throw new RuntimeException(e);
|
---|
52 | }
|
---|
53 | _metadata.addDirectory(_currentDirectory);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void endingIFD()
|
---|
57 | {
|
---|
58 | _currentDirectory = _directoryStack.empty() ? null : _directoryStack.pop();
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void pushDirectory(@NotNull Class<? extends Directory> directoryClass)
|
---|
62 | {
|
---|
63 | _directoryStack.push(_currentDirectory);
|
---|
64 | try {
|
---|
65 | _currentDirectory = directoryClass.newInstance();
|
---|
66 | } catch (InstantiationException e) {
|
---|
67 | throw new RuntimeException(e);
|
---|
68 | } catch (IllegalAccessException e) {
|
---|
69 | throw new RuntimeException(e);
|
---|
70 | }
|
---|
71 | _metadata.addDirectory(_currentDirectory);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void warn(@NotNull String message)
|
---|
75 | {
|
---|
76 | _currentDirectory.addError(message);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void error(@NotNull String message)
|
---|
80 | {
|
---|
81 | _currentDirectory.addError(message);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void setByteArray(int tagId, @NotNull byte[] bytes)
|
---|
85 | {
|
---|
86 | _currentDirectory.setByteArray(tagId, bytes);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void setString(int tagId, @NotNull String string)
|
---|
90 | {
|
---|
91 | _currentDirectory.setString(tagId, string);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void setRational(int tagId, @NotNull Rational rational)
|
---|
95 | {
|
---|
96 | _currentDirectory.setRational(tagId, rational);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void setRationalArray(int tagId, @NotNull Rational[] array)
|
---|
100 | {
|
---|
101 | _currentDirectory.setRationalArray(tagId, array);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void setFloat(int tagId, float float32)
|
---|
105 | {
|
---|
106 | _currentDirectory.setFloat(tagId, float32);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void setFloatArray(int tagId, @NotNull float[] array)
|
---|
110 | {
|
---|
111 | _currentDirectory.setFloatArray(tagId, array);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void setDouble(int tagId, double double64)
|
---|
115 | {
|
---|
116 | _currentDirectory.setDouble(tagId, double64);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void setDoubleArray(int tagId, @NotNull double[] array)
|
---|
120 | {
|
---|
121 | _currentDirectory.setDoubleArray(tagId, array);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void setInt8s(int tagId, byte int8s)
|
---|
125 | {
|
---|
126 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
127 | _currentDirectory.setInt(tagId, int8s);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void setInt8sArray(int tagId, @NotNull byte[] array)
|
---|
131 | {
|
---|
132 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
133 | _currentDirectory.setByteArray(tagId, array);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void setInt8u(int tagId, short int8u)
|
---|
137 | {
|
---|
138 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
139 | _currentDirectory.setInt(tagId, int8u);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void setInt8uArray(int tagId, @NotNull short[] array)
|
---|
143 | {
|
---|
144 | // TODO create and use a proper setter for short[]
|
---|
145 | _currentDirectory.setObjectArray(tagId, array);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void setInt16s(int tagId, int int16s)
|
---|
149 | {
|
---|
150 | // TODO create and use a proper setter for int16u?
|
---|
151 | _currentDirectory.setInt(tagId, int16s);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void setInt16sArray(int tagId, @NotNull short[] array)
|
---|
155 | {
|
---|
156 | // TODO create and use a proper setter for short[]
|
---|
157 | _currentDirectory.setObjectArray(tagId, array);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void setInt16u(int tagId, int int16u)
|
---|
161 | {
|
---|
162 | // TODO create and use a proper setter for
|
---|
163 | _currentDirectory.setInt(tagId, int16u);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void setInt16uArray(int tagId, @NotNull int[] array)
|
---|
167 | {
|
---|
168 | // TODO create and use a proper setter for short[]
|
---|
169 | _currentDirectory.setObjectArray(tagId, array);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void setInt32s(int tagId, int int32s)
|
---|
173 | {
|
---|
174 | _currentDirectory.setInt(tagId, int32s);
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void setInt32sArray(int tagId, @NotNull int[] array)
|
---|
178 | {
|
---|
179 | _currentDirectory.setIntArray(tagId, array);
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void setInt32u(int tagId, long int32u)
|
---|
183 | {
|
---|
184 | _currentDirectory.setLong(tagId, int32u);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public void setInt32uArray(int tagId, @NotNull long[] array)
|
---|
188 | {
|
---|
189 | // TODO create and use a proper setter for short[]
|
---|
190 | _currentDirectory.setObjectArray(tagId, array);
|
---|
191 | }
|
---|
192 | }
|
---|