| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.drew.metadata.photoshop; |
| 23 |
|
| 24 |
import com.drew.lang.SequentialReader; |
| 25 |
import com.drew.lang.annotations.NotNull; |
| 26 |
import com.drew.metadata.Metadata; |
| 27 |
|
| 28 |
import java.io.IOException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class PsdReader |
| 36 |
{ |
| 37 |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) |
| 38 |
{ |
| 39 |
PsdHeaderDirectory directory = new PsdHeaderDirectory(); |
| 40 |
metadata.addDirectory(directory); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
try { |
| 45 |
final int signature = reader.getInt32(); |
| 46 |
if (signature != 0x38425053) |
| 47 |
{ |
| 48 |
directory.addError("Invalid PSD file signature"); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
final int version = reader.getUInt16(); |
| 53 |
if (version != 1 && version != 2) |
| 54 |
{ |
| 55 |
directory.addError("Invalid PSD file version (must be 1 or 2)"); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
reader.skip(6); |
| 61 |
|
| 62 |
final int channelCount = reader.getUInt16(); |
| 63 |
directory.setInt(PsdHeaderDirectory.TAG_CHANNEL_COUNT, channelCount); |
| 64 |
|
| 65 |
|
| 66 |
final int imageHeight = reader.getInt32(); |
| 67 |
directory.setInt(PsdHeaderDirectory.TAG_IMAGE_HEIGHT, imageHeight); |
| 68 |
|
| 69 |
|
| 70 |
final int imageWidth = reader.getInt32(); |
| 71 |
directory.setInt(PsdHeaderDirectory.TAG_IMAGE_WIDTH, imageWidth); |
| 72 |
|
| 73 |
final int bitsPerChannel = reader.getUInt16(); |
| 74 |
directory.setInt(PsdHeaderDirectory.TAG_BITS_PER_CHANNEL, bitsPerChannel); |
| 75 |
|
| 76 |
final int colorMode = reader.getUInt16(); |
| 77 |
directory.setInt(PsdHeaderDirectory.TAG_COLOR_MODE, colorMode); |
| 78 |
} catch (IOException e) { |
| 79 |
directory.addError("Unable to read PSD header"); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
try { |
| 86 |
long sectionLength = reader.getUInt32(); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
reader.skip(sectionLength); |
| 101 |
} catch (IOException e) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
try { |
| 108 |
long sectionLength = reader.getUInt32(); |
| 109 |
|
| 110 |
assert(sectionLength <= Integer.MAX_VALUE); |
| 111 |
|
| 112 |
new PhotoshopReader().extract(reader, (int)sectionLength, metadata); |
| 113 |
} catch (IOException e) { |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
} |