-
Notifications
You must be signed in to change notification settings - Fork 122
/
GR32.ImageFormats.PSD.pas
939 lines (788 loc) · 26.9 KB
/
GR32.ImageFormats.PSD.pas
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
unit GR32.ImageFormats.PSD;
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1 or LGPL 2.1 with linking exception
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the terms of the
* Free Pascal modified version of the GNU Lesser General Public License
* Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
* of this license are applicable instead of those above.
* Please see the file LICENSE.txt for additional information concerning this
* license.
*
* The Original Code is PSD Image Format support for Graphics32
*
* The Initial Developer of the Original Code is
* Lamdalili
*
* Portions created by the Initial Developer are Copyright (C) 2023
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Anders Melander <anders@melander.dk>
*
* ***** END LICENSE BLOCK ***** *)
// WEAKPACKAGEUNIT so we can include the unit in the GR32 design time
// package in order to have the design time editor support the various formats.
{$WEAKPACKAGEUNIT ON}
interface
{$I GR32.inc}
uses
Generics.Collections,
Classes,
SysUtils,
GR32,
GR32_Image;
//------------------------------------------------------------------------------
//
// PSD simple types
//
//------------------------------------------------------------------------------
type
TPSDLayerBlendMode = (
lbmPass,
lbmNormal,
lbmDarken,
lbmLighten,
lbmHue,
lbmSaturation,
lbmColor,
lbmLuminosity,
lbmMultiply,
lbmScreen,
lbmDissolve,
lbmOverlay,
lbmHardLight,
lbmSoftLight,
lbmDifference,
lbmExclusion,
lbmColorDodge,
lbmColorBurn,
lbmLinearLight,
lbmLinearBurn,
lbmDarkerColor,
lbmLinearDodge,
lbmPinLight,
lbmVividLight,
lbmHardMix,
lbmLighterColor,
lbmSubtract,
lbmDivide
);
TPSDLayerCompression = (
lcRAW,
lcRLE,
lcZIP,
lcPredictedZIP
);
TPSDLayerOptions = set of (
loTransparencyProtected,
loHidden,
loIrrelevantData,
loFlag3,
loFlag4
);
type
EPhotoshopDocument = class(Exception);
//------------------------------------------------------------------------------
//
// TCustomPhotoshopLayer
//
//------------------------------------------------------------------------------
// Represents a single PSD layer
//------------------------------------------------------------------------------
type
TPhotoshopDocument = class;
TCustomPhotoshopLayer = class abstract
private
FDocument: TPhotoshopDocument;
FTop: integer;
FLeft: integer;
FHeight: integer;
FWidth: integer;
FName: string;
FBlendMode: TPSDLayerBlendMode;
FOpacity: Byte;
FOptions: TPSDLayerOptions;
FClipping: boolean;
FCompression: TPSDLayerCompression;
FUseDocumentCompression: boolean;
protected
procedure SetDocument(const Value: TPhotoshopDocument);
function GetIndex: integer;
procedure SetIndex(const Value: integer);
function GetBoundsRect: TRect;
procedure SetBoundsRect(const Value: TRect);
procedure SetCompression(const Value: TPSDLayerCompression);
function GetCompression: TPSDLayerCompression;
procedure SetUseDocumentCompression(const Value: boolean);
procedure GetChannelScanLine(AChannel: TColor32Component; ALine: integer; var Bytes); virtual; abstract;
function GetHeight: Integer; virtual;
function GetWidth: Integer; virtual;
procedure SetHeight(const Value: Integer); virtual;
procedure SetWidth(const Value: Integer); virtual;
public
constructor Create(ADocument: TPhotoshopDocument = nil); virtual;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight :integer);
procedure BeginScan; virtual;
procedure EndScan; virtual;
property Document: TPhotoshopDocument read FDocument write SetDocument;
property Index: integer read GetIndex write SetIndex;
property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
property Top: integer read FTop write FTop;
property Left: integer read FLeft write FLeft;
property Height: Integer read GetHeight write SetHeight;
property Width: Integer read GetWidth write SetWidth;
property Name: string read FName write FName;
property BlendMode: TPSDLayerBlendMode read FBlendMode write FBlendMode;
property Opacity: Byte read FOpacity write FOpacity;
property Options: TPSDLayerOptions read FOptions write FOptions;
property Clipping: boolean read FClipping write FClipping;
property Compression: TPSDLayerCompression read GetCompression write SetCompression;
property UseDocumentCompression: boolean read FUseDocumentCompression write SetUseDocumentCompression;
end;
TPhotoshopLayerClass = class of TCustomPhotoshopLayer;
//------------------------------------------------------------------------------
//
// TPhotoshopDocument
//
//------------------------------------------------------------------------------
// Represents a PSD document/file, (typically) containing one or more layers
//------------------------------------------------------------------------------
TPhotoshopDocument = class(TPersistent)
private type
TPhotoshopLayers = class
private
FDocument: TPhotoshopDocument;
FLayers: TObjectList<TCustomPhotoshopLayer>;
protected
function GetCount: integer;
function GetLayer(Index: integer): TCustomPhotoshopLayer;
procedure AddLayer(ALayer: TCustomPhotoshopLayer);
procedure RemoveLayer(ALayer: TCustomPhotoshopLayer);
function IndexOf(ALayer: TCustomPhotoshopLayer): integer;
procedure Move(OldIndex, NewIndex: integer);
public
constructor Create(ADocument: TPhotoshopDocument);
destructor Destroy; override;
function Add(ALayerClass: TPhotoshopLayerClass = nil): TCustomPhotoshopLayer;
procedure Clear;
property Count: integer read GetCount;
property Layers[Index: integer]: TCustomPhotoshopLayer read GetLayer; default;
end;
private
FLayers: TPhotoshopLayers;
FWidth: Integer;
FHeight: Integer;
FBackground: TCustomPhotoshopLayer;
FCompression: TPSDLayerCompression;
private
class var
FDefaultLayerClass: TPhotoshopLayerClass;
FDefaultCompression: TPSDLayerCompression;
protected
procedure SetBackground(const Value: TCustomPhotoshopLayer);
procedure SetCompression(const Value: TPSDLayerCompression);
procedure AddLayer(ALayer: TCustomPhotoshopLayer);
procedure RemoveLayer(ALayer: TCustomPhotoshopLayer);
public
constructor Create(ABackground: TCustomPhotoshopLayer = nil);
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure SetSize(AWidth, AHeight: Integer);
property Width: Integer read FWidth write FWidth;
property Height: Integer read FHeight write FHeight;
// Layers: The individual PSD layers
property Layers: TPhotoshopLayers read FLayers;
// Background: A composite of the flattened image.
// If the document contains no layers this is the primary image. Otherwise
// it is usually just used as a preview of the image. Applications that
// cannot handle layers will often just load this bitmap and ignore the
// layers, while application that does handle layers will ignore the
// background if the image contains layers. For this reason you should only
// omit the background if you are sure that the reader will not require it.
// If no background is specified then a fully transparent bitmap will be
// saved in its place.
property Background: TCustomPhotoshopLayer read FBackground write SetBackground;
// Default background and layer compression. Initialized to the
// value of DefaultCompression.
property Compression: TPSDLayerCompression read FCompression write SetCompression;
// DefaultLayerClass: The type of layer create when calling Layers.Add
// with no layer type specified.
class property DefaultLayerClass: TPhotoshopLayerClass read FDefaultLayerClass write FDefaultLayerClass;
// DefaultCompression: The default background and layer compression used
// if no explicit compression type is specified.
class property DefaultCompression: TPSDLayerCompression read FDefaultCompression write FDefaultCompression;
end;
//------------------------------------------------------------------------------
//
// TPhotoshopLayer32
//
//------------------------------------------------------------------------------
// Layer wrapping a TBitmap32
// Note that by default the layer only references the bitmap; It doesn't own it.
//------------------------------------------------------------------------------
type
TPhotoshopLayer32 = class(TCustomPhotoshopLayer)
private
FBitmap: TCustomBitmap32;
FOwnsBitmap: boolean;
FSourceTop: integer;
FSourceLeft: integer;
protected
procedure GetChannelScanLine(AChannel: TColor32Component; ALine: integer; var Bytes); override;
function GetHeight: Integer; override;
function GetWidth: Integer; override;
procedure SetBitmap(const Value: TCustomBitmap32);
function GetSourceRect: TRect;
procedure SetSourceRect(const Value: TRect);
public
destructor Destroy; override;
property Bitmap: TCustomBitmap32 read FBitmap write SetBitmap;
// OwnsBitmap: Specifies if the layers owns the bit referenced by
// the Bitmap property. Default: False
property OwnsBitmap: boolean read FOwnsBitmap write FOwnsBitmap;
// SourceRect: The area of the bitmap used to produce the layer bitmap.
// By default the whole bitmap is used, but SourceRect can be used to
// only use a section of it.
property SourceRect: TRect read GetSourceRect write SetSourceRect;
end;
//------------------------------------------------------------------------------
//
// Construct a TPhotoshopDocument from a TCustomImage32
//
//------------------------------------------------------------------------------
// The function produces a PSD where the background is the composite of the
// TCustomImage32 and its layers (i.e. a flattened view of the image) and one
// PSD layer for each bitmap layer in the TCustomImage32.
//------------------------------------------------------------------------------
procedure CreatePhotoshopDocument(AImage: TCustomImage32; ADocument: TPhotoshopDocument); overload;
//------------------------------------------------------------------------------
//
// Construct a TPhotoshopDocument from a TBitmap32
//
//------------------------------------------------------------------------------
// The function produces a PSD with no layers but with a background based on the
// bitmap.
//------------------------------------------------------------------------------
procedure CreatePhotoshopDocument(ABitmap: TCustomBitmap32; ADocument: TPhotoshopDocument); overload;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
Math,
GR32_Layers,
GR32_Backends_Generic,
GR32.ImageFormats,
GR32.ImageFormats.PSD.Writer;
const
PsdSignature: AnsiString = '8BPS'#00#01;
PsdSignatureMask: AnsiString = #$ff#$ff#$ff#$ff#$ff#$ff;
//------------------------------------------------------------------------------
//
// TImageFormatAdapterPSD
//
//------------------------------------------------------------------------------
// Implements IImageFormatAdapter for the PSD image format using
// TPhotoshopDocument.
//------------------------------------------------------------------------------
type
TImageFormatAdapterPSD = class(TCustomImageFormatAdapter,
IImageFormatAdapter,
IImageFormatFileInfo,
IImageFormatWriter)
strict protected
// IImageFormatAdapter
function CanAssignFrom(Source: TPersistent): boolean; override;
function AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean; override;
function CanAssignTo(Dest: TPersistent): boolean; override;
function AssignTo(Source: TCustomBitmap32; Dest: TPersistent): boolean; override;
private
// IImageFormatFileInfo
function ImageFormatDescription: string;
function ImageFormatFileTypes: TFileTypes;
private
// IImageFormatWriter
procedure SaveToStream(ASource: TCustomBitmap32; AStream: TStream);
end;
//------------------------------------------------------------------------------
// IImageFormatAdapter
//------------------------------------------------------------------------------
function TImageFormatAdapterPSD.CanAssignFrom(Source: TPersistent): boolean;
begin
Result := False;
end;
function TImageFormatAdapterPSD.AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean;
begin
Result := inherited;
end;
//------------------------------------------------------------------------------
function TImageFormatAdapterPSD.CanAssignTo(Dest: TPersistent): boolean;
begin
Result := (Dest is TPhotoshopDocument);
end;
function TImageFormatAdapterPSD.AssignTo(Source: TCustomBitmap32; Dest: TPersistent): boolean;
begin
if (Dest is TPhotoshopDocument) then
begin
CreatePhotoshopDocument(Source, TPhotoshopDocument(Dest));
Result := True;
end else
Result := inherited;
end;
//------------------------------------------------------------------------------
// IImageFormatFileInfo
//------------------------------------------------------------------------------
function TImageFormatAdapterPSD.ImageFormatFileTypes: TFileTypes;
begin
Result := ['psd'];
end;
resourcestring
sImageFormatPSDName = 'PSD images';
function TImageFormatAdapterPSD.ImageFormatDescription: string;
begin
Result := sImageFormatPSDName;
end;
//------------------------------------------------------------------------------
// IImageFormatWriter
//------------------------------------------------------------------------------
procedure TImageFormatAdapterPSD.SaveToStream(ASource: TCustomBitmap32; AStream: TStream);
var
PSD: TPhotoshopDocument;
begin
PSD := TPhotoshopDocument.Create;
try
CreatePhotoshopDocument(ASource, PSD);
TPhotoshopDocumentWriter.SaveToStream(PSD, AStream);
finally
PSD.Free;
end;
end;
//------------------------------------------------------------------------------
//
// Construct a TPhotoshopDocument from a TCustomImage32
//
//------------------------------------------------------------------------------
type
TBitmapLayerCracker = class(TCustomIndirectBitmapLayer);
procedure CreatePhotoshopDocument(AImage: TCustomImage32; ADocument: TPhotoshopDocument);
var
PSDLayer: TCustomPhotoshopLayer;
i: integer;
SourceLayer: TCustomLayer;
BackgroundBitmap: TBitmap32;
Location: TFloatRect;
LayerBitmap: TCustomBitmap32;
begin
ADocument.Clear;
if AImage.Bitmap.Empty then
Exit;
BackgroundBitmap := TBitmap32.Create(TMemoryBackend);
try
// Create flattened bitmap for use as background
BackgroundBitmap.SetSizeFrom(AImage.Bitmap);
AImage.PaintTo(BackgroundBitmap, BackgroundBitmap.BoundsRect);
PSDLayer := TPhotoshopLayer32.Create;
try
TPhotoshopLayer32(PSDLayer).Bitmap := BackgroundBitmap;
// We need to keep the bitmap alive when this function
// returns so transfer ownership to the layer.
TPhotoshopLayer32(PSDLayer).OwnsBitmap := True;
BackgroundBitmap := nil;
ADocument.Background := PSDLayer; // Document now owns the layer
except
PSDLayer.Free;
raise;
end;
except
BackgroundBitmap.Free;
raise;
end;
for i := 0 to AImage.Layers.Count -1 do
begin
SourceLayer := AImage.Layers[i];
if not (SourceLayer is TCustomIndirectBitmapLayer) then
continue;
LayerBitmap := TBitmapLayerCracker(SourceLayer).Bitmap;
Location := TBitmapLayerCracker(SourceLayer).Location;
PSDLayer := ADocument.Layers.Add(TPhotoshopLayer32);
PSDLayer.Opacity := LayerBitmap.MasterAlpha;
PSDLayer.Left := Round(Location.Left);
PSDLayer.Top := Round(Location.Top);
// Layer just references the bitmap; It doesn't own it.
TPhotoshopLayer32(PSDLayer).Bitmap := LayerBitmap;
if (not SourceLayer.Visible) then
PSDLayer.Options := PSDLayer.Options + [loHidden];
PSDLayer.Name := Format('Layer %d', [i+1]);
end;
end;
//------------------------------------------------------------------------------
//
// Construct a TPhotoshopDocument from a TBitmap32
//
//------------------------------------------------------------------------------
procedure CreatePhotoshopDocument(ABitmap: TCustomBitmap32; ADocument: TPhotoshopDocument); overload;
var
PSDLayer: TCustomPhotoshopLayer;
begin
ADocument.Clear;
if ABitmap.Empty then
Exit;
PSDLayer := TPhotoshopLayer32.Create;
try
TPhotoshopLayer32(PSDLayer).Bitmap := ABitmap;
ADocument.Background := PSDLayer; // Document now owns the layer
except
PSDLayer.Free;
raise;
end;
end;
//------------------------------------------------------------------------------
//
// TCustomPhotoshopLayer
//
//------------------------------------------------------------------------------
constructor TCustomPhotoshopLayer.Create(ADocument: TPhotoshopDocument);
begin
inherited Create;
FBlendMode := lbmNormal;
FOpacity := $FF;
FUseDocumentCompression := True;
SetDocument(ADocument);
end;
destructor TCustomPhotoshopLayer.Destroy;
begin
if (FDocument <> nil) and (FDocument.Background = Self) then
FDocument.FBackground := nil; // Do not go through setter
SetDocument(nil);
inherited;
end;
procedure TCustomPhotoshopLayer.BeginScan;
begin
end;
procedure TCustomPhotoshopLayer.EndScan;
begin
end;
function TCustomPhotoshopLayer.GetBoundsRect: TRect;
begin
Result := Rect(Left, Top, Left+Width, Top+Height);
end;
function TCustomPhotoshopLayer.GetCompression: TPSDLayerCompression;
begin
if (FUseDocumentCompression) and (FDocument <> nil) then
Result := FDocument.Compression
else
Result := FCompression;
end;
function TCustomPhotoshopLayer.GetIndex: integer;
begin
if (FDocument <> nil) and (FDocument.Background <> Self) then
Result := FDocument.FLayers.IndexOf(Self)
else
Result := -1;
end;
procedure TCustomPhotoshopLayer.SetBounds(ALeft, ATop, AWidth, AHeight: integer);
begin
Left := ALeft;
Top := ATop;
Width := AWidth;
Height := AHeight;
end;
procedure TCustomPhotoshopLayer.SetBoundsRect(const Value: TRect);
begin
SetBounds(Value.Left, Value.Top, Value.Width, Value.Height);
end;
function TCustomPhotoshopLayer.GetHeight: Integer;
begin
Result := FHeight;
end;
function TCustomPhotoshopLayer.GetWidth: Integer;
begin
Result := FWidth;
end;
procedure TCustomPhotoshopLayer.SetHeight(const Value: Integer);
begin
FHeight := Value;
end;
procedure TCustomPhotoshopLayer.SetWidth(const Value: Integer);
begin
FWidth := Value;
end;
procedure TCustomPhotoshopLayer.SetCompression(const Value: TPSDLayerCompression);
begin
if (Value = lcPredictedZIP) then
raise EPhotoshopDocument.Create('"ZIP with prediction"-compression is not implemented');
FCompression := Value;
FUseDocumentCompression := False;
end;
procedure TCustomPhotoshopLayer.SetDocument(const Value: TPhotoshopDocument);
begin
if (FDocument = Value) then
exit;
if (FDocument <> nil) then
FDocument.RemoveLayer(Self);
FDocument := Value;
if (FDocument <> nil) then
FDocument.AddLayer(Self);
end;
procedure TCustomPhotoshopLayer.SetIndex(const Value: integer);
begin
if (Value <> Index) and (FDocument <> nil) then
begin
if (FDocument.Background = Self) then
raise EPhotoshopDocument.Create('Cannot set the index of the background layer');
FDocument.FLayers.Move(Index, Value);
end;
end;
procedure TCustomPhotoshopLayer.SetUseDocumentCompression(const Value: boolean);
begin
FUseDocumentCompression := Value;
end;
//------------------------------------------------------------------------------
//
// TPhotoshopDocument.TPhotoshopLayers
//
//------------------------------------------------------------------------------
constructor TPhotoshopDocument.TPhotoshopLayers.Create(ADocument: TPhotoshopDocument);
begin
inherited Create;
FDocument := ADocument;
FLayers := TObjectList<TCustomPhotoshopLayer>.Create;
end;
destructor TPhotoshopDocument.TPhotoshopLayers.Destroy;
begin
FLayers.Free;
inherited;
end;
procedure TPhotoshopDocument.TPhotoshopLayers.Clear;
begin
FLayers.Clear;
end;
procedure TPhotoshopDocument.TPhotoshopLayers.AddLayer(ALayer: TCustomPhotoshopLayer);
begin
if (not FLayers.Contains(ALayer)) then
FLayers.Add(ALayer);
end;
procedure TPhotoshopDocument.TPhotoshopLayers.RemoveLayer(ALayer: TCustomPhotoshopLayer);
begin
FLayers.Extract(ALayer);
end;
function TPhotoshopDocument.TPhotoshopLayers.Add(ALayerClass: TPhotoshopLayerClass): TCustomPhotoshopLayer;
begin
if (ALayerClass = nil) then
ALayerClass := FDocument.DefaultLayerClass;
// Layer.Create->Layer.SetDocument->Document.AddLayer->Document.Layers.AddLayer
Result := ALayerClass.Create(FDocument);
end;
function TPhotoshopDocument.TPhotoshopLayers.GetCount: integer;
begin
Result := FLayers.Count;
end;
function TPhotoshopDocument.TPhotoshopLayers.GetLayer(Index: integer): TCustomPhotoshopLayer;
begin
Result := FLayers[Index];
end;
function TPhotoshopDocument.TPhotoshopLayers.IndexOf(ALayer: TCustomPhotoshopLayer): integer;
begin
Result := FLayers.IndexOf(ALayer);
end;
procedure TPhotoshopDocument.TPhotoshopLayers.Move(OldIndex, NewIndex: integer);
begin
FLayers.Move(OldIndex, NewIndex);
end;
//------------------------------------------------------------------------------
//
// TPhotoshopDocument
//
//------------------------------------------------------------------------------
constructor TPhotoshopDocument.Create(ABackground: TCustomPhotoshopLayer);
begin
inherited Create;
FLayers := TPhotoshopLayers.Create(Self);
FCompression := FDefaultCompression;
FBackground := ABackground;
end;
destructor TPhotoshopDocument.Destroy;
begin
FBackground.Free;
FLayers.Free;
inherited;
end;
procedure TPhotoshopDocument.Assign(Source: TPersistent);
begin
if (Source is TCustomImage32) then
CreatePhotoshopDocument(TCustomImage32(Source), Self)
else
inherited;
end;
procedure TPhotoshopDocument.Clear;
begin
SetBackground(nil);
FLayers.Clear;
SetSize(0, 0);
end;
procedure TPhotoshopDocument.AddLayer(ALayer: TCustomPhotoshopLayer);
begin
if (ALayer.Document = Self) and (ALayer <> Background) then
FLayers.AddLayer(ALayer);
end;
procedure TPhotoshopDocument.RemoveLayer(ALayer: TCustomPhotoshopLayer);
begin
if (ALayer.Document = Self) then
begin
if (ALayer <> Background) then
FLayers.RemoveLayer(ALayer)
else
FBackground := nil;
end;
end;
procedure TPhotoshopDocument.SetBackground(const Value: TCustomPhotoshopLayer);
begin
if (FBackground = Value) then
Exit;
FBackground.Free;
FBackground := Value;
if FBackground <> nil then
begin
// In case layer is already in layer list this extracts it...
FBackground.Document := nil;
// ...and reattaches it without adding it to the layer list
FBackground.Document := Self;
FWidth := FBackground.Width;
FHeight := FBackground.Height;
end;
end;
procedure TPhotoshopDocument.SetCompression(const Value: TPSDLayerCompression);
begin
if (Value = lcPredictedZIP) then
raise EPhotoshopDocument.Create('"ZIP with prediction"-compression is not implemented');
FCompression := Value;
end;
procedure TPhotoshopDocument.SetSize(AWidth, AHeight: Integer);
begin
Width := AWidth;
Height := AHeight;
end;
//------------------------------------------------------------------------------
//
// TPhotoshopLayer32
//
//------------------------------------------------------------------------------
destructor TPhotoshopLayer32.Destroy;
begin
if (FOwnsBitmap) then
FBitmap.Free;
inherited;
end;
procedure TPhotoshopLayer32.SetBitmap(const Value: TCustomBitmap32);
begin
if (FOwnsBitmap) and (FBitmap <> nil) then
FBitmap.Free;
FBitmap := Value;
FSourceTop := 0;
FSourceLeft := 0;
if (FBitmap <> nil) then
begin
Height := FBitmap.Height;
Width := FBitmap.Width;
end else
begin
Height := 0;
Width := 0;
end;
end;
procedure TPhotoshopLayer32.GetChannelScanLine(AChannel: TColor32Component; ALine: integer; var Bytes);
var
Count: integer;
pDest: PByte;
pSource: PByte;
begin
if (Width = 0) or (Height = 0) then
Exit;
if (Bitmap = nil) then
begin
FillChar(Bytes, Width, $FF);
Exit;
end;
pDest := @Bytes;
pSource := @(PColor32Entry(Bitmap.ScanLine[ALine + FSourceTop]).Components[AChannel]);
Inc(pSource, FSourceLeft * SizeOf(TColor32));
Count := Width;
while (Count > 0) do
begin
pDest^ := pSource^;
Inc(pDest);
Inc(pSource, SizeOf(TColor32));
Dec(Count);
end;
end;
function TPhotoshopLayer32.GetSourceRect: TRect;
begin
if (FBitmap <> nil) then
begin
Result.Top := Min(FSourceTop, FBitmap.Height);
Result.Left := Min(FSourceLeft, FBitmap.Width);
end else
begin
Result.Top := 0;
Result.Left := 0;
end;
Result.Width := Width;
Result.Height := Height;
end;
function TPhotoshopLayer32.GetHeight: Integer;
begin
// Size of bitmap can have changed since assignment
// so we need to reevaluate the size.
if (FBitmap <> nil) then
Result := Min(inherited GetHeight, Max(0, FBitmap.Height - FSourceTop))
else
Result := 0;
end;
function TPhotoshopLayer32.GetWidth: Integer;
begin
// Size of bitmap can have changed since assignment
// so we need to reevaluate the size.
if (FBitmap <> nil) then
Result := Min(inherited GetWidth, Max(0, FBitmap.Width - FSourceLeft))
else
Result := 0;
end;
procedure TPhotoshopLayer32.SetSourceRect(const Value: TRect);
var
SourceRect: TRect;
begin
if (FBitmap <> nil) then
IntersectRect(SourceRect, Value, FBitmap.BoundsRect)
else
begin
SourceRect.Top := Max(0, Value.Top);
SourceRect.Left := Max(0, Value.Left);
SourceRect.Bottom := Max(SourceRect.Bottom, Value.Top);
SourceRect.Right := Max(SourceRect.Right, Value.Left);
end;
FSourceTop := SourceRect.Top;
FSourceLeft := SourceRect.Left;
Width := SourceRect.Width;
Height := SourceRect.Height;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
initialization
TPhotoshopDocument.DefaultLayerClass := TPhotoshopLayer32;
TPhotoshopDocument.DefaultCompression := lcRLE;
ImageFormatManager.RegisterImageFormat(TImageFormatAdapterPSD.Create, ImageFormatPriorityNormal);
end.