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
324
325
326
327
//! Paint Shop Pro (PSP) file reader.
//!
//! Parses PSP/PSPIMAGE files.
//! Mirrors ExifTool's PSP.pm.
use crate::error::{Error, Result};
use crate::tag::{Tag, TagGroup, TagId};
use crate::value::Value;
pub fn read_psp(data: &[u8]) -> Result<Vec<Tag>> {
// Magic: "Paint Shop Pro Image File\x0a\x1a\0\0\0\0\0" (32 bytes)
if data.len() < 32 || !data.starts_with(b"Paint Shop Pro Image File\x0a\x1a\0\0\0\0\0") {
return Err(Error::InvalidData("not a PSP file".into()));
}
let mut tags = Vec::new();
// File version at offset 32 (4 bytes: major.minor as int16u[2])
if data.len() < 36 {
return Ok(tags);
}
let major = u16::from_le_bytes([data[32], data[33]]);
let minor = u16::from_le_bytes([data[34], data[35]]);
tags.push(mk(
"FileVersion",
"File Version",
Value::String(format!("{}.{}", major, minor)),
));
// Block header length depends on version:
// version > 3: 10 bytes; version <= 3: 14 bytes
let hlen: usize = if major > 3 { 10 } else { 14 };
// Parse blocks
let mut pos = 36;
while pos + hlen <= data.len() {
// Block marker: "~BK\0"
if &data[pos..pos + 4] != b"~BK\0" {
break;
}
let block_type = u16::from_le_bytes([data[pos + 4], data[pos + 5]]);
let block_len = u32::from_le_bytes([
data[pos + hlen - 4],
data[pos + hlen - 3],
data[pos + hlen - 2],
data[pos + hlen - 1],
]) as usize;
pos += hlen;
if pos + block_len > data.len() {
break;
}
let block_data = &data[pos..pos + block_len];
pos += block_len;
match block_type {
0 => {
// Image info block
let start = if major > 3 { 4usize } else { 0usize };
parse_image_block(&block_data[start.min(block_data.len())..], &mut tags);
}
1 => {
// Creator info block
parse_creator_block(block_data, &mut tags);
}
10 => {
// Extended data block
parse_ext_block(block_data, &mut tags);
}
_ => {}
}
}
Ok(tags)
}
fn parse_image_block(data: &[u8], tags: &mut Vec<Tag>) {
if data.len() < 4 {
return;
}
// ImageWidth (int32u) at 0
let width = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
tags.push(mk("ImageWidth", "Image Width", Value::U32(width)));
if data.len() < 8 {
return;
}
// ImageHeight (int32u) at 4
let height = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
tags.push(mk("ImageHeight", "Image Height", Value::U32(height)));
if data.len() < 16 {
return;
}
// ImageResolution (double) at 8
let res_bytes: [u8; 8] = data[8..16].try_into().unwrap_or([0; 8]);
let resolution = f64::from_le_bytes(res_bytes);
if resolution > 0.0 {
tags.push(mk(
"ImageResolution",
"Image Resolution",
Value::String(format!("{}", resolution)),
));
}
if data.len() < 17 {
return;
}
// ResolutionUnit (int8u) at 16
let res_unit = data[16];
let unit_str = match res_unit {
0 => "None",
1 => "inches",
2 => "cm",
_ => "Unknown",
};
tags.push(mk(
"ResolutionUnit",
"Resolution Unit",
Value::String(unit_str.into()),
));
if data.len() < 19 {
return;
}
// Compression (int16u) at 17
let compression = u16::from_le_bytes([data[17], data[18]]);
let comp_str = match compression {
0 => "None",
1 => "RLE",
2 => "LZ77",
3 => "JPEG",
_ => "Unknown",
};
tags.push(mk(
"Compression",
"Compression",
Value::String(comp_str.into()),
));
if data.len() < 21 {
return;
}
// BitsPerSample (int16u) at 19
let bps = u16::from_le_bytes([data[19], data[20]]);
tags.push(mk("BitsPerSample", "Bits Per Sample", Value::U16(bps)));
if data.len() < 23 {
return;
}
// Planes (int16u) at 21
let planes = u16::from_le_bytes([data[21], data[22]]);
tags.push(mk("Planes", "Planes", Value::U16(planes)));
if data.len() < 27 {
return;
}
// NumColors (int32u) at 23
let num_colors = u32::from_le_bytes([data[23], data[24], data[25], data[26]]);
tags.push(mk("NumColors", "Number of Colors", Value::U32(num_colors)));
// No XResolution/YResolution here: PSP.pm:79-83 gives the image-attributes
// block one resolution field, `8 => ImageResolution` (double), plus
// ResolutionUnit. The X/YResolution ExifTool reports for a PSP file come
// from the embedded EXIF IFD0, read by `parse_ext_block`.
}
fn parse_creator_block(data: &[u8], tags: &mut Vec<Tag>) {
// Sub-blocks: "~FL\0" + tag(uint16) + len(uint32)
let mut pos = 0;
while pos + 10 <= data.len() {
if &data[pos..pos + 4] != b"~FL\0" {
break;
}
let tag = u16::from_le_bytes([data[pos + 4], data[pos + 5]]);
let len = u32::from_le_bytes([data[pos + 6], data[pos + 7], data[pos + 8], data[pos + 9]])
as usize;
pos += 10;
if pos + len > data.len() {
break;
}
let val_data = &data[pos..pos + len];
pos += len;
match tag {
0 => {
// Title
let s = read_null_terminated_or_all(val_data);
if !s.is_empty() {
tags.push(mk("Title", "Title", Value::String(s)));
}
}
1 => {
// CreateDate (int32u unix timestamp)
if val_data.len() >= 4 {
let ts =
u32::from_le_bytes([val_data[0], val_data[1], val_data[2], val_data[3]])
as i64;
let dt = crate::formats::gzip::gzip_unix_to_datetime(ts);
tags.push(mk("CreateDate", "Create Date", Value::String(dt)));
}
}
2 => {
// ModifyDate
if val_data.len() >= 4 {
let ts =
u32::from_le_bytes([val_data[0], val_data[1], val_data[2], val_data[3]])
as i64;
let dt = crate::formats::gzip::gzip_unix_to_datetime(ts);
tags.push(mk("ModifyDate", "Modify Date", Value::String(dt)));
}
}
3 => {
let s = read_null_terminated_or_all(val_data);
if !s.is_empty() {
tags.push(mk("Artist", "Artist", Value::String(s)));
}
}
4 => {
let s = read_null_terminated_or_all(val_data);
if !s.is_empty() {
// Both this creator-block Copyright and the embedded-EXIF
// IFD0 Copyright exist; ExifTool keeps the IFD0 one as the
// default winner, so give this a lower priority. Duplicates
// mode (`-a`) still surfaces both.
let mut t = mk("Copyright", "Copyright", Value::String(s));
t.priority = -1;
tags.push(t);
}
}
5 => {
let s = read_null_terminated_or_all(val_data);
if !s.is_empty() {
tags.push(mk("Description", "Description", Value::String(s)));
}
}
6 => {
// CreatorAppID
if val_data.len() >= 4 {
let id =
u32::from_le_bytes([val_data[0], val_data[1], val_data[2], val_data[3]]);
let name = match id {
0 => "Unknown".to_string(),
1 => "Paint Shop Pro".to_string(),
n => format!("{}", n),
};
tags.push(mk("CreatorAppID", "Creator App ID", Value::String(name)));
}
}
7
// CreatorAppVersion (4 bytes little-endian, reversed)
if val_data.len() >= 4 => {
let v = format!(
"{}.{}.{}.{}",
val_data[3], val_data[2], val_data[1], val_data[0]
);
tags.push(mk(
"CreatorAppVersion",
"Creator App Version",
Value::String(v),
));
}
_ => {}
}
}
}
fn parse_ext_block(data: &[u8], tags: &mut Vec<Tag>) {
// Same structure as creator block, but tag 3 contains EXIF data
let mut pos = 0;
while pos + 10 <= data.len() {
if &data[pos..pos + 4] != b"~FL\0" {
break;
}
let tag = u16::from_le_bytes([data[pos + 4], data[pos + 5]]);
let len = u32::from_le_bytes([data[pos + 6], data[pos + 7], data[pos + 8], data[pos + 9]])
as usize;
pos += 10;
if pos + len > data.len() {
break;
}
let val_data = &data[pos..pos + len];
pos += len;
if tag == 3 && val_data.len() > 14 && &val_data[..6] == b"Exif\0\0" {
// EXIF block: "Exif\0\0", then an 8-byte TIFF header, then the IFD.
// PSP.pm:199-213 spells out the quirk: "They use a standard TIFF
// offset to point to the first IFD, but after that the offsets are
// relative to the start of the IFD instead of the TIFF base".
// ExifTool answers it by processing the directory at Exif+14 with
// `Base => $start`, i.e. the IFD is its own offset origin — reading
// it as a plain TIFF puts every value 8 bytes off.
let little_endian = val_data[6] == b'I';
let ifd = &val_data[14..];
let exif_tags =
crate::metadata::exif::ExifReader::read_ifd_at(ifd, little_endian, 0, "IFD0");
// PSP doesn't expose ExifByteOrder
tags.extend(exif_tags.into_iter().filter(|t| t.name != "ExifByteOrder"));
}
}
}
fn read_null_terminated_or_all(data: &[u8]) -> String {
let end = data.iter().position(|&b| b == 0).unwrap_or(data.len());
// PSP creator strings are stored in the file's charset: UTF-8 in modern
// files, Latin-1 in older ones (e.g. Copyright `©` as a single 0xA9 byte).
// Decode UTF-8 when valid, else Latin-1, so accented text stays readable —
// the same characters ExifTool surfaces (it passes the raw bytes through).
crate::encoding::decode_utf8_or_latin1(&data[..end])
}
fn mk(name: &str, description: &str, value: Value) -> Tag {
let pv = value.to_display_string();
Tag {
id: TagId::Text(name.to_string()),
name: name.to_string(),
description: description.to_string(),
group: TagGroup {
family0: "PSP".into(),
family1: "PSP".into(),
family2: "Image".into(),
family3: "Main".into(),
},
raw_value: value,
print_value: pv,
priority: 0,
}
}