-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bgraimagemanipulation.pas
4092 lines (3487 loc) · 122 KB
/
bgraimagemanipulation.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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-linking-exception
unit BGRAImageManipulation;
{ ============================================================================
BGRAImageManipulation Unit
originally written in 2011 by - Emerson Cavalcanti <emersoncavalcanti at googlesites>
============================================================================
Description:
TBGRAImageManipulation is a component designed to make simple changes in an
image while maintaining the aspect ratio of the final image and allow it to
cut to reduce the unnecessary edges. The selected area is painted with a
different transparency level for easy viewing of what will be cut.
============================================================================
History:
2011-05-03 - Emerson Cavalcanti
- Initial version
2011-06-01 - Emerson Cavalcanti
- Fixed aspect ratio when the image has a dimension smaller than
the size of the component.
- Fixed memory leak on temporary bitmaps.
- Fixed unecessary release of bitmap.
- Inserted Anchor and Align property on component.
- Implemented 'Keep aspect Ratio' property. Now you can select an
area without maintaining the aspect ratio.
2011-06-03 - Emerson Cavalcanti
- Improved selection when don't use aspect ratio.
- Improved response when resize component.
- Fixed memory leak on resample bitmap.
2011-06-04 - Circular
- Fixed divide by zero when calculate aspect ratio on
getImageRect.
2011-06-07 - Emerson Cavalcanti
- Improved function of aspect ratio including a variable to
provide the value directly in the component, instead of using
the dimensions of the component as the source of this value.
- Improved exhibition of anchors on selection.
- Improved mouse cursor.
- Included function to get the aspect ratio from image size.
- Included rotate Left and Right functions.
2013-10-13 - Massimo Magnano
- Add multi crop areas
- Add get Bitmap not resampled (original scale)
2014-08-04 - lainz-007-
- Included DataType.inc inside the unit
2021-03-30 - Massimo Magnano
- Each CropArea has its own AspectRatio, Add Events, Border Color
2021-04-30 - Massimo Magnano
- CropArea list Load/Save, bug fixes
2023-06 - Massimo Magnano
- the CropArea.Area property is relative to the unscaled image (unused in render/mouse events)
- added CropArea.ScaledArea property relative to the scaled image (used in render/mouse events)
- removed the use of DeltaX, DeltaY in render/mouse/etc
- CropAreas Area and ScaledArea property is updated during the mouse events
- rewriting of the methods for taking cropped images
-08 - the CropArea.Area property can be specified in Pixels,Cm,Inch
- Alt on MouseUp Undo the Crop Area Changes,Optimized mouse events
-09 - OverAnchor gives precedence to the selected area than Z Order
- EmptyImage property; CropAreas when Image is Empty; Old Code deleted and optimized
- XML Use Laz2_XMLCfg in fpc
- divide by zero in getImageRect on Component Loading
- EmptyImage size to ClientRect when Width/Height=0; Mouse Events when Image is Empty
- CropArea Rotate and Flip
- CropArea Duplicate and SetSize
- NewCropAreaDefault property (to Cm); ResolutionUnitConvert function; SetEmptyImageSizeToCropAreas
-10 - Load/Save XML Path Parameters, ContextMenu, UserData in GetAllBitmapCallback, CropArea Icons
2024-01 - Added CopyProperties to GetBitmap methods
-06 - Solved Bugs when load/save from xml
-08 - Removed EmptyImage.Allow, so is always allowed
CopyPropertiesToArea and Icons in NewCropAreaDefault
Updated Component icon
============================================================================
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
{$I bgracontrols.inc}
interface
{$IFDEF FPC}
{$DEFINE USE_Laz2_XMLCfg}
{$ENDIF}
uses
Classes, Contnrs, SysUtils,
{$IFDEF FPC}LCLIntf, LResources, FPImage, {$ENDIF}
Forms, Controls, Graphics, Dialogs,
{$IFNDEF FPC}Windows, Messages, BGRAGraphics, GraphType,{$ENDIF}
{$IFDEF USE_Laz2_XMLCfg}Laz2_XMLCfg,{$ELSE}XMLConf,{$ENDIF}
BCBaseCtrls, BGRABitmap, BGRABitmapTypes, BGRAGradientScanner;
{$IFNDEF FPC}
const
crSizeNW = TCursor(-23);
crSizeN = TCursor(-24);
crSizeNE = TCursor(-25);
crSizeW = TCursor(-26);
crSizeE = TCursor(-27);
crSizeSW = TCursor(-28);
crSizeS = TCursor(-29);
crSizeSE = TCursor(-30);
crUpArrow = TCursor(-10);
crHourGlass = TCursor(-11);
crDrag = TCursor(-12);
crNoDrop = TCursor(-13);
crHSplit = TCursor(-14);
crVSplit = TCursor(-15);
crMultiDrag = TCursor(-16);
{$ENDIF}
type
TCoord = packed record
x1 : LongInt;
y1 : LongInt;
x2 : LongInt;
y2 : LongInt;
end;
TRatio = packed record
Horizontal : LongInt;
Vertical : LongInt;
end;
TCardinalDirection = (NORTH, SOUTH, WEST, EAST);
TDirection = set of TCardinalDirection;
TSizeLimits = packed record
minWidth : LongInt;
maxWidth : LongInt;
minHeight : LongInt;
maxHeight : LongInt;
end;
TBGRAImageManipulation = class;
TCropAreaList = class;
{ TCropArea }
BoolParent = (bFalse=0, bTrue=1, bParent=2);
TCropAreaIcons = set of (cIcoIndex, cIcoLockSize, cIcoLockMove);
TCropArea = class(TObject)
protected
fOwner :TBGRAImageManipulation;
OwnerList:TCropAreaList;
rScaledArea:TRect;
rArea :TRectF;
rAreaUnit:TResolutionUnit;
rRatio :TRatio;
rAspectX,
rAspectY,
rMinHeight,
rMinWidth : Integer;
rAspectRatio,
rName: String;
rKeepAspectRatio: BoolParent;
Loading :Boolean;
rIcons: TCropAreaIcons;
procedure CopyAspectFromParent;
procedure setAspectRatio(AValue: string);
procedure setKeepAspectRatio(AValue: BoolParent);
procedure setScaledArea(AValue: TRect);
function getLeft: Single;
procedure setLeft(AValue: Single);
function getTop: Single;
procedure setTop(AValue: Single);
function getWidth: Single;
procedure setWidth(AValue: Single);
function getHeight: Single;
procedure setHeight(AValue: Single);
function getMaxHeight: Single;
function getMaxWidth: Single;
function getRealAspectRatio(var ARatio: TRatio):Boolean; //return Real KeepAspect
function getRealKeepAspectRatio:Boolean;
function getIndex: Longint;
function getIsNullSize: Boolean;
procedure setArea(AValue: TRectF);
procedure setAreaUnit(AValue: TResolutionUnit);
procedure setName(AValue: String);
procedure setIcons(AValue: TCropAreaIcons);
procedure Render_Refresh;
procedure GetImageResolution(var resX, resY:Single; var resUnit:TResolutionUnit);
procedure CalculateScaledAreaFromArea;
procedure CalculateAreaFromScaledArea;
function GetPixelArea(const AValue: TRectF):TRect;
function CheckScaledOutOfBounds(var AArea:TRect):Boolean;
function CheckAreaOutOfBounds(var AArea:TRectF):Boolean;
property ScaledArea :TRect read rScaledArea write setScaledArea;
public
Rotate :Single;
UserData :Integer;
BorderColor :TBGRAPixel;
function getResampledBitmap(ACopyProperties: Boolean=False): TBGRABitmap;
function getBitmap(ACopyProperties: Boolean=False): TBGRABitmap;
constructor Create(AOwner: TBGRAImageManipulation; AArea: TRectF;
AAreaUnit: TResolutionUnit = ruNone; //Pixels
AUserData: Integer = -1); overload;
constructor Create(AOwner: TBGRAImageManipulation;
DuplicateFrom: TCropArea; InsertInList:Boolean); overload;
destructor Destroy; override;
//ZOrder
procedure BringToFront;
procedure BringToBack;
procedure BringForward;
procedure BringBackward;
//Rotate/Flip
procedure RotateLeft;
procedure RotateRight;
procedure FlipHLeft;
procedure FlipHRight;
procedure FlipVUp;
procedure FlipVDown;
procedure SetSize(AWidth, AHeight:Single);
property Area:TRectF read rArea write setArea;
property AreaUnit:TResolutionUnit read rAreaUnit write setAreaUnit;
property Top:Single read getTop write setTop;
property Left:Single read getLeft write setLeft;
property Width:Single read getWidth write setWidth;
property Height:Single read getHeight write setHeight;
property MaxWidth:Single read getMaxWidth;
property MaxHeight:Single read getMaxHeight;
property AspectRatio: string read rAspectRatio write setAspectRatio;
property KeepAspectRatio: BoolParent read rKeepAspectRatio write setKeepAspectRatio default bParent;
property Index:Longint read getIndex;
property Name:String read rName write setName;
property isNullSize: Boolean read getIsNullSize;
property Icons:TCropAreaIcons read rIcons write setIcons;
end;
{ TCropAreaList }
TCropAreaList = class(TObjectList)
protected
fOwner :TBGRAImageManipulation;
rName :String;
rLoading :Boolean;
function getCropArea(aIndex: Integer): TCropArea;
procedure setCropArea(aIndex: Integer; const Value: TCropArea);
procedure setLoading(AValue: Boolean);
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
property Loading :Boolean read rLoading write setLoading;
public
constructor Create(AOwner: TBGRAImageManipulation);
function add(aCropArea: TCropArea): integer;
procedure Load(const XMLConf: TXMLConfig; XMLPath: String='');
procedure Save(const XMLConf: TXMLConfig; XMLPath: String='');
procedure LoadFromStream(Stream: TStream; XMLPath: String='');
procedure LoadFromFile(const FileName: String; XMLPath: String='');
procedure SaveToStream(Stream: TStream; XMLPath: String='');
procedure SaveToFile(const FileName: String; XMLPath: String='');
//Rotate/Flip
procedure RotateLeft;
procedure RotateRight;
procedure FlipHLeft;
procedure FlipHRight;
procedure FlipVUp;
procedure FlipVDown;
property items[aIndex: integer] : TCropArea read getCropArea write setCropArea; default;
property Name:String read rName write rName;
end;
TgetAllBitmapsCallback = procedure (Bitmap :TBGRABitmap; CropArea: TCropArea; AUserData:Integer) of object;
{ TBGRAEmptyImage }
TBGRAEmptyImage = class(TPersistent)
private
fOwner: TBGRAImageManipulation;
rResolutionHeight: Single;
rResolutionUnit: TResolutionUnit;
rResolutionWidth: Single;
rShowBorder: Boolean;
function getHeight: Integer;
function getWidth: Integer;
procedure SetResolutionUnit(AValue: TResolutionUnit);
public
property Width:Integer read getWidth;
property Height:Integer read getHeight;
constructor Create(AOwner: TBGRAImageManipulation);
published
property ResolutionUnit: TResolutionUnit read rResolutionUnit write SetResolutionUnit default ruPixelsPerCentimeter;
property ResolutionWidth: Single read rResolutionWidth write rResolutionWidth;
property ResolutionHeight: Single read rResolutionHeight write rResolutionHeight;
property ShowBorder: Boolean read rShowBorder write rShowBorder default False;
end;
{ TBGRANewCropAreaDefault }
TBGRANewCropAreaDefault = class(TPersistent)
private
fOwner: TBGRAImageManipulation;
rAspectRatio: string;
rIcons: TCropAreaIcons;
rKeepAspectRatio: BoolParent;
rResolutionUnit: TResolutionUnit;
public
constructor Create(AOwner: TBGRAImageManipulation);
procedure CopyPropertiesToArea(ANewArea: TCropArea);
published
property Icons: TCropAreaIcons read rIcons write rIcons;
property ResolutionUnit: TResolutionUnit read rResolutionUnit write rResolutionUnit default ruPixelsPerCentimeter;
property AspectRatio: string read rAspectRatio write rAspectRatio;
property KeepAspectRatio: BoolParent read rKeepAspectRatio write rKeepAspectRatio default bFalse;
end;
{ TBGRAImageManipulation }
TCropAreaEvent = procedure (Sender: TBGRAImageManipulation; CropArea: TCropArea) of object;
TCropAreaLoadEvent = function (Sender: TBGRAImageManipulation; CropArea: TCropArea;
const XMLConf: TXMLConfig; const Path:String):Integer of object;
TCropAreaSaveEvent = procedure (Sender: TBGRAImageManipulation; CropArea: TCropArea;
const XMLConf: TXMLConfig; const Path:String) of object;
TBGRAIMContextPopupEvent = procedure(Sender: TBGRAImageManipulation; CropArea: TCropArea;
AnchorSelected :TDirection; MousePos: TPoint; var Handled: Boolean) of object;
TBGRAImageManipulation = class(TBGRAGraphicCtrl)
private
{ Private declarations }
fAnchorSize: byte;
fAnchorSelected: TDirection;
fBorderSize: byte;
fAspectRatio: string;
fAspectX: integer;
fAspectY: integer;
fKeepAspectRatio: boolean;
fMinHeight: integer;
fMinWidth: integer;
fMouseCaught: boolean;
fStartPoint,
fEndPoint: TPoint;
fStartArea: TRect;
fRatio: TRatio;
fSizeLimits: TSizeLimits;
fImageBitmap, fResampledBitmap, fBackground, fVirtualScreen: TBGRABitmap;
rNewCropAreaDefault: TBGRANewCropAreaDefault;
rOnContextPopup: TBGRAIMContextPopupEvent;
function getAnchorSize: byte;
function getPixelsPerInch: Integer;
procedure setAnchorSize(const Value: byte);
function getEmpty: boolean;
procedure setBitmap(const Value: TBGRABitmap);
procedure setBorderSize(const Value: byte);
procedure setAspectRatio(const Value: string);
procedure setEmptyImage(AValue: TBGRAEmptyImage);
procedure setKeepAspectRatio(const Value: boolean);
procedure setMinHeight(const Value: integer);
procedure setMinWidth(const Value: integer);
procedure setSelectedCropArea(AValue: TCropArea);
protected
{ Protected declarations }
rCropAreas :TCropAreaList;
rNewCropArea,
rSelectedCropArea :TCropArea;
rOnCropAreaAdded: TCropAreaEvent;
rOnCropAreaDeleted: TCropAreaEvent;
rOnCropAreaChanged: TCropAreaEvent;
rOnSelectedCropAreaChanged: TCropAreaEvent;
rOnCropAreaLoad: TCropAreaLoadEvent;
rOnCropAreaSave: TCropAreaSaveEvent;
rEmptyImage: TBGRAEmptyImage;
rLoading: Boolean;
function ApplyDimRestriction(Coords: TCoord; Direction: TDirection; Bounds: TRect; AKeepAspectRatio:Boolean): TCoord;
function ApplyRatioToAxes(Coords: TCoord; Direction: TDirection; Bounds: TRect; ACropArea :TCropArea = Nil): TCoord;
procedure ApplyRatioToArea(ACropArea :TCropArea);
procedure CalcMaxSelection(ACropArea :TCropArea);
procedure findSizeLimits;
function getDirection(const Point1, Point2: TPoint): TDirection;
function getImageRect(Picture: TBGRABitmap): TRect;
function getWorkRect: TRect;
function isOverAnchor(APoint :TPoint; var AnchorSelected :TDirection; var ACursor :TCursor) :TCropArea;
procedure CreateEmptyImage;
procedure CreateResampledBitmap;
procedure Loaded; override;
procedure Paint; override;
procedure RepaintBackground;
procedure Resize; override;
procedure Render;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure DoContextPopup(MousePos: TPoint; var Handled: Boolean); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Invalidate; override;
function getAspectRatioFromImage(const Value: TBGRABitmap): string;
function getResampledBitmap(ACropArea :TCropArea = Nil; ACopyProperties: Boolean=False) : TBGRABitmap;
function getBitmap(ACropArea :TCropArea = Nil; ACopyProperties: Boolean=False) : TBGRABitmap;
procedure rotateLeft(ACopyProperties: Boolean=False);
procedure rotateRight(ACopyProperties: Boolean=False);
procedure tests;
//Crop Areas Manipulation functions
function addCropArea(AArea : TRectF; AAreaUnit: TResolutionUnit = ruNone;
AUserData: Integer = -1) :TCropArea;
function addScaledCropArea(AArea : TRect; AUserData: Integer = -1) :TCropArea;
procedure delCropArea(ACropArea :TCropArea);
procedure clearCropAreas;
procedure getAllResampledBitmaps(ACallBack :TgetAllBitmapsCallback; AUserData:Integer=0; ACopyProperties: Boolean=False);
procedure getAllBitmaps(ACallBack :TgetAllBitmapsCallback; AUserData:Integer=0; ACopyProperties: Boolean=False);
procedure SetEmptyImageSizeToCropAreas(ReduceLarger: Boolean=False);
procedure SetEmptyImageSizeToNull;
procedure SetEmptyImageSize(AResolutionUnit: TResolutionUnit; AResolutionWidth, AResolutionHeight: Single);
property SelectedCropArea :TCropArea read rSelectedCropArea write setSelectedCropArea;
property CropAreas :TCropAreaList read rCropAreas;
property PixelsPerInch: Integer read getPixelsPerInch;
published
{ Published declarations }
property Align;
property Anchors;
property AnchorSize: byte Read getAnchorSize Write setAnchorSize default 5;
property Bitmap: TBGRABitmap Read fImageBitmap Write setBitmap;
property BorderSize: byte Read fBorderSize Write setBorderSize default 2;
property AspectRatio: string Read fAspectRatio Write setAspectRatio;
property KeepAspectRatio: boolean Read fKeepAspectRatio Write setKeepAspectRatio default True;
property MinHeight: integer Read fMinHeight Write setMinHeight;
property MinWidth: integer Read fMinWidth Write setMinWidth;
property Empty: boolean Read getEmpty;
property EmptyImage: TBGRAEmptyImage read rEmptyImage write setEmptyImage stored True;
property NewCropAreaDefault: TBGRANewCropAreaDefault read rNewCropAreaDefault write rNewCropAreaDefault stored True;
//Events
property OnCropAreaAdded:TCropAreaEvent read rOnCropAreaAdded write rOnCropAreaAdded;
property OnCropAreaDeleted:TCropAreaEvent read rOnCropAreaDeleted write rOnCropAreaDeleted;
property OnCropAreaChanged:TCropAreaEvent read rOnCropAreaChanged write rOnCropAreaChanged;
property OnCropAreaLoad:TCropAreaLoadEvent read rOnCropAreaLoad write rOnCropAreaLoad;
property OnCropAreaSave:TCropAreaSaveEvent read rOnCropAreaSave write rOnCropAreaSave;
//CropArea Parameter is the Old Selected Area, use SelectedCropArea property for current
property OnSelectedCropAreaChanged:TCropAreaEvent read rOnSelectedCropAreaChanged write rOnSelectedCropAreaChanged;
property OnContextPopup: TBGRAIMContextPopupEvent read rOnContextPopup write rOnContextPopup;
(* property OnStartDrag: TStartDragEvent;
property OnDragDrop: TDragDropEvent;
property OnDragOver: TDragOverEvent;
property OnEndDrag: TEndDragEvent;*)
end;
function RoundUp(AValue:Single):Integer;
function ResolutionUnitConvert(const AValue:Single; fromRes, toRes:TResolutionUnit; predefInchRes:Integer=96):Single;
procedure PixelXResolutionUnitConvert(var resX, resY:Single; fromRes, toRes:TResolutionUnit);
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
uses Math, ExtCtrls;
resourcestring
SAnchorSizeIsTooLarge =
'Anchor size is too large. %d is not within the valid range of %d..%d';
SAnchorSizeIsTooSmall =
'Anchor size is too small. %d is not within the valid range of %d..%d';
SAnchorSizeIsNotOdd = 'Anchor size is invalid. %d is not an odd number.';
SBorderSizeIsTooLarge =
'Border size is too large. %d is not within the valid range of %d..%d';
SBorderSizeIsTooSmall =
'Border size is too small. %d is not within the valid range of %d..%d';
SAspectRatioIsNotValid = 'Aspect ratio value is invalid. %s contain invalid number.';
{ Calculate the Greatest Common Divisor (GCD) using the algorithm of Euclides }
function getGCD(Nr1, Nr2: longint): longint;
begin
if Nr2 = 0 then
Result := Nr1
else
Result := getGCD(Nr2, Nr1 mod Nr2);
end;
{ Calculate the Lowest Common Multiple (LCM) using the algorithm of Euclides }
function getLCM(Nr1, Nr2: longint): longint;
begin
Result := (Nr1 * Nr2) div getGCD(Nr1, Nr2);
end;
procedure CheckAspectRatio(const Value :String; var AspectRatioText :String; var XValue, YValue :Integer);
const
ValidChars = ['0'..'9', ':'];
var
Count :Integer;
begin
if ((pos(':', Value) > 0) and (pos(':', Value) < Length(Value))) then
begin
// Check if value is valid
XValue := 0;
YValue := 0;
AspectRatioText := '';
for Count := 1 to Length(Value) do
begin
if (Value[Count] in ValidChars) then
begin
if ((Value[Count] = ':') and (Length(AspectRatioText) > 0) and
(XValue = 0)) then
begin
XValue := StrToInt(AspectRatioText);
end;
AspectRatioText := AspectRatioText + Value[Count];
end
else
begin
// Value contain invalid characters
raise EInvalidArgument.CreateFmt(SAspectRatioIsNotValid, [Value]);
end;
end;
YValue := StrToInt(Copy(AspectRatioText, Pos(':', AspectRatioText) + 1,
Length(AspectRatioText)));
end
else
begin
// Value contain invalid characters
raise EInvalidArgument.CreateFmt(SAspectRatioIsNotValid, [Value]);
end;
end;
function RoundUp(AValue:Single):Integer;
var
oRoundMode :TFPURoundingMode;
begin
oRoundMode :=Math.GetRoundMode;
//Round to Upper Value
Math.SetRoundMode(rmUp);
Result :=Round(AValue);
Math.SetRoundMode(oRoundMode);
end;
function ResolutionUnitConvert(const AValue:Single; fromRes, toRes:TResolutionUnit; predefInchRes:Integer):Single;
begin
if (fromRes<>toRes)
then Case fromRes of
ruNone: begin
if toRes=ruPixelsPerInch
then Result :=AValue/predefInchRes //in
else Result :=(AValue/predefInchRes)*2.54; //cm
end;
ruPixelsPerInch :begin
if toRes=ruPixelsPerCentimeter
then Result :=AValue*2.54 //cm
else Result :=AValue*predefInchRes; //pixel
end;
ruPixelsPerCentimeter :begin
if toRes=ruPixelsPerInch
then Result :=AValue/2.54 //in
else Result :=(AValue/2.54)*predefInchRes;//cm
end;
end
else Result:=AValue;
end;
procedure PixelXResolutionUnitConvert(var resX, resY: Single; fromRes, toRes: TResolutionUnit);
begin
//Do Conversion from/to PixelXInch/PixelXCm
if (toRes <> fromRes) then
begin
if (toRes=ruPixelsPerInch)
then begin
resX :=resX*2.54;
resY :=resY*2.54;
end
else begin
resX :=resX/2.54;
resY :=resY/2.54;
end
end;
end;
{ TCropArea }
procedure TCropArea.Render_Refresh;
begin
if not(fOwner.rCropAreas.loading) then
begin
fOwner.Render;
fOwner.Refresh;
end;
end;
procedure TCropArea.GetImageResolution(var resX, resY: Single; var resUnit: TResolutionUnit);
begin
resX :=fOwner.fImageBitmap.ResolutionX;
resY :=fOwner.fImageBitmap.ResolutionY;
resUnit :=fOwner.fImageBitmap.ResolutionUnit;
if (resX<2) or (resY<2) then //Some images have 1x1 PixelPerInch ?
begin
//No Resolution use predefined Form Values
resUnit :=rAreaUnit;
if (rAreaUnit=ruPixelsPerInch)
then resX :=fOwner.PixelsPerInch
else resX :=fOwner.PixelsPerInch/2.54;
resY :=resX;
end;
end;
function TCropArea.getIsNullSize: Boolean;
begin
Result := not((abs(rArea.Right - rArea.Left) > 0) and (abs(rArea.Bottom - rArea.Top) > 0));
end;
procedure TCropArea.setName(AValue: String);
begin
if rName=AValue then Exit;
rName:=AValue;
if assigned(fOwner.rOnCropAreaChanged)
then fOwner.rOnCropAreaChanged(fOwner, Self);
end;
procedure TCropArea.setIcons(AValue: TCropAreaIcons);
begin
if rIcons=AValue then Exit;
rIcons:=AValue;
Render_Refresh;
end;
function TCropArea.getTop: Single;
begin
Result :=rArea.Top;
end;
procedure TCropArea.setTop(AValue: Single);
var
tempArea:TRectF;
begin
if AValue=rArea.Top then Exit;
tempArea :=rArea;
tempArea.Top:=AValue;
tempArea.Height:=rArea.Height;
//CheckAreaOutOfBounds(tempArea);
Area :=tempArea;
end;
function TCropArea.getLeft: Single;
begin
Result :=rArea.Left;
end;
procedure TCropArea.setLeft(AValue: Single);
var
tempArea:TRectF;
tempSArea:TRect;
begin
if AValue=rArea.Left then Exit;
tempArea :=rArea;
tempArea.Left:=AValue;
tempArea.Width:=rArea.Width;
//CheckAreaOutOfBounds(tempArea);
Area :=tempArea;
(* if CheckScaledOutOfBounds(rScaledArea)
then begin
CalculateAreaFromScaledArea;
if assigned(fOwner.rOnCropAreaChanged)
then fOwner.rOnCropAreaChanged(fOwner, Self);
end; *)
end;
function TCropArea.getHeight: Single;
begin
Result :=rArea.Height;
end;
procedure TCropArea.setHeight(AValue: Single);
var
tempArea:TRectF;
begin
if AValue=rArea.Height then Exit;
tempArea :=rArea;
tempArea.Height:=AValue;
//CheckAreaOutOfBounds(tempArea);
Area :=tempArea;
end;
function TCropArea.getWidth: Single;
begin
Result :=rArea.Width;
end;
procedure TCropArea.setWidth(AValue: Single);
var
tempArea:TRectF;
begin
if AValue=rArea.Width then Exit;
tempArea :=rArea;
tempArea.Width:=AValue;
//CheckAreaOutOfBounds(tempArea);
Area :=tempArea;
end;
function TCropArea.getMaxHeight: Single;
begin
if (rAreaUnit=ruNone)
then Result :=fOwner.fImageBitmap.Height
else begin
if (fOwner.fImageBitmap.ResolutionY<2)
then Result :=fOwner.fImageBitmap.Height //No Resolution, Some images have 1x1 PixelPerInch ?
else begin
Result :=fOwner.fImageBitmap.ResolutionHeight;
//Do Conversion from/to inch/cm
if (rAreaUnit <> fOwner.fImageBitmap.ResolutionUnit) then
begin
if (rAreaUnit=ruPixelsPerInch)
then Result :=Result/2.54 //Bitmap is in Cm, i'm in Inch
else Result :=Result*2.54; //Bitmap is in Inch, i'm in Cm
end;
end;
end;
end;
function TCropArea.getMaxWidth: Single;
begin
if (rAreaUnit=ruNone)
then Result :=fOwner.fImageBitmap.Width
else begin
if (fOwner.fImageBitmap.ResolutionX<2)
then Result :=fOwner.fImageBitmap.Width //No Resolution, Some images have 1x1 PixelPerInch ?
else begin
Result :=fOwner.fImageBitmap.ResolutionWidth;
//Do Conversion from/to inch/cm
if (rAreaUnit <> fOwner.fImageBitmap.ResolutionUnit) then
begin
if (rAreaUnit=ruPixelsPerInch)
then Result :=Result/2.54 //Bitmap is in Cm, i'm in Inch
else Result :=Result*2.54; //Bitmap is in Inch, i'm in Cm
end;
end;
end;
end;
function TCropArea.getIndex: Longint;
begin
Result :=fOwner.CropAreas.IndexOf(Self);
end;
procedure TCropArea.CalculateScaledAreaFromArea;
var
xRatio, yRatio: Single;
resX, resY: Single;
resUnit:TResolutionUnit;
begin
if not(isNullSize) then
begin
// Calculate Scaled Area given Scale and Resolution
if (fOwner.fImageBitmap.Width=0) or (fOwner.fImageBitmap.Height=0)
then begin
xRatio :=1;
yRatio :=1;
end
else begin
xRatio := fOwner.fResampledBitmap.Width / fOwner.fImageBitmap.Width;
yRatio := fOwner.fResampledBitmap.Height / fOwner.fImageBitmap.Height;
end;
resX :=1; //if rAreaUnit=ruNone use only Ratio
resY :=1;
if (rAreaUnit<>ruNone) then
begin
GetImageResolution(resX, resY, resUnit);
PixelXResolutionUnitConvert(resX, resY, resUnit, rAreaUnit);
end;
//MaxM: Use Trunc for Top/Left and Round for Right/Bottom so we
// preserve as much data as possible when do the crop
rScaledArea.Left := Trunc(rArea.Left * resX * xRatio);
rScaledArea.Top := Trunc(rArea.Top * resY * yRatio);
rScaledArea.Right := Round(rArea.Right* resX * xRatio);
rScaledArea.Bottom := Round(rArea.Bottom * resY * yRatio);
end;
end;
procedure TCropArea.CalculateAreaFromScaledArea;
var
xRatio, yRatio: Single;
resX, resY: Single;
resUnit:TResolutionUnit;
begin
// Calculate Scaled Area given Scale and Resolution
if (fOwner.fImageBitmap.Width=0) or (fOwner.fImageBitmap.Height=0)
then begin
xRatio :=1;
yRatio :=1;
end
else begin
xRatio := fOwner.fResampledBitmap.Width / fOwner.fImageBitmap.Width;
yRatio := fOwner.fResampledBitmap.Height / fOwner.fImageBitmap.Height;
end;
resX :=1; //if rAreaUnit=ruNone use only Ratio
resY :=1;
if (rAreaUnit<>ruNone) then
begin
GetImageResolution(resX, resY, resUnit);
PixelXResolutionUnitConvert(resX, resY, resUnit, rAreaUnit);
end;
rArea.Left := (rScaledArea.Left / resX) / xRatio;
rArea.Right := (rScaledArea.Right / resX) / xRatio;
rArea.Top := (rScaledArea.Top / resY) / yRatio;
rArea.Bottom := (rScaledArea.Bottom / resY) / yRatio;
end;
function TCropArea.GetPixelArea(const AValue: TRectF): TRect;
var
resX, resY: Single;
resUnit: TResolutionUnit;
begin
if (rAreaUnit=ruNone)
then begin
Result.Left := Trunc(AValue.Left);
Result.Right := Trunc(AValue.Right);
Result.Top := Trunc(AValue.Top);
Result.Bottom := Trunc(AValue.Bottom);
end
else begin
if (rAreaUnit=ruNone)
then begin
resX :=1;
resY :=1;
end
else GetImageResolution(resX, resY, resUnit);
PixelXResolutionUnitConvert(resX, resY, resUnit, rAreaUnit);
Result.Left := Trunc(AValue.Left * resX);
Result.Top := Trunc(AValue.Top * resY);
Result.Right := Round(AValue.Right* resX);
Result.Bottom := Round(AValue.Bottom * resY);
end;
end;
function TCropArea.CheckScaledOutOfBounds(var AArea: TRect): Boolean;
var
tmpValue: Integer;
begin
Result :=False;
if (AArea.Left<0)
then begin
tmpValue :=-AArea.Left;
AArea.Left :=0;
AArea.Right:=AArea.Right+tmpValue;
Result :=True;
end;
if (AArea.Top<0)
then begin
tmpValue :=-AArea.Top;
AArea.Top :=0;
AArea.Bottom:=AArea.Bottom+tmpValue;
Result :=True;
end;
if (AArea.Right>fOwner.fResampledBitmap.Width)
then begin
tmpValue :=AArea.Right-fOwner.fResampledBitmap.Width;
AArea.Right :=fOwner.fResampledBitmap.Width;
AArea.Left:=AArea.Left-tmpValue; //if <0 ? a vicious circle
Result :=True;
end;
if (AArea.Bottom>fOwner.fResampledBitmap.Height)
then begin
tmpValue :=AArea.Bottom-fOwner.fResampledBitmap.Height;
AArea.Bottom :=fOwner.fResampledBitmap.Height;
AArea.Top:=AArea.Top-tmpValue; //if <0 ? a vicious circle
Result :=True;
end;
end;
function TCropArea.CheckAreaOutOfBounds(var AArea: TRectF):Boolean;
var
tmpValue, resWH: Single;
begin
Result :=False;
if (AArea.Left<0)
then begin
tmpValue :=-AArea.Left;
AArea.Left :=0;
AArea.Right:=AArea.Right+tmpValue;
Result :=True;
end;
if (AArea.Top<0)
then begin
tmpValue :=-AArea.Top;
AArea.Top :=0;
AArea.Bottom:=AArea.Bottom+tmpValue;
Result :=True;
end;
resWH :=fOwner.fImageBitmap.ResolutionWidth;
if (AArea.Right>resWH)
then begin
tmpValue :=AArea.Right-resWH;
AArea.Right :=resWH;
AArea.Left:=AArea.Left-tmpValue; //if <0 ? a vicious circle
Result :=True;
end;
resWH :=fOwner.fImageBitmap.ResolutionHeight;
if (AArea.Bottom>resWH)
then begin
tmpValue :=AArea.Bottom-resWH;
AArea.Bottom :=resWH;
AArea.Top:=AArea.Top-tmpValue; //if <0 ? a vicious circle
Result :=True;
end;
end;
procedure TCropArea.CopyAspectFromParent;
begin
rAspectX :=fOwner.fAspectX;
rAspectY :=fOwner.fAspectY;
rMinHeight :=fOwner.fMinHeight;
rMinWidth :=fOwner.fMinWidth;
rAspectRatio:=fOwner.fAspectRatio;
rRatio :=fOwner.fRatio;
end;
procedure TCropArea.setAspectRatio(AValue: string);