[8132] | 1 | /*
|
---|
[13061] | 2 | * Copyright 2002-2017 Drew Noakes
|
---|
[8132] | 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.iptc;
|
---|
| 22 |
|
---|
| 23 | import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
|
---|
| 24 | import com.drew.imaging.jpeg.JpegSegmentType;
|
---|
| 25 | import com.drew.lang.SequentialByteArrayReader;
|
---|
| 26 | import com.drew.lang.SequentialReader;
|
---|
| 27 | import com.drew.lang.annotations.NotNull;
|
---|
[10862] | 28 | import com.drew.lang.annotations.Nullable;
|
---|
[8132] | 29 | import com.drew.metadata.Directory;
|
---|
| 30 | import com.drew.metadata.Metadata;
|
---|
[13061] | 31 | import com.drew.metadata.StringValue;
|
---|
[8132] | 32 |
|
---|
| 33 | import java.io.IOException;
|
---|
[13061] | 34 | import java.nio.charset.Charset;
|
---|
[10862] | 35 | import java.util.Collections;
|
---|
[8132] | 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Decodes IPTC binary data, populating a {@link Metadata} object with tag values in an {@link IptcDirectory}.
|
---|
| 39 | * <p>
|
---|
| 40 | * http://www.iptc.org/std/IIM/4.1/specification/IIMV4.1.pdf
|
---|
| 41 | *
|
---|
| 42 | * @author Drew Noakes https://drewnoakes.com
|
---|
| 43 | */
|
---|
| 44 | public class IptcReader implements JpegSegmentMetadataReader
|
---|
| 45 | {
|
---|
| 46 | // TODO consider breaking the IPTC section up into multiple directories and providing segregation of each IPTC directory
|
---|
| 47 | /*
|
---|
| 48 | public static final int DIRECTORY_IPTC = 2;
|
---|
| 49 |
|
---|
| 50 | public static final int ENVELOPE_RECORD = 1;
|
---|
| 51 | public static final int APPLICATION_RECORD_2 = 2;
|
---|
| 52 | public static final int APPLICATION_RECORD_3 = 3;
|
---|
| 53 | public static final int APPLICATION_RECORD_4 = 4;
|
---|
| 54 | public static final int APPLICATION_RECORD_5 = 5;
|
---|
| 55 | public static final int APPLICATION_RECORD_6 = 6;
|
---|
| 56 | public static final int PRE_DATA_RECORD = 7;
|
---|
| 57 | public static final int DATA_RECORD = 8;
|
---|
| 58 | public static final int POST_DATA_RECORD = 9;
|
---|
| 59 | */
|
---|
[13061] | 60 | private static final byte IptcMarkerByte = 0x1c;
|
---|
[8132] | 61 |
|
---|
| 62 | @NotNull
|
---|
| 63 | public Iterable<JpegSegmentType> getSegmentTypes()
|
---|
| 64 | {
|
---|
[10862] | 65 | return Collections.singletonList(JpegSegmentType.APPD);
|
---|
[8132] | 66 | }
|
---|
| 67 |
|
---|
[8243] | 68 | public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
|
---|
[8132] | 69 | {
|
---|
[8243] | 70 | for (byte[] segmentBytes : segments) {
|
---|
| 71 | // Ensure data starts with the IPTC marker byte
|
---|
[13061] | 72 | if (segmentBytes.length != 0 && segmentBytes[0] == IptcMarkerByte) {
|
---|
[8243] | 73 | extract(new SequentialByteArrayReader(segmentBytes), metadata, segmentBytes.length);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
[8132] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Performs the IPTC data extraction, adding found values to the specified instance of {@link Metadata}.
|
---|
| 80 | */
|
---|
| 81 | public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata, long length)
|
---|
| 82 | {
|
---|
[10862] | 83 | extract(reader, metadata, length, null);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Performs the IPTC data extraction, adding found values to the specified instance of {@link Metadata}.
|
---|
| 88 | */
|
---|
| 89 | public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata, long length, @Nullable Directory parentDirectory)
|
---|
| 90 | {
|
---|
[8243] | 91 | IptcDirectory directory = new IptcDirectory();
|
---|
| 92 | metadata.addDirectory(directory);
|
---|
[8132] | 93 |
|
---|
[10862] | 94 | if (parentDirectory != null)
|
---|
| 95 | directory.setParent(parentDirectory);
|
---|
| 96 |
|
---|
[8132] | 97 | int offset = 0;
|
---|
| 98 |
|
---|
| 99 | // for each tag
|
---|
| 100 | while (offset < length) {
|
---|
| 101 |
|
---|
| 102 | // identifies start of a tag
|
---|
| 103 | short startByte;
|
---|
| 104 | try {
|
---|
| 105 | startByte = reader.getUInt8();
|
---|
| 106 | offset++;
|
---|
| 107 | } catch (IOException e) {
|
---|
| 108 | directory.addError("Unable to read starting byte of IPTC tag");
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[13061] | 112 | if (startByte != IptcMarkerByte) {
|
---|
[8132] | 113 | // NOTE have seen images where there was one extra byte at the end, giving
|
---|
| 114 | // offset==length at this point, which is not worth logging as an error.
|
---|
| 115 | if (offset != length)
|
---|
[13061] | 116 | directory.addError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x" + Integer.toHexString(IptcMarkerByte) + "' but got '0x" + Integer.toHexString(startByte) + "'.");
|
---|
[8132] | 117 | return;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | // we need at least five bytes left to read a tag
|
---|
[10862] | 121 | if (offset + 5 > length) {
|
---|
[8132] | 122 | directory.addError("Too few bytes remain for a valid IPTC tag");
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | int directoryType;
|
---|
| 127 | int tagType;
|
---|
| 128 | int tagByteCount;
|
---|
| 129 | try {
|
---|
| 130 | directoryType = reader.getUInt8();
|
---|
| 131 | tagType = reader.getUInt8();
|
---|
| 132 | // TODO support Extended DataSet Tag (see 1.5(c), p14, IPTC-IIMV4.2.pdf)
|
---|
| 133 | tagByteCount = reader.getUInt16();
|
---|
| 134 | offset += 4;
|
---|
| 135 | } catch (IOException e) {
|
---|
| 136 | directory.addError("IPTC data segment ended mid-way through tag descriptor");
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if (offset + tagByteCount > length) {
|
---|
| 141 | directory.addError("Data for tag extends beyond end of IPTC segment");
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | try {
|
---|
| 146 | processTag(reader, directory, directoryType, tagType, tagByteCount);
|
---|
| 147 | } catch (IOException e) {
|
---|
| 148 | directory.addError("Error processing IPTC tag");
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | offset += tagByteCount;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | private void processTag(@NotNull SequentialReader reader, @NotNull Directory directory, int directoryType, int tagType, int tagByteCount) throws IOException
|
---|
| 157 | {
|
---|
| 158 | int tagIdentifier = tagType | (directoryType << 8);
|
---|
| 159 |
|
---|
| 160 | // Some images have been seen that specify a zero byte tag, which cannot be of much use.
|
---|
| 161 | // We elect here to completely ignore the tag. The IPTC specification doesn't mention
|
---|
| 162 | // anything about the interpretation of this situation.
|
---|
| 163 | // https://raw.githubusercontent.com/wiki/drewnoakes/metadata-extractor/docs/IPTC-IIMV4.2.pdf
|
---|
| 164 | if (tagByteCount == 0) {
|
---|
| 165 | directory.setString(tagIdentifier, "");
|
---|
| 166 | return;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | switch (tagIdentifier) {
|
---|
| 170 | case IptcDirectory.TAG_CODED_CHARACTER_SET:
|
---|
| 171 | byte[] bytes = reader.getBytes(tagByteCount);
|
---|
[13061] | 172 | String charsetName = Iso2022Converter.convertISO2022CharsetToJavaCharset(bytes);
|
---|
| 173 | if (charsetName == null) {
|
---|
[8132] | 174 | // Unable to determine the charset, so fall through and treat tag as a regular string
|
---|
[13061] | 175 | charsetName = new String(bytes);
|
---|
[8132] | 176 | }
|
---|
[13061] | 177 | directory.setString(tagIdentifier, charsetName);
|
---|
[8132] | 178 | return;
|
---|
| 179 | case IptcDirectory.TAG_ENVELOPE_RECORD_VERSION:
|
---|
| 180 | case IptcDirectory.TAG_APPLICATION_RECORD_VERSION:
|
---|
| 181 | case IptcDirectory.TAG_FILE_VERSION:
|
---|
| 182 | case IptcDirectory.TAG_ARM_VERSION:
|
---|
| 183 | case IptcDirectory.TAG_PROGRAM_VERSION:
|
---|
| 184 | // short
|
---|
| 185 | if (tagByteCount >= 2) {
|
---|
| 186 | int shortValue = reader.getUInt16();
|
---|
| 187 | reader.skip(tagByteCount - 2);
|
---|
| 188 | directory.setInt(tagIdentifier, shortValue);
|
---|
| 189 | return;
|
---|
| 190 | }
|
---|
| 191 | break;
|
---|
| 192 | case IptcDirectory.TAG_URGENCY:
|
---|
| 193 | // byte
|
---|
| 194 | directory.setInt(tagIdentifier, reader.getUInt8());
|
---|
| 195 | reader.skip(tagByteCount - 1);
|
---|
| 196 | return;
|
---|
| 197 | default:
|
---|
| 198 | // fall through
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | // If we haven't returned yet, treat it as a string
|
---|
| 202 | // NOTE that there's a chance we've already loaded the value as a string above, but failed to parse the value
|
---|
[13061] | 203 | String charSetName = directory.getString(IptcDirectory.TAG_CODED_CHARACTER_SET);
|
---|
| 204 | Charset charset = null;
|
---|
| 205 | try {
|
---|
| 206 | if (charSetName != null)
|
---|
| 207 | charset = Charset.forName(charSetName);
|
---|
| 208 | } catch (Throwable ignored) {
|
---|
[8132] | 209 | }
|
---|
| 210 |
|
---|
[13061] | 211 | StringValue string;
|
---|
| 212 | if (charSetName != null) {
|
---|
| 213 | string = reader.getStringValue(tagByteCount, charset);
|
---|
| 214 | } else {
|
---|
| 215 | byte[] bytes = reader.getBytes(tagByteCount);
|
---|
| 216 | Charset charSet = Iso2022Converter.guessCharSet(bytes);
|
---|
| 217 | string = charSet != null ? new StringValue(bytes, charSet) : new StringValue(bytes, null);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[8132] | 220 | if (directory.containsTag(tagIdentifier)) {
|
---|
[13061] | 221 | // this fancy StringValue[] business avoids using an ArrayList for performance reasons
|
---|
| 222 | StringValue[] oldStrings = directory.getStringValueArray(tagIdentifier);
|
---|
| 223 | StringValue[] newStrings;
|
---|
[8132] | 224 | if (oldStrings == null) {
|
---|
[10862] | 225 | // TODO hitting this block means any prior value(s) are discarded
|
---|
[13061] | 226 | newStrings = new StringValue[1];
|
---|
[8132] | 227 | } else {
|
---|
[13061] | 228 | newStrings = new StringValue[oldStrings.length + 1];
|
---|
[8132] | 229 | System.arraycopy(oldStrings, 0, newStrings, 0, oldStrings.length);
|
---|
| 230 | }
|
---|
| 231 | newStrings[newStrings.length - 1] = string;
|
---|
[13061] | 232 | directory.setStringValueArray(tagIdentifier, newStrings);
|
---|
[8132] | 233 | } else {
|
---|
[13061] | 234 | directory.setStringValue(tagIdentifier, string);
|
---|
[8132] | 235 | }
|
---|
| 236 | }
|
---|
| 237 | }
|
---|