-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit2.pas
410 lines (377 loc) · 10.3 KB
/
Unit2.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
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons, ExtCtrls, imgFunction, IniFiles;
type
TfrmAjuste = class(TForm)
Painel: TPanel;
BitBtn1: TBitBtn;
Button1: TButton;
Label1: TLabel;
edtBias: TEdit;
Button2: TButton;
Grade: TStringGrid;
Panel2: TPanel;
ListBox: TListBox;
Button3: TButton;
Splitter1: TSplitter;
ckbSobrepor: TCheckBox;
procedure GradeKeyPress(Sender: TObject; var Key: Char);
procedure GradeKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure edtBiasKeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure ListBoxClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ListBoxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormDeactivate(Sender: TObject);
procedure ListBoxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Splitter1CanResize(Sender: TObject; var NewSize: Integer;
var Accept: Boolean);
procedure GradeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure GradeMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure GradeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
function ContGrid: TSize;
procedure Limpar;
public
Mask: TFloatXYArray;
Bias: Integer;
Ini, DefIni: TMemIniFile;
procedure ApplyMask;
end;
const
RowSeparator: char = '|';
ColSeparator: char = ',';
var
frmAjuste: TfrmAjuste;
implementation
uses Unit1, Math;
{$R *.dfm}
procedure TfrmAjuste.GradeKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9', #8{BackSpace}, '-', DecimalSeparator]) then
Key := #0
else
begin
ListBox.ItemIndex := 0;
Caption := 'Ajuste';
end;
end;
procedure TfrmAjuste.GradeKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if not Grade.EditorMode then Exit;
case Key of
VK_LEFT: if Grade.Col > 0 then Grade.Col := Grade.Col - 1;
VK_RIGHT: if Grade.Col < Grade.ColCount then Grade.Col := Grade.Col + 1;
end;
end;
procedure TfrmAjuste.FormShow(Sender: TObject);
begin
Grade.SetFocus;
end;
procedure TfrmAjuste.Button1Click(Sender: TObject);
var
i, j: Integer;
Texto: string;
begin
if not InputQuery('Informe o novo valor', 'Alteração múltipla', Texto) then Exit;
for j := Grade.Selection.Top to Grade.Selection.Bottom do
for i := Grade.Selection.Left to Grade.Selection.Right do
Grade.Cells[i, j] := Texto;
end;
procedure TfrmAjuste.edtBiasKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9', #8, '-']) then
Key := #0;
end;
procedure TfrmAjuste.Limpar;
var
I: Integer;
begin
for I := 0 to Grade.ColCount -1 do
Grade.Cols[I].Clear;
end;
function TfrmAjuste.ContGrid: TSize;
var
i, j: Integer;
n: Double;
begin
Result.cx := 0;
Result.cy := 0;
for j := 0 to Grade.RowCount -1 do
for i := 0 to Grade.ColCount -1 do
if TryStrToFloat(Grade.Cells[i, j], n) then
begin
if j >= Result.cy then
Result.cy := j + 1;
if i >= Result.cx then
Result.cx := i + 1;
end;
end;
procedure TfrmAjuste.ApplyMask;
var
Size: TSize;
i, j: integer;
begin
Size := ContGrid;
SetLength(Mask, Size.cy, Size.cx);
Bias := MulDiv(StrToIntDef(Trim(edtBias.Text), 0), 255, 100);
for j := 0 to Size.cy - 1 do
for i := 0 to Size.cx - 1 do
begin
Mask[j, i] := StrToFloatDef(Grade.Cells[i, j], 0);
end;
frmMain.btnConvolve.Enabled := (Length(frmAjuste.Mask) > 0) and (not frmMain.imgOriginal.Picture.Bitmap.Empty);
end;
procedure TfrmAjuste.FormCreate(Sender: TObject);
const
FilterExtension = '.ini';
DefaultFile = 'Filtros' + FilterExtension;
var
SearchRec: TSearchRec;
I: Integer;
Strings: TStrings;
begin
I := FindFirst(ExtractFilePath(Application.ExeName) + '*' + FilterExtension, faAnyFile, SearchRec);
Strings := TStringList.Create;
with TMemIniFile.Create('') do
try
while I = 0 do
begin
Rename(SearchRec.Name, True);
GetStrings(Strings);
I := FindNext(SearchRec);
end;
finally
Free;
FindClose(SearchRec);
end;
DefIni := TMemIniFile.Create(ExtractFilePath(Application.ExeName) + DefaultFile);
Ini := TMemIniFile.Create('');
Ini.SetStrings(Strings);
Strings.Free;
Ini.ReadSections(ListBox.Items);
ListBox.Sorted := True;
ListBox.Items.Insert(0, ' Custom');
ListBox.ItemIndex := 0;
end;
procedure TfrmAjuste.FormDestroy(Sender: TObject);
begin
Ini.Free;
DefIni.Free;
end;
procedure TfrmAjuste.Button3Click(Sender: TObject);
var
Nome, s: string;
I: Integer;
begin
ApplyMask;
if Length(Mask) = 0 then Exit;
Nome := Trim(InputBox('Salvar', 'Informe um nome para o Filtro', ''));
if Nome = '' then Exit;
if Ini.SectionExists(Nome) then
begin
ShowMessage('Já existe um Filtro com este nome');
Exit;
end
else
ListBox.Items.Add(Nome);
s := '';
with TStringList.Create do
try
Delimiter := ColSeparator;
for I := Low(Mask) to High(Mask) do
begin
Text := TrimRight(Grade.Rows[I].Text);
s := s + DelimitedText + RowSeparator;
end;
finally
Free;
end;
Ini.WriteInteger(Nome, 'Bias', StrToIntDef(edtBias.Text, 0));
Ini.WriteString(Nome, 'Data', s);
DefIni.WriteInteger(Nome, 'Bias', StrToIntDef(edtBias.Text, 0));
DefIni.WriteString(Nome, 'Data', s);
DefIni.UpdateFile;
ListBox.Sorted := True;
end;
procedure TfrmAjuste.BitBtn1Click(Sender: TObject);
begin
Close;
ApplyMask;
end;
procedure TfrmAjuste.ListBoxClick(Sender: TObject);
var
I: Integer;
Data, Section: string;
Row: TStrings;
begin
if ListBox.ItemIndex = -1 then Exit;
Limpar;
if ListBox.ItemIndex = 0 then
begin
ApplyMask;
Caption := 'Ajuste';
end
else
begin
Section := ListBox.Items[ListBox.ItemIndex];
Caption := 'Ajuste ' + Section;
Data := Ini.ReadString(Section, 'Data', '');
edtBias.Text := Ini.ReadString(Section, 'Bias', '0');
with TStringList.Create do
try
Delimiter := RowSeparator;
DelimitedText := Data;
for I := 0 to Count - 1 do
begin
Row := Grade.Rows[I];
Row.Delimiter := ColSeparator;
Row.DelimitedText := Strings[I];
Grade.Rows[I] := Row;
end;
finally
Free;
end;
ApplyMask;
end;
end;
procedure TfrmAjuste.Button2Click(Sender: TObject);
begin
Limpar;
ListBox.ItemIndex := 0;
end;
procedure TfrmAjuste.ListBoxMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
Index: Integer;
begin
Index := ListBox.ItemAtPos(Point(X, Y), True);
if Index <> -1 then
ListBox.Hint := ListBox.Items[Index];
end;
procedure TfrmAjuste.ListBoxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
Section: string;
cpos: Integer;
begin
with TListBox(Control), Canvas do
begin
if odSelected in State then
begin
Brush.Color := clHighlight;
Font.Color := clHighlightText;
end
else if Odd(Index) then
Brush.Color := $D0ECD0;
if (Index <> 0) and ( not DefIni.SectionExists(Items[Index])) then
begin
Section := '>' + Items[Index];
if not (odSelected in State) then Font.Color := $202040;
end
else
Section := Items[Index];
cpos := AnsiPos('#', Section);
if cpos > 0 then
Section := Copy(Section, 1, cpos - 1);
FillRect(Rect);
TextRect(Rect, Rect.Left, Rect.Top, Section);
end;
end;
procedure TfrmAjuste.FormDeactivate(Sender: TObject);
begin
ApplyMask;
end;
procedure TfrmAjuste.ListBoxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
Index: Integer;
Section, NewSection, Data: string;
Bias: Integer;
begin
Index := ListBox.ItemIndex;
if Index <> -1 then
begin
Section := ListBox.Items[ListBox.ItemIndex];
if DefIni.SectionExists(Section) then
begin
if Key = VK_DELETE then
begin
if Application.MessageBox(PChar('Excluir o Filtro ' + Section + '?'), 'Convoluções', MB_YESNO + MB_ICONQUESTION) = ID_YES then
begin
ListBox.Items.Delete(Index);
DefIni.EraseSection(Section);
DefIni.UpdateFile;
ListBox.Sorted := True;
end;
end
else if Key = VK_F2 then
begin
NewSection := InputBox('Digite o novo nome para o Filtro', 'Convoluções', Section);
if NewSection <> '' then
begin
Data := DefIni.ReadString(Section, 'Data', '');
Bias := DefIni.ReadInteger(Section, 'Bias', 0);
DefIni.EraseSection(Section);
DefIni.WriteString(NewSection, 'Data', Data);
DefIni.WriteInteger(NewSection, 'Bias', Bias);
DefIni.UpdateFile;
ListBox.Items[ListBox.ItemIndex] := NewSection;
ListBox.Sorted := True;
end;
end;
end;
end;
end;
procedure TfrmAjuste.Splitter1CanResize(Sender: TObject;
var NewSize: Integer; var Accept: Boolean);
begin
Accept := (NewSize > 90) and (NewSize < Width div 2);
end;
var
ACol, ARow: Integer;
procedure TfrmAjuste.GradeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if not (ssShift in Shift) then
Grade.MouseToCell(X, Y, ACol, ARow);
end;
procedure TfrmAjuste.GradeMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
gRect: TGridRect;
begin
if csLButtonDown in Grade.ControlState then
begin
gRect.Left := ACol;
gRect.Top := ARow;
Grade.MouseToCell(X, Y, gRect.Right, gRect.Bottom);
Grade.Selection := gRect;
Grade.Repaint;
Grade.Canvas.Brush.Style := bsClear;
with Grade.ScreenToClient(Mouse.CursorPos), gRect do
Grade.Canvas.TextOut(X + 8, Y + 16, Format('%d,%d x %d,%d', [Left + 1, Top + 1, Right - Left + 1, Bottom - Top + 1]));
end;
end;
procedure TfrmAjuste.GradeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Grade.Repaint;
end;
end.