-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
872 lines (804 loc) · 22.8 KB
/
layer.go
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
package psd
import (
"errors"
"image"
"io"
)
// Layer represents layer.
type Layer struct {
SeqID int
Name string
UnicodeName string
MBCSName string
Rect image.Rectangle
// Channel key is the channel ID.
// 0 = red, 1 = green, etc.
// -1 = transparency mask
// -2 = user supplied layer mask
// -3 = real user supplied layer mask
// (when both a user mask and a vector mask are present)
Channel map[int]Channel
BlendMode BlendMode
Opacity uint8
Clipping bool
BlendClippedElements bool
Flags uint8
Mask Mask
Picker image.Image
AdditionalLayerInfo map[AdditionalInfoKey][]byte
SectionDividerSetting struct {
Type int
BlendMode BlendMode
SubType int
}
Layer []Layer
}
// TransparencyProtected returns whether the layer's transparency being protected.
func (l *Layer) TransparencyProtected() bool {
return l.Flags&1 != 0
}
// Visible returns whether the layer is visible.
func (l *Layer) Visible() bool {
return l.Flags&2 == 0
}
// HasImage returns whether the layer has an image data.
func (l *Layer) HasImage() bool {
return l.SectionDividerSetting.Type == 0
}
// Folder returns whether the layer is folder(group layer).
func (l *Layer) Folder() bool {
return l.SectionDividerSetting.Type == 1 || l.SectionDividerSetting.Type == 2
}
// FolderIsOpen returns whether the folder is opened when layer is folder.
func (l *Layer) FolderIsOpen() bool {
return l.SectionDividerSetting.Type == 1
}
func (l *Layer) String() string {
return l.Name
}
// Mask represents layer mask and vector mask.
type Mask struct {
Rect image.Rectangle
DefaultColor int
Flags int
RealRect image.Rectangle
RealBackgroundColor int
RealFlags int
UserMaskDensity int
UserMaskFeather float64
VectorMaskDensity int
VectorMaskFeather float64
}
// Enabled returns whether the mask is enabled.
func (m *Mask) Enabled() bool {
return m.Flags&2 == 0
}
// RealEnabled returns whether the user real mask is enabled.
func (m *Mask) RealEnabled() bool {
return m.RealFlags&2 == 0
}
// Channel represents a channel of the color.
type Channel struct {
// Data is uncompressed channel image data.
Data []byte
Picker image.Image
}
func readLayerAndMaskInfo(r io.Reader, cfg *Config, o *DecodeOptions) (psd *PSD, read int, err error) {
if Debug != nil {
Debug.Println("start - layer and mask information section")
}
psd = &PSD{
Config: *cfg,
}
// Layer and Mask Information Section
// http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_75067
intSize := get4or8(cfg.PSB())
b := make([]byte, intSize)
var l int
if l, err = io.ReadFull(r, b[:intSize]); err != nil {
return nil, 0, err
}
read += l
layerAndMaskInfoLen := int(readUint(b, 0, intSize))
if Debug != nil {
Debug.Println(" layerAndMaskInfoLen:", layerAndMaskInfoLen)
reportReaderPosition(" file offset: 0x%08x", r)
}
if layerAndMaskInfoLen == 0 {
return psd, read, nil
}
var layer []Layer
if read < layerAndMaskInfoLen+intSize {
if layer, l, err = readLayerInfo(r, cfg, o); err != nil {
return nil, read, err
}
read += l
}
if layerAndMaskInfoLen+intSize-read >= 4 {
// Global layer mask info
// http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_17115
if l, err = io.ReadFull(r, b[:4]); err != nil {
return nil, read, err
}
read += l
if globalLayerMaskInfoLen := int(readUint32(b, 0)); globalLayerMaskInfoLen > 0 {
if Debug != nil {
Debug.Println(" globalLayerMaskInfoLen:", globalLayerMaskInfoLen)
reportReaderPosition(" file offset: 0x%08x", r)
}
// TODO(oov): implement
if l, err = discard(r, globalLayerMaskInfoLen); err != nil {
return nil, read, err
}
read += l
}
}
if layerAndMaskInfoLen+intSize-read >= 8 {
var layer2 []Layer
if psd.AdditinalLayerInfo, layer2, l, err = readAdditionalLayerInfo(r, layerAndMaskInfoLen+intSize-read, cfg, o); err != nil {
return nil, read, err
}
read += l
if layer != nil && layer2 != nil {
return nil, read, errors.New("psd: unexpected layer structure")
}
if layer2 != nil {
layer = layer2
}
}
if psd.Layer, err = buildTree(layer); err != nil {
return nil, read, err
}
if remain := layerAndMaskInfoLen + intSize - read; remain != 0 && remain < intSize {
if l, err = discard(r, remain); err != nil {
return nil, read, err
}
read += l
}
if read != layerAndMaskInfoLen+intSize {
return nil, read, errors.New("psd: layer and mask info read size mismatched. expected " + itoa(layerAndMaskInfoLen+4) + " actual " + itoa(read))
}
if Debug != nil {
Debug.Println("end - layer and mask information section")
}
return psd, read, nil
}
func buildTree(layer []Layer) ([]Layer, error) {
// idx type
// 0: 3
// 1: 3
// 2: 0
// 3: 0
// 4: 1
// 5: 0
// 6: 0
// 7: 1
stack := make([][]Layer, 0, 8)
n := []Layer{}
for i, l := range layer {
switch l.SectionDividerSetting.Type {
case 1, 2:
l.Layer = n
if len(stack) == 0 {
return nil, errors.New("psd: layer tree structure is broken(#" + itoa(i) + ")")
}
n = stack[len(stack)-1]
stack = stack[:len(stack)-1]
n = append(n, l)
case 3:
stack = append(stack, n)
n = []Layer{}
default:
n = append(n, l)
}
}
return n, nil
}
func readSectionDividerSetting(l *Layer) (typ int, blendMode BlendMode, subType int, err error) {
b, ok := l.AdditionalLayerInfo[AdditionalInfoKeySectionDividerSetting]
if ok {
typ = int(readUint32(b, 0))
if len(b) < 12 {
return typ, "", 0, nil
}
if string(b[4:8]) != sectionSignature {
return 0, "", 0, errors.New("psd: unexpected signature in section divider setting")
}
blendMode = BlendMode(b[8:12])
if len(b) < 16 {
return typ, blendMode, 0, nil
}
subType = int(readUint32(b, 12))
return typ, blendMode, subType, nil
}
b, ok = l.AdditionalLayerInfo[AdditionalInfoKeySectionDividerSetting2]
if ok {
typ = int(readUint32(b, 0))
if len(b) < 12 {
return typ, "", 0, nil
}
if string(b[4:8]) != sectionSignature {
return 0, "", 0, errors.New("psd: unexpected signature in section divider setting 2")
}
blendMode = BlendMode(b[8:12])
if len(b) < 16 {
return typ, blendMode, 0, nil
}
subType = int(readUint32(b, 12))
return typ, blendMode, subType, nil
}
return 0, "", 0, nil
}
type channelData struct {
ChIndex int
DataLen int
}
type layerChannelInfo struct {
Layer *Layer
ChannelData []channelData
}
func readLayerInfo(r io.Reader, cfg *Config, o *DecodeOptions) (layer []Layer, read int, err error) {
intSize := get4or8(cfg.PSB())
if Debug != nil {
Debug.Println("start - layer info section")
}
b := make([]byte, 16)
var l int
if l, err = io.ReadFull(r, b[:intSize]); err != nil {
return nil, 0, err
}
read += l
layerInfoLen := int(readUint(b, 0, intSize))
if Debug != nil {
Debug.Println(" layerInfoLen:", layerInfoLen)
}
if layerInfoLen == 0 {
if Debug != nil {
Debug.Println("end - layer info section")
}
return nil, read, nil
}
if l, err = io.ReadFull(r, b[:2]); err != nil {
return nil, read, err
}
read += l
numLayers := readUint16(b, 0)
if numLayers&0x8000 != 0 {
numLayers = ^numLayers + 1
// how can I use this info...?
// hasAlphaForMergedResult = true
}
if Debug != nil {
Debug.Println(" numLayers:", numLayers)
}
layerSlice := make([]Layer, numLayers)
lcis := make([]layerChannelInfo, numLayers)
for i := range layerSlice {
if Debug != nil {
Debug.Printf("start - layer #%d structure", i)
}
layer := &layerSlice[i]
layer.SeqID = i
if l, err = io.ReadFull(r, b[:16]); err != nil {
return nil, read, err
}
read += l
layer.Rect = image.Rect(
int(int32(readUint32(b, 4))), int(int32(readUint32(b, 0))),
int(int32(readUint32(b, 12))), int(int32(readUint32(b, 8))),
)
if l, err = io.ReadFull(r, b[:2]); err != nil {
return nil, read, err
}
read += l
numChannels := int(readUint16(b, 0))
lci := layerChannelInfo{
Layer: layer,
ChannelData: make([]channelData, numChannels),
}
for j := 0; j < numChannels; j++ {
if l, err = io.ReadFull(r, b[:2+intSize]); err != nil {
return nil, read, err
}
read += l
lci.ChannelData[j] = channelData{
ChIndex: int(int16(readUint16(b, 0))),
DataLen: int(readUint(b, 2, intSize)),
}
}
lcis[i] = lci
if l, err = io.ReadFull(r, b[:12]); err != nil {
return nil, read, err
}
read += l
if string(b[:4]) != sectionSignature {
return nil, read, errors.New("psd: unexpected the blend mode signature")
}
layer.BlendMode = BlendMode(b[4:8])
layer.Opacity = b[8]
layer.Clipping = b[9] != 0
layer.Flags = b[10]
// b[11] - Filler(zero)
if l, err = readLayerExtraData(r, layer, cfg, o); err != nil {
return nil, read, err
}
read += l
if data, ok := layer.AdditionalLayerInfo[AdditionalInfoKeyUnicodeLayerName]; ok && data != nil {
layer.UnicodeName = readUnicodeString(data)
layer.Name = layer.UnicodeName
delete(layer.AdditionalLayerInfo, AdditionalInfoKeyUnicodeLayerName)
} else {
layer.Name = layer.MBCSName
}
// this is called "Blend Clipped Layers as Group" in Photoshop's Layer Style dialog.
if data, ok := layer.AdditionalLayerInfo[AdditionalInfoKeyBlendClippingElements]; ok && data != nil {
layer.BlendClippedElements = data[0] != 0
delete(layer.AdditionalLayerInfo, AdditionalInfoKeyBlendClippingElements)
} else {
layer.BlendClippedElements = true
}
if layer.SectionDividerSetting.Type,
layer.SectionDividerSetting.BlendMode,
layer.SectionDividerSetting.SubType,
err = readSectionDividerSetting(layer); err != nil {
return nil, read, err
}
if layer.SectionDividerSetting.BlendMode == "" {
layer.SectionDividerSetting.BlendMode = layer.BlendMode
}
delete(layer.AdditionalLayerInfo, AdditionalInfoKeySectionDividerSetting)
if Debug != nil {
Debug.Printf("layer #%d", layer.SeqID)
Debug.Println(" Name:", layer.Name)
Debug.Println(" Rect:", layer.Rect)
Debug.Println(" Channels:", len(layer.Channel))
Debug.Println(" BlendMode:", layer.BlendMode)
Debug.Println(" Opacity:", layer.Opacity)
Debug.Println(" Clipping:", layer.Clipping)
Debug.Println(" Flags:", layer.Flags)
Debug.Println(" TransparecyProtected:", layer.TransparencyProtected())
Debug.Println(" Visible:", layer.Visible())
Debug.Println(" Photoshop 5.0 and later:", layer.Flags&8 != 0)
// this information is useful for the group layer,
// but sadly it isn't written correctly by some other softwares.
Debug.Println(" Pixel data irrelevant to appearance of document:", layer.Flags&8 != 0 && layer.Flags&16 != 0)
Debug.Println(" SectionDividerSetting:")
Debug.Println(" Type:", layer.SectionDividerSetting.Type)
Debug.Println(" BlendMode:", layer.SectionDividerSetting.BlendMode)
Debug.Println(" SubType:", layer.SectionDividerSetting.SubType)
Debug.Println(" LayerMask:")
Debug.Println(" Rect:", layer.Mask.Rect)
Debug.Println(" DefaultColor:", layer.Mask.DefaultColor)
Debug.Println(" Flags:", layer.Mask.Flags)
Debug.Println(" Position relative to layer:", layer.Mask.Flags&1 != 0)
Debug.Println(" Layer mask enabled:", layer.Mask.Enabled())
Debug.Println(" Invert layer mask when blending (Obsolete):", layer.Mask.Flags&4 != 0)
Debug.Println(" The user mask actually came from rendering other data:", layer.Mask.Flags&8 != 0)
Debug.Printf("end - layer #%d structure", i)
}
}
if !o.SkipLayerImage {
if l, err = readLayerImage(lcis, r, cfg, o); err != nil {
return nil, read, err
}
read += l
}
// Discard any remaining data.
if layerInfoLen+intSize > read {
if l, err = discard(r, layerInfoLen+intSize-read); err != nil {
return nil, read, err
}
read += l
}
if Debug != nil {
Debug.Println("end - layer info section")
}
return layerSlice, read, nil
}
func readLayerImage(lcis []layerChannelInfo, r io.Reader, cfg *Config, o *DecodeOptions) (read int, err error) {
var l int
b := make([]byte, 2)
for i, lci := range lcis {
chMap := map[int]Channel{}
pickerChs := make([][]byte, cfg.ColorMode.Channels(), 8)
for _, j := range lci.ChannelData {
var readCh int
if j.DataLen >= 2 {
if l, err = io.ReadFull(r, b[:2]); err != nil {
return read, err
}
readCh += l
read += l
}
cmpMethod := CompressionMethod(readUint16(b, 0))
if Debug != nil {
Debug.Printf(" layer #%d %q channel #%d image data", i, lci.Layer.Name, j.ChIndex)
Debug.Println(" length:", j.DataLen, "compression method:", cmpMethod)
reportReaderPosition(" file offset: 0x%08x", r)
}
var rect image.Rectangle
switch j.ChIndex {
case -3:
rect = lci.Layer.Mask.RealRect
case -2:
rect = lci.Layer.Mask.Rect
default:
rect = lci.Layer.Rect
}
data := make([]byte, (rect.Dx()*cfg.Depth+7)>>3*rect.Dy())
pk := findGrayPicker(cfg.Depth)
pk.setSource(rect, data)
if j.DataLen > 2 {
if l, err = cmpMethod.Decode(data, r, int64(j.DataLen-2), rect, cfg.Depth, 1, cfg.PSB()); err != nil {
return read, err
}
readCh += l
read += l
}
// It seems we can discard in this case
if readCh < j.DataLen {
if l, err = discard(r, j.DataLen-readCh); err != nil {
return read, err
}
readCh += l
read += l
}
if readCh != j.DataLen {
return read, errors.New("psd: layer: " + itoa(i) + " channel: " + itoa(j.ChIndex) + " read size mismatched. expected " + itoa(j.DataLen) + " actual " + itoa(readCh))
}
chMap[j.ChIndex] = Channel{
Data: data,
Picker: pk,
}
switch {
case j.ChIndex >= 0:
pickerChs[j.ChIndex] = data
case j.ChIndex == -1:
pickerChs = append(pickerChs, data)
}
}
pk := findPicker(cfg.Depth, cfg.ColorMode, cfg.ColorMode.Channels() < len(pickerChs))
pk.setSource(lci.Layer.Rect, pickerChs...)
lci.Layer.Picker = pk
lci.Layer.Channel = chMap
if o.LayerImageLoaded != nil {
o.LayerImageLoaded(lci.Layer, i, len(lcis))
}
}
return read, nil
}
func readLayerExtraData(r io.Reader, layer *Layer, cfg *Config, o *DecodeOptions) (read int, err error) {
// http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_22582
// Layer mask / adjustment layer data
if Debug != nil {
Debug.Println("start - layer extra data section")
}
b := make([]byte, 16)
var l int
if l, err = io.ReadFull(r, b[:4]); err != nil {
return 0, err
}
read += l
extraDataLen := int(readUint32(b, 0))
if Debug != nil {
Debug.Println(" extraDataLen:", extraDataLen)
}
if extraDataLen == 0 {
return read, err
}
l, err = readLayerMaskAndAdjustmentLayerData(r, layer, cfg, o)
read += l
if err != nil {
return read, err
}
// http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_21332
// Layer blending ranges data
if l, err = io.ReadFull(r, b[:4]); err != nil {
return read, err
}
read += l
if blendingRangesLen := int(readUint32(b, 0)); blendingRangesLen > 0 {
if Debug != nil {
Debug.Println(" layer blending ranges data skipped:", blendingRangesLen)
}
// TODO(oov): implement
if l, err = discard(r, blendingRangesLen); err != nil {
return read, err
}
read += l
}
// Layer name: Pascal string, padded to a multiple of 4 bytes.
if layer.MBCSName, l, err = readPascalString(r); err != nil {
return read, err
}
read += l
if l, err = adjustAlign4(r, l); err != nil {
return read, err
}
read += l
if read < extraDataLen+4 {
var layers []Layer
if layer.AdditionalLayerInfo, layers, l, err = readAdditionalLayerInfo(r, extraDataLen+4-read, cfg, o); err != nil {
return read, err
}
read += l
if len(layers) > 0 {
return read, errors.New("psd: unexpected layer structure")
}
} else {
layer.AdditionalLayerInfo = map[AdditionalInfoKey][]byte{}
}
if extraDataLen+4 != read {
return read, errors.New("psd: layer extra info read size mismatched. expected " + itoa(extraDataLen+4) + " actual " + itoa(read))
}
if Debug != nil {
Debug.Println("end - layer extra data section")
}
return read, nil
}
func readLayerMaskAndAdjustmentLayerData(r io.Reader, layer *Layer, cfg *Config, o *DecodeOptions) (read int, err error) {
// Layer mask / adjustment layer data
// Can be 40 bytes, 24 bytes, or 4 bytes if no layer mask.
// http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_22582
var l int
b := make([]byte, 16)
if l, err = io.ReadFull(r, b[:4]); err != nil {
return read, err
}
read += l
maskLen := int(readUint32(b, 0))
if maskLen == 0 {
// no layer mask
return read, nil
}
var readMask int
if maskLen-readMask < 16 {
// "Rectangle enclosing layer mask" is not present.
if maskLen-readMask > 0 {
l, err = discard(r, maskLen-readMask)
read += l
if err != nil {
return read, err
}
}
return read, nil
}
if l, err = io.ReadFull(r, b[:16]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.Rect = image.Rect(
int(int32(readUint32(b, 4))), int(int32(readUint32(b, 0))),
int(int32(readUint32(b, 12))), int(int32(readUint32(b, 8))),
)
if maskLen-readMask < 1 {
// "Default color" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.DefaultColor = int(b[0])
// bit 0 = position relative to layer
// bit 1 = layer mask disabled
// bit 2 = invert layer mask when blending (Obsolete)
// bit 3 = indicates that the user mask actually came from rendering other data
// bit 4 = indicates that the user and/or vector masks have parameters applied to them
if maskLen-readMask < 1 {
// "Flags" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.Flags = int(b[0])
layer.Mask.UserMaskDensity = 255
layer.Mask.VectorMaskDensity = 255
// Mask Parameters. Only present if bit 4 of Flags set above.
if layer.Mask.Flags&16 != 0 {
if maskLen-readMask < 1 {
// "Mask Parameters" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
// Mask Parameters bit flags present as follows:
// bit 0 = user mask density, 1 byte
// bit 1 = user mask feather, 8 byte, double
// bit 2 = vector mask density, 1 byte
// bit 3 = vector mask feather, 8 bytes, double
maskParam := int(b[0])
if maskParam&1 != 0 {
if maskLen-readMask < 1 {
// "user mask density" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.UserMaskDensity = int(b[0])
}
if maskParam&2 != 0 {
if maskLen-readMask < 8 {
// "user mask feather" is not present.
if maskLen-readMask > 0 {
l, err = discard(r, maskLen-readMask)
read += l
if err != nil {
return read, err
}
}
return read, nil
}
if l, err = io.ReadFull(r, b[:8]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.UserMaskFeather = readFloat64(b, 0)
}
if maskParam&4 != 0 {
if maskLen-readMask < 1 {
// "vector mask density" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.VectorMaskDensity = int(b[0])
}
if maskParam&8 != 0 {
if maskLen-readMask < 8 {
// "vector mask feather" is not present.
if maskLen-readMask > 0 {
l, err = discard(r, maskLen-readMask)
read += l
if err != nil {
return read, err
}
}
return read, nil
}
if l, err = io.ReadFull(r, b[:8]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.VectorMaskFeather = readFloat64(b, 0)
}
}
if maskLen == 20 {
// Padding. Only present if size = 20.
if readMask < maskLen && maskLen-readMask <= 2 {
if l, err = io.ReadFull(r, b[:maskLen-readMask]); err != nil {
return read, err
}
read += l
readMask += l
}
return read, nil
}
if maskLen-readMask < 1 {
// "Real Flags" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.RealFlags = int(b[0])
if maskLen-readMask < 1 {
// "Real user mask background" is not present.
return read, nil
}
if l, err = io.ReadFull(r, b[:1]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.RealBackgroundColor = int(b[0])
if maskLen-readMask < 16 {
// "Rectangle enclosing layer mask" is not present.
if maskLen-readMask > 0 {
l, err = discard(r, maskLen-readMask)
read += l
if err != nil {
return read, err
}
}
return read, nil
}
if l, err = io.ReadFull(r, b[:16]); err != nil {
return read, err
}
read += l
readMask += l
layer.Mask.RealRect = image.Rect(
int(readUint32(b, 4)), int(readUint32(b, 0)),
int(readUint32(b, 12)), int(readUint32(b, 8)),
)
return read, nil
}
func readAdditionalLayerInfo(r io.Reader, infoLen int, cfg *Config, o *DecodeOptions) (infos map[AdditionalInfoKey][]byte, layers []Layer, read int, err error) {
if Debug != nil {
Debug.Println("start - additional layer info section")
Debug.Println(" infoLen:", infoLen)
}
infos = map[AdditionalInfoKey][]byte{}
b := make([]byte, 8)
var l int
for read < infoLen {
// Sometimes I encountered padded section and unpadded section,
// so find the signature to avoid failure.
b[0] = 0
for read < infoLen && b[0] != '8' {
if l, err = io.ReadFull(r, b[:1]); err != nil {
return nil, nil, read, err
}
read += l
}
if b[0] != '8' {
break
}
if l, err = io.ReadFull(r, b[1:]); err != nil {
return nil, nil, read, err
}
read += l
if sig := string(b[:4]); sig != sectionSignature && sig != "8B64" {
return nil, nil, read, errors.New("psd: unexpected the signature found in the additional layer information block")
}
key := AdditionalInfoKey(b[4:])
switch key {
case AdditionalInfoKeyLayerInfo,
AdditionalInfoKeyLayerInfo16,
AdditionalInfoKeyLayerInfo32:
if Debug != nil {
Debug.Println(" key:", key)
}
var layrs []Layer
if layrs, l, err = readLayerInfo(r, cfg, o); err != nil {
return nil, nil, read, err
}
read += l
layers = append(layers, layrs...)
default:
intSize := key.LenSize(cfg.PSB())
if l, err = io.ReadFull(r, b[:intSize]); err != nil {
return nil, nil, read, err
}
read += l
var data []byte
if ln := int(readUint(b, 0, intSize)); ln > 0 {
data = make([]byte, ln)
if l, err = io.ReadFull(r, data); err != nil {
return nil, nil, read, err
}
read += l
}
infos[key] = data
if Debug != nil {
Debug.Println(" key:", key, "dataLen:", len(data))
}
}
}
if infoLen != read {
return nil, nil, read, errors.New("psd: additional layer info read size mismatched. expected " + itoa(infoLen) + " actual " + itoa(read))
}
if Debug != nil {
Debug.Println("end - additional layer info section")
}
return infos, layers, read, nil
}