-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
409 lines (372 loc) · 8.92 KB
/
main.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
unit main;
{$mode delphiunicode}
{$codepage UTF8}
{$WARN 5091 off : Local variable "$1" of a managed type does not seem to be initialized}
{$WARN 4105 off : Implicit string type conversion with potential data loss from "$1" to "$2"}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
ExtDlgs, ComCtrls, Types;
type
{ TFormMain }
TFormMain = class(TForm)
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
Bevel4: TBevel;
ButtonLoad: TButton;
ButtonSave: TButton;
CheckBoxScale: TCheckBox;
ComboBoxPalette: TComboBox;
ImageDither: TImage;
ImageListPalette: TImageList;
ImageOrigin: TImage;
Label1: TLabel;
Label2: TLabel;
LabelOrigin: TLabel;
LabelDither: TLabel;
OpenPictureDialog: TOpenPictureDialog;
Panel1: TPanel;
PanelMain: TPanel;
PanelOrigin: TPanel;
PanelDither: TPanel;
SavePictureDialog: TSavePictureDialog;
ScrollBoxOrigin: TScrollBox;
ScrollBoxDither: TScrollBox;
TrackBarDitherPower: TTrackBar;
procedure ButtonLoadClick(Sender: TObject);
procedure ButtonSaveClick(Sender: TObject);
procedure CheckBoxScaleChange(Sender: TObject);
procedure ComboBoxPaletteChange(Sender: TObject);
procedure ComboBoxPaletteDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
procedure PanelMainResize(Sender: TObject);
procedure ScrollBoxDitherPaint(Sender: TObject);
procedure ScrollBoxOriginPaint(Sender: TObject);
procedure TrackBarDitherPowerChange(Sender: TObject);
private
procedure DitherImage;
procedure UpdateImagesSize;
public
end;
var
FormMain: TFormMain;
implementation
uses
BitmapPixels, Dither;
type
TPaletteEntry = record
Name: string;
Palette: array of TPixel;
end;
const
Palettes: array of TPaletteEntry = [
(
Name: 'Solar Eclipse';
Palette:
[
$FFFFFCFE,
$FFFFC200,
$FFFF2A00,
$FF11070A
]
),
(
Name: 'Monochrome';
Palette:
[
$FFFFFFFF,
$FF000000
]
),
(
Name: 'Gray 4';
Palette:
[
$FF000000,
$FF676767,
$FFb6b6b6,
$FFffffff
]
),
(
Name: 'Lemon Lime GB';
Palette:
[
$FFcdb81b,
$FFafb525,
$FF87b133,
$FF5fad41
]
),
(
Name: 'Pollen 8';
Palette:
[
$FF73464C,
$FFAB5675,
$FFEE6A7C,
$FFFFA7A5,
$FFFFE07E,
$FFFFE7D6,
$FF72DCBB,
$FF34ACBA
]
),
(
Name: 'PC 88';
Palette:
[
$FF0000DB,
$FF00B6DB,
$FF00DB6D,
$FFFFB600,
$FFFF926D,
$FFDB0000,
$FFDBDBDB,
$FF000000
]
),
(
Name: 'Pico 8';
Palette:
[
$FF000000,
$FF1D2B53,
$FF7E2553,
$FF008751,
$FFAB5236,
$FF5F574F,
$FFC2C3C7,
$FFFFF1E8,
$FFFF004D,
$FFFFA300,
$FFFFEC27,
$FF00E436,
$FF29ADFF,
$FF83769C,
$FFFF77A8,
$FFFFCCAA
]
),
(
Name: 'Apple II Lo Res';
Palette:
[
$FF000000,
$FF515c16,
$FF843d52,
$FFea7d27,
$FF514888,
$FFe85def,
$FFf5b7c9,
$FF006752,
$FF00c82c,
$FF919191,
$FFc9d199,
$FF00a6f0,
$FF98dbc9,
$FFc8c1f7,
$FFffffff
]
),
(
Name: 'Commodore 64';
Palette:
[
$FF000000,
$FF626262,
$FF898989,
$FFadadad,
$FFffffff,
$FF9f4e44,
$FFcb7e75,
$FF6d5412,
$FFa1683c,
$FFc9d487,
$FF9ae29b,
$FF5cab5e,
$FF6abfc6,
$FF887ecb,
$FF50459b,
$FFa057a3
]
),
(
Name: 'Funky Future';
Palette:
[
$FF2b0f54,
$FFab1f65,
$FFff4f69,
$FFfff7f8,
$FFff8142,
$FFffda45,
$FF3368dc,
$FF49e7ec
]
),
(
Name: 'ArgeeBey';
Palette:
[
$FF000000,
$FF1f246a,
$FF8a1181,
$FFd14444,
$FF2ca53e,
$FF68cbcb,
$FFe3c72d,
$FFffffff
]
),
(
Name: 'FuzzyFour';
Palette:
[
$FF302387,
$FFff3796,
$FF00faac,
$FFfffdaf
]
),
(
Name: 'Ceral GB';
Palette:
[
$FF2b061e,
$FF875053,
$FFd8c86e,
$FFffeed6
]
)
];
{$R *.lfm}
{ TFormMain }
procedure TFormMain.ButtonLoadClick(Sender: TObject);
begin
if OpenPictureDialog.Execute then
begin
ImageOrigin.Picture.LoadFromFile(OpenPictureDialog.FileName);
DitherImage;
UpdateImagesSize;
end;
end;
procedure TFormMain.ButtonSaveClick(Sender: TObject);
begin
if SavePictureDialog.Execute then
begin
ImageDither.Picture.SaveToFile(SavePictureDialog.FileName);
end;
end;
procedure TFormMain.CheckBoxScaleChange(Sender: TObject);
begin
UpdateImagesSize;
end;
procedure TFormMain.ComboBoxPaletteChange(Sender: TObject);
begin
DitherImage;
end;
procedure TFormMain.ComboBoxPaletteDrawItem(Control: TWinControl;
Index: Integer; ARect: TRect; State: TOwnerDrawState);
const
MaxColorCount = 64;
var
Count, I: Integer;
Color: TPixelRec;
OldColor: TColor;
begin
TComboBox(Control).Canvas.FillRect(ARect);
OldColor := TComboBox(Control).Canvas.Brush.Color;
Count := Length(Palettes[Index].Palette);
for I := 0 to Count - 1 do
begin
Color := TPixelRec(Palettes[Index].Palette[I]);
TComboBox(Control).Canvas.Brush.Color := RGBToColor(Color.R, Color.G, Color.B);
TComboBox(Control).Canvas.FillRect(
ARect.Left + I * (MaxColorCount div Count) + 2,
ARect.Top + 1,
ARect.Left + (I + 1) * (MaxColorCount div Count) + 2,
ARect.Bottom - 1
);
end;
TComboBox(Control).Canvas.Brush.Color := OldColor;
ARect.Left := ARect.Left + MaxColorCount + 2;
TComboBox(Control).Canvas.TextOut(ARect.Left, ARect.Top, TComboBox(Control).Items[Index]);
end;
procedure TFormMain.FormCreate(Sender: TObject);
var
PaletteEntry: TPaletteEntry;
begin
for PaletteEntry in Palettes do
begin
ComboBoxPalette.Items.Add(PaletteEntry.Name);
end;
// defaults
ComboBoxPalette.ItemIndex := 0;
TrackBarDitherPower.Position := 200;
CheckBoxScale.Checked := False;
DitherImage;
UpdateImagesSize;
end;
procedure TFormMain.PanelMainResize(Sender: TObject);
begin
PanelOrigin.Width :=
(PanelMain.ClientWidth - PanelMain.ChildSizing.HorizontalSpacing - PanelMain.BorderWidth * 2) div 2 - 1;
end;
procedure TFormMain.ScrollBoxDitherPaint(Sender: TObject);
begin
// это ужастный хак
ScrollBoxOrigin.VertScrollBar.Position := ScrollBoxDither.VertScrollBar.Position;
ScrollBoxOrigin.HorzScrollBar.Position := ScrollBoxDither.HorzScrollBar.Position;
end;
procedure TFormMain.ScrollBoxOriginPaint(Sender: TObject);
begin
// это ужастный хак
ScrollBoxDither.VertScrollBar.Position := ScrollBoxOrigin.VertScrollBar.Position;
ScrollBoxDither.HorzScrollBar.Position := ScrollBoxOrigin.HorzScrollBar.Position;
end;
procedure TFormMain.TrackBarDitherPowerChange(Sender: TObject);
begin
DitherImage;
end;
procedure TFormMain.DitherImage;
var
DitherBitmap: TBitmap;
BitmapData: TBitmapData;
begin
// создаем временный bitmap
DitherBitmap := TBitmap.Create();
try
// копируем в него ImageOrigin
DitherBitmap.Assign(ImageOrigin.Picture.Bitmap);
// получаем доступ к пикселам временного bitmap
BitmapData.Map(DitherBitmap, TAccessMode.ReadWrite, False);
try
// дизеринг (смотри Dither.pas)
FloydSteinbergDithering(BitmapData, Palettes[ComboBoxPalette.ItemIndex].Palette, TrackBarDitherPower.Position);
finally
BitmapData.Unmap();
end;
// применяем временный bitmap к ImageDither
ImageDither.Picture.Assign(DitherBitmap);
finally
DitherBitmap.Free();
end;
ImageDither.Refresh;
end;
procedure TFormMain.UpdateImagesSize;
var
Scale: Integer;
begin
if CheckBoxScale.Checked then
Scale := 2
else
Scale := 1;
ImageOrigin.Width := ImageOrigin.Picture.Width * Scale;
ImageOrigin.Height := ImageOrigin.Picture.Height * Scale;
ImageDither.Width := ImageDither.Picture.Width * Scale;
ImageDither.Height := ImageDither.Picture.Height * Scale;
end;
end.