forked from 0xC0000054/pdn-photoshop-brush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbrLoad.cs
552 lines (464 loc) · 14.1 KB
/
AbrLoad.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
// Portions of this file has been adapted from:
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See src/Resources/Files/License.txt for full licensing and attribution //
// details. //
// . //
/////////////////////////////////////////////////////////////////////////////////
using AbrFileTypePlugin.Properties;
using PaintDotNet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
namespace AbrFileTypePlugin
{
internal static class AbrLoad
{
public static Document Load(Stream stream)
{
using (BinaryReverseReader reader = new BinaryReverseReader(stream))
{
ReadOnlyCollection<Brush> brushes;
short version = reader.ReadInt16();
switch (version)
{
case 1:
case 2:
brushes = DecodeVersion1(reader, version);
break;
case 6:
case 7: // Used by Photoshop CS and later for brushes containing 16-bit data.
case 10: // Used by Photoshop CS6 and/or CC?
brushes = DecodeVersion6(reader, version);
break;
default:
throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedABRVersion, version));
}
if (brushes.Count == 0)
{
throw new FormatException(Resources.NoSampledBrushes);
}
int maxWidth = 0;
int maxHeight = 0;
foreach (var item in brushes)
{
if (item.Surface.Width > maxWidth)
{
maxWidth = item.Surface.Width;
}
if (item.Surface.Height > maxHeight)
{
maxHeight = item.Surface.Height;
}
}
Document doc = null;
Document tempDoc = null;
try
{
tempDoc = new Document(maxWidth, maxHeight);
for (int i = 0; i < brushes.Count; i++)
{
Brush abr = brushes[i];
BitmapLayer layer = null;
BitmapLayer tempLayer = null;
try
{
tempLayer = new BitmapLayer(maxWidth, maxHeight);
tempLayer.IsBackground = i == 0;
tempLayer.Name = !string.IsNullOrEmpty(abr.Name) ? abr.Name : string.Format(CultureInfo.CurrentCulture, Resources.BrushNameFormat, i);
tempLayer.Metadata.SetUserValue(AbrMetadataNames.BrushSpacing, abr.Spacing.ToString(CultureInfo.InvariantCulture));
tempLayer.Surface.CopySurface(abr.Surface);
layer = tempLayer;
tempLayer = null;
}
finally
{
if (tempLayer != null)
{
tempLayer.Dispose();
tempLayer = null;
}
abr.Dispose();
}
tempDoc.Layers.Add(layer);
}
doc = tempDoc;
tempDoc = null;
}
finally
{
if (tempDoc != null)
{
tempDoc.Dispose();
tempDoc = null;
}
}
return doc;
}
}
private static ReadOnlyCollection<Brush> DecodeVersion1(BinaryReverseReader reader, short version)
{
short count = reader.ReadInt16();
List<Brush> brushes = new List<Brush>(count);
for (int i = 0; i < count; i++)
{
AbrBrushType type = (AbrBrushType)reader.ReadInt16();
int size = reader.ReadInt32();
long endOffset = reader.BaseStream.Position + size;
#if DEBUG
System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Brush: {0}, type: {1}, size: {2} bytes", i, type, size));
#endif
if (type == AbrBrushType.Computed)
{
#if DEBUG
int misc = reader.ReadInt32();
short spacing = reader.ReadInt16();
string name = string.Empty;
if (version == 2)
{
name = reader.ReadUnicodeString();
}
short diameter = reader.ReadInt16();
short roundness = reader.ReadInt16();
short angle = reader.ReadInt16();
short hardness = reader.ReadInt16();
#else
reader.BaseStream.Position += size;
#endif
}
else if (type == AbrBrushType.Sampled)
{
int misc = reader.ReadInt32();
short spacing = reader.ReadInt16();
string name = string.Empty;
if (version == 2)
{
name = reader.ReadUnicodeString();
}
bool antiAlias = reader.ReadByte() != 0;
// Skip the Int16 bounds.
reader.BaseStream.Position += 8L;
Rectangle bounds = reader.ReadInt32Rectangle();
if (bounds.Width <= 0 || bounds.Height <= 0)
{
// Skip any brushes that have invalid dimensions.
reader.BaseStream.Position += (endOffset - reader.BaseStream.Position);
continue;
}
short depth = reader.ReadInt16();
if (depth != 8)
{
// The format specs state that brushes must be 8-bit, skip any that are not.
reader.BaseStream.Position += (endOffset - reader.BaseStream.Position);
continue;
}
int height = bounds.Height;
int width = bounds.Width;
int rowsRemaining = height;
int rowsRead = 0;
byte[] alphaData = new byte[width * height];
do
{
// Sampled brush data is broken into repeating chunks for brushes taller that 16384 pixels.
int chunkHeight = Math.Min(rowsRemaining, 16384);
// The format specs state that compression is stored as a 2-byte field, but it is written as a 1-byte field in actual files.
AbrImageCompression compression = (AbrImageCompression)reader.ReadByte();
if (compression == AbrImageCompression.RLE)
{
short[] compressedRowLengths = new short[height];
for (int y = 0; y < height; y++)
{
compressedRowLengths[y] = reader.ReadInt16();
}
for (int y = 0; y < chunkHeight; y++)
{
int row = rowsRead + y;
RLEHelper.DecodedRow(reader, alphaData, row * width, width);
}
}
else
{
int rowByteOffset = rowsRead * width;
int numBytesToRead = chunkHeight * width;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = reader.Read(alphaData, rowByteOffset + numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n == 0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
}
rowsRemaining -= 16384;
rowsRead += 16384;
} while (rowsRemaining > 0);
brushes.Add(CreateSampledBrush(width, height, depth, alphaData, name, spacing));
}
else
{
// Skip any unknown brush types.
reader.BaseStream.Position += size;
}
}
return brushes.AsReadOnly();
}
private static ReadOnlyCollection<Brush> DecodeVersion6(BinaryReverseReader reader, short majorVersion)
{
short minorVersion = reader.ReadInt16();
long unusedDataLength;
switch (minorVersion)
{
case 1:
// Skip the Int16 bounds rectangle and the unknown Int16.
unusedDataLength = 10L;
break;
case 2:
// Skip the unknown bytes.
unusedDataLength = 264L;
break;
default:
throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedABRSubVersion, majorVersion, minorVersion));
}
BrushSectionParser parser = new BrushSectionParser(reader);
List<Brush> brushes = new List<Brush>(parser.SampledBrushes.Count);
long sampleSectionOffset = parser.SampleSectionOffset;
if (parser.SampledBrushes.Count > 0 && sampleSectionOffset >= 0)
{
reader.BaseStream.Position = sampleSectionOffset;
uint sectionLength = reader.ReadUInt32();
long sectionEnd = reader.BaseStream.Position + sectionLength;
while (reader.BaseStream.Position < sectionEnd)
{
uint brushLength = reader.ReadUInt32();
// The brush data is padded to 4 byte alignment.
long paddedBrushLength = ((long)brushLength + 3) & ~3;
long endOffset = reader.BaseStream.Position + paddedBrushLength;
string tag = reader.ReadPascalString();
// Skip the unneeded data that comes before the Int32 bounds rectangle.
reader.BaseStream.Position += unusedDataLength;
Rectangle bounds = reader.ReadInt32Rectangle();
if (bounds.Width <= 0 || bounds.Height <= 0)
{
// Skip any brushes that have invalid dimensions.
reader.BaseStream.Position += (endOffset - reader.BaseStream.Position);
continue;
}
short depth = reader.ReadInt16();
if (depth != 8 && depth != 16)
{
// Skip any brushes with an unknown bit depth.
reader.BaseStream.Position += (endOffset - reader.BaseStream.Position);
continue;
}
SampledBrush sampledBrush = parser.SampledBrushes.FindBrush(tag);
if (sampledBrush != null)
{
AbrImageCompression compression = (AbrImageCompression)reader.ReadByte();
int height = bounds.Height;
int width = bounds.Width;
byte[] alphaData = null;
if (compression == AbrImageCompression.RLE)
{
short[] compressedRowLengths = new short[height];
for (int y = 0; y < height; y++)
{
compressedRowLengths[y] = reader.ReadInt16();
}
int alphaDataSize = width * height;
int bytesPerRow = width;
if (depth == 16)
{
alphaDataSize *= 2;
bytesPerRow *= 2;
}
alphaData = new byte[alphaDataSize];
for (int y = 0; y < height; y++)
{
RLEHelper.DecodedRow(reader, alphaData, y * width, bytesPerRow);
}
}
else
{
int alphaDataSize = width * height;
if (depth == 16)
{
alphaDataSize *= 2;
}
alphaData = reader.ReadBytes(alphaDataSize);
}
var brush = CreateSampledBrush(width, height, depth, alphaData, sampledBrush.Name, sampledBrush.Spacing);
brushes.Add(brush);
// Some brushes only store the largest item and scale it down.
var scaledBrushes = parser.SampledBrushes.Where(i => i.Tag.Equals(tag, StringComparison.Ordinal) && i.Diameter < sampledBrush.Diameter);
if (scaledBrushes.Any())
{
int originalWidth = brush.Surface.Width;
int originalHeight = brush.Surface.Height;
foreach (var item in scaledBrushes.OrderByDescending(p => p.Diameter))
{
Size size = ComputeBrushSize(originalWidth, originalHeight, item.Diameter);
Brush scaledBrush = new Brush(size.Width, size.Height, item.Name, item.Spacing);
scaledBrush.Surface.FitSurface(ResamplingAlgorithm.SuperSampling, brush.Surface);
brushes.Add(scaledBrush);
}
}
}
long remaining = endOffset - reader.BaseStream.Position;
// Skip any remaining bytes until the next sampled brush.
if (remaining > 0)
{
reader.BaseStream.Position += remaining;
}
}
}
return brushes.AsReadOnly();
}
private static unsafe Brush CreateSampledBrush(int width, int height, int depth, byte[] alphaData, string name, int spacing)
{
Brush brush = null;
Brush tempBrush = null;
try
{
tempBrush = new Brush(width, height, name, spacing);
Surface surface = tempBrush.Surface;
fixed (byte* ptr = alphaData)
{
if (depth == 16)
{
int srcStride = width * 2;
for (int y = 0; y < height; y++)
{
byte* src = ptr + (y * srcStride);
ColorBgra* dst = surface.GetRowAddressUnchecked(y);
for (int x = 0; x < width; x++)
{
ushort val = (ushort)((src[0] << 8) | src[1]);
dst->B = dst->G = dst->R = 0;
// The 16-bit brush data is stored in the range of [0, 32768].
dst->A = (byte)((val * 10) / 1285);
src += 2;
dst++;
}
}
}
else
{
for (int y = 0; y < height; y++)
{
byte* src = ptr + (y * width);
ColorBgra* dst = surface.GetRowAddressUnchecked(y);
for (int x = 0; x < width; x++)
{
dst->B = dst->G = dst->R = 0;
dst->A = *src;
src++;
dst++;
}
}
}
}
brush = tempBrush;
tempBrush = null;
}
finally
{
if (tempBrush != null)
{
tempBrush.Dispose();
tempBrush = null;
}
}
return brush;
}
private static Size ComputeBrushSize(int originalWidth, int originalHeight, int maxEdgeLength)
{
Size thumbSize = Size.Empty;
if (originalWidth <= 0 || originalHeight <= 0)
{
thumbSize.Width = 1;
thumbSize.Height = 1;
}
else if (originalWidth > originalHeight)
{
int longSide = Math.Min(originalWidth, maxEdgeLength);
thumbSize.Width = longSide;
thumbSize.Height = Math.Max(1, (originalHeight * longSide) / originalWidth);
}
else if (originalHeight > originalWidth)
{
int longSide = Math.Min(originalHeight, maxEdgeLength);
thumbSize.Width = Math.Max(1, (originalWidth * longSide) / originalHeight);
thumbSize.Height = longSide;
}
else
{
int longSide = Math.Min(originalWidth, maxEdgeLength);
thumbSize.Width = longSide;
thumbSize.Height = longSide;
}
return thumbSize;
}
private sealed class Brush : IDisposable
{
private Surface surface;
private readonly string name;
private readonly int spacing;
public Brush(int width, int height, string name, int spacing)
{
this.surface = new Surface(width, height);
this.name = name;
this.spacing = spacing;
}
public Surface Surface
{
get
{
return this.surface;
}
}
public string Name
{
get
{
return this.name;
}
}
public int Spacing
{
get
{
return this.spacing;
}
}
public void Dispose()
{
if (this.surface != null)
{
this.surface.Dispose();
this.surface = null;
}
}
}
}
}