forked from PsdPlugin/PsdPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageDecoderPdn.cs
More file actions
640 lines (545 loc) · 20 KB
/
Copy pathImageDecoderPdn.cs
File metadata and controls
640 lines (545 loc) · 20 KB
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/////////////////////////////////////////////////////////////////////////////////
//
// Photoshop PSD FileType Plugin for Paint.NET
// http://psdplugin.codeplex.com/
//
// This software is provided under the MIT License:
// Copyright (c) 2006-2007 Frank Blumenberg
// Copyright (c) 2010-2014 Tao Yue
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
using PaintDotNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using PhotoshopFile;
namespace PaintDotNet.Data.PhotoshopFileType
{
public static class ImageDecoderPdn
{
private class DecodeContext
{
public PhotoshopFile.Layer Layer { get; private set; }
public int ByteDepth { get; private set; }
public Channel[] Channels { get; private set; }
public Channel AlphaChannel { get; private set; }
public PsdColorMode ColorMode { get; private set; }
public byte[] ColorModeData { get; private set; }
public Rectangle Rectangle { get; private set; }
public MaskDecodeContext LayerMaskContext { get; private set; }
public MaskDecodeContext UserMaskContext { get; private set; }
public DecodeContext(PhotoshopFile.Layer layer, Rectangle bounds)
{
Layer = layer;
ByteDepth = Util.BytesFromBitDepth(layer.PsdFile.BitDepth);
Channels = layer.Channels.ToIdArray();
AlphaChannel = layer.AlphaChannel;
ColorMode = layer.PsdFile.ColorMode;
ColorModeData = layer.PsdFile.ColorModeData;
// Clip the layer to the specified bounds
Rectangle = Layer.Rect.IntersectWith(bounds);
if (layer.Masks != null)
{
LayerMaskContext = GetMaskContext(layer.Masks.LayerMask);
UserMaskContext = GetMaskContext(layer.Masks.UserMask);
}
}
private MaskDecodeContext GetMaskContext(Mask mask)
{
if ((mask == null) || (mask.Disabled))
{
return null;
}
return new MaskDecodeContext(mask, this);
}
}
private class MaskDecodeContext
{
public Mask Mask { get; private set; }
public Rectangle Rectangle { get; private set; }
public byte[] AlphaBuffer { get; private set; }
public MaskDecodeContext(Mask mask, DecodeContext layerContext)
{
Mask = mask;
// The PositionVsLayer flag is documented to indicate a position
// relative to the layer, but Photoshop treats the position as
// absolute. So that's what we do, too.
Rectangle = mask.Rect.IntersectWith(layerContext.Rectangle);
AlphaBuffer = new byte[layerContext.Rectangle.Width];
}
public bool IsRowEmpty(int row)
{
return (Mask.ImageData == null)
|| (Mask.ImageData.Length == 0)
|| (Rectangle.Size.IsEmpty)
|| (row < Rectangle.Top)
|| (row >= Rectangle.Bottom);
}
}
/// <summary>
/// Decode image from Photoshop's channel-separated formats to BGRA.
/// </summary>
public static unsafe void DecodeImage(BitmapLayer pdnLayer,
PhotoshopFile.Layer psdLayer)
{
var decodeContext = new DecodeContext(psdLayer, pdnLayer.Bounds);
DecodeDelegate decoder = null;
if (decodeContext.ByteDepth == 4)
decoder = GetDecodeDelegate32(decodeContext.ColorMode);
else
decoder = GetDecodeDelegate(decodeContext.ColorMode);
DecodeImage(pdnLayer, decodeContext, decoder);
}
private unsafe delegate void DecodeDelegate(
ColorBgra* pDestStart, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context);
private static unsafe DecodeDelegate GetDecodeDelegate(PsdColorMode psdColorMode)
{
switch (psdColorMode)
{
case PsdColorMode.Bitmap:
return SetPDNRowBitmap;
case PsdColorMode.Grayscale:
case PsdColorMode.Duotone:
return SetPDNRowGrayscale;
case PsdColorMode.Indexed:
return SetPDNRowIndexed;
case PsdColorMode.RGB:
return SetPDNRowRgb;
case PsdColorMode.CMYK:
return SetPDNRowCmyk;
case PsdColorMode.Lab:
return SetPDNRowLab;
case PsdColorMode.Multichannel:
throw new Exception("Cannot decode multichannel.");
default:
throw new Exception("Unknown color mode.");
}
}
private static unsafe DecodeDelegate GetDecodeDelegate32(PsdColorMode psdColorMode)
{
switch (psdColorMode)
{
case PsdColorMode.Grayscale:
return SetPDNRowGrayscale32;
case PsdColorMode.RGB:
return SetPDNRowRgb32;
default:
throw new PsdInvalidException(
"32-bit HDR images must be either RGB or grayscale.");
}
}
/// <summary>
/// Decode image from Photoshop's channel-separated formats to BGRA,
/// using the specified decode delegate on each row.
/// </summary>
private static unsafe void DecodeImage(BitmapLayer pdnLayer,
DecodeContext decodeContext, DecodeDelegate decoder)
{
var psdLayer = decodeContext.Layer;
var surface = pdnLayer.Surface;
var rect = decodeContext.Rectangle;
// Convert rows from the Photoshop representation, writing the
// resulting ARGB values to to the Paint.NET Surface.
for (int y = rect.Top; y < rect.Bottom; y++)
{
// Calculate index into ImageData source from row and column.
int idxSrcPixel = (y - psdLayer.Rect.Top) * psdLayer.Rect.Width
+ (rect.Left - psdLayer.Rect.Left);
int idxSrcByte = idxSrcPixel * decodeContext.ByteDepth;
// Calculate pointers to destination Surface.
var pDestRow = surface.GetRowAddress(y);
var pDestStart = pDestRow + decodeContext.Rectangle.Left;
var pDestEnd = pDestRow + decodeContext.Rectangle.Right;
// For 16-bit images, take the higher-order byte from the image
// data, which is now in little-endian order.
if (decodeContext.ByteDepth == 2)
idxSrcByte++;
// Decode the color and alpha channels
decoder(pDestStart, pDestEnd, idxSrcByte, decodeContext);
SetPDNAlphaRow(pDestStart, pDestEnd, idxSrcByte,
decodeContext.ByteDepth, decodeContext.AlphaChannel);
// Apply layer masks(s) to the alpha channel
var layerMaskAlphaRow = GetMaskAlphaRow(y,
decodeContext, decodeContext.LayerMaskContext);
var userMaskAlphaRow = GetMaskAlphaRow(y,
decodeContext, decodeContext.UserMaskContext);
ApplyPDNMask(pDestStart, pDestEnd, layerMaskAlphaRow, userMaskAlphaRow);
}
}
///////////////////////////////////////////////////////////////////////////
unsafe private static void SetPDNAlphaRow(
ColorBgra* pDestStart, ColorBgra* pDestEnd, int idxSrc, int byteDepth,
Channel alphaChannel)
{
// Set alpha to fully-opaque if there is no alpha channel
if (alphaChannel == null)
{
ColorBgra* pDest = pDestStart;
while (pDest < pDestEnd)
{
pDest->A = 255;
pDest++;
}
}
// Set the alpha channel data
else
{
fixed (byte* pSrcAlphaChannel = &alphaChannel.ImageData[0])
{
ColorBgra* pDest = pDestStart;
byte* pSrcAlpha = pSrcAlphaChannel + idxSrc;
while (pDest < pDestEnd)
{
pDest->A = (byteDepth < 4)
? *pSrcAlpha
: RGBByteFromHDRFloat(pSrcAlpha);
pDest++;
pSrcAlpha += byteDepth;
}
}
}
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets one row of alpha values from the mask.
/// </summary>
/// <param name="y">The y-coordinate of the row.</param>
/// <param name="layerContext">The decode context for the layer containing
/// the mask.</param>
/// <param name="maskContext">The decode context for the mask.</param>
/// <returns>An array of alpha values for the row, corresponding to the
/// width of the layer decode context.</returns>
unsafe private static byte[] GetMaskAlphaRow(
int y, DecodeContext layerContext, MaskDecodeContext maskContext)
{
if (maskContext == null)
{
return null;
}
var mask = maskContext.Mask;
// Background color for areas not covered by the mask
byte backgroundColor = mask.InvertOnBlend
? (byte)(255 - mask.BackgroundColor)
: mask.BackgroundColor;
fixed (byte* pAlpha = &maskContext.AlphaBuffer[0])
{
Util.Fill(pAlpha, pAlpha + maskContext.AlphaBuffer.Length,
backgroundColor);
}
if (maskContext.IsRowEmpty(y))
{
return maskContext.AlphaBuffer;
}
//////////////////////////////////////
// Transfer mask into the alpha array
fixed (byte* pAlphaRow = &maskContext.AlphaBuffer[0],
pMaskData = &mask.ImageData[0])
{
// Get pointers to starting positions
int alphaColumn = maskContext.Rectangle.X - layerContext.Rectangle.X;
byte* pAlpha = pAlphaRow + alphaColumn;
byte* pAlphaEnd = pAlpha + maskContext.Rectangle.Width;
int maskRow = y - maskContext.Mask.Rect.Y;
int maskColumn = maskContext.Rectangle.X - maskContext.Mask.Rect.X;
int idxMaskPixel = (maskRow * mask.Rect.Width) + maskColumn;
byte* pMask = pMaskData + idxMaskPixel * layerContext.ByteDepth;
// Take the high-order byte if values are 16-bit (little-endian)
if (layerContext.ByteDepth == 2)
pMask++;
// Decode mask into the alpha array.
if (layerContext.ByteDepth == 4)
{
DecodeMaskAlphaRow32(pAlpha, pAlphaEnd, pMask);
}
else
{
DecodeMaskAlphaRow(pAlpha, pAlphaEnd, pMask, layerContext.ByteDepth);
}
// Obsolete since Photoshop CS6, but retained for compatibility with
// older versions. Note that the background has already been inverted.
if (mask.InvertOnBlend)
{
Util.Invert(pAlpha, pAlphaEnd);
}
}
return maskContext.AlphaBuffer;
}
private static unsafe void DecodeMaskAlphaRow32(
byte* pAlpha, byte* pAlphaEnd, byte* pMask)
{
while (pAlpha < pAlphaEnd)
{
*pAlpha = RGBByteFromHDRFloat(pMask);
pAlpha++;
pMask += 4;
}
}
private static unsafe void DecodeMaskAlphaRow(
byte* pAlpha, byte* pAlphaEnd, byte* pMask, int byteDepth)
{
while (pAlpha < pAlphaEnd)
{
*pAlpha = *pMask;
pAlpha++;
pMask += byteDepth;
}
}
///////////////////////////////////////////////////////////////////////////
private static unsafe void ApplyPDNMask(ColorBgra* pDestStart, ColorBgra* pDestEnd,
byte[] layerMaskAlpha, byte[] userMaskAlpha)
{
// Do nothing if there are no masks
if ((layerMaskAlpha == null) && (userMaskAlpha == null))
return;
// Apply one mask
else if ((layerMaskAlpha == null) || (userMaskAlpha == null))
{
var maskAlpha = layerMaskAlpha ?? userMaskAlpha;
fixed (byte* pMaskAlpha = &maskAlpha[0])
{
var pDest = pDestStart;
var pMask = pMaskAlpha;
while (pDest < pDestEnd)
{
pDest->A = (byte)(pDest->A * *pMask / 255);
pDest++;
pMask++;
}
}
}
// Apply both masks in one pass, to minimize rounding error
else
{
fixed (byte* pLayerMaskAlpha = &layerMaskAlpha[0],
pUserMaskAlpha = &userMaskAlpha[0])
{
var pDest = pDestStart;
var pMask1 = pLayerMaskAlpha;
var pMask2 = pUserMaskAlpha;
while (pDest < pDestEnd)
{
var alphaFactor = (*pMask1) * (*pMask2);
pDest->A = (byte)(pDest->A * alphaFactor / 65025);
pDest++;
pMask1++;
pMask2++;
}
}
}
}
///////////////////////////////////////////////////////////////////////////
#region Decode 32-bit HDR channels
private static unsafe void SetPDNRowRgb32(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
fixed (byte* pSrcRedChannel = &context.Channels[0].ImageData[0],
pSrcGreenChannel = &context.Channels[1].ImageData[0],
pSrcBlueChannel = &context.Channels[2].ImageData[0])
{
while (pDest < pDestEnd)
{
pDest->R = RGBByteFromHDRFloat(pSrcRedChannel + idxSrc);
pDest->G = RGBByteFromHDRFloat(pSrcGreenChannel + idxSrc);
pDest->B = RGBByteFromHDRFloat(pSrcBlueChannel + idxSrc);
pDest++;
idxSrc += 4;
}
}
}
private static unsafe void SetPDNRowGrayscale32(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
fixed (byte* channelPtr = &context.Channels[0].ImageData[0])
{
while (pDest < pDestEnd)
{
byte* pSource = channelPtr + idxSrc;
byte rgbValue = RGBByteFromHDRFloat(pSource);
pDest->R = rgbValue;
pDest->G = rgbValue;
pDest->B = rgbValue;
pDest++;
idxSrc += 4;
}
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
#region Decode 8-bit and 16-bit channels
private static unsafe void SetPDNRowRgb(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
while (pDest < pDestEnd)
{
pDest->R = context.Channels[0].ImageData[idxSrc];
pDest->G = context.Channels[1].ImageData[idxSrc];
pDest->B = context.Channels[2].ImageData[idxSrc];
pDest++;
idxSrc += context.ByteDepth;
}
}
///////////////////////////////////////////////////////////////////////////////
//
// The color-conversion formulas come from the Colour Space Conversions FAQ:
// http://www.poynton.com/PDFs/coloureq.pdf
//
// RGB --> CMYK CMYK --> RGB
// --------------------------------------- --------------------------------------------
// Black = minimum(1-Red,1-Green,1-Blue) Red = 1-minimum(1,Cyan*(1-Black)+Black)
// Cyan = (1-Red-Black)/(1-Black) Green = 1-minimum(1,Magenta*(1-Black)+Black)
// Magenta = (1-Green-Black)/(1-Black) Blue = 1-minimum(1,Yellow*(1-Black)+Black)
// Yellow = (1-Blue-Black)/(1-Black)
//
///////////////////////////////////////////////////////////////////////////////
private static unsafe void SetPDNRowCmyk(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
while (pDest < pDestEnd)
{
// CMYK values are stored as complements, presumably to allow for some
// measure of compatibility with RGB-only applications.
var C = 255 - context.Channels[0].ImageData[idxSrc];
var M = 255 - context.Channels[1].ImageData[idxSrc];
var Y = 255 - context.Channels[2].ImageData[idxSrc];
var K = 255 - context.Channels[3].ImageData[idxSrc];
int nRed = 255 - Math.Min(255, C * (255 - K) / 255 + K);
int nGreen = 255 - Math.Min(255, M * (255 - K) / 255 + K);
int nBlue = 255 - Math.Min(255, Y * (255 - K) / 255 + K);
pDest->R = (byte)nRed;
pDest->G = (byte)nGreen;
pDest->B = (byte)nBlue;
pDest++;
idxSrc += context.ByteDepth;
}
}
private static unsafe void SetPDNRowBitmap(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
var bitmap = context.Channels[0].ImageData;
while (pDest < pDestEnd)
{
byte mask = (byte)(0x80 >> (idxSrc % 8));
byte bwValue = (byte)(bitmap[idxSrc / 8] & mask);
bwValue = (bwValue == 0) ? (byte)255 : (byte)0;
pDest->R = bwValue;
pDest->G = bwValue;
pDest->B = bwValue;
pDest++;
idxSrc += context.ByteDepth;
}
}
private static unsafe void SetPDNRowGrayscale(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
while (pDest < pDestEnd)
{
var level = context.Channels[0].ImageData[idxSrc];
pDest->R = level;
pDest->G = level;
pDest->B = level;
pDest++;
idxSrc += context.ByteDepth;
}
}
private static unsafe void SetPDNRowIndexed(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
while (pDest < pDestEnd)
{
int index = (int)context.Channels[0].ImageData[idxSrc];
pDest->R = (byte)context.Layer.PsdFile.ColorModeData[index];
pDest->G = context.Layer.PsdFile.ColorModeData[index + 256];
pDest->B = context.Layer.PsdFile.ColorModeData[index + 2 * 256];
pDest++;
idxSrc += context.ByteDepth;
}
}
private static unsafe void SetPDNRowLab(ColorBgra* pDest, ColorBgra* pDestEnd,
int idxSrc, DecodeContext context)
{
while (pDest < pDestEnd)
{
double exL, exA, exB;
exL = (double)context.Channels[0].ImageData[idxSrc];
exA = (double)context.Channels[1].ImageData[idxSrc];
exB = (double)context.Channels[2].ImageData[idxSrc];
int L = (int)(exL / 2.55);
int a = (int)(exA - 127.5);
int b = (int)(exB - 127.5);
// First, convert from Lab to XYZ.
// Standards used Observer = 2, Illuminant = D65
const double ref_X = 95.047;
const double ref_Y = 100.000;
const double ref_Z = 108.883;
double var_Y = ((double)L + 16.0) / 116.0;
double var_X = (double)a / 500.0 + var_Y;
double var_Z = var_Y - (double)b / 200.0;
double var_X3 = var_X * var_X * var_X;
double var_Y3 = var_Y * var_Y * var_Y;
double var_Z3 = var_Z * var_Z * var_Z;
if (var_Y3 > 0.008856)
var_Y = var_Y3;
else
var_Y = (var_Y - 16 / 116) / 7.787;
if (var_X3 > 0.008856)
var_X = var_X3;
else
var_X = (var_X - 16 / 116) / 7.787;
if (var_Z3 > 0.008856)
var_Z = var_Z3;
else
var_Z = (var_Z - 16 / 116) / 7.787;
double X = ref_X * var_X;
double Y = ref_Y * var_Y;
double Z = ref_Z * var_Z;
// Then, convert from XYZ to RGB.
// Standards used Observer = 2, Illuminant = D65
// ref_X = 95.047, ref_Y = 100.000, ref_Z = 108.883
double var_R = X * 0.032406 + Y * (-0.015372) + Z * (-0.004986);
double var_G = X * (-0.009689) + Y * 0.018758 + Z * 0.000415;
double var_B = X * 0.000557 + Y * (-0.002040) + Z * 0.010570;
if (var_R > 0.0031308)
var_R = 1.055 * (Math.Pow(var_R, 1 / 2.4)) - 0.055;
else
var_R = 12.92 * var_R;
if (var_G > 0.0031308)
var_G = 1.055 * (Math.Pow(var_G, 1 / 2.4)) - 0.055;
else
var_G = 12.92 * var_G;
if (var_B > 0.0031308)
var_B = 1.055 * (Math.Pow(var_B, 1 / 2.4)) - 0.055;
else
var_B = 12.92 * var_B;
int nRed = (int)(var_R * 256.0);
int nGreen = (int)(var_G * 256.0);
int nBlue = (int)(var_B * 256.0);
if (nRed < 0) nRed = 0;
else if (nRed > 255) nRed = 255;
if (nGreen < 0) nGreen = 0;
else if (nGreen > 255) nGreen = 255;
if (nBlue < 0) nBlue = 0;
else if (nBlue > 255) nBlue = 255;
pDest->R = (byte)nRed;
pDest->G = (byte)nGreen;
pDest->B = (byte)nBlue;
pDest++;
idxSrc += context.ByteDepth;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////
private static double rgbExponent = 1 / 2.19921875;
unsafe private static byte RGBByteFromHDRFloat(byte* ptr)
{
float* floatPtr = (float*)ptr;
var result = (byte)(255 * Math.Pow(*floatPtr, rgbExponent));
return result;
}
}
}