UnitMain.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. unit UnitMain;
  2. interface
  3. uses
  4. {$IFDEF FPC}LCLIntf, LResources, LCLType, {$ELSE} Winapi.Windows, {$ENDIF}
  5. SysUtils, Variants, Classes, Graphics,
  6. Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
  7. GR32,
  8. GR32_Image,
  9. GR32_Layers;
  10. type
  11. TRectangles = array of TRect;
  12. // A simple custom paint layer used to visualize the tile rectangles
  13. TTileLayer = class(TCustomLayer)
  14. private
  15. FRectangles: TRectangles;
  16. protected
  17. procedure Paint(Buffer: TBitmap32); override;
  18. public
  19. procedure SetRectangles(const ARectangles: TRectangles);
  20. end;
  21. type
  22. TFormMain = class(TForm)
  23. ButtonSave: TButton;
  24. Panel1: TPanel;
  25. ButtonOpen: TButton;
  26. ImgView: TImgView32;
  27. CheckBoxViewTiles: TCheckBox;
  28. procedure ButtonSaveClick(Sender: TObject);
  29. procedure ButtonOpenClick(Sender: TObject);
  30. procedure CheckBoxViewTilesClick(Sender: TObject);
  31. private
  32. FRectangles: TRectangles;
  33. FTileLayer: TTileLayer;
  34. public
  35. constructor Create(AOwner: TComponent); override;
  36. end;
  37. var
  38. FormMain: TFormMain;
  39. implementation
  40. {$R *.dfm}
  41. uses
  42. Types,
  43. GR32.ImageFormats,
  44. GR32.ImageFormats.PSD,
  45. GR32.ImageFormats.PSD.Writer,
  46. GR32.ImageFormats.JPG;
  47. {$ifdef FPC}
  48. function PromptForFilename(var AFilename: string; const AFilter: string;
  49. const ADefaultExt: string = ''; Dummy1: string = ''; Dummy2: string = '';
  50. Save: boolean = False): boolean;
  51. var
  52. Dialog: TOpenDialog;
  53. begin
  54. if (Save) then
  55. Dialog := TSaveDialog.Create(nil)
  56. else
  57. Dialog := TOpenDialog.Create(nil);
  58. try
  59. if (Save) then
  60. Dialog.Options := [ofPathMustExist, ofOverwritePrompt]
  61. else
  62. Dialog.Options := [ofFileMustExist];
  63. Dialog.Filter := AFilter;
  64. Dialog.Filename := AFilename;
  65. Dialog.DefaultExt := ADefaultExt;
  66. Result := Dialog.Execute;
  67. If Result then
  68. AFilename := Dialog.Filename;
  69. finally
  70. Dialog.Free;
  71. end;
  72. end;
  73. {$endif}
  74. procedure ExportTiles(ABitmap: TCustomBitmap32; const ARectangles: TRectangles);
  75. var
  76. Writer: IImageFormatWriter;
  77. FileInfo: IImageFormatFileInfo;
  78. Filter: string;
  79. Filename: string;
  80. PSD: TPhotoshopDocument;
  81. PSDLayer: TCustomPhotoshopLayer;
  82. i: integer;
  83. Stream: TStream;
  84. begin
  85. // Find the image format writer for PSD images
  86. Writer := ImageFormatManager.Writers.FindWriter('psd');
  87. // Get the FileInfo interface from it and construct a filter for the open dialog
  88. if (Writer <> nil) and (Supports(Writer, IImageFormatFileInfo, FileInfo)) then
  89. Filter := Format('%0:s (*.%1:s)|*.%1:s', [FileInfo.ImageFormatDescription, FileInfo.ImageFormatFileTypes[0]])
  90. else
  91. Filter := 'PhotoShop files (*.psd)|*.psd';
  92. if not PromptForFilename(Filename, Filter, 'psd', '', '', True) then
  93. Exit;
  94. PSD := TPhotoshopDocument.Create;
  95. try
  96. // This creates the PSD background image and set the size of the PSD image
  97. PSD.Assign(ABitmap);
  98. // Create a layer for each of the tiles
  99. for i := 0 to High(ARectangles) do
  100. begin
  101. PSDLayer := PSD.Layers.Add;
  102. PSDLayer.BoundsRect := ARectangles[i];
  103. PSDLayer.Name := Format('Layer %d', [i+1]);
  104. // All layers reference the same source ABitmap
  105. TPhotoshopLayer32(PSDLayer).Bitmap := ABitmap;
  106. // Specify the area of the bitmap the PSD layer image should be created from
  107. TPhotoshopLayer32(PSDLayer).SourceRect := ARectangles[i];
  108. end;
  109. Stream := TFileStream.Create(Filename, fmCreate);
  110. try
  111. TPhotoshopDocumentWriter.SaveToStream(PSD, Stream);
  112. finally
  113. Stream.Free;
  114. end;
  115. finally
  116. PSD.Free;
  117. end;
  118. end;
  119. { TTileLayer }
  120. procedure TTileLayer.SetRectangles(const ARectangles: TRectangles);
  121. begin
  122. FRectangles := ARectangles;
  123. Changed;
  124. end;
  125. procedure TTileLayer.Paint(Buffer: TBitmap32);
  126. var
  127. i: integer;
  128. r: TFloatRect;
  129. begin
  130. for i := 0 to High(FRectangles) do
  131. begin
  132. r := FloatRect(FRectangles[i]);
  133. // Rectangle is in bitmap coordinates. Translate it to viewport coordinates
  134. r.TopLeft := LayerCollection.LocalToViewport(r.TopLeft, True);
  135. r.BottomRight := LayerCollection.LocalToViewport(r.BottomRight, True);
  136. // Outline the tile as a semitransparent red rectangle
  137. Buffer.FrameRectTS(MakeRect(r), clTrRed32);
  138. end;
  139. end;
  140. { TFormMain }
  141. procedure TFormMain.CheckBoxViewTilesClick(Sender: TObject);
  142. begin
  143. FTileLayer.Visible := TCheckBox(Sender).Checked;
  144. end;
  145. constructor TFormMain.Create(AOwner: TComponent);
  146. begin
  147. inherited;
  148. FTileLayer := TTileLayer.Create(ImgView.Layers);
  149. FTileLayer.Visible := False;
  150. end;
  151. procedure TFormMain.ButtonOpenClick(Sender: TObject);
  152. var
  153. Filter: string;
  154. Filename: string;
  155. X, Y: integer;
  156. SizeX, SizeY: integer;
  157. begin
  158. Filter := ImageFormatManager.BuildFileFilter(IImageFormatReader, True);
  159. if not PromptForFilename(Filename, Filter) then
  160. exit;
  161. ImgView.Bitmap.LoadFromFile(Filename);
  162. ImgView.Bitmap.DrawMode := dmBlend;
  163. // Divide the bitmap into 3*3=9 equally sized tiles
  164. SizeX := ImgView.Bitmap.Width div 3;
  165. SizeY := ImgView.Bitmap.Height div 3;
  166. SetLength(FRectangles, 9);
  167. // Note that due to rounding the tiles might not cover the whole bitmap.
  168. // For example a 100*100 bitmap will be divided into nine 33*33 tiles
  169. // thus not covering the last column and the last row.
  170. for Y := 0 to 2 do
  171. for X := 0 to 2 do
  172. FRectangles[Y * 3 + X] := Bounds(X * SizeX, Y * SizeY, SizeX, SizeY);
  173. // Pass the rectangles to the custom paint layer so it can draw them
  174. FTileLayer.SetRectangles(FRectangles);
  175. end;
  176. procedure TFormMain.ButtonSaveClick(Sender: TObject);
  177. var
  178. Bitmap: TCustomBitmap32;
  179. begin
  180. Bitmap := ImgView.Bitmap;
  181. if Bitmap.Empty then
  182. Exit;
  183. ExportTiles(Bitmap, FRectangles);
  184. end;
  185. end.