| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.photoshop; |
| 22 |
|
| 23 |
import com.drew.imaging.ImageProcessingException; |
| 24 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 25 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 26 |
import com.drew.lang.ByteArrayReader; |
| 27 |
import com.drew.lang.SequentialByteArrayReader; |
| 28 |
import com.drew.lang.SequentialReader; |
| 29 |
import com.drew.lang.annotations.NotNull; |
| 30 |
import com.drew.lang.annotations.Nullable; |
| 31 |
import com.drew.metadata.Directory; |
| 32 |
import com.drew.metadata.Metadata; |
| 33 |
import com.drew.metadata.exif.ExifReader; |
| 34 |
import com.drew.metadata.icc.IccReader; |
| 35 |
import com.drew.metadata.iptc.IptcReader; |
| 36 |
import com.drew.metadata.xmp.XmpReader; |
| 37 |
|
| 38 |
import java.util.Arrays; |
| 39 |
import java.util.Collections; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class PhotoshopReader implements JpegSegmentMetadataReader |
| 51 |
{ |
| 52 |
@NotNull |
| 53 |
private static final String JPEG_SEGMENT_PREAMBLE = "Photoshop 3.0"; |
| 54 |
|
| 55 |
@NotNull |
| 56 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 57 |
{ |
| 58 |
return Collections.singletonList(JpegSegmentType.APPD); |
| 59 |
} |
| 60 |
|
| 61 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 62 |
{ |
| 63 |
final int preambleLength = JPEG_SEGMENT_PREAMBLE.length(); |
| 64 |
|
| 65 |
for (byte[] segmentBytes : segments) { |
| 66 |
|
| 67 |
if (segmentBytes.length < preambleLength + 1 || !JPEG_SEGMENT_PREAMBLE.equals(new String(segmentBytes, 0, preambleLength))) |
| 68 |
continue; |
| 69 |
|
| 70 |
extract( |
| 71 |
new SequentialByteArrayReader(segmentBytes, preambleLength + 1), |
| 72 |
segmentBytes.length - preambleLength - 1, |
| 73 |
metadata); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public void extract(@NotNull final SequentialReader reader, int length, @NotNull final Metadata metadata) |
| 78 |
{ |
| 79 |
extract(reader, length, metadata, null); |
| 80 |
} |
| 81 |
|
| 82 |
public void extract(@NotNull final SequentialReader reader, int length, @NotNull final Metadata metadata, @Nullable final Directory parentDirectory) |
| 83 |
{ |
| 84 |
PhotoshopDirectory directory = new PhotoshopDirectory(); |
| 85 |
metadata.addDirectory(directory); |
| 86 |
|
| 87 |
if (parentDirectory != null) |
| 88 |
directory.setParent(parentDirectory); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
int pos = 0; |
| 101 |
int clippingPathCount = 0; |
| 102 |
while (pos < length) { |
| 103 |
try { |
| 104 |
|
| 105 |
String signature = reader.getString(4); |
| 106 |
pos += 4; |
| 107 |
|
| 108 |
|
| 109 |
int tagType = reader.getUInt16(); |
| 110 |
pos += 2; |
| 111 |
|
| 112 |
|
| 113 |
int descriptionLength = reader.getUInt8(); |
| 114 |
pos += 1; |
| 115 |
|
| 116 |
if (descriptionLength < 0 || descriptionLength + pos > length) |
| 117 |
throw new ImageProcessingException("Invalid string length"); |
| 118 |
|
| 119 |
|
| 120 |
StringBuilder description = new StringBuilder(); |
| 121 |
descriptionLength += pos; |
| 122 |
|
| 123 |
while (pos < descriptionLength) { |
| 124 |
description.append((char)reader.getUInt8()); |
| 125 |
pos ++; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
if (pos % 2 != 0) { |
| 130 |
reader.skip(1); |
| 131 |
pos++; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
int byteCount = reader.getInt32(); |
| 136 |
pos += 4; |
| 137 |
|
| 138 |
byte[] tagBytes = reader.getBytes(byteCount); |
| 139 |
pos += byteCount; |
| 140 |
|
| 141 |
if (pos % 2 != 0) { |
| 142 |
reader.skip(1); |
| 143 |
pos++; |
| 144 |
} |
| 145 |
|
| 146 |
if (signature.equals("8BIM")) { |
| 147 |
if (tagType == PhotoshopDirectory.TAG_IPTC) |
| 148 |
new IptcReader().extract(new SequentialByteArrayReader(tagBytes), metadata, tagBytes.length, directory); |
| 149 |
else if (tagType == PhotoshopDirectory.TAG_ICC_PROFILE_BYTES) |
| 150 |
new IccReader().extract(new ByteArrayReader(tagBytes), metadata, directory); |
| 151 |
else if (tagType == PhotoshopDirectory.TAG_EXIF_DATA_1 || tagType == PhotoshopDirectory.TAG_EXIF_DATA_3) |
| 152 |
new ExifReader().extract(new ByteArrayReader(tagBytes), metadata, 0, directory); |
| 153 |
else if (tagType == PhotoshopDirectory.TAG_XMP_DATA) |
| 154 |
new XmpReader().extract(tagBytes, metadata, directory); |
| 155 |
else if (tagType >= 0x07D0 && tagType <= 0x0BB6) { |
| 156 |
clippingPathCount++; |
| 157 |
tagBytes = Arrays.copyOf(tagBytes, tagBytes.length + description.length() + 1); |
| 158 |
|
| 159 |
for (int i = tagBytes.length - description.length() - 1; i < tagBytes.length; i++) { |
| 160 |
if (i % (tagBytes.length - description.length() - 1 + description.length()) == 0) |
| 161 |
tagBytes[i] = (byte)description.length(); |
| 162 |
else |
| 163 |
tagBytes[i] = (byte)description.charAt(i - (tagBytes.length - description.length() - 1)); |
| 164 |
} |
| 165 |
PhotoshopDirectory._tagNameMap.put(0x07CF + clippingPathCount, "Path Info " + clippingPathCount); |
| 166 |
directory.setByteArray(0x07CF + clippingPathCount, tagBytes); |
| 167 |
} |
| 168 |
else |
| 169 |
directory.setByteArray(tagType, tagBytes); |
| 170 |
|
| 171 |
if (tagType >= 0x0fa0 && tagType <= 0x1387) |
| 172 |
PhotoshopDirectory._tagNameMap.put(tagType, String.format("Plug-in %d Data", tagType - 0x0fa0 + 1)); |
| 173 |
} |
| 174 |
} catch (Exception ex) { |
| 175 |
directory.addError(ex.getMessage()); |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |