| 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.exif; |
| 23 | |
| 24 | import com.drew.lang.annotations.NotNull; |
| 25 | |
| 26 | import java.util.HashMap; |
| 27 | |
| 28 | /** |
| 29 | * One of several Exif directories. Otherwise known as IFD1, this directory holds information about an embedded thumbnail image. |
| 30 | * |
| 31 | * @author Drew Noakes https://drewnoakes.com |
| 32 | */ |
| 33 | @SuppressWarnings("WeakerAccess") |
| 34 | public class ExifThumbnailDirectory extends ExifDirectoryBase |
| 35 | { |
| 36 | /** |
| 37 | * The offset to thumbnail image bytes, relative to the start of the IFD. |
| 38 | * |
| 39 | * To obtain the offset relative to the start of the TIFF data stream, use |
| 40 | * <code>getAdjustedThumbnailOffset</code>, which includes the value of |
| 41 | * <code>getExifStartOffset</code>. |
| 42 | */ |
| 43 | public static final int TAG_THUMBNAIL_OFFSET = 0x0201; |
| 44 | /** |
| 45 | * The size of the thumbnail image data in bytes. |
| 46 | */ |
| 47 | public static final int TAG_THUMBNAIL_LENGTH = 0x0202; |
| 48 | |
| 49 | /** |
| 50 | * @deprecated use {@link com.drew.metadata.exif.ExifDirectoryBase#TAG_COMPRESSION} instead. |
| 51 | */ |
| 52 | @Deprecated |
| 53 | public static final int TAG_THUMBNAIL_COMPRESSION = 0x0103; |
| 54 | |
| 55 | @NotNull |
| 56 | private static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>(); |
| 57 | |
| 58 | static |
| 59 | { |
| 60 | addExifTagNames(_tagNameMap); |
| 61 | |
| 62 | _tagNameMap.put(TAG_THUMBNAIL_OFFSET, "Thumbnail Offset"); |
| 63 | _tagNameMap.put(TAG_THUMBNAIL_LENGTH, "Thumbnail Length"); |
| 64 | } |
| 65 | |
| 66 | private final int _exifStartOffset; |
| 67 | |
| 68 | public ExifThumbnailDirectory(int exifStartOffset) |
| 69 | { |
| 70 | _exifStartOffset = exifStartOffset; |
| 71 | this.setDescriptor(new ExifThumbnailDescriptor(this)); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | @NotNull |
| 76 | public String getName() |
| 77 | { |
| 78 | return "Exif Thumbnail"; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | @NotNull |
| 83 | protected HashMap<Integer, String> getTagNameMap() |
| 84 | { |
| 85 | return _tagNameMap; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the offset at which the Exif data stream commenced within any containing stream. |
| 90 | */ |
| 91 | public int getExifStartOffset() |
| 92 | { |
| 93 | return _exifStartOffset; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the offset to thumbnail data within the outermost data stream. |
| 98 | * |
| 99 | * The value for <code>TagThumbnailOffset</code> is relative to the Exif data stream. |
| 100 | * Generally, consumers of thumbnail data need this value relative to the outermost stream, |
| 101 | * so that the thumbnail data may be extracted from that stream. |
| 102 | * |
| 103 | * This property adds the value of <code>ExifStartOffset</code> to this tag's value in order |
| 104 | * to produce that value. |
| 105 | * |
| 106 | * Returns <code>null</code> when the tag is not defined in this directory. |
| 107 | */ |
| 108 | public Integer getAdjustedThumbnailOffset() |
| 109 | { |
| 110 | Integer offset = this.getInteger(TAG_THUMBNAIL_OFFSET); |
| 111 | |
| 112 | if (offset == null) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | return offset.intValue() + _exifStartOffset; |
| 117 | } |
| 118 | } |