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