forked from 0xC0000054/pdn-photoshop-brush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrushSectionParser.cs
473 lines (401 loc) · 14.9 KB
/
BrushSectionParser.cs
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/////////////////////////////////////////////////////////////////////////////////
//
// ABR FileType Plugin for Paint.NET
//
// This software is provided under the MIT License:
// Copyright (c) 2012-2017 Nicholas Hayes
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Globalization;
using System.Text;
namespace AbrFileTypePlugin
{
/// <summary>
/// Parser for the 8BIM sections used by ABR version 6 and later.
/// </summary>
internal sealed class BrushSectionParser
{
private readonly BrushSectionOffsets sectionOffsets;
private SampledBrushCollection sampledBrushes;
private const uint PhotoshopSignature = 0x3842494D; // 8BIM
private const uint SampleSectionId = 0x73616D70; // samp
private const uint DescriptorSectionId = 0x64657363; // desc
/// <summary>
/// Initializes a new instance of the <see cref="BrushSectionParser"/> class.
/// </summary>
/// <param name="reader">The reader.</param>
public BrushSectionParser(BinaryReverseReader reader)
{
this.sectionOffsets = GetBrushSectionOffsets(reader);
this.sampledBrushes = new SampledBrushCollection();
if (this.sectionOffsets != null && this.sectionOffsets.descriptorSectionOffset >= 0)
{
reader.BaseStream.Position = this.sectionOffsets.descriptorSectionOffset;
ParseBrushDescriptorSection(reader);
}
}
private enum DescriptorTypes : uint
{
Alias = 0x976c6973, // 'alis'
Boolean = 0x626f6f6c, // 'bool'
String = 0x54455854, // 'TEXT'
Class = 0x74797065, // 'type'
Descriptor = 0x4f626a63, // 'Objc'
Enumerated = 0x656e756d, // 'enum'
Float = 0x646f7562, // 'doub'
Integer = 0x6c6f6e67, // 'long'
List = 0x566c4c73, // 'VlLs'
Null = 0x6e756c6c, // 'null'
ObjectRefrence = 0x6f626a20, // 'obj '
Path = 0x50746820, // 'Pat '
UnitFloat = 0x556e7446, // 'UntF'
}
private enum UnitTypes : uint
{
Angle = 0x23416e67, // '#Ang'
Density = 0x2352736c, // '#Rsl'
Distance = 0x23526c74,// '#Rlt'
None = 0x234e6e65, // '#Nne'
Percent = 0x23507263, // '#Prc'
Pixel = 0x2350786c // '#Pxl'
}
/// <summary>
/// Gets a collection containing the sampled brush information.
/// </summary>
/// <value>
/// The sampled brush information.
/// </value>
public SampledBrushCollection SampledBrushes
{
get
{
return this.sampledBrushes;
}
}
/// <summary>
/// Gets the sample section offset.
/// </summary>
/// <value>
/// The sample section offset.
/// </value>
public long SampleSectionOffset
{
get
{
if (this.sectionOffsets != null)
{
return this.sectionOffsets.sampleSectionOffset;
}
return -1;
}
}
private static BrushSectionOffsets GetBrushSectionOffsets(BinaryReverseReader reader)
{
long sampleSectionOffset = -1;
long descriptorSectionOffset = -1;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
uint sig = reader.ReadUInt32();
if (sig != PhotoshopSignature)
{
return null;
}
uint sectionId = reader.ReadUInt32();
switch (sectionId)
{
case SampleSectionId:
sampleSectionOffset = reader.BaseStream.Position;
break;
case DescriptorSectionId:
descriptorSectionOffset = reader.BaseStream.Position;
break;
default:
break;
}
uint size = reader.ReadUInt32();
reader.BaseStream.Position += size;
}
return new BrushSectionOffsets(sampleSectionOffset, descriptorSectionOffset);
}
private void ParseBrushDescriptorSection(BinaryReverseReader reader)
{
uint sectionSize = reader.ReadUInt32();
long sectionEnd = reader.BaseStream.Position + sectionSize;
// Skip the unknown data.
reader.BaseStream.Position += 22L;
if (reader.BaseStream.Position < sectionEnd)
{
string key = ParseKey(reader);
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(
CultureInfo.CurrentCulture,
"Item: {0} ({1}) at {2:X8}",
new object[] { key, type, reader.BaseStream.Position }));
#endif
ParseType(reader, type);
}
}
private static string ParseKey(BinaryReverseReader reader)
{
int length = reader.ReadInt32();
if (length == 0)
{
length = 4;
}
byte[] bytes = reader.ReadBytes(length);
return Encoding.ASCII.GetString(bytes);
}
private static string ParseClassId(BinaryReverseReader reader)
{
int length = reader.ReadInt32();
if (length == 0)
{
length = 4;
}
return Encoding.ASCII.GetString(reader.ReadBytes(length)).TrimEnd('\0');
}
private void ParseList(BinaryReverseReader reader)
{
uint count = reader.ReadUInt32();
for (int i = 0; i < count; i++)
{
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
ParseType(reader, type);
}
}
private void ParseDescriptor(BinaryReverseReader reader)
{
string name = ParseString(reader);
string classId = ParseClassId(reader);
uint count = reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Parsing descriptor '{0}' ({1} items)", classId, count));
#endif
if (classId.Equals("brushPreset", StringComparison.Ordinal))
{
ParseBrushPreset(reader, count);
}
else
{
for (uint i = 0; i < count; i++)
{
string key = ParseKey(reader);
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(
CultureInfo.CurrentCulture,
"Item {0}: {1} ({2}) at 0x{3:X8}",
new object[] { i, key, type, reader.BaseStream.Position }));
#endif
ParseType(reader, type);
}
}
}
private void ParseBrushPreset(BinaryReverseReader reader, uint count)
{
string presetName = null;
BrushData brushData = null;
for (uint i = 0; i < count; i++)
{
string key = ParseKey(reader);
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(
CultureInfo.CurrentCulture,
"brushPreset item {0}: {1} ({2}) at 0x{3:X8}",
new object[] { i, key, type, reader.BaseStream.Position }));
#endif
if (key.Equals("Nm ", StringComparison.Ordinal))
{
presetName = ParseString(reader);
}
else if (key.Equals("Brsh", StringComparison.Ordinal) && type == DescriptorTypes.Descriptor)
{
brushData = ParseBrushDescriptor(reader);
}
else
{
ParseType(reader, type);
}
}
if (brushData != null && !string.IsNullOrEmpty(brushData.sampledDataTag))
{
this.sampledBrushes.Add(new SampledBrush(presetName, brushData.sampledDataTag, brushData.diameter, brushData.spacing));
}
}
private BrushData ParseBrushDescriptor(BinaryReverseReader reader)
{
string name = ParseString(reader);
string classId = ParseClassId(reader);
uint count = reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Parsing {0} ({1} items)", classId, count));
#endif
BrushData data = null;
if (classId.Equals("sampledBrush", StringComparison.Ordinal))
{
data = ParseSampledBrush(reader, count);
}
else
{
for (uint i = 0; i < count; i++)
{
string key = ParseKey(reader);
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(
CultureInfo.CurrentCulture,
"{0} item {1}: {2} ({3}) at 0x{4:X8}",
new object[] { classId, i, key, type, reader.BaseStream.Position }));
#endif
ParseType(reader, type);
}
}
return data;
}
private BrushData ParseSampledBrush(BinaryReverseReader reader, uint count)
{
BrushData data = new BrushData();
for (uint i = 0; i < count; i++)
{
string key = ParseKey(reader);
DescriptorTypes type = (DescriptorTypes)reader.ReadUInt32();
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(
CultureInfo.CurrentCulture,
"sampledBrush item {0}: {1} ({2}) at 0x{3:X8}",
new object[] { i, key, type, reader.BaseStream.Position }));
#endif
UnitFloat unitFloat;
switch (key)
{
case "Dmtr":
unitFloat = ParseUnitFloat(reader);
if (unitFloat.type == UnitTypes.Pixel)
{
data.diameter = (int)unitFloat.value;
}
break;
case "Spcn":
unitFloat = ParseUnitFloat(reader);
if (unitFloat.type == UnitTypes.Percent)
{
data.spacing = (int)unitFloat.value;
}
break;
case "sampledData":
data.sampledDataTag = ParseString(reader);
break;
default:
ParseType(reader, type);
break;
}
}
return data;
}
private static string ParseString(BinaryReverseReader reader)
{
return reader.ReadUnicodeString();
}
private static UnitFloat ParseUnitFloat(BinaryReverseReader reader)
{
UnitFloat value = new UnitFloat()
{
type = (UnitTypes)reader.ReadUInt32(),
value = reader.ReadDouble()
};
return value;
}
private static bool ParseBoolean(BinaryReverseReader reader)
{
bool value = reader.ReadByte() != 0;
return value;
}
private static uint ParseInteger(BinaryReverseReader reader)
{
uint value = reader.ReadUInt32();
return value;
}
private static double ParseFloat(BinaryReverseReader reader)
{
double value = reader.ReadDouble();
return value;
}
private static EnumeratedValue ParseEnumerated(BinaryReverseReader reader)
{
EnumeratedValue value = new EnumeratedValue()
{
type = ParseKey(reader),
value = ParseKey(reader)
};
return value;
}
private void ParseType(BinaryReverseReader reader, DescriptorTypes type)
{
switch (type)
{
case DescriptorTypes.List:
ParseList(reader);
break;
case DescriptorTypes.Descriptor:
ParseDescriptor(reader);
break;
case DescriptorTypes.String:
ParseString(reader);
break;
case DescriptorTypes.UnitFloat:
ParseUnitFloat(reader);
break;
case DescriptorTypes.Boolean:
ParseBoolean(reader);
break;
case DescriptorTypes.Integer:
ParseInteger(reader);
break;
case DescriptorTypes.Float:
ParseFloat(reader);
break;
case DescriptorTypes.Enumerated:
ParseEnumerated(reader);
break;
default:
throw new FormatException(string.Format(CultureInfo.CurrentCulture, "Unsupported brush descriptor type: '{0}'", DescriptorTypeToString(type)));
}
}
private static string DescriptorTypeToString(DescriptorTypes type)
{
byte[] bytes = BitConverter.GetBytes((uint)type);
return Encoding.ASCII.GetString(bytes);
}
private struct UnitFloat
{
public UnitTypes type;
public double value;
}
private sealed class BrushData
{
public int diameter;
public int spacing;
public string sampledDataTag;
}
private sealed class BrushSectionOffsets
{
public readonly long sampleSectionOffset;
public readonly long descriptorSectionOffset;
public BrushSectionOffsets(long sampleSectionOffset, long descriptorSectionOffset)
{
this.sampleSectionOffset = sampleSectionOffset;
this.descriptorSectionOffset = descriptorSectionOffset;
}
}
private sealed class EnumeratedValue
{
public string type;
public string value;
}
}
}