Debian Codegraph @

Wrapper/Delphi/demo/ImagePreview/MainFrm.pas

raw
1 unit MainFrm;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, Menus, ExtCtrls, Math, GR32, GR32_Image, GR32_Transforms,
8 ExtDlgs;
9
10 type
11 TMainForm = class(TForm)
12 PopupMenu: TPopupMenu;
13 ZoomInItem: TMenuItem;
14 ZoomOutItem: TMenuItem;
15 ActualSizeItem: TMenuItem;
16 ImgView32: TImgView32;
17 N1: TMenuItem;
18 AlphaView: TImgView32;
19 ShowAlphaItem: TMenuItem;
20 RotateClockwiseItem: TMenuItem;
21 RotateAntiClockwiseItem: TMenuItem;
22 N3: TMenuItem;
23 ShowWithAlphaItem: TMenuItem;
24 N4: TMenuItem;
25 FlipHorizontalItem: TMenuItem;
26 FilpVerticalItem: TMenuItem;
27 FilterTimer: TTimer;
28 OpenImageItem: TMenuItem;
29 N2: TMenuItem;
30 OpenDialog: TOpenDialog;
31 procedure FormCreate(Sender: TObject);
32 procedure FormDestroy(Sender: TObject);
33 procedure FormShow(Sender: TObject);
34 procedure ZoomInItemClick(Sender: TObject);
35 procedure ZoomOutItemClick(Sender: TObject);
36 procedure ActualSizeItemClick(Sender: TObject);
37 procedure ScrollBoxMouseWheel(Sender: TObject; Shift: TShiftState;
38 WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
39 procedure FormKeyUp(Sender: TObject; var Key: Word;
40 Shift: TShiftState);
41 procedure ShowAlphaItemClick(Sender: TObject);
42 procedure RotateClockwiseItemClick(Sender: TObject);
43 procedure RotateAntiClockwiseItemClick(Sender: TObject);
44 procedure ShowWithAlphaItemClick(Sender: TObject);
45 procedure FlipHorizontalItemClick(Sender: TObject);
46 procedure FilpVerticalItemClick(Sender: TObject);
47 procedure FilterTimerTimer(Sender: TObject);
48 procedure ImgView32Scroll(Sender: TObject);
49 procedure OpenImageItemClick(Sender: TObject);
50 private
51 { Private declarations }
52 OrigWidth : integer;
53 OrigHeight : integer;
54 BPP : longword;
55
56 procedure LoadImage( Name : string);
57 procedure RecalcWindowSize;
58 public
59 { Public declarations }
60 end;
61
62 var
63 MainForm: TMainForm;
64
65 implementation
66
67 {$R *.dfm}
68
69 uses FreeImage, GR32_Resamplers;
70
71 // -----------------------------------------------------------------------------
72 // -----------------------------------------------------------------------------
73 procedure TMainForm.FormCreate(Sender: TObject);
74 begin
75 AlphaView.Visible := False;
76 AlphaView.Align := alClient;
77 end;
78 // -----------------------------------------------------------------------------
79 procedure TMainForm.FormDestroy(Sender: TObject);
80 begin
81 // ...
82 end;
83 // -----------------------------------------------------------------------------
84 procedure TMainForm.FormShow(Sender: TObject);
85 var
86 Resampler: TKernelResampler;
87 begin
88 Resampler := TKernelResampler.Create(ImgView32.Bitmap);
89 Resampler.Kernel := TSplineKernel.Create;
90 if ParamCount = 1 then
91 LoadImage(ParamStr(1));
92 end;
93 // -----------------------------------------------------------------------------
94 procedure TMainForm.LoadImage( Name : string);
95 var
96 dib : PFIBITMAP;
97 PBH : PBITMAPINFOHEADER;
98 PBI : PBITMAPINFO;
99 t : FREE_IMAGE_FORMAT;
100 Ext : string;
101 BM : TBitmap;
102 x, y : integer;
103 BP : PLONGWORD;
104 DC : HDC;
105 begin
106 try
107 t := FreeImage_GetFileType(PAnsiChar(AnsiString(Name)), 16);
108
109 if t = FIF_UNKNOWN then
110 begin
111 // Check for types not supported by GetFileType
112 Ext := UpperCase(ExtractFileExt(Name));
113 if (Ext = '.TGA') or(Ext = '.TARGA') then
114 t := FIF_TARGA
115 else if Ext = '.MNG' then
116 t := FIF_MNG
117 else if Ext = '.PCD' then
118 t := FIF_PCD
119 else if Ext = '.WBMP' then
120 t := FIF_WBMP
121 else if Ext = '.CUT' then
122 t := FIF_CUT
123 else
124 raise Exception.Create('The file "' + Name + '" cannot be displayed because SFM does not recognise the file type.');
125 end;
126
127 dib := FreeImage_Load(t, PAnsiChar(AnsiString(name)), 0);
128 if Dib = nil then
129 Close;
130 PBH := FreeImage_GetInfoHeader(dib);
131 PBI := FreeImage_GetInfo(dib);
132
133 BPP := FreeImage_GetBPP(dib);
134
135 ShowWithAlphaItem.Enabled := BPP = 32;
136 ShowAlphaItem.Enabled := BPP = 32;
137
138 if BPP = 32 then
139 begin
140 ImgView32.Bitmap.SetSize(FreeImage_GetWidth(dib), FreeImage_GetHeight(dib));
141
142 BP := PLONGWORD(FreeImage_GetBits(dib));
143 for y := ImgView32.Bitmap.Height - 1 downto 0 do
144 for x := 0 to ImgView32.Bitmap.Width - 1 do
145 begin
146 ImgView32.Bitmap.Pixel[x, y] := BP^;
147 inc(BP);
148 end;
149 end
150 else
151 begin
152 BM := TBitmap.Create;
153
154 BM.Assign(nil);
155 DC := GetDC(Handle);
156
157 BM.handle := CreateDIBitmap(DC,
158 PBH^,
159 CBM_INIT,
160 PChar(FreeImage_GetBits(dib)),
161 PBI^,
162 DIB_RGB_COLORS);
163
164 ImgView32.Bitmap.Assign(BM);
165 AlphaView.Bitmap.Assign(BM);
166
167 BM.Free;
168 ReleaseDC(Handle, DC);
169 end;
170 FreeImage_Unload(dib);
171
172 OrigWidth := ImgView32.Bitmap.Width;
173 OrigHeight := ImgView32.Bitmap.Height;
174
175 Caption := ExtractFileName( Name ) + ' (' + IntToStr(OrigWidth) +
176 ' x ' + IntToStr(OrigHeight) + ')';
177 if BPP = 32 then
178 Caption := Caption + ' + Alpha';
179
180 AlphaView.Bitmap.SetSize(OrigWidth, OrigWidth);
181
182 ImgView32.Hint := 'Name: ' + Name + #13 +
183 'Width: ' + IntToStr(OrigWidth) + #13 +
184 'Height: ' + IntToStr(OrigHeight) + #13 +
185 'BPP: ' + IntToStr(BPP);
186
187 RecalcWindowSize;
188
189 Show;
190 except
191 on e:exception do
192 begin
193 Application.BringToFront;
194 MessageDlg(e.message, mtInformation, [mbOK], 0);
195 Close;
196 end;
197 end;
198 end;
199 // -----------------------------------------------------------------------------
200 procedure TMainForm.ZoomInItemClick(Sender: TObject);
201 begin
202 FilterTimer.Enabled := False;
203 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
204 TNearestResampler.Create(ImgView32.Bitmap);
205 FilterTimer.Enabled := True;
206
207 ImgView32.Scale := ImgView32.Scale * 2.0;
208 RecalcWindowSize;
209 end;
210 // -----------------------------------------------------------------------------
211 procedure TMainForm.ZoomOutItemClick(Sender: TObject);
212 begin
213 FilterTimer.Enabled := False;
214 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
215 TNearestResampler.Create(ImgView32.Bitmap);
216 FilterTimer.Enabled := True;
217
218 ImgView32.Scale := ImgView32.Scale / 2.0;
219 RecalcWindowSize;
220 end;
221 // -----------------------------------------------------------------------------
222 procedure TMainForm.ActualSizeItemClick(Sender: TObject);
223 begin
224 FilterTimer.Enabled := False;
225 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
226 TNearestResampler.Create(ImgView32.Bitmap);
227 FilterTimer.Enabled := True;
228
229 ImgView32.Scale := 1.0;
230
231 RecalcWindowSize;
232 end;
233 // -----------------------------------------------------------------------------
234 procedure TMainForm.RecalcWindowSize;
235 var
236 Rect : TRect;
237 CW, CH : integer;
238 WSH, WSW : integer;
239 TitleH : integer;
240 BorderY : integer;
241 BorderX : integer;
242 begin
243 CW := ImgView32.Bitmap.Width + GetSystemMetrics(SM_CXVSCROLL);
244 CH := ImgView32.Bitmap.Height + GetSystemMetrics(SM_CYVSCROLL);
245
246 SystemParametersInfo( SPI_GETWORKAREA, 0, @Rect, 0);
247
248 WSH := Rect.Bottom - Rect.Top;
249 WSW := Rect.Right - Rect.Left;
250 TitleH := GetSystemMetrics(SM_CYCAPTION);
251 BorderY := GetSystemMetrics(SM_CYSIZEFRAME) * 2;
252 BorderX := GetSystemMetrics(SM_CXSIZEFRAME) * 2;
253
254 if (Top + CH + TitleH + BorderY > WSH) or (CH + TitleH + BorderY > WSH) then
255 begin
256 Top := Rect.Bottom - CH - BorderY;
257 if Top < 0 then
258 begin
259 Top := 0;
260 CH := WSH - TitleH - BorderY;
261 CW := CW + GetSystemMetrics(SM_CXVSCROLL);
262
263 if CW + BorderX > WSW then
264 CH := CH - GetSystemMetrics(SM_CYVSCROLL);
265 end;
266 end;
267
268 if (Left + CW + BorderX > WSW) or (CW + BorderX > WSW) then
269 begin
270 Left := Rect.Right - CW - BorderX;
271 if Left < 0 then
272 begin
273 Left := 0;
274 CW := WSW - BorderX;
275 CH := CH + GetSystemMetrics(SM_CYVSCROLL);
276
277 if CH + TitleH + BorderY > WSH then
278 CW := CW + GetSystemMetrics(SM_CXVSCROLL);
279 end
280 end;
281
282 ClientWidth := CW;
283 ClientHeight := CH;
284 end;
285 // -----------------------------------------------------------------------------
286 procedure TMainForm.ScrollBoxMouseWheel(Sender: TObject;
287 Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
288 var Handled: Boolean);
289 begin
290 FilterTimer.Enabled := False;
291 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
292 TNearestResampler.Create(ImgView32.Bitmap);
293 FilterTimer.Enabled := True;
294
295 if WheelDelta < 0 then
296 ImgView32.Scroll(0, 20)
297 else
298 ImgView32.Scroll(0, -20);
299 Handled := True;
300 end;
301 // -----------------------------------------------------------------------------
302 procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word;
303 Shift: TShiftState);
304 var
305 Amount : integer;
306 begin
307 FilterTimer.Enabled := False;
308 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
309 TNearestResampler.Create(ImgView32.Bitmap);
310 FilterTimer.Enabled := True;
311
312 if ssShift in Shift then
313 Amount := 20 * 2
314 else
315 Amount := 20;
316
317 case Key of
318 VK_ESCAPE:
319 Close;
320 VK_UP:
321 ImgView32.Scroll(0, -Amount);
322 VK_DOWN:
323 ImgView32.Scroll(0, Amount);
324 VK_LEFT:
325 ImgView32.Scroll(-Amount, 0);
326 VK_RIGHT:
327 ImgView32.Scroll(Amount, 0);
328 VK_HOME:
329 ImgView32.ScrollToCenter(0, 0);
330 VK_END:
331 ImgView32.ScrollToCenter(ImgView32.Bitmap.Width, ImgView32.Bitmap.Height);
332 VK_NEXT:
333 ImgView32.Scroll(0, (Trunc(ImgView32.Bitmap.Height div 4)));
334 VK_PRIOR:
335 ImgView32.Scroll(0, -(Trunc(ImgView32.Bitmap.Height div 4)));
336 end;
337 end;
338 // -----------------------------------------------------------------------------
339 procedure TMainForm.ShowAlphaItemClick(Sender: TObject);
340 var
341 x, y : integer;
342 Col : TColor32;
343 Alpha : TColor;
344 begin
345 if ShowAlphaItem.Checked then
346 begin
347 AlphaView.Visible := False;
348 AlphaView.Bitmap.Delete;
349 end
350 else
351 begin
352 AlphaView.Bitmap.Width := ImgView32.Bitmap.Width;
353 AlphaView.Bitmap.Height := ImgView32.Bitmap.Height;
354
355 for x := 0 to AlphaView.Bitmap.Width - 1 do
356 for y := 0 to AlphaView.Bitmap.Height - 1 do
357 begin
358 Col := ImgView32.Bitmap.Pixel[x, y];
359 Alpha := Col shr 24;
360 AlphaView.Bitmap.Pixel[x, y] := Alpha + (Alpha shl 8) + (Alpha shl 16);
361 end;
362 AlphaView.Visible := True;
363 end;
364 ShowAlphaItem.Checked := not ShowAlphaItem.Checked;
365 end;
366 // -----------------------------------------------------------------------------
367 procedure TMainForm.RotateClockwiseItemClick(Sender: TObject);
368 var
369 x : integer;
370 y : integer;
371 DestX : integer;
372 DestY : integer;
373 C : TColor32;
374 begin
375 AlphaView.Bitmap.Assign(ImgView32.Bitmap);
376
377 ImgView32.BeginUpdate;
378 ImgView32.Bitmap.Width := AlphaView.Bitmap.Height;
379 ImgView32.Bitmap.Height := AlphaView.Bitmap.Width;
380
381 for x := 0 to AlphaView.Bitmap.Width - 1 do
382 for y := 0 to AlphaView.Bitmap.Height - 1 do
383 begin
384 C := AlphaView.Bitmap.Pixel[x, y];
385
386 DestX := (ImgView32.Bitmap.Width - 1) - Y;
387 DestY := X;
388
389 ImgView32.Bitmap.Pixels[DestX, DestY] := C;
390 end;
391
392 ImgView32.EndUpdate;
393 ImgView32.Refresh;
394 end;
395
396 // -----------------------------------------------------------------------------
397 procedure TMainForm.RotateAntiClockwiseItemClick(Sender: TObject);
398 var
399 x : integer;
400 y : integer;
401 DestX : integer;
402 DestY : integer;
403 C : TColor32;
404 begin
405 AlphaView.Bitmap.Assign(ImgView32.Bitmap);
406
407 ImgView32.BeginUpdate;
408 ImgView32.Bitmap.Width := AlphaView.Bitmap.Height;
409 ImgView32.Bitmap.Height := AlphaView.Bitmap.Width;
410
411 for x := 0 to AlphaView.Bitmap.Width - 1 do
412 for y := 0 to AlphaView.Bitmap.Height - 1 do
413 begin
414 C := AlphaView.Bitmap.Pixel[x, y];
415
416 DestX := Y;
417 DestY := (ImgView32.Bitmap.Height - 1) -X;
418
419 ImgView32.Bitmap.Pixels[DestX, DestY] := C;
420 end;
421
422 ImgView32.EndUpdate;
423 ImgView32.Refresh;
424 end;
425 // -----------------------------------------------------------------------------
426 procedure TMainForm.ShowWithAlphaItemClick(Sender: TObject);
427 begin
428 if ShowWithAlphaItem.Checked then
429 ImgView32.Bitmap.DrawMode := dmOpaque
430 else
431 ImgView32.Bitmap.DrawMode := dmBlend;
432 ShowWithAlphaItem.Checked := not ShowWithAlphaItem.Checked;
433 end;
434 // -----------------------------------------------------------------------------
435 procedure TMainForm.FlipHorizontalItemClick(Sender: TObject);
436 var
437 x : integer;
438 y : integer;
439 DestX : integer;
440 DestY : integer;
441 C : TColor32;
442 begin
443 AlphaView.Bitmap.Assign(ImgView32.Bitmap);
444
445 ImgView32.BeginUpdate;
446 ImgView32.Bitmap.Width := AlphaView.Bitmap.Width;
447 ImgView32.Bitmap.Height := AlphaView.Bitmap.Height;
448
449 for x := 0 to AlphaView.Bitmap.Width - 1 do
450 for y := 0 to AlphaView.Bitmap.Height - 1 do
451 begin
452 C := AlphaView.Bitmap.Pixel[x, y];
453
454 DestX := (ImgView32.Bitmap.Width - 1) -X;
455 DestY := Y;
456
457 ImgView32.Bitmap.Pixels[DestX, DestY] := C;
458 end;
459
460 ImgView32.EndUpdate;
461 ImgView32.Refresh;
462 end;
463 // -----------------------------------------------------------------------------
464 procedure TMainForm.FilpVerticalItemClick(Sender: TObject);
465 var
466 x : integer;
467 y : integer;
468 DestX : integer;
469 DestY : integer;
470 C : TColor32;
471 begin
472 AlphaView.Bitmap.Assign(ImgView32.Bitmap);
473
474 ImgView32.BeginUpdate;
475 ImgView32.Bitmap.Width := AlphaView.Bitmap.Width;
476 ImgView32.Bitmap.Height := AlphaView.Bitmap.Height;
477
478 for x := 0 to AlphaView.Bitmap.Width - 1 do
479 for y := 0 to AlphaView.Bitmap.Height - 1 do
480 begin
481 C := AlphaView.Bitmap.Pixel[x, y];
482
483 DestX := X;
484 DestY := (ImgView32.Bitmap.Height - 1) - Y;
485
486 ImgView32.Bitmap.Pixels[DestX, DestY] := C;
487 end;
488
489 ImgView32.EndUpdate;
490 ImgView32.Refresh;
491 end;
492
493 // -----------------------------------------------------------------------------
494 procedure TMainForm.FilterTimerTimer(Sender: TObject);
495 var
496 Resampler: TKernelResampler;
497 begin
498 FilterTimer.Enabled := False;
499 Resampler := TKernelResampler.Create(ImgView32.Bitmap);
500 Resampler.Kernel := TSplineKernel.Create;
501 end;
502 // -----------------------------------------------------------------------------
503 procedure TMainForm.ImgView32Scroll(Sender: TObject);
504 begin
505 FilterTimer.Enabled := False;
506 if not (ImgView32.Bitmap.Resampler is TNearestResampler) then
507 TNearestResampler.Create(ImgView32.Bitmap);
508 FilterTimer.Enabled := True;
509 end;
510 // -----------------------------------------------------------------------------
511 procedure TMainForm.OpenImageItemClick(Sender: TObject);
512 begin
513 if OpenDialog.Execute then
514 begin
515 try
516 Screen.Cursor := crHourGlass;
517 LoadImage(OpenDialog.FileName);
518 finally
519 Screen.Cursor := crDefault;
520 end;
521 end;
522 end;
523
524 end.