Main.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. Vampyre Imaging Library Demo
  3. VCL Image Browser (ObjectPascal, high level/component sets/canvas, Win32)
  4. tested in Delphi 7/10
  5. written by Marek Mauder
  6. This simple viewer application shows usage of high level class interface
  7. to Imaging library and also drawing images onto standard VCL TCanvas.
  8. TImagingCanvas class is also used here.
  9. In the left part of the window is shell tree view component. Here you can
  10. select files located in your computer. If the selected file is in one of the
  11. supported formats (JPG, BMP, TGA, DDS, PNG, MNG, JNG) it is displayed in the viewer
  12. area and some information about the file is displayed in the info area.
  13. If image file contains subimages you can view them too. Select active subimage
  14. by clicking on buttons with arrows (Previous/Next).
  15. When supported file is selected in shell tree view it is loaded to
  16. TMultiImage and converted to ifA8R8G8B8
  17. data format. Active subimage is then drawn TPainBox component's
  18. client area using DisplayImage procedure (direct bit copy, no need to
  19. convert Imaging's data to TGraphic).
  20. }
  21. unit Main;
  22. {$I ImagingOptions.inc}
  23. {$IF not Defined(COMPONENT_SET_VCL) or not Defined(DELPHI)}
  24. {$MESSAGE ERROR 'This program requires Delphi with VCL'}
  25. {$IFEND}
  26. interface
  27. uses
  28. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  29. Dialogs, ComCtrls, ShellCtrls, ExtCtrls, StdCtrls, Buttons,
  30. ImagingTypes,
  31. Imaging,
  32. ImagingClasses,
  33. ImagingComponents,
  34. ImagingCanvases,
  35. ImagingUtility;
  36. type
  37. TMainForm = class(TForm)
  38. LeftPanel: TPanel;
  39. RightPanel: TPanel;
  40. InfoPanel: TPanel;
  41. LabDataFormat: TLabel;
  42. LabFileFormat: TLabel;
  43. LabDim: TLabel;
  44. LabFileName: TLabel;
  45. Label3: TLabel;
  46. Label2: TLabel;
  47. Label1: TLabel;
  48. Lab1: TLabel;
  49. ViewPanel: TPanel;
  50. PaintBox: TPaintBox;
  51. Tree: TShellTreeView;
  52. Splitter1: TSplitter;
  53. Label4: TLabel;
  54. LabActImage: TLabel;
  55. StatusBar: TStatusBar;
  56. BtnPrev: TSpeedButton;
  57. BtnNext: TSpeedButton;
  58. procedure PaintBoxPaint(Sender: TObject);
  59. procedure FormCreate(Sender: TObject);
  60. procedure FormDestroy(Sender: TObject);
  61. procedure TreeChange(Sender: TObject; Node: TTreeNode);
  62. procedure BtnPrevClick(Sender: TObject);
  63. procedure BtnNextClick(Sender: TObject);
  64. private
  65. FImg: TMultiImage; // Class that hold multiple images (load from MNG or DDS files for instance)
  66. FFileName: string;
  67. FLastTime: LongInt;
  68. public
  69. procedure SetSupported;
  70. procedure SetUnsupported;
  71. procedure LoadFile;
  72. procedure FillDefault;
  73. end;
  74. const
  75. FillColor = $00FFFFA6;
  76. CheckersDensity = 8;
  77. SUnsupportedFormat = 'Selected item format not supported';
  78. var
  79. MainForm: TMainForm;
  80. implementation
  81. {$R *.dfm}
  82. procedure TMainForm.LoadFile;
  83. var
  84. T: Int64;
  85. begin
  86. try
  87. // DetermineFileFormat reads file header and returns image
  88. // file format identifier (like 'jpg', 'tga') if file is valid,
  89. // otherwise empty string is returned
  90. if Imaging.DetermineFileFormat(FFileName) <> '' then
  91. begin
  92. // Load all subimages in file
  93. T := ImagingUtility.GetTimeMicroseconds;
  94. FImg.LoadMultiFromFile(FFileName);
  95. FLastTime := (ImagingUtility.GetTimeMicroseconds - T) div 1000;
  96. StatusBar.SimpleText := Format('Last image loaded in: %.0n ms', [FLastTime * 1.0]);
  97. SetSupported;
  98. // Convert images to 32bit ARGB format for easier drawing later
  99. FImg.ConvertImages(ifA8R8G8B8);
  100. PaintBox.Repaint;
  101. end
  102. else
  103. SetUnsupported;
  104. except
  105. SetUnsupported;
  106. end;
  107. end;
  108. procedure TMainForm.SetSupported;
  109. begin
  110. // Update image info and enable previous/next buttons
  111. LabDim.Caption := Format('%dx%d pixels', [FImg.Width, FImg.Height]);
  112. LabFileFormat.Caption := Imaging.FindImageFileFormat(GetFileExt(FFileName)).Name;
  113. LabDataFormat.Caption := Imaging.GetFormatName(FImg.Format);
  114. LabActImage.Caption := Format('%d/%d', [FImg.ActiveImage + 1, FImg.ImageCount]);
  115. BtnPrev.Enabled := True;
  116. BtnNext.Enabled := True;
  117. end;
  118. procedure TMainForm.SetUnsupported;
  119. var
  120. ImgCanvas: TImagingCanvas;
  121. X, Y, Step: LongInt;
  122. begin
  123. // Set info texts to 'unsupported' and create default image to show
  124. LabDim.Caption := SUnsupportedFormat;
  125. LabFileFormat.Caption := SUnsupportedFormat;
  126. LabDataFormat.Caption := SUnsupportedFormat;
  127. LabActImage.Caption := '0/0';
  128. StatusBar.SimpleText := 'No image loaded';
  129. BtnPrev.Enabled := False;
  130. BtnNext.Enabled := False;
  131. if Assigned(FImg) then
  132. begin
  133. FImg.CreateFromParams(CheckersDensity, CheckersDensity, ifA8R8G8B8, 1);
  134. // Create canvas for image and draw checker board
  135. ImgCanvas := ImagingCanvases.FindBestCanvasForImage(FImg).CreateForImage(FImg);
  136. Step := FImg.Width div CheckersDensity;
  137. for Y := 0 to CheckersDensity - 1 do
  138. for X := 0 to CheckersDensity - 1 do
  139. begin
  140. ImgCanvas.FillColor32 := IffUnsigned((Odd(X) and not Odd(Y)) or (not Odd(X) and Odd(Y)),
  141. pcWhite, pcBlack);
  142. ImgCanvas.FillRect(Rect(X * Step, Y * Step, (X + 1) * Step, (Y + 1) * Step));
  143. end;
  144. ImgCanvas.Free;
  145. end;
  146. PaintBox.Repaint;
  147. end;
  148. procedure TMainForm.BtnPrevClick(Sender: TObject);
  149. begin
  150. FImg.ActiveImage := FImg.ActiveImage - 1;
  151. SetSupported;
  152. PaintBox.Repaint;
  153. end;
  154. procedure TMainForm.BtnNextClick(Sender: TObject);
  155. begin
  156. FImg.ActiveImage := FImg.ActiveImage + 1;
  157. SetSupported;
  158. PaintBox.Repaint;
  159. end;
  160. procedure TMainForm.TreeChange(Sender: TObject; Node: TTreeNode);
  161. begin
  162. // Selected item in the shell tree view has been changed
  163. // we check whether the selected item is valid file in one of the
  164. // supported formats
  165. FFileName := Tree.Path;
  166. LabFileName.Caption := ExtractFileName(FFileName);
  167. if FileExists(FFileName) and Assigned(Imaging.FindImageFileFormat(GetFileExt(FFileName))) then
  168. LoadFile
  169. else
  170. SetUnsupported;
  171. end;
  172. procedure TMainForm.FormDestroy(Sender: TObject);
  173. begin
  174. FImg.Free;
  175. end;
  176. procedure TMainForm.FormCreate(Sender: TObject);
  177. begin
  178. Caption := Caption + ' version ' + Imaging.GetVersionStr;
  179. FImg := TMultiImage.Create;
  180. SetUnsupported;
  181. end;
  182. procedure TMainForm.PaintBoxPaint(Sender: TObject);
  183. var
  184. R: TRect;
  185. begin
  186. FillDefault;
  187. if (FImg.Width > 0) and (FImg.Height > 0) and (FImg.Format = ifA8R8G8B8) then
  188. begin
  189. // Scale image to fit the paint box
  190. R := ImagingUtility.ScaleRectToRect(FImg.BoundsRect, PaintBox.ClientRect);
  191. // Draw image to canvas (without conversion) using OS drawing functions.
  192. // Note that DisplayImage only supports images in ifA8R8G8B8 format so
  193. // if you have image in different format you must convert it or
  194. // create standard TBitmap by calling ImagingComponents.ConvertImageToBitmap
  195. ImagingComponents.DisplayImage(PaintBox.Canvas, R, FImg);
  196. end;
  197. end;
  198. procedure TMainForm.FillDefault;
  199. begin
  200. PaintBox.Canvas.Brush.Color := FillColor;
  201. PaintBox.Canvas.FillRect(PaintBox.ClientRect);
  202. end;
  203. {
  204. File Notes:
  205. -- TODOS ----------------------------------------------------
  206. - nothing now
  207. -- 0.19 Changes/Bug Fixes -----------------------------------
  208. - added canvas usage too
  209. - added support for viewing multiimages (like MNG)
  210. - change drawing to use stuff from ImagingComponents unit instead of
  211. converting to TBitmap
  212. - changed demo to use high level interface instead of low level
  213. }
  214. end.