File: PhotoshopDescriptor.java

package info (click to toggle)
libmetadata-extractor-java 2.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,124 kB
  • ctags: 2,408
  • sloc: java: 15,158; xml: 110; sh: 93; makefile: 18
file content (323 lines) | stat: -rw-r--r-- 11,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright 2002-2012 Drew Noakes
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * More information about this project is available at:
 *
 *    http://drewnoakes.com/code/exif/
 *    http://code.google.com/p/metadata-extractor/
 */
package com.drew.metadata.photoshop;

import com.drew.lang.BufferBoundsException;
import com.drew.lang.BufferReader;
import com.drew.lang.ByteArrayReader;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.TagDescriptor;

/** @author Yuri Binev, Drew Noakes http://drewnoakes.com */
public class PhotoshopDescriptor extends TagDescriptor<PhotoshopDirectory>
{
    public PhotoshopDescriptor(@NotNull PhotoshopDirectory directory)
    {
        super(directory);
    }

    public String getDescription(int tagType)
    {
        switch (tagType) {
            case PhotoshopDirectory.TAG_PHOTOSHOP_THUMBNAIL:
            case PhotoshopDirectory.TAG_PHOTOSHOP_THUMBNAIL_OLD:
                return getThumbnailDescription(tagType);
            case PhotoshopDirectory.TAG_PHOTOSHOP_URL:
            case PhotoshopDirectory.TAG_PHOTOSHOP_XML:
                return getSimpleString(tagType);
            case PhotoshopDirectory.TAG_PHOTOSHOP_IPTC:
                return getBinaryDataString(tagType);
            case PhotoshopDirectory.TAG_PHOTOSHOP_SLICES:
                return getSlicesDescription();
            case PhotoshopDirectory.TAG_PHOTOSHOP_VERSION:
                return getVersionDescription();
            case PhotoshopDirectory.TAG_PHOTOSHOP_COPYRIGHT:
                return getBooleanString(tagType);
            case PhotoshopDirectory.TAG_PHOTOSHOP_RESOLUTION_INFO:
                return getResolutionInfoDescription();
            case PhotoshopDirectory.TAG_PHOTOSHOP_GLOBAL_ANGLE:
            case PhotoshopDirectory.TAG_PHOTOSHOP_GLOBAL_ALTITUDE:
            case PhotoshopDirectory.TAG_PHOTOSHOP_URL_LIST:
            case PhotoshopDirectory.TAG_PHOTOSHOP_SEED_NUMBER:
                return get32BitNumberString(tagType);
            case PhotoshopDirectory.TAG_PHOTOSHOP_JPEG_QUALITY:
                return getJpegQualityString();
            case PhotoshopDirectory.TAG_PHOTOSHOP_PRINT_SCALE:
                return getPrintScaleDescription();
            case PhotoshopDirectory.TAG_PHOTOSHOP_PIXEL_ASPECT_RATIO:
                return getPixelAspectRatioString();
            default:
                return super.getDescription(tagType);
        }
    }

    @Nullable
    public String getJpegQualityString()
    {
        try {
            byte[] b = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_JPEG_QUALITY);
            BufferReader reader = new ByteArrayReader(b);
            int q = reader.getUInt16(0); // & 0xFFFF;
            int f = reader.getUInt16(2); // & 0xFFFF;
            int s = reader.getUInt16(4);

            int q1;
            if (q <= 0xFFFF && q >= 0xFFFD)
                q1 = q - 0xFFFC;
            else if (q <= 8)
                q1 = q + 4;
            else
                q1 = q;

            String quality;
            switch (q) {
                case 0xFFFD:
                case 0xFFFE:
                case 0xFFFF:
                case 0:
                    quality = "Low";
                    break;
                case 1:
                case 2:
                case 3:
                    quality = "Medium";
                    break;
                case 4:
                case 5:
                    quality = "High";
                    break;
                case 6:
                case 7:
                case 8:
                    quality = "Maximum";
                    break;
                default:
                    quality = "Unknown";
            }
            String format;
            switch (f) {
                case 0x0000:
                    format = "Standard";
                    break;
                case 0x0001:
                    format = "Optimised";
                    break;
                case 0x0101:
                    format = "Progressive ";
                    break;
                default:
                    format = String.format("Unknown 0x%04X", f);
            }
            String scans = s >= 1 && s <= 3
                    ? String.format("%d", s + 2)
                    : String.format("Unknown 0x%04X", s);
            return String.format("%d (%s), %s format, %s scans", q1, quality, format, scans);
        } catch (BufferBoundsException e) {
            return null;
        }
    }

    @Nullable
    public String getPixelAspectRatioString()
    {
        try {
            byte[] bytes = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_PIXEL_ASPECT_RATIO);
            if (bytes == null)
                return null;
            BufferReader reader = new ByteArrayReader(bytes);
            double d = reader.getDouble64(4);
            return Double.toString(d);
        } catch (Exception e) {
            return null;
        }
    }

    @Nullable
    public String getPrintScaleDescription()
    {
        try {
            byte bytes[] = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_PRINT_SCALE);
            if (bytes == null)
                return null;
            BufferReader reader = new ByteArrayReader(bytes);
            int style = reader.getInt32(0);
            float locX = reader.getFloat32(2);
            float locY = reader.getFloat32(6);
            float scale = reader.getFloat32(10);
            switch (style) {
                case 0:
                    return "Centered, Scale " + scale;
                case 1:
                    return "Size to fit";
                case 2:
                    return String.format("User defined, X:%s Y:%s, Scale:%s", locX, locY, scale);
                default:
                    return String.format("Unknown %04X, X:%s Y:%s, Scale:%s", style, locX, locY, scale);
            }
        } catch (Exception e) {
            return null;
        }
    }

    @Nullable
    public String getResolutionInfoDescription()
    {
        try {
            byte[] bytes = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_RESOLUTION_INFO);
            if (bytes == null)
                return null;
            BufferReader reader = new ByteArrayReader(bytes);
            float resX = reader.getS15Fixed16(0);
            float resY = reader.getS15Fixed16(8); // is this the correct offset? it's only reading 4 bytes each time
            return resX + "x" + resY + " DPI";
        } catch (Exception e) {
            return null;
        }
    }

    @Nullable
    public String getVersionDescription()
    {
        try {
            final byte[] bytes = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_VERSION);
            if (bytes == null)
                return null;
            BufferReader reader = new ByteArrayReader(bytes);
            int pos = 0;
            int ver = reader.getInt32(0);
            pos += 4;
            pos++;
            int readerLength = reader.getInt32(5);
            pos += 4;
            String readerStr = reader.getString(9, readerLength * 2, "UTF-16");
            pos += readerLength * 2;
            int writerLength = reader.getInt32(pos);
            pos += 4;
            String writerStr = reader.getString(pos, writerLength * 2, "UTF-16");
            pos += writerLength * 2;
            int fileVersion = reader.getInt32(pos);
            return String.format("%d (%s, %s) %d", ver, readerStr, writerStr, fileVersion);
        } catch (BufferBoundsException e) {
            return null;
        }
    }

    @Nullable
    public String getSlicesDescription()
    {
        try {
            final byte bytes[] = _directory.getByteArray(PhotoshopDirectory.TAG_PHOTOSHOP_SLICES);
            if (bytes == null)
                return null;
            BufferReader reader = new ByteArrayReader(bytes);
            int nameLength = reader.getInt32(20);
            String name = reader.getString(24, nameLength * 2, "UTF-16");
            int pos = 24 + nameLength * 2;
            int sliceCount = reader.getInt32(pos);
            //pos += 4;
            return String.format("%s (%d,%d,%d,%d) %d Slices",
                    name, reader.getInt32(4), reader.getInt32(8), reader.getInt32(12), reader.getInt32(16), sliceCount);
            /*for (int i=0;i<sliceCount;i++){
                pos+=16;
                int slNameLen=getInt32(b,pos);
                pos+=4;
                String slName=new String(b, pos, slNameLen*2,"UTF-16");
                res+=slName;
            }*/
        } catch (BufferBoundsException e) {
            return null;
        }
    }

    @Nullable
    public String getThumbnailDescription(int tagType)
    {
        try {
            byte[] v = _directory.getByteArray(tagType);
            if (v == null)
                return null;
            BufferReader reader = new ByteArrayReader(v);
            //int pos = 0;
            int format = reader.getInt32(0);
            //pos += 4;
            int width = reader.getInt32(4);
            //pos += 4;
            int height = reader.getInt32(8);
            //pos += 4;
            //pos += 4; //skip WidthBytes
            int totalSize = reader.getInt32(16);
            //pos += 4;
            int compSize = reader.getInt32(20);
            //pos += 4;
            int bpp = reader.getInt32(24);
            //pos+=2;
            //pos+=2; //skip Number of planes
            //int thumbSize=v.length-pos;
            return String.format("%s, %dx%d, Decomp %d bytes, %d bpp, %d bytes",
                    format == 1 ? "JpegRGB" : "RawRGB",
                    width, height, totalSize, bpp, compSize);
        } catch (BufferBoundsException e) {
            return null;
        }
    }

    @Nullable
    private String getBooleanString(int tag)
    {
        final byte[] bytes = _directory.getByteArray(tag);
        if (bytes == null)
            return null;
        return bytes[0] == 0 ? "No" : "Yes";
    }

    @Nullable
    private String get32BitNumberString(int tag)
    {
        byte[] bytes = _directory.getByteArray(tag);
        if (bytes == null)
            return null;
        BufferReader reader = new ByteArrayReader(bytes);
        try {
            return String.format("%d", reader.getInt32(0));
        } catch (BufferBoundsException e) {
            return null;
        }
    }

    @Nullable
    private String getSimpleString(int tagType)
    {
        final byte[] bytes = _directory.getByteArray(tagType);
        if (bytes == null)
            return null;
        return new String(bytes);
    }

    @Nullable
    private String getBinaryDataString(int tagType)
    {
        final byte[] bytes = _directory.getByteArray(tagType);
        if (bytes == null)
            return null;
        return String.format("%d bytes binary data", bytes.length);
    }
}