-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpsd.h
354 lines (296 loc) · 8.31 KB
/
psd.h
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#pragma once
#include <cstdint>
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cassert>
namespace psd
{
template <typename T> void BEtoLE(T& x);
template <> inline void BEtoLE<uint8_t>(uint8_t& x)
{
// do nothing;
}
template <> inline void BEtoLE<uint32_t>(uint32_t& x)
{
uint8_t t;
uint8_t* p = reinterpret_cast<uint8_t*>(&x);
t = p[0]; p[0] = p[3]; p[3] = t;
t = p[1]; p[1] = p[2]; p[2] = t;
}
template <> inline void BEtoLE<uint16_t>(uint16_t& x)
{
uint8_t t;
uint8_t* p = reinterpret_cast<uint8_t*>(&x);
t = p[0]; p[0] = p[1]; p[1] = t;
}
template <> inline void BEtoLE<int32_t>(int32_t& x)
{
BEtoLE(*(uint32_t*)&x);
}
template <> inline void BEtoLE<int16_t>(int16_t& x)
{
BEtoLE(*(uint16_t*)&x);
}
template <typename T>
struct be
{
be()
: x(0)
{
}
be(T x)
: x(x)
{
BEtoLE(this->x);
}
be(const be& y)
: x(y.x)
{
}
operator T() const
{
T y = x;
BEtoLE(y);
return y;
}
be& operator = (be y)
{
x = y.x;
return *this;
}
be& operator = (T y)
{
x = y;
BEtoLE(x);
return *this;
}
T x;
T operator += (T y)
{
BEtoLE(x);
x += y;
T xx = x;
BEtoLE(x);
return xx;
}
T operator -= (T y)
{
BEtoLE(x);
x -= y;
T xx = x;
BEtoLE(x);
return xx;
}
};
struct Signature
{
Signature()
: sig(0)
{
}
Signature(uint32_t sig)
: sig(sig)
{
}
Signature(const std::string& str)
{
assert(str.size() == 4);
sig = *(uint32_t*)str.data();
}
uint32_t sig;
operator std::string()
{
return std::string((char*)&sig, (char*)&sig+4);
}
};
inline bool operator == (const Signature& sig, const std::string& str)
{
return sig.sig == Signature(str).sig;
}
inline bool operator == (const std::string& str, const Signature& sig)
{
return sig.sig == Signature(str).sig;
}
inline bool operator != (const Signature& sig, const std::string& str)
{
return sig.sig != Signature(str).sig;
}
inline bool operator != (const std::string& str, const Signature& sig)
{
return sig.sig != Signature(str).sig;
}
enum class ColorMode : uint16_t
{
Bitmap = 0,
Grayscale = 1,
Indexed = 2,
RGB = 3,
CMYK = 4,
Multichannel = 7,
Duotone = 8,
Lab = 9,
};
#pragma pack(push, 1)
struct Header
{
Header()
: dummy1(0), dummy2(0)
{}
Signature signature;
be<uint16_t> version;
uint16_t dummy1;
uint32_t dummy2;
be<uint16_t> num_channels;
be<uint32_t> height;
be<uint32_t> width;
be<uint16_t> bit_depth;
be<uint16_t> color_mode;
};
struct ImageResourceBlock
{
Signature signature;
be<uint16_t> image_resource_id;
std::string name; // encoded as pascal string; 1 byte length header
std::vector<char> buffer;
uint32_t size() const;
bool read(std::istream& stream);
bool write(std::ostream& stream);
};
struct ExtraData
{
Signature signature;
Signature key;
be<uint32_t> length;
std::vector<char> data;
uint32_t size() const { return 12+data.size() + (data.size()%2); }
bool read(std::istream& stream);
bool write(std::ostream& stream);
void luni_read_name(std::wstring& wname, std::string& utf8name);
};
struct ImageData
{
uint32_t w;
uint32_t h;
be<uint16_t> compression_method;
std::vector<std::vector<char>> data;
bool read(std::istream& f, uint32_t w, uint32_t h);
bool write(std::ostream& f);
bool read_with_method(std::istream& f, uint32_t w, uint32_t h, uint16_t compression_method);
};
struct MultipleImageData
{
uint32_t w;
uint32_t h;
uint32_t count;
be<uint16_t> compression_method;
std::vector<std::vector<std::vector<char>>> datas;
bool read(std::istream& f, uint32_t w, uint32_t h, uint32_t count, uint16_t bit_depth);
bool write(std::ostream& f);
};
struct Layer
{
Layer() : has_text(false) {}
be<uint32_t> top, left, bottom, right;
be<uint16_t> num_channels;
std::vector<std::pair<be<int16_t>, be<uint32_t>>> channel_infos; // ID, length
std::vector<ImageData> channel_info_data;
ImageData* get_channel_info_by_id(int16_t id)
{
for(uint16_t i = 0; i < channel_infos.size(); i ++)
if (channel_infos[i].first == id)
return &channel_info_data[i];
return nullptr;
}
Signature blend_signature;
be<uint32_t> blend_key;
uint8_t opacity; // 0 for transparent
uint8_t clipping; // 0 base, 1 non-base
uint8_t bit_flags;
uint8_t dummy1;
be<uint32_t> extra_data_length;
std::vector<ExtraData> additional_extra_data;
uint16_t name_size();
struct LayerMask
{
uint32_t size() const { return 4 + (uint32_t)length; }
be<uint32_t> length;
be<uint32_t> top, left, bottom, right;
uint8_t default_color;
uint8_t flags;
std::vector<char> additional_data;
bool read(std::istream& f);
bool write(std::ostream& f);
} mask;
struct LayerBlendingRanges
{
uint32_t size() const { return data.size() + 4; }
std::vector<char> data;
bool read(std::istream& f);
bool write(std::ostream& f);
} blending_ranges;
std::string name;
std::wstring wname;
std::string utf8name;
bool has_text;
bool read(std::istream& f);
bool write(std::ostream& f);
bool read_images(std::istream& f);
bool write_images(std::ostream& f);
};
struct LayerInfo
{
LayerInfo()
: num_layers(0), has_merged_alpha_channel(false)
{}
be<int16_t> num_layers;
bool has_merged_alpha_channel;
std::vector<Layer> layers;
bool read(std::istream& stream);
bool write(std::ostream& stream);
};
struct GlobalLayerMaskInfo
{
be<uint32_t> length;
be<uint16_t> overlay_colorspace;
be<uint16_t> color_component[4];
be<uint16_t> opacity; // 0 = transparent 100 = opaque
uint8_t kind;
std::vector<char> data;
bool read(std::istream& stream);
bool write(std::ostream& stream);
};
#pragma pack(pop)
class psd
{
public:
psd();
template <typename Stream>
psd(Stream&& stream)
{
load(stream);
}
bool load(std::istream& stream);
bool save(std::ostream& f);
Header header;
std::vector<ImageResourceBlock> image_resources;
LayerInfo layer_info;
GlobalLayerMaskInfo global_layer_mask_info;
std::vector<char> additional_layer_data;
std::vector<Layer>& layers() { return layer_info.layers; }
MultipleImageData merged_image;
operator bool();
private:
bool read_header(std::istream& f);
bool read_color_mode(std::istream& f);
bool read_image_resources(std::istream& f);
bool read_layers_and_masks(std::istream& f);
bool read_layer_info(std::istream& f);
bool write_header(std::ostream& f);
bool write_color_mode(std::ostream& f);
bool write_image_resources(std::ostream& f);
bool write_layers_and_masks(std::ostream& f);
bool valid_;
};
}