-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormCanvasResize.pas
423 lines (372 loc) · 9.52 KB
/
FormCanvasResize.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
unit FormCanvasResize;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Spin, SHP_DataMatrix, OS_SHP_Tools, Palette,
SHP_File, XPMan;
type
TFrmCanvasResize = class(TForm)
PaintAreaPanel: TPanel;
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
SpinT: TSpinEdit;
SpinB: TSpinEdit;
SpinL: TSpinEdit;
SpinR: TSpinEdit;
Bevel1: TBevel;
BtOK: TButton;
BtCancel: TButton;
XPManifest: TXPManifest;
procedure FormResize(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure SpinTChange(Sender: TObject);
procedure SpinBChange(Sender: TObject);
procedure SpinLChange(Sender: TObject);
procedure SpinRChange(Sender: TObject);
procedure Refresh_Image;
procedure Refresh_Image_Bitmap;
procedure FormShow(Sender: TObject);
procedure BtOKClick(Sender: TObject);
procedure BtCancelClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
LockSize : boolean;
FrameImage:TFrameImage;
Bitmap:TBitmap;
SHPPalette:TPalette;
Width,Height:Word;
xstart,ystart,xfinal,yfinal:integer;
changed : boolean; // return
Loop:boolean; //Avoid unlimited loop when importing files
{ Public declarations }
end;
implementation
{$R *.dfm}
procedure TFrmCanvasResize.SpinTChange(Sender: TObject);
begin
if not loop then // avoid crash (unlimited loop) when
begin // Locksize is true
loop := true;
exit;
end;
if ((SpinT.Text = '') or (SpinT.Text = '-')) then exit;
If LockSize then
begin
Loop := false;
SpinB.Value := SpinT.Value + (Height - Bitmap.Height);
Refresh_Image_Bitmap;
end
else
Refresh_Image;
end;
procedure TFrmCanvasResize.SpinBChange(Sender: TObject);
begin
if not loop then
begin
loop := true;
exit;
end;
if ((SpinB.Text = '') or (SpinB.Text = '-')) then exit;
If LockSize then
begin
Loop := false;
SpinT.Value := SpinB.Value + (Height - Bitmap.Height);
Refresh_Image_Bitmap;
end
else
Refresh_Image;
end;
procedure TFrmCanvasResize.SpinLChange(Sender: TObject);
begin
if not loop then
begin
loop := true;
exit;
end;
if ((SpinL.Text = '') or (SpinL.Text = '-')) then exit;
If LockSize then
begin
Loop := false;
SpinR.Value := SpinL.Value + (Width - Bitmap.Width);
Refresh_Image_Bitmap;
end
else
Refresh_Image;
end;
procedure TFrmCanvasResize.SpinRChange(Sender: TObject);
begin
if not loop then
begin
loop := true;
exit;
end;
if ((SpinR.Text = '') or (SpinR.Text = '-')) then exit;
If LockSize then
begin
Loop := false;
SpinL.Value := SpinR.Value + (Width - Bitmap.Width);
Refresh_Image_Bitmap;
end
else
Refresh_Image;
end;
procedure TFrmCanvasResize.Refresh_Image;
var
x,y,xbegin,xend,ybegin,yend: integer;
xpos,ypos,default: integer;
Final: TRect;
begin
if self.Visible = false then exit; // ghosts doesnt need to view it.
// Refresh Image
Image1.Canvas.Brush.Color := SHPPalette[0];
Image1.Canvas.FillRect(Rect(0,0,Image1.Width-1,Image1.Height-1));
if xstart < 0 then
begin
xbegin := 0;
xpos := -xstart;
end
else
begin
xbegin := xstart;
xpos := 0
end;
if xfinal > Image1.Width then
xend := Image1.Width
else
xend := xfinal;
if ystart < 0 then
begin
ybegin := 0;
ypos := -ystart;
end
else
begin
ybegin := ystart;
ypos := 0;
end;
if yfinal > Image1.Height then
yend := Image1.Height
else
yend := yfinal;
dec(xend);
dec(yend);
default := ypos;
// Post Image
for x := xbegin to xend do
begin
for y := ybegin to yend do
begin
Image1.Canvas.Pixels[x,y] := SHPPalette[FrameImage[xpos,ypos]];
inc(ypos);
end;
ypos := default;
inc(xpos);
end;
// Post Selection Canvas
// top horizontal line
if (ybegin - SpinT.Value) > 0 then
Final.Top := ybegin - SpinT.Value
else
Final.Top := 0;
// bottom horizontal line
if (yend + SpinB.Value+1) < Image1.Height then
Final.Bottom := yend + SpinB.Value+1
else
Final.Bottom := Image1.Height-1;
// left vertical line
if (xbegin - SpinL.Value) > 0 then
Final.Left := xbegin - SpinL.Value
else
Final.Left := 0;
// right vertical line
if (xend + SpinR.Value+1) < Image1.Width then
Final.Right := xend + SpinR.Value+1
else
Final.Right := Image1.Width-1;
Image1.Canvas.Brush.Color := OpositeColour(SHPPalette[0]);
Image1.Canvas.DrawFocusRect(Final);
end;
procedure TFrmCanvasResize.Refresh_Image_Bitmap;
var
x,y,xbegin,xend,ybegin,yend:word;
xpos,ypos,default:word;
Final:TRect;
begin
if self.Visible = false then exit; // ghosts doesnt need to view it.
// Refresh Image
Image1.Canvas.Brush.Color := Bitmap.Canvas.Pixels[0,0];
Image1.Canvas.FillRect(Rect(0,0,Image1.Width-1,Image1.Height-1));
if xstart < 0 then
begin
xbegin := 0;
xpos := -xstart;
end
else
begin
xbegin := xstart;
xpos := 0
end;
if xfinal > Image1.Width then
xend := Image1.Width
else
xend := xfinal;
if ystart < 0 then
begin
ybegin := 0;
ypos := -ystart;
end
else
begin
ybegin := ystart;
ypos := 0;
end;
if yfinal > Image1.Height then
yend := Image1.Height
else
yend := yfinal;
default := ypos;
// Post Image
for x := xbegin to xend do
begin
for y := ybegin to yend do
begin
Image1.Canvas.Pixels[x,y] := Bitmap.Canvas.Pixels[xpos,ypos];
inc(ypos);
end;
ypos := default;
inc(xpos);
end;
// Post Selection Canvas
// top horizontal line
if (ybegin - SpinT.Value) > 0 then
Final.Top := ybegin - SpinT.Value
else
Final.Top := 0;
// bottom horizontal line
if (yend + SpinB.Value+1) < Image1.Height then
Final.Bottom := yend + SpinB.Value+1
else
Final.Bottom := Image1.Height-1;
// left vertical line
if (xbegin - SpinL.Value) > 0 then
Final.Left := xbegin - SpinL.Value
else
Final.Left := 0;
// right vertical line
if (xend + SpinR.Value+1) < Image1.Width then
Final.Right := xend + SpinR.Value+1
else
Final.Right := Image1.Width-1;
Image1.Canvas.Brush.Color := OpositeColour(SHPPalette[0]);
Image1.Canvas.DrawFocusRect(Final);
end;
procedure TFrmCanvasResize.FormShow(Sender: TObject);
begin
if Locksize then
begin
// Calculate Image Position
xstart := (Image1.Width - Bitmap.Width) div 2;
xfinal := (Image1.Width + Bitmap.Width) div 2;
ystart := (Image1.Height - Bitmap.Height) div 2;
yfinal := (Image1.Height + Bitmap.Height) div 2;
SpinT.Value := abs(Height - Bitmap.Height) div 2;
SpinL.Value := abs(Width - Bitmap.Width) div 2;
Refresh_Image_Bitmap;
BtCancel.Enabled := false;
changed := true; // always true to avoid headache!
end
else
begin
// Calculate Image Position
xstart := (Image1.Width - Width) div 2;
xfinal := (Image1.Width + Width) div 2;
ystart := (Image1.Height - Height) div 2;
yfinal := (Image1.Height + Height) div 2;
Refresh_Image;
end;
Loop := true;
end;
procedure TFrmCanvasResize.BtOKClick(Sender: TObject);
var
xbegin,xend,ybegin,yend : integer;
begin
// 3.3: Added code that prevents negative sizes ;).
// Check for borders where the image is created...
if xstart < 0 then
xbegin := 0
else
xbegin := xstart;
if xfinal > Image1.Width then
xend := Image1.Width
else
xend := xfinal;
if ystart < 0 then
ybegin := 0
else
ybegin := ystart;
if yfinal > Image1.Height then
yend := Image1.Height
else
yend := yfinal;
// Calculate if they bypass their limits
if (ybegin - SpinT.Value) > (yend + SpinB.Value) then
changed := false
else if (xbegin - SpinL.Value) > (xend + SpinR.Value) then
changed := false
else
changed := true;
// Close.
close;
end;
procedure TFrmCanvasResize.BtCancelClick(Sender: TObject);
begin
changed := false;
close;
end;
procedure TFrmCanvasResize.FormCreate(Sender: TObject);
begin
PaintAreaPanel.DoubleBuffered := true;
end;
procedure TFrmCanvasResize.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_RETURN then
begin
BtOkClick(Sender);
end
else if Key = VK_ESCAPE then
begin
BtCancelClick(Sender);
end;
end;
procedure TFrmCanvasResize.FormResize(Sender: TObject);
begin
if Locksize then
begin
// Calculate Image Position
xstart := (Image1.Width - Bitmap.Width) div 2;
xfinal := (Image1.Width + Bitmap.Width) div 2;
ystart := (Image1.Height - Bitmap.Height) div 2;
yfinal := (Image1.Height + Bitmap.Height) div 2;
SpinT.Value := abs(Height - Bitmap.Height) div 2;
SpinL.Value := abs(Width - Bitmap.Width) div 2;
Refresh_Image_Bitmap;
BtCancel.Enabled := false;
changed := true; // always true to avoid headache!
end
else
begin
// Calculate Image Position
xstart := (Image1.Width - Width) div 2;
xfinal := (Image1.Width + Width) div 2;
ystart := (Image1.Height - Height) div 2;
yfinal := (Image1.Height + Height) div 2;
Refresh_Image;
end;
Loop := true;
end;
end.