-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit1.pas
343 lines (311 loc) · 10 KB
/
unit1.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
(******************************************************************************)
(* Demo for uLinedit.pas 15.04.2025 *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Visual component to edit a function with multiple base points*)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Buttons, uLineEdit;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Image1: TImage;
Image2: TImage;
Label1: TLabel;
Label2: TLabel;
ScrollBar1: TScrollBar;
ScrollBar2: TScrollBar;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure Button3Click(Sender: TObject);
Procedure Button4Click(Sender: TObject);
Procedure Button5Click(Sender: TObject);
Procedure Button6Click(Sender: TObject);
Procedure Button7Click(Sender: TObject);
Procedure Button8Click(Sender: TObject);
Procedure FormCreate(Sender: TObject);
Procedure ScrollBar1Change(Sender: TObject);
Procedure ScrollBar2Change(Sender: TObject);
Procedure SpeedButton1Click(Sender: TObject);
private
Procedure OnLineEdit1PointChange(Sender: TObject);
Function Scale(value: Word): Word;
public
LineEdit1: TLineEdit;
End;
Var
Form1: TForm1;
Implementation
{$R *.lfm}
Uses
IntfGraphics, // TLazIntfImage
fpImage, // TFPColor
math;
(*
* in x in [0..1], a in [-1 .. 1]
* => result in [0..1]
*)
Function f(x, a: Single): Single;
Begin
// Result := x + a * (1 - 2 * x) * x * (1 - x); // Eine "Andere" Art das Exp via ein Polynom an zu nähern..
// exit;
x := x * 2 - 1; // x in [0..1] -> x in [-1 .. 1]
If x < 0 Then Begin
result := -power(-x, 1 + a);
End
Else Begin
result := power(x, 1 + a);
End;
result := (result + 1) / 2; // result in [-1 ..1 ] -> [0 .. 1]
End;
(*
* in x in [0..1], a in [-0.035 .. 0.035]
* => result.x in [0..1]
* result.y in [0..1]
*)
Function f2(x, a: Single): TPercentPoint;
Var
vx, vy, s, xx, yy: Single;
Begin
// Quell X transformieren
xx := (x - 0.5) * sqrt(2); // [0..1] --> [-sqrt(2)/2 .. sqrt(2)/2]
// Berechnen der Parabel
yy := a * (1 - 2 * sqr(xx)); // [xx, yy]
// Rücktransformation in Ursprungsraum [-0.5 .. 0.5]
s := sqrt(2) / 2;
vx := s * xx - s * yy;
vy := s * xx + s * yy;
// Translation, auf [0..1]
result.x := vx + 0.5;
result.y := vy + 0.5;
End;
{ TForm1 }
Procedure TForm1.FormCreate(Sender: TObject);
Begin
caption := 'LineEdit demo ver. 0.01';
LineEdit1 := TLineEdit.Create(self);
LineEdit1.Name := 'LineEdit1';
LineEdit1.Parent := self;
LineEdit1.Top := 10;
LineEdit1.Left := 10;
LineEdit1.Width := Scale96ToForm(256);
LineEdit1.Height := Scale96ToForm(256);
LineEdit1.BrushColor := clBlack;
LineEdit1.PenColor := clYellow;
LineEdit1.GridColor := clYellow Div 3;
LineEdit1.GridPercent := 25;
LineEdit1.LineColor := clWhite;
LineEdit1.PointBoxColor := clBlue;
LineEdit1.OnPointsChange := @OnLineEdit1PointChange;
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.ScrollBar1Change(Sender: TObject);
Const
steps = 10;
Var
i: integer;
Begin
If LineEdit1.PointCount <> steps Then Begin
LineEdit1.Reset;
For i := 1 To steps - 2 Do Begin
LineEdit1.AddPoint(PercentPoint(100 * i / (steps - 1), 0));
End;
End;
For i := 0 To steps - 1 Do Begin
LineEdit1.SetPointXValue(i, 100 * i / (steps - 1));
LineEdit1.SetPointYValue(i, 100 * f(i / (steps - 1), ScrollBar1.Position / 100));
End;
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.ScrollBar2Change(Sender: TObject);
Const
steps = 10;
Var
i: integer;
p: TPercentPoint;
Begin
LineEdit1.Reset;
For i := 1 To steps - 2 Do Begin
p := f2(i / (steps - 1), ScrollBar2.Position / 100);
p.x := p.x * 100;
p.Y := p.Y * 100;
LineEdit1.AddPoint(p);
End;
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button1Click(Sender: TObject);
Begin
LineEdit1.Reset;
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button2Click(Sender: TObject);
Begin
LineEdit1.Reset;
LineEdit1.SetPointYValue(0, 100);
LineEdit1.SetPointYValue(1, 0);
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button3Click(Sender: TObject);
Var
d: Single;
Begin
// Reduce Colors 5
d := 100 / LineEdit1.Width;
LineEdit1.Reset;
// first point 0,0 already exists
LineEdit1.AddPoint(PercentPoint(20, 0));
LineEdit1.AddPoint(PercentPoint(20 + d, 25));
LineEdit1.AddPoint(PercentPoint(40, 25));
LineEdit1.AddPoint(PercentPoint(40 + d, 50));
LineEdit1.AddPoint(PercentPoint(60, 50));
LineEdit1.AddPoint(PercentPoint(60 + d, 75));
LineEdit1.AddPoint(PercentPoint(80, 75));
LineEdit1.AddPoint(PercentPoint(80 + d, 100));
// last point 100, 100 already exists
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button4Click(Sender: TObject);
Var
d: Single;
Begin
// Reduce Colors 3
d := 100 / LineEdit1.Width;
LineEdit1.Reset;
// first point 0,0 already exists
LineEdit1.AddPoint(PercentPoint(33, 0));
LineEdit1.AddPoint(PercentPoint(33 + d, 50));
LineEdit1.AddPoint(PercentPoint(66, 50));
LineEdit1.AddPoint(PercentPoint(66 + d, 100));
// last point 100, 100 already exists
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button5Click(Sender: TObject);
Var
d: Single;
Begin
// Binarize
d := 100 / LineEdit1.Width;
LineEdit1.Reset;
// first point 0,0 already exists
LineEdit1.AddPoint(PercentPoint(50, 0));
LineEdit1.AddPoint(PercentPoint(50 + d, 100));
// last point 100, 100 already exists
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button6Click(Sender: TObject);
Begin
close;
End;
Procedure TForm1.Button7Click(Sender: TObject);
Begin
// Rainbow 1
LineEdit1.Reset;
LineEdit1.SetPointYValue(0, 100);
LineEdit1.AddPoint(PercentPoint(25, 0));
LineEdit1.AddPoint(PercentPoint(50, 100));
LineEdit1.AddPoint(PercentPoint(75, 0));
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.Button8Click(Sender: TObject);
Begin
// Rainbow 2
LineEdit1.Reset;
LineEdit1.AddPoint(PercentPoint(25, 100));
LineEdit1.AddPoint(PercentPoint(50, 0));
LineEdit1.AddPoint(PercentPoint(75, 100));
LineEdit1.SetPointYValue(100, 0);
OnLineEdit1PointChange(Nil);
End;
Procedure TForm1.SpeedButton1Click(Sender: TObject);
Begin
OnLineEdit1PointChange(Nil);
End;
Function TForm1.Scale(value: Word): Word;
Var
s: Single;
Begin
// 1. Farbwert -> 0..100
s := (value Shr 8) / 2.55;
// 2. Umwandeln durch LineEdit1
s := LineEdit1.F(s);
// 3. 0..100 -> Farbwert
result := min(255, max(0, round(s * 2.55))) Shl 8;
End;
Procedure TForm1.OnLineEdit1PointChange(Sender: TObject);
Var
Target: TBitmap;
SourceIntf: TLazIntfImage;
i, j: Integer;
col: TFPColor;
Begin
SourceIntf := Image1.Picture.Bitmap.CreateIntfImage;
// Anwenden der Filter
For i := 0 To SourceIntf.Width - 1 Do Begin
For j := 0 To SourceIntf.Height - 1 Do Begin
col := SourceIntf.Colors[i, j];
If SpeedButton1.Down Then Begin
col.Red := Scale(col.Red);
End
Else Begin
If SpeedButton2.Down Then Begin
col.Green := Scale(col.Green);
End
Else Begin
If SpeedButton3.Down Then Begin
col.Blue := Scale(col.Blue);
End
Else Begin
// RGB
col.Red := Scale(col.Red);
col.Green := Scale(col.Green);
col.Blue := Scale(col.Blue);
End;
End;
End;
SourceIntf.Colors[i, j] := col;
End;
End;
Target := TBitmap.Create;
Target.LoadFromIntfImage(SourceIntf);
image2.Picture.Assign(Target);
Target.Free;
SourceIntf.free;
End;
End.