File: DirectoryTiffHandler.java

package info (click to toggle)
libmetadata-extractor-java 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 6,416 kB
  • sloc: java: 35,343; xml: 200; sh: 11; makefile: 2
file content (218 lines) | stat: -rw-r--r-- 6,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright 2002-2017 Drew Noakes
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * More information about this project is available at:
 *
 *    https://drewnoakes.com/code/exif/
 *    https://github.com/drewnoakes/metadata-extractor
 */
package com.drew.metadata.tiff;

import com.drew.imaging.tiff.TiffHandler;
import com.drew.lang.Rational;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.Directory;
import com.drew.metadata.ErrorDirectory;
import com.drew.metadata.Metadata;
import com.drew.metadata.StringValue;

import java.util.Stack;

/**
 * Adapter between the {@link TiffHandler} interface and the {@link Metadata}/{@link Directory} object model.
 *
 * @author Drew Noakes https://drewnoakes.com
 */
public abstract class DirectoryTiffHandler implements TiffHandler
{
    private final Stack<Directory> _directoryStack = new Stack<Directory>();

    @Nullable private Directory _rootParentDirectory;
    @Nullable protected Directory _currentDirectory;
    protected final Metadata _metadata;

    protected DirectoryTiffHandler(Metadata metadata, @Nullable Directory parentDirectory)
    {
        _metadata = metadata;
        _rootParentDirectory = parentDirectory;
    }

    public void endingIFD()
    {
        _currentDirectory = _directoryStack.empty() ? null : _directoryStack.pop();
    }

    protected void pushDirectory(@NotNull Class<? extends Directory> directoryClass)
    {
        Directory newDirectory;

        try {
            newDirectory = directoryClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        // If this is the first directory, don't add to the stack
        if (_currentDirectory == null) {
            // Apply any pending root parent to this new directory
            if (_rootParentDirectory != null) {
                newDirectory.setParent(_rootParentDirectory);
                _rootParentDirectory = null;
            }
        }
        else {
            // The current directory is pushed onto the stack, and set as the new directory's parent
            _directoryStack.push(_currentDirectory);
            newDirectory.setParent(_currentDirectory);
        }

        _currentDirectory = newDirectory;
        _metadata.addDirectory(_currentDirectory);
    }

    public void warn(@NotNull String message)
    {
        getCurrentOrErrorDirectory().addError(message);
    }

    public void error(@NotNull String message)
    {
        getCurrentOrErrorDirectory().addError(message);
    }

    @NotNull
    private Directory getCurrentOrErrorDirectory()
    {
        if (_currentDirectory != null)
            return _currentDirectory;
        ErrorDirectory error = _metadata.getFirstDirectoryOfType(ErrorDirectory.class);
        if (error != null)
            return error;
        pushDirectory(ErrorDirectory.class);
        return _currentDirectory;
    }

    public void setByteArray(int tagId, @NotNull byte[] bytes)
    {
        _currentDirectory.setByteArray(tagId, bytes);
    }

    public void setString(int tagId, @NotNull StringValue string)
    {
        _currentDirectory.setStringValue(tagId, string);
    }

    public void setRational(int tagId, @NotNull Rational rational)
    {
        _currentDirectory.setRational(tagId, rational);
    }

    public void setRationalArray(int tagId, @NotNull Rational[] array)
    {
        _currentDirectory.setRationalArray(tagId, array);
    }

    public void setFloat(int tagId, float float32)
    {
        _currentDirectory.setFloat(tagId, float32);
    }

    public void setFloatArray(int tagId, @NotNull float[] array)
    {
        _currentDirectory.setFloatArray(tagId, array);
    }

    public void setDouble(int tagId, double double64)
    {
        _currentDirectory.setDouble(tagId, double64);
    }

    public void setDoubleArray(int tagId, @NotNull double[] array)
    {
        _currentDirectory.setDoubleArray(tagId, array);
    }

    public void setInt8s(int tagId, byte int8s)
    {
        // NOTE Directory stores all integral types as int32s, except for int32u and long
        _currentDirectory.setInt(tagId, int8s);
    }

    public void setInt8sArray(int tagId, @NotNull byte[] array)
    {
        // NOTE Directory stores all integral types as int32s, except for int32u and long
        _currentDirectory.setByteArray(tagId, array);
    }

    public void setInt8u(int tagId, short int8u)
    {
        // NOTE Directory stores all integral types as int32s, except for int32u and long
        _currentDirectory.setInt(tagId, int8u);
    }

    public void setInt8uArray(int tagId, @NotNull short[] array)
    {
        // TODO create and use a proper setter for short[]
        _currentDirectory.setObjectArray(tagId, array);
    }

    public void setInt16s(int tagId, int int16s)
    {
        // TODO create and use a proper setter for int16u?
        _currentDirectory.setInt(tagId, int16s);
    }

    public void setInt16sArray(int tagId, @NotNull short[] array)
    {
        // TODO create and use a proper setter for short[]
        _currentDirectory.setObjectArray(tagId, array);
    }

    public void setInt16u(int tagId, int int16u)
    {
        // TODO create and use a proper setter for
        _currentDirectory.setInt(tagId, int16u);
    }

    public void setInt16uArray(int tagId, @NotNull int[] array)
    {
        // TODO create and use a proper setter for short[]
        _currentDirectory.setObjectArray(tagId, array);
    }

    public void setInt32s(int tagId, int int32s)
    {
        _currentDirectory.setInt(tagId, int32s);
    }

    public void setInt32sArray(int tagId, @NotNull int[] array)
    {
        _currentDirectory.setIntArray(tagId, array);
    }

    public void setInt32u(int tagId, long int32u)
    {
        _currentDirectory.setLong(tagId, int32u);
    }

    public void setInt32uArray(int tagId, @NotNull long[] array)
    {
        // TODO create and use a proper setter for short[]
        _currentDirectory.setObjectArray(tagId, array);
    }
}