package com.drew.metadata.eps; import com.drew.imaging.tiff.TiffProcessingException; import com.drew.imaging.tiff.TiffReader; import com.drew.lang.*; import com.drew.lang.annotations.NotNull; import com.drew.lang.annotations.Nullable; import com.drew.metadata.Metadata; import com.drew.metadata.icc.IccReader; import com.drew.metadata.photoshop.PhotoshopReader; import com.drew.metadata.photoshop.PhotoshopTiffHandler; import com.drew.metadata.xmp.XmpReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * Reads file passed in through SequentialReader and parses encountered data: *
%ImageData comment which holds several values including width in px,
* height in px and color type.
*/
private static void extractImageData(@NotNull final EpsDirectory directory, String imageData) throws IOException
{
// %ImageData: 1000 1000 8 3 1 1000 7 "beginimage"
directory.setString(EpsDirectory.TAG_IMAGE_DATA, imageData.trim());
String[] imageDataParts = imageData.split(" ");
int width = Integer.parseInt(imageDataParts[0]);
int height = Integer.parseInt(imageDataParts[1]);
int colorType = Integer.parseInt(imageDataParts[3]);
// Only add values that are not already present
if (!directory.containsTag(EpsDirectory.TAG_IMAGE_WIDTH))
directory.setInt(EpsDirectory.TAG_IMAGE_WIDTH, width);
if (!directory.containsTag(EpsDirectory.TAG_IMAGE_HEIGHT))
directory.setInt(EpsDirectory.TAG_IMAGE_HEIGHT, height);
if (!directory.containsTag(EpsDirectory.TAG_COLOR_TYPE))
directory.setInt(EpsDirectory.TAG_COLOR_TYPE, colorType);
if (!directory.containsTag(EpsDirectory.TAG_RAM_SIZE)) {
int bytesPerPixel = 0;
if (colorType == 1)
bytesPerPixel = 1; // grayscale
else if (colorType == 2 || colorType == 3)
bytesPerPixel = 3; // Lab or RGB
else if (colorType == 4)
bytesPerPixel = 3; // CMYK
if (bytesPerPixel != 0)
directory.setInt(EpsDirectory.TAG_RAM_SIZE, bytesPerPixel * width * height);
}
}
/**
* Decodes a commented hex section, and uses {@link PhotoshopReader} to decode the resulting data.
*/
private static void extractPhotoshopData(@NotNull final Metadata metadata, @NotNull SequentialReader reader) throws IOException
{
byte[] buffer = decodeHexCommentBlock(reader);
if (buffer != null)
new PhotoshopReader().extract(new SequentialByteArrayReader(buffer), buffer.length, metadata);
}
/**
* Decodes a commented hex section, and uses {@link IccReader} to decode the resulting data.
*/
private static void extractIccData(@NotNull final Metadata metadata, @NotNull SequentialReader reader) throws IOException
{
byte[] buffer = decodeHexCommentBlock(reader);
if (buffer != null)
new IccReader().extract(new ByteArrayReader(buffer), metadata);
}
/**
* Extracts an XMP xpacket, and uses {@link XmpReader} to decode the resulting data.
*/
private static void extractXmpData(@NotNull final Metadata metadata, @NotNull SequentialReader reader) throws IOException
{
byte[] bytes = readUntil(reader, "".getBytes());
String xmp = new String(bytes, Charsets.UTF_8);
new XmpReader().extract(xmp, metadata);
}
/**
* Reads all bytes until the given sentinel is observed.
* The sentinel will be included in the returned bytes.
*/
private static byte[] readUntil(@NotNull SequentialReader reader, @NotNull byte[] sentinel) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final int length = sentinel.length;
int depth = 0;
while (depth != length) {
byte b = reader.getByte();
if (b == sentinel[depth])
depth++;
else
depth = 0;
bytes.write(b);
}
return bytes.toByteArray();
}
/**
* EPS files can contain hexadecimal-encoded ASCII blocks, each prefixed with
* %BeginPhotoshop: 9564
* % 3842494D040400000000005D1C015A00031B25471C0200000200041C02780004
* % 6E756C6C1C027A00046E756C6C1C025000046E756C6C1C023700083230313630
* % 3331311C023C000B3131343335362B303030301C023E00083230313630333131
* % 48000000010000003842494D03FD0000000000080101000000000000
* %EndPhotoshop
*
* When calling this method, the reader must be positioned at the start of the first line containing
* hex data, not at the introductory line.
*
* @return The decoded bytes, or null if decoding failed.
*/
@Nullable
private static byte[] decodeHexCommentBlock(@NotNull SequentialReader reader) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Use a state machine to efficiently parse data in a single traversal
final int AwaitingPercent = 0;
final int AwaitingSpace = 1;
final int AwaitingHex1 = 2;
final int AwaitingHex2 = 3;
int state = AwaitingPercent;
int carry = 0;
boolean done = false;
byte b = 0;
while (!done) {
b = reader.getByte();
switch (state) {
case AwaitingPercent: {
switch (b) {
case '\r':
case '\n':
case ' ':
// skip newline chars and spaces
break;
case '%':
state = AwaitingSpace;
break;
default:
return null;
}
break;
}
case AwaitingSpace: {
switch (b) {
case ' ':
state = AwaitingHex1;
break;
default:
done = true;
break;
}
break;
}
case AwaitingHex1: {
int i = tryHexToInt(b);
if (i != -1) {
carry = i * 16;
state = AwaitingHex2;
} else if (b == '\r' || b == '\n') {
state = AwaitingPercent;
} else {
return null;
}
break;
}
case AwaitingHex2: {
int i = tryHexToInt(b);
if (i == -1)
return null;
bytes.write(carry + i);
state = AwaitingHex1;
break;
}
}
}
// skip through the remainder of the last line
while (b != '\n')
b = reader.getByte();
return bytes.toByteArray();
}
/**
* Treats a byte as an ASCII character, and returns it's numerical value in hexadecimal.
* If conversion is not possible, returns -1.
*/
private static int tryHexToInt(byte b)
{
if (b >= '0' && b <= '9')
return b - '0';
if (b >= 'A' && b <= 'F')
return b - 'A' + 10;
if (b >= 'a' && b <= 'f')
return b - 'a' + 10;
return -1;
}
}