-
Notifications
You must be signed in to change notification settings - Fork 2
/
Unit_ImageDropDown.pas
269 lines (239 loc) · 8.1 KB
/
Unit_ImageDropDown.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
Unit Unit_ImageDropDown;
Interface
Uses
WinApi.Windows,
WinApi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
System.Skia,
Vcl.Skia,
Vcl.Graphics,
Vcl.Controls,
Vcl.ExtCtrls,
Vcl.Forms,
Vcl.Buttons,
System.ImageList,
Vcl.Imaging.jpeg,
Vcl.Imaging.pngimage;
type
TLoadImageEvent = procedure(Sender: TObject; const ALoadFile: string) of object;
TLoadIndexEvent = procedure(Sender: TObject; const AIndex: Integer) of object;
type
TImageDropDown = Class
private
FImage: TImage;
FPanel: TPanel;
FOriginalPanelWndProc: TWndMethod;
FDropFlag: Integer;
//
FOnLoadImage: TLoadImageEvent;
FOnLoadIndex: TLoadIndexEvent;
FCurrentIndex: Integer;
FLavaSourceList: TStringList;
FLavaPrevButton: TSpeedButton;
FLavaNextButton: TSpeedButton;
procedure WM_ImageDrop(var Msg: TWMDROPFILES);
procedure PanelWindowProc(var Msg: TMessage);
//
procedure Do_UpdateButtons();
procedure SetCurrentIndex(const Value: Integer);
procedure LavaPrevButtonClick(Sender: TObject);
procedure LavaNextButtonClick(Sender: TObject);
procedure SetLavaNextButton(const Value: TSpeedButton);
procedure SetLavaPrevButton(const Value: TSpeedButton);
function Load_IMG(const ASourceFile: string): Boolean;
procedure Do_LoadIMG(const AIndex: Integer);
public
constructor Create(AImage: TImage; APanel: TPanel);
destructor Destroy; override;
procedure LoadIMG_Drop(const ADropedFile: string);
// property ...
property Image: TImage read FImage;
property DropFlag: Integer read FDropFlag write FDropFlag;
property OnLoadImage: TLoadImageEvent read FOnLoadImage write FOnLoadImage;
property OnLoadIndex: TLoadIndexEvent read FOnLoadIndex write FOnLoadIndex;
//
property LavaPrevButton: TSpeedButton read FLavaPrevButton write SetLavaPrevButton;
property LavaNextButton: TSpeedButton read FLavaNextButton write SetLavaNextButton;
property CurrentIndex: Integer read FCurrentIndex write SetCurrentIndex;
end;
function GetResizedImage(const AImage: ISkImage; const ANewWidth, ANewHeight: Integer): ISkImage;
implementation
uses
WinApi.ShellAPI,
Vcl.Dialogs,
System.Math,
System.UITypes,
Unit_Common,
Unit_Main;
function GetResizedImage(const AImage: ISkImage; const ANewWidth, ANewHeight: Integer): ISkImage;
begin
var _ScaleFactor: single := Min(ANewWidth / AImage.Width, ANewHeight / AImage.Height);
var _NewWidth: single := AImage.Width * _ScaleFactor;
var _NewHeight: single := AImage.Height * _ScaleFactor;
var _OffsetX: single := (ANewWidth - _NewWidth) / 2;
var _OffsetY: single := (ANewHeight - _NewHeight) / 2;
var _Surface: ISkSurface := TSkSurface.MakeRaster(ANewWidth, ANewHeight);
_Surface.Canvas.Clear(TAlphaColors.Null);
_Surface.Canvas.Scale(_ScaleFactor, _ScaleFactor);
_Surface.Canvas.DrawImage(AImage, _OffsetX / _ScaleFactor, _OffsetY / _ScaleFactor, TSkSamplingOptions.Medium);
Result := _Surface.MakeImageSnapshot;
// reserved ...
// var _Bitmap := TBitmap.CreateFromSkImage(_Surface.MakeImageSnapshot)
end;
function TImageDropDown.Load_IMG(const ASourceFile: string): Boolean;
begin
Result := False;
if FileExists(ASourceFile) then
try
var _ext := LowerCase(ExtractFileExt(ASourceFile));
if (_ext = '.webp') or (_ext = '.gif') then { Unsupported Image Format in Llava model }
begin
var _BytesStreamJpg: TBytesStream := TBytesStream.Create();
try
var _skImage: ISkImage := TSkImage.MakeFromEncodedFile(ASourceFile);
if _skImage.EncodeToStream(_BytesStreamJpg, TSkEncodedImageFormat.jpeg) then
begin
_BytesStreamJpg.Position := 0;
FImage.Picture.LoadFromStream(_BytesStreamJpg);
end;
finally
_BytesStreamJpg.Free;
end
end
else
FImage.Picture.LoadFromFile(ASourceFile);
Result := True;
except
Raise;
end;
end;
procedure TImageDropDown.WM_ImageDrop(var Msg: TWMDROPFILES);
begin
inherited;
var _DropH: HDROP := Msg.Drop;
try
var _numFiles := DragQueryFile(_DropH, $FFFFFFFF, nil, 0);
if _numFiles >= 1 then
begin
var _FileNameLength := DragQueryFile(_DropH, 0, nil, 0);
var _FileName: string := '';
SetLength(_FileName, _FileNameLength);
DragQueryFile(_DropH, 0, PChar(_FileName), _FileNameLength + 1);
LoadIMG_Drop(_FileName);
end;
finally
DragFinish(_DropH);
end;
Msg.Result := 0;
end;
procedure TImageDropDown.LoadIMG_Drop(const ADropedFile: string);
const
c_VerifyImgFormat = '...*.jpg...*.jpeg...*.png...*.webp...*.gif';
begin
FDropFlag := -1;
var _ext := LowerCase(ExtractFileExt(ADropedFile));
if Pos(_ext, c_VerifyImgFormat) > 3 then
try
if (_ext = '.webp') or (_ext = '.gif') then { Unsupported Image Format in Llava model }
begin
var _BytesStreamJpg: TBytesStream := TBytesStream.Create();
try
var _skImage := TSkImage.MakeFromEncodedFile(ADropedFile);
if _skImage.EncodeToStream(_BytesStreamJpg, TSkEncodedImageFormat.jpeg) then
begin
_BytesStreamJpg.Position := 0;
FImage.Picture.LoadFromStream(_BytesStreamJpg);
end;
finally
_BytesStreamJpg.Free;
end
end
else
FImage.Picture.LoadFromFile(ADropedFile);
CurrentIndex := FLavaSourceList.Add(ADropedFile);
if Assigned(FOnLoadImage) then
FOnLoadImage(Self, ADropedFile);
except
Raise;
end
else
ShowMessage('Not Supported Image Format'#13#10' - supported format - (*.jpg, *.jpeg, *.png, *.webp, *.gif)');
FDropFlag := 0;
end;
constructor TImageDropDown.Create(AImage: TImage; APanel: TPanel);
begin
FImage := AImage;
FPanel := APanel;
FDropFlag := 0;
FCurrentIndex := -1;
FLavaSourceList := TStringList.Create;
FImage.Picture.Graphic.EnableScaledDrawer(TWICScaledGraphicDrawer);
FOriginalPanelWndProc := APanel.WindowProc;
APanel.WindowProc := PanelWindowProc;
DragAcceptFiles(APanel.Handle, True);
end;
procedure TImageDropDown.PanelWindowProc(var Msg: TMessage);
begin
if Msg.Msg = WM_DROPFILES then
WM_ImageDrop(TWMDROPFILES(Msg))
else
FOriginalPanelWndProc(Msg);
end;
destructor TImageDropDown.Destroy;
begin
if Assigned(FPanel) then
begin
FPanel.WindowProc := FOriginalPanelWndProc;
DragAcceptFiles(FPanel.Handle, False);
end;
FLavaSourceList.Free;
inherited;
end;
procedure TImageDropDown.Do_LoadIMG(const AIndex: Integer);
begin
if (AIndex >= 0) and (AIndex < FLavaSourceList.Count) then
begin
var _source := FLavaSourceList.Strings[AIndex];
if Load_IMG(_source) then
begin
CurrentIndex := AIndex;
if Assigned(FOnLoadIndex) then
FOnLoadIndex(Self, AIndex);
end;
end;
end;
procedure TImageDropDown.Do_UpdateButtons;
begin
if Assigned(FLavaPrevButton) then
FLavaPrevButton.Enabled := (FLavaSourceList.Count > 0) and (FCurrentIndex > 0);
if Assigned(FLavaNextButton) then
FLavaNextButton.Enabled := (FLavaSourceList.Count > 0) and (FCurrentIndex < FLavaSourceList.Count-1);
end;
procedure TImageDropDown.SetCurrentIndex(const Value: Integer);
begin
FCurrentIndex := Value;
Do_UpdateButtons;
end;
procedure TImageDropDown.LavaNextButtonClick(Sender: TObject);
begin
var _nextindex: Integer := FCurrentIndex+1;
Do_LoadIMG(_nextindex);
end;
procedure TImageDropDown.LavaPrevButtonClick(Sender: TObject);
begin
var _previndex: Integer := FCurrentIndex-1;
Do_LoadIMG(_previndex);
end;
procedure TImageDropDown.SetLavaNextButton(const Value: TSpeedButton);
begin
FLavaNextButton := Value;
FLavaNextButton.onClick := LavaNextButtonClick;
end;
procedure TImageDropDown.SetLavaPrevButton(const Value: TSpeedButton);
begin
FLavaPrevButton := Value;
FLavaPrevButton.onClick := LavaPrevButtonClick;
end;
End.