-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathunit1.pas
More file actions
448 lines (411 loc) · 13.9 KB
/
unit1.pas
File metadata and controls
448 lines (411 loc) · 13.9 KB
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
(******************************************************************************)
(* PNG Editor ??.??.???? *)
(* *)
(* Version : 0.07 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Split and merge alpha channel of .png Images *)
(* *)
(* 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 *)
(* 0.02 - Anzeige der Bild Dimensionen, wenn vorhanden *)
(* 0.03 - Checkbox beim Laden der Transparenz zum Binarisieren *)
(* via clFuchsia *)
(* 0.04 - Umstellung auf eigene Zoom Algorithmen beim Rendern *)
(* 0.05 - start mit drag / drop *)
(* 0.06 - improve drag / drop *)
(* 0.07 - FIX: Memleak *)
(* ADD: new .png creation method *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
CheckBox1: TCheckBox;
Image1: TImage;
Image2: TImage;
Label1: TLabel;
Label2: TLabel;
OpenDialog1: TOpenDialog;
OpenDialog2: TOpenDialog;
SaveDialog1: TSaveDialog;
SaveDialog2: TSaveDialog;
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 FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
Procedure FormCreate(Sender: TObject);
Procedure FormDropFiles(Sender: TObject; Const FileNames: Array Of String);
private
fNormalImage: TBitmap; // Original unscalliert
fAlphaImage: TBitmap; // Original unscalliert
Procedure LoadAlpha(Const FileName: String);
Procedure LoadNormal(Const FileName: String);
Procedure RefreshDimensions();
Procedure Clear(Normal, Alpha: Boolean);
Function CreateScaledImage(Const Source: TBitmap): TBitmap;
Function LoadImage(Const Filename: String): TBitmap;
Procedure LoadPNG(Const Filename: String);
public
End;
Var
Form1: TForm1;
Implementation
{$R *.lfm}
Uses
FPWritePNG, IntfGraphics, fpImage, LCLType, ugraphics, GraphType, Math, types;
{ TForm1 }
Procedure TForm1.Button1Click(Sender: TObject);
Begin
// Load PNG
If OpenDialog1.Execute Then Begin
LoadPNG(OpenDialog1.FileName);
End;
End;
Procedure TForm1.Button2Click(Sender: TObject);
Begin
// Load Normal
If OpenDialog2.Execute Then Begin
LoadNormal(OpenDialog2.FileName);
End;
End;
Procedure TForm1.Button3Click(Sender: TObject);
Begin
// Load Alpha
If OpenDialog2.Execute Then Begin
LoadAlpha(OpenDialog2.FileName);
End;
End;
Procedure TForm1.Button4Click(Sender: TObject);
Begin
// Save Normal
If Not assigned(fNormalImage) Then Begin
showmessage('Error, no image to save.');
exit;
End;
If SaveDialog1.Execute Then Begin
fNormalImage.SaveToFile(SaveDialog1.FileName);
End;
End;
Procedure TForm1.Button5Click(Sender: TObject);
Begin
// Save Alpha
If Not assigned(fAlphaImage) Then Begin
showmessage('Error, no image to save.');
exit;
End;
If SaveDialog1.Execute Then Begin
fAlphaImage.SaveToFile(SaveDialog1.FileName);
End;
End;
Procedure TForm1.Button6Click(Sender: TObject);
Var
DestBitmap: TBitmap;
NormalSource, AlphaSource, Dest: TLazIntfImage;
NormalColor, AlphaColor: TFPColor;
i, j: Integer;
writer: TFPWriterPNG;
Begin
If Not assigned(fNormalImage) Or Not assigned(fAlphaImage) Then Begin
showmessage('Error, need both images to store as .png');
exit;
End;
If (fAlphaImage.Width <> fNormalImage.Width) Or (fAlphaImage.Height <> fNormalImage.Height) Then Begin
ShowMessage('Error, alphamask and normal image differ in size.');
exit;
End;
// Save PNG
If SaveDialog2.Execute Then Begin
DestBitmap := TBitmap.Create;
DestBitmap.PixelFormat := pf32bit; // Wichtig für Alpha !
NormalSource := TLazIntfImage.Create(0, 0);
NormalSource.LoadFromBitmap(fNormalImage.Handle, fNormalImage.MaskHandle);
AlphaSource := TLazIntfImage.Create(0, 0);
AlphaSource.LoadFromBitmap(fAlphaImage.Handle, fAlphaImage.MaskHandle);
Dest := DestBitmap.CreateIntfImage;
Dest.SetSize(fAlphaImage.Width, fAlphaImage.Height);
For i := 0 To fAlphaImage.Width - 1 Do Begin
For j := 0 To fAlphaImage.Height - 1 Do Begin
NormalColor := NormalSource.Colors[i, j];
AlphaColor := AlphaSource.Colors[i, j];
NormalColor.Alpha := AlphaColor.red; // Egal ist ja eh Graustufen
Dest.Colors[i, j] := NormalColor;
End;
End;
writer := TFPWriterPNG.Create;
writer.UseAlpha := true;
writer.WordSized := false; // Reduce to 32-Bit (default 64-Bit)
dest.SaveToFile(SaveDialog2.Filename, writer);
writer.Free;
dest.free;
DestBitmap.free;
NormalSource.free;
AlphaSource.free;
End;
End;
Procedure TForm1.Button7Click(Sender: TObject);
Begin
close;
End;
Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
Begin
Clear(true, true);
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
Label2.Caption := '';
caption := 'PNG-Editor ver. 0.06 by Corpsman';
Application.Title := caption;
fNormalImage := Nil;
fAlphaImage := Nil;
End;
Procedure TForm1.FormDropFiles(Sender: TObject; Const FileNames: Array Of String
);
Begin
// Differenzieren nach "ziel" und "quelle" ;)
If FileExists(FileNames[0]) Then Begin
If lowercase(ExtractFileExt(FileNames[0])) = '.png' Then Begin
LoadPNG(FileNames[0]);
End
Else Begin
If PtInRect(Image1.BoundsRect, ScreenToClient(Mouse.CursorPos)) Then Begin
LoadNormal(FileNames[0]);
End
Else Begin
LoadAlpha(FileNames[0]);
End;
End;
End;
End;
Procedure TForm1.RefreshDimensions;
Var
s: String;
Begin
s := '';
If assigned(fNormalImage) Then Begin
s := format('Image: %d x %d', [fNormalImage.Width, fNormalImage.Height]);
End;
If assigned(fAlphaImage) Then Begin
If s <> '' Then s := s + LineEnding;
s := s + format('Alpha: %d x %d', [fAlphaImage.Width, fAlphaImage.Height]);
End;
label2.caption := s;
End;
Procedure TForm1.LoadNormal(Const FileName: String);
Var
ScaledImage: TBitmap;
Begin
Clear(true, false);
fNormalImage := LoadImage(FileName);
fNormalImage.Transparent := false;
ScaledImage := CreateScaledImage(fNormalImage);
Image1.Picture.Assign(ScaledImage);
ScaledImage.free;
RefreshDimensions();
End;
Procedure TForm1.LoadAlpha(Const FileName: String);
Procedure BinariseByCLFuchsia(Const Bitmap: TBitmap);
Var
TempIntfImg: TLazIntfImage;
ImgHandle, ImgMaskHandle: HBitmap;
fuchsia, CurColor: TFPColor;
i, j: Integer;
Begin
TempIntfImg := TLazIntfImage.Create(0, 0);
TempIntfImg.LoadFromBitmap(Bitmap.Handle, Bitmap.MaskHandle);
For j := 0 To bitmap.height - 1 Do
For i := 0 To bitmap.width - 1 Do Begin
fuchsia := ColorToFPColor(clFuchsia);
If TempIntfImg.Colors[i, j] = fuchsia Then Begin
curcolor.red := 0;
End
Else Begin
curcolor.red := 255 * 256;
End;
curcolor.green := curcolor.red;
curcolor.blue := curcolor.red;
curcolor.alpha := 255 * 256;
TempIntfImg.Colors[i, j] := curcolor;
End;
TempIntfImg.CreateBitmaps(ImgHandle, ImgMaskHandle, false);
Bitmap.Handle := ImgHandle;
Bitmap.MaskHandle := ImgMaskHandle;
TempIntfImg.free;
End;
Var
ScaledImage: TBitmap;
Begin
Clear(false, true);
fAlphaImage := LoadImage(FileName);
fAlphaImage.Transparent := false;
If CheckBox1.Checked Then Begin
BinariseByCLFuchsia(fAlphaImage);
End
Else Begin
ConvertToGrayscale(fAlphaImage);
End;
ScaledImage := CreateScaledImage(fAlphaImage);
Image2.Picture.Assign(ScaledImage);
ScaledImage.free;
RefreshDimensions();
End;
Procedure TForm1.Clear(Normal, Alpha: Boolean);
Begin
If Normal Then Begin
If assigned(fNormalImage) Then Begin
fNormalImage.free;
End;
fNormalImage := Nil;
image1.Picture.Clear;
End;
If Alpha Then Begin
If assigned(fAlphaImage) Then Begin
fAlphaImage.free;
End;
fAlphaImage := Nil;
image2.Picture.Clear;
End;
End;
Function TForm1.CreateScaledImage(Const Source: TBitmap): TBitmap;
Var
dimMax, dimx, dimy: integer;
scale: Single;
Mode: TInterpolationMode;
Begin
// Rechnet Source so um, dass es Maximal in ein Bild der Größe 256x256 passt
// Ist Source zu Groß wird es Bilinear klein gerechnet
// sonst Nearest Neighbor gezoomt
result := TBitmap.Create;
result.Width := 256;
result.Height := 256;
// Den nachher nicht übermalten Teil "transparent" machen
result.Transparent := false;
result.Canvas.Pen.Color := clbtnface;
result.Canvas.Brush.Color := clbtnface;
// result.Transparent := true;
// result.TransparentColor := form1.Color;
// result.Canvas.Pen.Color := form1.Color;
// result.Canvas.Brush.Color := form1.Color;
result.Canvas.Rectangle(0, 0, 257, 257);
dimx := source.Width;
dimy := source.Height;
dimMax := max(dimx, dimy);
scale := 256 / dimMax;
If dimMax > 256 Then Begin
// Shrink
mode := imBilinear;
End
Else Begin
// Zoom
mode := imNone;
End;
// Source Transparents kaputt machen
source.TransparentColor := clNone;
source.Transparent := false;
Stretchdraw(result,
rect(
round((256 - scale * dimx) / 2),
round((256 - scale * dimy) / 2),
round((256 + scale * dimx) / 2),
round((256 + scale * dimy) / 2)
), source, Mode);
End;
Function TForm1.LoadImage(Const Filename: String): TBitmap;
Var
gc: TGraphicClass;
g: TGraphic;
Begin
result := TBitmap.Create;
// So generisch wie es eben nur geht Das Bild laden und als TBitmap wieder raus ;)
gc := TPicture.FindGraphicClassWithFileExt(ExtractFileExt(Filename));
g := gc.Create;
g.LoadFromFile(FileName);
result.Assign(g);
g.free;
End;
Procedure TForm1.LoadPNG(Const Filename: String);
Var
PNG: TPortableNetworkGraphic;
i, j: Integer;
Normal, Alpha: TLazIntfImage;
ImgHandle, ImgMaskHandle: HBitmap;
NormalColor, AlphaColor: TFPColor;
ScaledImage: TBitmap;
Begin
OpenDialog1.InitialDir := ExtractFileDir(Filename);
OpenDialog2.InitialDir := ExtractFileDir(Filename);
SaveDialog1.InitialDir := ExtractFileDir(Filename);
SaveDialog2.InitialDir := ExtractFileDir(Filename);
Clear(true, true);
PNG := TPortableNetworkGraphic.Create;
PNG.LoadFromFile(FileName);
fNormalImage := TBitmap.Create;
fNormalImage.Assign(PNG);
PNG.free;
fAlphaImage := TBitmap.Create;
fAlphaImage.Width := fNormalImage.Width;
fAlphaImage.Height := fNormalImage.Height;
Normal := TLazIntfImage.Create(0, 0);
Normal.LoadFromBitmap(fNormalImage.Handle, fNormalImage.MaskHandle);
Alpha := TLazIntfImage.Create(0, 0);
Alpha.LoadFromBitmap(fAlphaImage.Handle, fAlphaImage.MaskHandle);
For i := 0 To fNormalImage.Width - 1 Do Begin
For j := 0 To fNormalImage.Height - 1 Do Begin
NormalColor := Normal.Colors[i, j];
AlphaColor.red := NormalColor.alpha;
AlphaColor.green := NormalColor.alpha;
AlphaColor.blue := NormalColor.alpha;
AlphaColor.alpha := 255 * 256;
Alpha.Colors[i, j] := AlphaColor;
NormalColor.alpha := 255 * 256;
Normal.Colors[i, j] := NormalColor;
End;
End;
Normal.CreateBitmaps(ImgHandle, ImgMaskHandle, false);
fNormalImage.Handle := ImgHandle;
fNormalImage.MaskHandle := ImgMaskHandle;
Alpha.CreateBitmaps(ImgHandle, ImgMaskHandle, false);
fAlphaImage.Handle := ImgHandle;
fAlphaImage.MaskHandle := ImgMaskHandle;
ScaledImage := CreateScaledImage(fNormalImage);
Image1.Picture.Assign(ScaledImage);
ScaledImage.free;
ScaledImage := CreateScaledImage(fAlphaImage);
Image2.Picture.Assign(ScaledImage);
ScaledImage.free;
Normal.free;
Alpha.free;
RefreshDimensions();
End;
End.