forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbgralayeroriginal.pas
More file actions
851 lines (751 loc) · 26.2 KB
/
bgralayeroriginal.pas
File metadata and controls
851 lines (751 loc) · 26.2 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
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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRALayerOriginal;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, Types, BGRATypes, {$IFNDEF FPC}GraphType, {$ENDIF} BGRAGraphics, BGRABitmap, BGRABitmapTypes, BGRATransform, BGRAMemDirectory;
type
//TAffineMatrix = BGRATransform.TAffineMatrix;
TBGRALayerCustomOriginal = class;
TBGRALayerOriginalAny = class of TBGRALayerCustomOriginal;
TOriginalMovePointEvent = procedure(ASender: TObject; APrevCoord, ANewCoord: TPointF) of object;
TOriginalChangeEvent = procedure(ASender: TObject) of object;
TOriginalEditorCursor = (oecDefault, oecMove);
{ TBGRAOriginalEditor }
TBGRAOriginalEditor = class
protected
FMatrix: TAffineMatrix;
FPoints: array of record
Origin, Coord: TPointF;
OnMove: TOriginalMovePointEvent;
RightButton: boolean;
SnapToPoint: integer;
end;
FPointSize: single;
FPointMoving: integer;
FPointCoordDelta: TPointF;
FMovingRightButton: boolean;
FPrevMousePos: TPointF;
function RenderPoint(ADest: TBGRABitmap; ACoord: TPointF): TRect; virtual;
function RenderArrow(ADest: TBGRABitmap; AOrigin, AEndCoord: TPointF): TRect; virtual;
procedure SetMatrix(AValue: TAffineMatrix);
public
constructor Create;
procedure Clear;
function AddPoint(ACoord: TPointF; AOnMove: TOriginalMovePointEvent; ARightButton: boolean = false; ASnapToPoint: integer = -1): integer;
procedure AddArrow(AOrigin, AEndCoord: TPointF; AOnMoveEnd: TOriginalMovePointEvent; ARightButton: boolean = false);
procedure MouseMove(Shift: TShiftState; X, Y: single; out ACursor: TOriginalEditorCursor); virtual;
procedure MouseDown(RightButton: boolean; Shift: TShiftState; X, Y: single; out ACursor: TOriginalEditorCursor); virtual;
procedure MouseUp(RightButton: boolean; Shift: TShiftState; X, Y: single; out ACursor: TOriginalEditorCursor); virtual;
function GetPointAt(ACoord: TPointF; ARightButton: boolean): integer;
function Render(ADest: TBGRABitmap): TRect; virtual;
property Matrix: TAffineMatrix read FMatrix write SetMatrix;
property PointSize: single read FPointSize write FPointSize;
end;
TBGRACustomOriginalStorage = class;
{ TBGRALayerCustomOriginal }
TBGRALayerCustomOriginal = class
private
FOnChange: TOriginalChangeEvent;
procedure SetOnChange(AValue: TOriginalChangeEvent);
protected
FGuid: TGuid;
function GetGuid: TGuid;
procedure SetGuid(AValue: TGuid);
procedure NotifyChange;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean); virtual; abstract;
function GetRenderBounds(ADestRect: TRect; AMatrix: TAffineMatrix): TRect; virtual; abstract;
procedure ConfigureEditor({%H-}AEditor: TBGRAOriginalEditor); virtual;
procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); virtual; abstract;
procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); virtual; abstract;
procedure LoadFromFile(AFilenameUTF8: string); virtual;
procedure LoadFromStream(AStream: TStream); virtual;
procedure SaveToFile(AFilenameUTF8: string); virtual;
procedure SaveToStream(AStream: TStream); virtual;
function CreateEditor: TBGRAOriginalEditor; virtual;
class function StorageClassName: RawByteString; virtual; abstract;
property Guid: TGuid read GetGuid write SetGuid;
property OnChange: TOriginalChangeEvent read FOnChange write SetOnChange;
end;
{ TBGRALayerImageOriginal }
TBGRALayerImageOriginal = class(TBGRALayerCustomOriginal)
private
function GetImageHeight: integer;
function GetImageWidth: integer;
protected
FImage: TBGRABitmap;
FJpegStream: TMemoryStream;
FContentVersion: integer;
procedure ContentChanged;
public
constructor Create; override;
destructor Destroy; override;
procedure Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean); override;
function GetRenderBounds({%H-}ADestRect: TRect; AMatrix: TAffineMatrix): TRect; override;
procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); override;
procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); override;
procedure LoadFromStream(AStream: TStream); override;
procedure SaveToStream(AStream: TStream); override;
class function StorageClassName: RawByteString; override;
property Width: integer read GetImageWidth;
property Height: integer read GetImageHeight;
end;
{ TBGRACustomOriginalStorage }
TBGRACustomOriginalStorage = class
private
function GetBool(AName: utf8string): boolean;
function GetIntegerDef(AName: utf8string; ADefault: integer): integer;
function GetSingleDef(AName: utf8string; ADefault: single): single;
procedure SetBool(AName: utf8string; AValue: boolean);
protected
FFormats: TFormatSettings;
function GetColorArray(AName: UTF8String): ArrayOfTBGRAPixel;
function GetInteger(AName: utf8string): integer;
function GetPointF(AName: utf8string): TPointF;
function GetRawString(AName: utf8string): RawByteString; virtual; abstract;
function GetSingle(AName: utf8string): single;
function GetColor(AName: UTF8String): TBGRAPixel;
procedure SetColorArray(AName: UTF8String; AValue: ArrayOfTBGRAPixel);
procedure SetInteger(AName: utf8string; AValue: integer);
procedure SetPointF(AName: utf8string; AValue: TPointF);
procedure SetRawString(AName: utf8string; AValue: RawByteString); virtual; abstract;
procedure SetSingle(AName: utf8string; AValue: single);
procedure SetColor(AName: UTF8String; AValue: TBGRAPixel);
public
constructor Create;
procedure RemoveAttribute(AName: utf8string); virtual; abstract;
procedure RemoveFile(AName: utf8string); virtual; abstract;
function ReadFile(AName: UTF8String; ADest: TStream): boolean; virtual; abstract;
procedure WriteFile(AName: UTF8String; ASource: TStream; ACompress: boolean); virtual; abstract;
property RawString[AName: utf8string]: RawByteString read GetRawString write SetRawString;
property Int[AName: utf8string]: integer read GetInteger write SetInteger;
property IntDef[AName: utf8string; ADefault: integer]: integer read GetIntegerDef;
property Bool[AName: utf8string]: boolean read GetBool write SetBool;
property Float[AName: utf8string]: single read GetSingle write SetSingle;
property FloatDef[AName: utf8string; ADefault: single]: single read GetSingleDef;
property PointF[AName: utf8string]: TPointF read GetPointF write SetPointF;
property Color[AName: UTF8String]: TBGRAPixel read GetColor write SetColor;
property ColorArray[AName: UTF8String]: ArrayOfTBGRAPixel read GetColorArray write SetColorArray;
end;
{ TBGRAMemOriginalStorage }
TBGRAMemOriginalStorage = class(TBGRACustomOriginalStorage)
protected
FMemDir: TMemDirectory;
function GetRawString(AName: utf8string): RawByteString; override;
procedure SetRawString(AName: utf8string; AValue: RawByteString); override;
public
constructor Create(AMemDir: TMemDirectory);
procedure RemoveAttribute(AName: utf8string); override;
procedure RemoveFile(AName: utf8string); override;
function ReadFile(AName: UTF8String; ADest: TStream): boolean; override;
procedure WriteFile(AName: UTF8String; ASource: TStream; ACompress: boolean); override;
end;
procedure RegisterLayerOriginal(AClass: TBGRALayerOriginalAny);
function FindLayerOriginalClass(AStorageClassName: string): TBGRALayerOriginalAny;
implementation
uses BGRAPolygon, math, BGRAMultiFileType, BGRAUTF8;
var
LayerOriginalClasses: array of TBGRALayerOriginalAny;
procedure RegisterLayerOriginal(AClass: TBGRALayerOriginalAny);
begin
setlength(LayerOriginalClasses, length(LayerOriginalClasses)+1);
LayerOriginalClasses[high(LayerOriginalClasses)] := AClass;
end;
function FindLayerOriginalClass(AStorageClassName: string): TBGRALayerOriginalAny;
var
i: Integer;
begin
for i := 0 to high(LayerOriginalClasses) do
if LayerOriginalClasses[i].StorageClassName = AStorageClassName then
exit(LayerOriginalClasses[i]);
exit(nil);
end;
{ TBGRAOriginalEditor }
procedure TBGRAOriginalEditor.SetMatrix(AValue: TAffineMatrix);
begin
if AffMatrixEqual(FMatrix,{=}AValue) then Exit;
FMatrix:=AValue;
end;
function TBGRAOriginalEditor.RenderPoint(ADest: TBGRABitmap; ACoord: TPointF): TRect;
const alpha = 192;
var filler: TBGRAMultishapeFiller;
begin
if isEmptyPointF(ACoord) then
result := EmptyRect
else
begin
result := rect(floor(ACoord.x - FPointSize - 2), floor(ACoord.y - FPointSize - 2), ceil(ACoord.x + FPointSize + 3), ceil(ACoord.y + FPointSize + 3));
filler := TBGRAMultishapeFiller.Create;
filler.AddEllipseBorder(ACoord.x,ACoord.y, FPointSize-1.5,FPointSize-1.5, 4, BGRA(0,0,0,alpha));
filler.AddEllipseBorder(ACoord.x,ACoord.y, FPointSize-1.5,FPointSize-1.5, 1, BGRA(255,255,255,alpha));
filler.PolygonOrder:= poLastOnTop;
filler.Draw(ADest);
filler.Free;
end;
end;
function TBGRAOriginalEditor.RenderArrow(ADest: TBGRABitmap; AOrigin,
AEndCoord: TPointF): TRect;
const alpha = 192;
var
pts: ArrayOfTPointF;
i: Integer;
begin
if isEmptyPointF(AOrigin) or isEmptyPointF(AEndCoord) then
result := EmptyRect
else
begin
result := Rect(floor(AOrigin.x-1),floor(AOrigin.y-1),ceil(AOrigin.x+1),ceil(AOrigin.y+1));
ADest.Pen.Arrow.EndAsClassic;
ADest.Pen.Arrow.EndSize := PointF(FPointSize,FPointSize);
pts := ADest.ComputeWidePolyline([AOrigin,AEndCoord],1);
ADest.DrawPolygonAntialias(pts, BGRA(0,0,0,alpha),2);
ADest.FillPolyAntialias(pts, BGRA(255,255,255,alpha));
ADest.Pen.Arrow.EndAsNone;
for i := 0 to high(pts) do
if not isEmptyPointF(pts[i]) then
begin
if floor(pts[i].x - 1) < result.Left then result.Left := floor(pts[i].x - 1);
if ceil(pts[i].x + 1) > result.Right then result.Right := ceil(pts[i].x - 1);
if floor(pts[i].y - 1) < result.Top then result.Top := floor(pts[i].y - 1);
if ceil(pts[i].y + 1) > result.Bottom then result.Bottom := ceil(pts[i].y - 1);
end;
end;
end;
constructor TBGRAOriginalEditor.Create;
begin
FPointSize:= 6;
FMatrix := AffineMatrixIdentity;
FPointMoving:= -1;
end;
procedure TBGRAOriginalEditor.Clear;
begin
FPoints := nil;
end;
function TBGRAOriginalEditor.AddPoint(ACoord: TPointF;
AOnMove: TOriginalMovePointEvent; ARightButton: boolean; ASnapToPoint: integer): integer;
begin
setlength(FPoints, length(FPoints)+1);
result := High(FPoints);
with FPoints[result] do
begin
Origin := EmptyPointF;
Coord := ACoord;
OnMove := AOnMove;
RightButton:= ARightButton;
SnapToPoint:= ASnapToPoint;
end;
end;
procedure TBGRAOriginalEditor.AddArrow(AOrigin, AEndCoord: TPointF;
AOnMoveEnd: TOriginalMovePointEvent; ARightButton: boolean);
begin
setlength(FPoints, length(FPoints)+1);
with FPoints[High(FPoints)] do
begin
Origin := AOrigin;
Coord := AEndCoord;
OnMove := AOnMoveEnd;
RightButton:= ARightButton;
SnapToPoint:= -1;
end;
end;
procedure TBGRAOriginalEditor.MouseMove(Shift: TShiftState; X, Y: single; out
ACursor: TOriginalEditorCursor);
var newMousePos, newCoord, snapCoord: TPointF;
begin
newMousePos := AffMatrixMult(Matrix,PointF(X,Y));
if (FPointMoving <> -1) and (FPointMoving < length(FPoints)) then
begin
newCoord := newMousePos + FPointCoordDelta;
if FPoints[FPointMoving].SnapToPoint <> -1 then
begin
snapCoord := FPoints[FPoints[FPointMoving].SnapToPoint].Coord;
if VectLen(snapCoord - newMousePos) < FPointSize then
newCoord := snapCoord;
end;
FPoints[FPointMoving].OnMove(self, FPoints[FPointMoving].Coord, newCoord);
FPoints[FPointMoving].Coord := newCoord;
end;
FPrevMousePos:= newMousePos;
end;
procedure TBGRAOriginalEditor.MouseDown(RightButton: boolean;
Shift: TShiftState; X, Y: single; out ACursor: TOriginalEditorCursor);
begin
FPrevMousePos:= AffMatrixMult(Matrix,PointF(X,Y));
if FPointMoving = -1 then
begin
FPointMoving:= GetPointAt(AffMatrixMult(Matrix,PointF(X,Y)), RightButton);
if FPointMoving <> -1 then
begin
FMovingRightButton:= RightButton;
FPointCoordDelta := FPoints[FPointMoving].Coord - FPrevMousePos;
end;
end;
if FPointMoving <> -1 then
ACursor := oecMove
else
ACursor := oecDefault;
end;
procedure TBGRAOriginalEditor.MouseUp(RightButton: boolean; Shift: TShiftState;
X, Y: single; out ACursor: TOriginalEditorCursor);
begin
if RightButton = FMovingRightButton then
FPointMoving:= -1;
ACursor := oecDefault;
end;
function TBGRAOriginalEditor.GetPointAt(ACoord: TPointF; ARightButton: boolean): integer;
var v: TPointF;
curDist,newDist: single;
i: Integer;
begin
curDist := 4*FPointSize*FPointSize;
result := -1;
for i := 0 to high(FPoints) do
if FPoints[i].RightButton = ARightButton then
begin
v := FPoints[i].Coord - ACoord;
newDist := v*v;
if newDist <= curDist then
begin
curDist:= newDist;
result := i;
end;
end;
if result <> -1 then exit;
for i := 0 to high(FPoints) do
if FPoints[i].RightButton <> ARightButton then
begin
v := FPoints[i].Coord - ACoord;
newDist := v*v;
if newDist <= curDist then
begin
curDist:= newDist;
result := i;
end;
end;
end;
function TBGRAOriginalEditor.Render(ADest: TBGRABitmap): TRect;
var
i: Integer;
begin
result := EmptyRect;
for i := 0 to high(FPoints) do
begin
if isEmptyPointF(FPoints[i].Origin) then
UnionRect(result, result, RenderPoint(ADest, AffMatrixMult(FMatrix,FPoints[i].Coord)))
else
UnionRect(result, result, RenderArrow(ADest, AffMatrixMult(FMatrix,FPoints[i].Origin), AffMatrixMult(FMatrix,FPoints[i].Coord)));
end;
end;
{ TBGRAMemOriginalStorage }
function TBGRAMemOriginalStorage.GetRawString(AName: utf8string): RawByteString;
begin
if pos('.',AName)<>0 then exit('');
result := FMemDir.RawStringByFilename[AName];
end;
procedure TBGRAMemOriginalStorage.SetRawString(AName: utf8string;
AValue: RawByteString);
begin
if pos('.',AName)<>0 then exit;
FMemDir.RawStringByFilename[AName] := AValue;
end;
constructor TBGRAMemOriginalStorage.Create(AMemDir: TMemDirectory);
begin
inherited Create;
FMemDir := AMemDir;
end;
procedure TBGRAMemOriginalStorage.RemoveAttribute(AName: utf8string);
begin
if pos('.',AName)<>0 then exit;
FMemDir.Delete(AName,'');
end;
procedure TBGRAMemOriginalStorage.RemoveFile(AName: utf8string);
begin
FMemDir.Delete(EntryFilename(AName));
end;
function TBGRAMemOriginalStorage.ReadFile(AName: UTF8String; ADest: TStream): boolean;
var
entryId: Integer;
begin
entryId := FMemDir.IndexOf(EntryFilename(AName));
if entryId <> -1 then
begin
with FMemDir.Entry[entryId] do
result := CopyTo(ADest) = FileSize
end
else
result := false;
end;
procedure TBGRAMemOriginalStorage.WriteFile(AName: UTF8String; ASource: TStream; ACompress: boolean);
var
idxEntry: Integer;
begin
idxEntry := FMemDir.Add(EntryFilename(AName), ASource, true, false);
if ACompress then FMemDir.IsEntryCompressed[idxEntry] := true;
end;
{ TBGRACustomOriginalStorage }
function TBGRACustomOriginalStorage.GetColor(AName: UTF8String): TBGRAPixel;
begin
result := StrToBGRA(RawString[AName], BGRAPixelTransparent);
end;
procedure TBGRACustomOriginalStorage.SetColor(AName: UTF8String;
AValue: TBGRAPixel);
begin
RawString[AName] := LowerCase(BGRAToStr(AValue, CSSColors));
end;
function TBGRACustomOriginalStorage.GetBool(AName: utf8string): boolean;
begin
result := StrToBool(RawString[AName]);
end;
function TBGRACustomOriginalStorage.GetColorArray(AName: UTF8String
): ArrayOfTBGRAPixel;
var colorNames: TStringList;
i: Integer;
begin
colorNames := TStringList.Create;
colorNames.StrictDelimiter := true;
colorNames.Delimiter:= ',';
colorNames.DelimitedText:= RawString[AName];
setlength(result, colorNames.Count);
for i := 0 to high(result) do
result[i] := StrToBGRA(colorNames[i],BGRAPixelTransparent);
colorNames.Free;
end;
function TBGRACustomOriginalStorage.GetIntegerDef(AName: utf8string;
ADefault: integer): integer;
begin
result := StrToIntDef(RawString[AName],ADefault);
end;
function TBGRACustomOriginalStorage.GetSingleDef(AName: utf8string;
ADefault: single): single;
begin
result := StrToFloatDef(RawString[AName], ADefault, FFormats);
end;
procedure TBGRACustomOriginalStorage.SetBool(AName: utf8string; AValue: boolean);
begin
{$IFDEF BDS}
RawString[AName] := BoolToStr(AValue);
{$ELSE}
RawString[AName] := BoolToStr(AValue, 'true','false');
{$ENDIF}
end;
procedure TBGRACustomOriginalStorage.SetColorArray(AName: UTF8String;
AValue: ArrayOfTBGRAPixel);
var colorNames: TStringList;
i: Integer;
begin
colorNames := TStringList.Create;
colorNames.StrictDelimiter := true;
colorNames.Delimiter:= ',';
for i := 0 to high(AValue) do
colorNames.Add(LowerCase(BGRAToStr(AValue[i], CSSColors)));
RawString[AName] := colorNames.DelimitedText;
colorNames.Free;
end;
function TBGRACustomOriginalStorage.GetInteger(AName: utf8string): integer;
begin
result := GetIntegerDef(AName,0);
end;
function TBGRACustomOriginalStorage.GetPointF(AName: utf8string): TPointF;
var
s: String;
posComma: integer;
begin
s := RawString[AName];
posComma := pos(',',s);
if posComma = 0 then
exit(EmptyPointF);
result.x := StrToFloat(copy(s,1,posComma-1));
result.y := StrToFloat(copy(s,posComma+1,length(s)-posComma));
end;
function TBGRACustomOriginalStorage.GetSingle(AName: utf8string): single;
begin
result := GetSingleDef(AName, EmptySingle);
end;
procedure TBGRACustomOriginalStorage.SetInteger(AName: utf8string;
AValue: integer);
begin
RawString[AName] := IntToStr(AValue);
end;
procedure TBGRACustomOriginalStorage.SetPointF(AName: utf8string;
AValue: TPointF);
begin
if isEmptyPointF(AValue) then RemoveAttribute(AName)
else RawString[AName] := FloatToStrF(AValue.x, ffGeneral,7,3, FFormats)+','+FloatToStrF(AValue.y, ffGeneral,7,3, FFormats);
end;
procedure TBGRACustomOriginalStorage.SetSingle(AName: utf8string; AValue: single);
begin
if AValue = EmptySingle then RemoveAttribute(AName)
else RawString[AName] := FloatToStrF(AValue, ffGeneral,7,3, FFormats);
end;
constructor TBGRACustomOriginalStorage.Create;
begin
FFormats := {$IFDEF FPC}DefaultFormatSettings{$ELSE}FormatSettings{$ENDIF} ;
FFormats.DecimalSeparator := '.';
end;
{ TBGRALayerCustomOriginal }
procedure TBGRALayerCustomOriginal.SetOnChange(AValue: TOriginalChangeEvent);
begin
if @FOnChange=@AValue then Exit;
FOnChange:=AValue;
end;
function TBGRALayerCustomOriginal.GetGuid: TGuid;
begin
result := FGuid;
end;
procedure TBGRALayerCustomOriginal.SetGuid(AValue: TGuid);
begin
FGuid := AValue;
end;
procedure TBGRALayerCustomOriginal.NotifyChange;
begin
if Assigned(FOnChange) then
FOnChange(self);
end;
constructor TBGRALayerCustomOriginal.Create;
begin
FGuid := GUID_NULL;
end;
destructor TBGRALayerCustomOriginal.Destroy;
begin
inherited Destroy;
end;
procedure TBGRALayerCustomOriginal.ConfigureEditor(AEditor: TBGRAOriginalEditor);
begin
//nothing
end;
procedure TBGRALayerCustomOriginal.LoadFromFile(AFilenameUTF8: string);
var
s: TFileStreamUTF8;
begin
s := TFileStreamUTF8.Create(AFilenameUTF8, fmOpenRead, fmShareDenyWrite);
try
LoadFromStream(s);
finally
s.Free;
end;
end;
procedure TBGRALayerCustomOriginal.LoadFromStream(AStream: TStream);
var storage: TBGRAMemOriginalStorage;
memDir: TMemDirectory;
begin
memDir := TMemDirectory.Create;
storage := nil;
try
memDir.LoadFromStream(AStream);
storage := TBGRAMemOriginalStorage.Create(memDir);
if storage.RawString['class'] <> StorageClassName then
raise exception.Create('Invalid class');
LoadFromStorage(storage);
FreeAndNil(storage);
finally
storage.Free;
memDir.Free;
end;
end;
procedure TBGRALayerCustomOriginal.SaveToFile(AFilenameUTF8: string);
var
s: TFileStreamUTF8;
begin
s := TFileStreamUTF8.Create(AFilenameUTF8, fmCreate);
try
SaveToStream(s);
finally
s.Free;
end;
end;
procedure TBGRALayerCustomOriginal.SaveToStream(AStream: TStream);
var storage: TBGRAMemOriginalStorage;
memDir: TMemDirectory;
begin
memDir := TMemDirectory.Create;
storage := nil;
try
storage := TBGRAMemOriginalStorage.Create(memDir);
storage.RawString['class'] := StorageClassName;
SaveToStorage(storage);
FreeAndNil(storage);
memDir.SaveToStream(AStream);
finally
storage.Free;
memDir.Free;
end;
end;
function TBGRALayerCustomOriginal.CreateEditor: TBGRAOriginalEditor;
begin
result := TBGRAOriginalEditor.Create;
end;
{ TBGRALayerImageOriginal }
function TBGRALayerImageOriginal.GetImageHeight: integer;
begin
result := FImage.Height;
end;
function TBGRALayerImageOriginal.GetImageWidth: integer;
begin
result := FImage.Width;
end;
procedure TBGRALayerImageOriginal.ContentChanged;
begin
FContentVersion := FContentVersion +1;
NotifyChange;
end;
constructor TBGRALayerImageOriginal.Create;
begin
inherited Create;
FImage := TBGRABitmap.Create;
FContentVersion := 0;
FJpegStream := nil;
end;
destructor TBGRALayerImageOriginal.Destroy;
begin
FImage.Free;
FJpegStream.Free;
inherited Destroy;
end;
procedure TBGRALayerImageOriginal.Render(ADest: TBGRABitmap;
AMatrix: TAffineMatrix; ADraft: boolean);
var resampleFilter: TResampleFilter;
begin
if ADraft then resampleFilter := rfBox else resampleFilter:= rfCosine;
if Assigned(FImage) then
ADest.PutImageAffine(AMatrix, FImage, resampleFilter, dmSet);
end;
function TBGRALayerImageOriginal.GetRenderBounds(ADestRect: TRect;
AMatrix: TAffineMatrix): TRect;
var
aff: TAffineBox;
begin
if Assigned(FImage) then
begin
aff := AffMatrixMult(AMatrix,TAffineBox.AffineBox(PointF(0,0),PointF(FImage.Width,0),PointF(0,FImage.Height)));
result := aff.RectBounds;
end else
result := EmptyRect;
end;
procedure TBGRALayerImageOriginal.LoadFromStorage(
AStorage: TBGRACustomOriginalStorage);
var imgStream: TMemoryStream;
begin
if not Assigned(FImage) then FImage := TBGRABitmap.Create;
imgStream := TMemoryStream.Create;
try
if AStorage.ReadFile('content.png', imgStream) then
begin
imgStream.Position:= 0;
FImage.LoadFromStream(imgStream);
FreeAndNil(FJpegStream);
end else
if AStorage.ReadFile('content.jpg', imgStream) then
begin
FreeAndNil(FJpegStream);
FJpegStream := imgStream;
imgStream:= nil;
FJpegStream.Position:= 0;
FImage.LoadFromStream(FJpegStream);
end else
begin
FImage.SetSize(0,0);
FreeAndNil(FJpegStream);
end;
FContentVersion := AStorage.Int['content-version'];
finally
imgStream.Free;
end;
end;
procedure TBGRALayerImageOriginal.SaveToStorage(
AStorage: TBGRACustomOriginalStorage);
var imgStream: TMemoryStream;
begin
if Assigned(FImage) then
begin
if FContentVersion > AStorage.Int['content-version'] then
begin
if Assigned(FJpegStream) then
begin
AStorage.WriteFile('content.jpg', FJpegStream, false);
AStorage.RemoveFile('content.png');
AStorage.Int['content-version'] := FContentVersion;
end else
begin
imgStream := TMemoryStream.Create;
try
FImage.SaveToStreamAsPng(imgStream);
AStorage.RemoveFile('content.jpg');
AStorage.WriteFile('content.png', imgStream, false);
AStorage.Int['content-version'] := FContentVersion;
finally
imgStream.Free;
end;
end;
end;
end;
end;
procedure TBGRALayerImageOriginal.LoadFromStream(AStream: TStream);
var
newJpegStream: TMemoryStream;
begin
if DetectFileFormat(AStream) = ifJpeg then
begin
newJpegStream := TMemoryStream.Create;
try
newJpegStream.CopyFrom(AStream, AStream.Size);
newJpegStream.Position := 0;
FImage.LoadFromStream(newJpegStream);
FJpegStream.Free;
FJpegStream := newJpegStream;
newJpegStream := nil;
finally
newJpegStream.Free;
end;
end else
begin
FreeAndNil(FJpegStream);
FImage.LoadFromStream(AStream);
end;
ContentChanged;
end;
procedure TBGRALayerImageOriginal.SaveToStream(AStream: TStream);
begin
if Assigned(FJpegStream) then
begin
FJpegStream.Position := 0;
if AStream.CopyFrom(FJpegStream, FJpegStream.Size)<>FJpegStream.Size then
raise exception.Create('Error while saving');
end else
FImage.SaveToStreamAsPng(AStream);
end;
class function TBGRALayerImageOriginal.StorageClassName: RawByteString;
begin
result := 'image';
end;
initialization
RegisterLayerOriginal(TBGRALayerImageOriginal);
end.