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.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 | * Reads metadata stored within PSD file format data.
|
---|
32 | *
|
---|
33 | * @author Drew Noakes https://drewnoakes.com
|
---|
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 | // FILE HEADER SECTION
|
---|
43 |
|
---|
44 | try {
|
---|
45 | final int signature = reader.getInt32();
|
---|
46 | if (signature != 0x38425053) // "8BPS"
|
---|
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 | // 6 reserved bytes are skipped here. They should be zero.
|
---|
60 | reader.skip(6);
|
---|
61 |
|
---|
62 | final int channelCount = reader.getUInt16();
|
---|
63 | directory.setInt(PsdHeaderDirectory.TAG_CHANNEL_COUNT, channelCount);
|
---|
64 |
|
---|
65 | // even though this is probably an unsigned int, the max height in practice is 300,000
|
---|
66 | final int imageHeight = reader.getInt32();
|
---|
67 | directory.setInt(PsdHeaderDirectory.TAG_IMAGE_HEIGHT, imageHeight);
|
---|
68 |
|
---|
69 | // even though this is probably an unsigned int, the max width in practice is 300,000
|
---|
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 | // COLOR MODE DATA SECTION
|
---|
84 |
|
---|
85 | try {
|
---|
86 | long sectionLength = reader.getUInt32();
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Only indexed color and duotone (see the mode field in the File header section) have color mode data.
|
---|
90 | * For all other modes, this section is just the 4-byte length field, which is set to zero.
|
---|
91 | *
|
---|
92 | * Indexed color images: length is 768; color data contains the color table for the image,
|
---|
93 | * in non-interleaved order.
|
---|
94 | * Duotone images: color data contains the duotone specification (the format of which is not documented).
|
---|
95 | * Other applications that read Photoshop files can treat a duotone image as a gray image,
|
---|
96 | * and just preserve the contents of the duotone information when reading and writing the
|
---|
97 | * file.
|
---|
98 | */
|
---|
99 |
|
---|
100 | reader.skip(sectionLength);
|
---|
101 | } catch (IOException e) {
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // IMAGE RESOURCES SECTION
|
---|
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 | // ignore
|
---|
115 | }
|
---|
116 |
|
---|
117 | // LAYER AND MASK INFORMATION SECTION (skipped)
|
---|
118 |
|
---|
119 | // IMAGE DATA SECTION (skipped)
|
---|
120 | }
|
---|
121 | }
|
---|