-
Notifications
You must be signed in to change notification settings - Fork 4
/
uMainForm.pas
595 lines (527 loc) · 17.5 KB
/
uMainForm.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
unit uMainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Effects, FMX.Controls.Presentation, MyPaintBox, FMX.Objects, FMX.Colors,
FMX.ListBox, FMX.Layouts, FMX.DialogService.Async, FMX.Edit,
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
REST.Types, REST.Client, REST.Response.Adapter, Data.Bind.Components,
Data.Bind.ObjectScope, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent, FMX.ExtCtrls,
FMX.TabControl, FMX.ListView.Types, FMX.ListView.Appearances,
FMX.ListView.Adapters.Base, FMX.ListView, FireDAC.Stan.StorageBin,
System.Rtti, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt,
Fmx.Bind.DBEngExt, Data.Bind.DBScope;
type
TMainForm = class(TForm)
MaterialOxfordBlueSB: TStyleBook;
ToolBar1: TToolBar;
Label1: TLabel;
ShadowEffect1: TShadowEffect;
ScreenshotButton: TButton;
DragImage: TImage;
IconsListBox: TListBox;
PanelMenu: TPanel;
Rectangle1: TRectangle;
DrawListBox: TListBox;
inonedraw: TListBoxItem;
idrawpen: TListBoxItem;
idrawline: TListBoxItem;
idrawrectangle: TListBoxItem;
idrawlinerectangle: TListBoxItem;
idrawellipse: TListBoxItem;
idrawlineellipse: TListBoxItem;
idrawerase: TListBoxItem;
idrawfill: TListBoxItem;
Toolbar: TToolBar;
ForegroundColorBox: TComboColorBox;
BackgroundColorBox: TComboColorBox;
ImageMenu: TImage;
LineThicknessTB: TTrackBar;
LineWidthText: TText;
ListBoxItem1: TListBoxItem;
ListBoxItem2: TListBoxItem;
ListBoxItem3: TListBoxItem;
ListBoxItem4: TListBoxItem;
ListBoxItem5: TListBoxItem;
ListBoxItem6: TListBoxItem;
ListBoxItem7: TListBoxItem;
ListBoxItem8: TListBoxItem;
SaveDialog1: TSaveDialog;
ListBoxItem9: TListBoxItem;
APIKeyEdit: TEdit;
APIKeyButton: TButton;
GenerateButton: TButton;
Timer1: TTimer;
FDMemTable2: TFDMemTable;
RESTResponse2: TRESTResponse;
RESTResponseDataSetAdapter2: TRESTResponseDataSetAdapter;
RESTClient2: TRESTClient;
RESTRequest2: TRESTRequest;
FDMemTable1: TFDMemTable;
RESTResponse1: TRESTResponse;
RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
ProgressBar: TProgressBar;
TemplateMemo: TMemo;
Layout1: TLayout;
Label2: TLabel;
PromptMemo: TMemo;
Label5: TLabel;
NegativePromptEdit: TEdit;
NetHTTPClient1: TNetHTTPClient;
Timer2: TTimer;
TabControl: TTabControl;
PaintTabItem: TTabItem;
ViewTabItem: TTabItem;
ImageViewer: TImageViewer;
PreviewImage: TImage;
Layout2: TLayout;
ListView1: TListView;
FileMemTable: TFDMemTable;
ImagePathEdit: TEdit;
BindSourceDB1: TBindSourceDB;
BindingsList1: TBindingsList;
LinkListControlToField1: TLinkListControlToField;
LinkControlToField1: TLinkControlToField;
StatusBar1: TStatusBar;
Img2ImgLayout: TLayout;
Layout3: TLayout;
ImageEdit: TEdit;
OpenButton: TButton;
Label6: TLabel;
StrengthLabel: TLabel;
StrengthTB: TTrackBar;
SourceImage: TImage;
OpenDialog: TOpenDialog;
Label7: TLabel;
Layout7: TLayout;
Label16: TLabel;
WidthEdit: TEdit;
HeightEdit: TEdit;
PaintLayout: TLayout;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BackgroundColorBoxChange(Sender: TObject);
procedure ForegroundColorBoxChange(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
procedure IconsListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure IconsListBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ImageMenuClick(Sender: TObject);
procedure LineThicknessTBChange(Sender: TObject);
procedure DrawListBoxChange(Sender: TObject);
procedure DrawListBoxClick(Sender: TObject);
procedure ListBoxItem1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ScreenshotButtonClick(Sender: TObject);
procedure GenerateButtonClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure APIKeyButtonClick(Sender: TObject);
procedure TabControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
procedure OpenButtonClick(Sender: TObject);
procedure StrengthTBChange(Sender: TObject);
private
{ Private declarations }
FOriginalFGColor: TColor;
FOriginalBGColor: TColor;
FPaintBox: TMyPaintBox;
procedure PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ListBoxItemMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Single);
public
{ Public declarations }
function MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
uses
System.Hash, System.NetEncoding, System.Net.Mime, System.JSON, System.Generics.Collections,
System.IOUtils;
function TMainForm.MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
var
OutputStringStream: TStringStream;
Base64Encoder: TBase64Encoding;
MimeType: string;
begin
MemoryStream.Position := 0;
OutputStringStream := TStringStream.Create('', TEncoding.ASCII);
try
Base64Encoder := TBase64Encoding.Create;
try
Base64Encoder.Encode(MemoryStream, OutputStringStream);
MimeType := 'image/png';
Result := 'data:' + MimeType + ';base64,' + OutputStringStream.DataString;
finally
Base64Encoder.Free;
end;
finally
OutputStringStream.Free;
end;
end;
procedure TMainForm.OpenButtonClick(Sender: TObject);
begin
if OpenDialog.Execute then
begin
ImageEdit.Text := OpenDialog.FileName;
SourceImage.Bitmap.LoadFromFile(ImageEdit.Text);
PaintLayout.Width := SourceImage.Bitmap.Width;
PaintLayout.Height := SourceImage.Bitmap.Height;
WidthEdit.Text := SourceImage.Bitmap.Width.ToString;
HeightEdit.Text := SourceImage.Bitmap.Height.ToString;
end;
end;
procedure TMainForm.APIKeyButtonClick(Sender: TObject);
begin
APIKeyEdit.Visible := not APIKeyEdit.Visible;
end;
procedure TMainForm.BackgroundColorBoxChange(Sender: TObject);
begin
FPaintBox.BackgroundColor := BackgroundColorBox.Color;
end;
function EncodeJSONStr(const S: String): String;
var
JSONValue: TJSONString;
begin
JSONValue := TJSONString.Create(S);
try
Result := JSONValue.ToJSON;
finally
JSONValue.Free;
end;
end;
procedure TMainForm.GenerateButtonClick(Sender: TObject);
begin
if APIKeyEdit.Text='' then
begin
ShowMessage('Enter a Replicate.com API key.');
Exit;
end;
ProgressBar.Value := 0;
ProgressBar.Visible := True;
GenerateButton.Enabled := False;
Application.ProcessMessages;
var LMaskStream := TMemoryStream.Create;
FPaintBox.SaveToStream(LMaskStream);
var LSourceStream := TMemoryStream.Create;
if ImageEdit.Text.Substring(0,4)<>'http' then
begin
if ImageEdit.Text<>'' then
begin
LSourceStream.LoadFromFile(ImageEdit.Text);
end
else
SourceImage.Bitmap.SaveToStream(LSourceStream);
end;
RestRequest1.Params[0].Value := 'Token ' + APIKeyEdit.Text;
RestRequest1.Params[1].Value := TemplateMemo.Lines.Text.Replace('%prompt%',PromptMemo.Lines.Text)
.Replace('%base64mask%','"'+MemoryStreamToBase64(LMaskStream)+'"')
.Replace('%base64source%','"'+MemoryStreamToBase64(LSourceStream)+'"')
.Replace('%negative_prompt%',NegativePromptEdit.Text).Replace(#13#10,'')
.Replace('%width%',WidthEdit.Text)
.Replace('%height%',HeightEdit.Text)
.Replace('%prompt_strength%',Format('%.2f',[StrengthTB.Value]).Replace(',','.'));
RESTRequest1.Execute;
LMaskStream.Free;
LSourceStream.Free;
var F := FDMemTable1.FindField('status');
if F<>nil then
begin
if F.AsWideString='starting' then
begin
RESTRequest2.Resource := FDMemTable1.FieldByName('id').AsWideString;
Timer1.Enabled := True;
end
else
begin
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
ShowMessage(F.AsWideString);
end;
end;
end;
procedure TMainForm.DrawListBoxChange(Sender: TObject);
begin
if Assigned(ImageMenu) then
ImageMenu.Bitmap := DrawListBox.Selected.ItemData.Bitmap;
PanelMenu.Visible := False;
if Assigned(FPaintBox) then
begin
FPaintBox.NoFill := False;
if FOriginalBGColor<>TAlphaColorRec.Null then
begin
BackgroundColorBox.Color := FOriginalBGColor;
FOriginalBGColor := TAlphaColorRec.Null;
end;
if FOriginalFGColor<>TAlphaColorRec.Null then
begin
ForegroundColorBox.Color := FOriginalFGColor;
FOriginalFGColor := TAlphaColorRec.Null;
end;
case DrawListBox.ItemIndex of
0: FPaintBox.FuncDraw := TFunctionDraw(DrawListBox.ItemIndex);
1: FPaintBox.FuncDraw := TFunctionDraw(DrawListBox.ItemIndex);
2: FPaintBox.FuncDraw := TFunctionDraw(DrawListBox.ItemIndex);
3: begin
FPaintBox.FuncDraw := TFunctionDraw(DrawListBox.ItemIndex);
end;
4: begin
FPaintBox.NoFill := True;
FPaintBox.FuncDraw := TFunctionDraw(3);
end;
5: begin
FPaintBox.FuncDraw := TFunctionDraw(4);
end;
6: begin
FPaintBox.NoFill := True;
FPaintBox.FuncDraw := TFunctionDraw(4);
end;
7: begin
FOriginalBGColor := BackgroundColorBox.Color;
FOriginalFGColor := ForegroundColorBox.Color;
BackgroundColorBox.Color := TAlphaColorRec.Black;
ForegroundColorBox.Color := TAlphaColorRec.Black;
FPaintBox.FuncDraw := TFunctionDraw(3);
end;
8: FPaintBox.FuncDraw := TFunctionDraw(5);
end;
end;
end;
procedure TMainForm.DrawListBoxClick(Sender: TObject);
begin
PanelMenu.Visible := False;
end;
procedure TMainForm.ForegroundColorBoxChange(Sender: TObject);
begin
if Assigned(FPaintBox) then
FPaintBox.ForegroundColor := ForegroundColorBox.Color;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
var LDataFile := ExtractFilePath(ParamStr(0)) + 'sdxli_database.fds';
if TFile.Exists(LDataFile) then
FileMemTable.LoadFromFile(LDataFile);
FPaintBox := TMyPaintBox.Create(Self);
FPaintBox.Parent := PaintLayout;
FPaintBox.SendToBack;
FOriginalBGColor := TAlphaColorRec.Null;
FOriginalFGColor := TAlphaColorRec.Null;
FPaintBox.ForegroundColor := ForegroundColorBox.Color;
FPaintBox.BackgroundColor := BackgroundColorBox.Color;
FPaintBox.Align := TAlignLayout.Client;
FPaintBox.OnMouseDown := PaintBoxMouseDown;
LineThicknessTBChange(LineThicknessTB);
ImageMenu.Bitmap := DrawListBox.Selected.ItemData.Bitmap;
PanelMenu.BringToFront;
Toolbar.BringToFront;
PaintLayout.Width := SourceImage.Bitmap.Width;
PaintLayout.Height := SourceImage.Bitmap.Height;
WidthEdit.Text := SourceImage.Bitmap.Width.ToString;
HeightEdit.Text := SourceImage.Bitmap.Height.ToString;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FPaintBox.DisposeOf;
end;
procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
begin
if Assigned(FPaintBox) then
begin
FPaintBox.MouseLeave;
end;
end;
procedure TMainForm.IconsListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
begin
if Assigned(FPaintBox) then
begin
if DragImage.Visible then
begin
DragImage.Position.X := X+IconsListBox.Position.X-(DragImage.Width/2);
DragImage.Position.Y := Y+IconsListBox.Position.Y-(DragImage.Height/2);
end;
end;
end;
procedure TMainForm.IconsListBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
if Assigned(FPaintBox) then
begin
TThread.Queue(nil,procedure begin
if DragImage.Visible=True then
begin
if DragImage.Tag=1 then
begin
TDialogServiceAsync.InputQuery('Enter your text', ['Text:'], [''],
procedure(const AResult: TModalResult; const AValues: array of string)
begin
case AResult of
mrOk: begin
FPaintBox.StampText(X-IconsListBox.Width,Y,LineWidthText.TextSettings.Font,AValues[0]);
end;
end;
end);
DragImage.Tag := 0;
end
else
FPaintBox.StampBitmap(X-IconsListBox.Width,Y,DragImage.Bitmap);
DragImage.Visible := False;
FPaintBox.MouseLeave;
end;
end);
end;
end;
procedure TMainForm.ImageMenuClick(Sender: TObject);
begin
PanelMenu.Visible := not PanelMenu.Visible;
end;
procedure TMainForm.PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
if Assigned(FPaintBox) then
begin
if PanelMenu.Visible=True then
begin
PanelMenu.Visible := False;
end;
end;
end;
procedure TMainForm.ScreenshotButtonClick(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
FPaintBox.SaveToFile(SaveDialog1.FileName);
end;
end;
procedure TMainForm.StrengthTBChange(Sender: TObject);
begin
StrengthLabel.Text := 'Strength: ' + Format('%.2f',[StrengthTB.Value]).Replace(',','.');
end;
function ParseJSONStrArray(const JSONStr: String): TArray<String>;
var
JSONArray: TJSONArray;
I: Integer;
begin
JSONArray := TJSONObject.ParseJSONValue(JSONStr) as TJSONArray;
try
SetLength(Result, JSONArray.Count);
for I := 0 to JSONArray.Count - 1 do
Result[I] := JSONArray.Items[I].Value;
finally
JSONArray.Free;
end;
end;
procedure TMainForm.TabControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
begin
if Assigned(FPaintBox) then
begin
FPaintBox.MouseLeave;
end;
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
RestRequest2.Params[0].Value := 'Token ' + APIKeyEdit.Text;
RESTRequest2.Execute;
var F := FDMemTable2.FindField('status');
if F<>nil then
begin
if F.AsWideString='succeeded' then
begin
Timer1.Enabled := False;
var OutputArray := ParseJSONStrArray(FDMemTable2.FieldByName('output').AsWideString);
var LImageURL := OutputArray[0];
var LMS := TMemoryStream.Create;
NetHTTPClient1.Get(LImageURL,LMS);
var LHash := THashMD5.GetHashString(LImageURL);
var LFilename := ExtractFilePath(ParamStr(0)) + LHash + '.png';
LMS.SaveToFile(LFilename);
LMS.Free;
var LMaskFilename := ExtractFilePath(ParamStr(0)) + LHash + '_mask.png';
FPaintBox.SaveToFile(LMaskFileName);
var LOriginalFilename := ExtractFilePath(ParamStr(0)) + LHash + '_original.png';
SourceImage.Bitmap.SaveToFile(LOriginalFileName);
PreviewImage.Bitmap.LoadFromFile(LFilename);
ImageViewer.Bitmap.LoadFromFile(LFilename);
TabControl.ActiveTab := ViewTabItem;
FileMemTable.AppendRecord([LFilename,PromptMemo.Lines.Text]);
FileMemTable.SaveToFile(ExtractFilePath(ParamStr(0)) + 'sdxli_database.fds');
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
end
else
if F.AsWideString='failed' then
begin
Timer1.Enabled := False;
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
ShowMessage(FDMemTable2.FieldByName('error').AsWideString);
end;
end;
end;
procedure TMainForm.Timer2Timer(Sender: TObject);
begin
if ProgressBar.Value=ProgressBar.Max then
ProgressBar.Value := ProgressBar.Min
else
ProgressBar.Value := ProgressBar.Value+5;
end;
procedure TMainForm.LineThicknessTBChange(Sender: TObject);
begin
if Assigned(FPaintBox) then
FPaintBox.Thickness := TTrackBar(Sender).Value;
end;
procedure TMainForm.ListBoxItem1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
ListBoxItemMouseDown(Sender, Button, Shift, X, Y);
end;
procedure TMainForm.ListBoxItemMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
if Assigned(FPaintBox) then
begin
DragImage.Bitmap.Assign(TListBoxItem(Sender).ItemData.Bitmap);
DragImage.Bitmap.Resize(50,50);
DragImage.Position.X := X+IconsListBox.Position.X-Trunc(DragImage.Bitmap.Width/2);
DragImage.Position.Y := TListBoxItem(Sender).Position.Y+Y+IconsListBox.Position.Y-Trunc(DragImage.Bitmap.Height/2);
DragImage.Tag := TListBoxItem(Sender).Tag;
DragImage.Visible := True;
DragImage.BringToFront;
end;
end;
procedure TMainForm.ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
begin
if TFile.Exists(ImagePathEdit.Text) then
begin
ImageViewer.Bitmap.LoadFromFile(ImagePathEdit.Text);
PreviewImage.Bitmap.LoadFromFile(ImagePathEdit.Text);
end;
var LMaskImage := ImagePathEdit.Text.Replace('.png','_mask.png');
if TFile.Exists(LMaskImage) then
begin
FPaintBox.LoadFromFile(LMaskImage);
end;
var LOriginalImage := ImagePathEdit.Text.Replace('.png','_original.png');
if TFile.Exists(LOriginalImage) then
begin
SourceImage.Bitmap.LoadFromFile(LOriginalImage);
end;
PromptMemo.Lines.Text := FileMemTable.FieldByName('prompt').AsWideString;
TabControl.ActiveTab := ViewTabItem;
end;
end.