UnitMain.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, Buttons,
  7. GR32,
  8. GR32_Image,
  9. GR32_Layers,
  10. GR32_Polygons;
  11. type
  12. TFormMain = class(TForm)
  13. ImgView: TImgView32;
  14. ButtonSave: TButton;
  15. Panel1: TPanel;
  16. ButtonRandom: TButton;
  17. ComboBoxCompression: TComboBox;
  18. LabelCompression: TLabel;
  19. ButtonCompressionWarning: TSpeedButton;
  20. procedure FormCreate(Sender: TObject);
  21. procedure ButtonSaveClick(Sender: TObject);
  22. procedure ButtonRandomClick(Sender: TObject);
  23. procedure ComboBoxCompressionChange(Sender: TObject);
  24. procedure ButtonCompressionWarningClick(Sender: TObject);
  25. private
  26. procedure Star(Opacity:integer);
  27. public
  28. end;
  29. var
  30. FormMain: TFormMain;
  31. implementation
  32. {$R *.dfm}
  33. uses
  34. {$ifndef FPC}
  35. System.UITypes,
  36. System.Types,
  37. {$endif}
  38. GR32.Examples,
  39. GR32.ImageFormats.PSD,
  40. GR32.ImageFormats.PSD.Writer,
  41. GR32.ImageFormats.JPG;
  42. {$ifdef FPC}
  43. function PromptForFilename(var AFilename: string; const AFilter: string;
  44. const ADefaultExt: string = ''; Dummy1: string = ''; Dummy2: string = '';
  45. Save: boolean = False): boolean;
  46. var
  47. Dialog: TOpenDialog;
  48. begin
  49. if (Save) then
  50. Dialog := TSaveDialog.Create(nil)
  51. else
  52. Dialog := TOpenDialog.Create(nil);
  53. try
  54. if (Save) then
  55. Dialog.Options := [ofPathMustExist, ofOverwritePrompt]
  56. else
  57. Dialog.Options := [ofFileMustExist];
  58. Dialog.Filter := AFilter;
  59. Dialog.Filename := AFilename;
  60. Dialog.DefaultExt := ADefaultExt;
  61. Result := Dialog.Execute;
  62. If Result then
  63. AFilename := Dialog.Filename;
  64. finally
  65. Dialog.Free;
  66. end;
  67. end;
  68. {$endif}
  69. procedure SaveToPSD(AImgView: TImgView32; ACompression: TPsdLayerCompression);
  70. var
  71. Filename: string;
  72. PhotoshopDocument: TPhotoshopDocument;
  73. Stream: TStream;
  74. begin
  75. if not PromptForFilename(Filename, 'PhotoShop files (*.psd)|*.psd', 'psd', '', '', True) then
  76. Exit;
  77. Stream := TFileStream.Create(Filename, fmCreate);
  78. try
  79. PhotoshopDocument := TPhotoshopDocument.Create;
  80. try
  81. PhotoshopDocument.Compression := ACompression;
  82. // Construct a PSD based on the layers of the TImgView32
  83. PhotoshopDocument.Assign(AImgView);
  84. TPhotoshopDocumentWriter.SaveToStream(PhotoshopDocument, Stream);
  85. finally
  86. PhotoshopDocument.Free;
  87. end;
  88. finally
  89. Stream.Free;
  90. end;
  91. end;
  92. procedure TFormMain.ButtonSaveClick(Sender: TObject);
  93. begin
  94. SaveToPSD(ImgView, TPsdLayerCompression(ComboBoxCompression.ItemIndex));
  95. end;
  96. procedure TFormMain.ComboBoxCompressionChange(Sender: TObject);
  97. begin
  98. ButtonCompressionWarning.Visible := (TPsdLayerCompression(ComboBoxCompression.ItemIndex) >= lcZIP)
  99. end;
  100. function RandomColor():TColor32;
  101. begin
  102. Result := Color32(64 + Random(192),
  103. 64 + Random(192),
  104. 64 + Random(192));
  105. end;
  106. procedure TFormMain.Star(Opacity: integer);
  107. var
  108. BitmapLayer: TBitmapLayer;
  109. i, Steps, nCorners ,X, Y, Diam, t2:integer;
  110. r, Ang: Double;
  111. Poly:TArrayOfFloatPoint;
  112. begin
  113. BitmapLayer := TBitmapLayer.Create(ImgView.Layers);
  114. X := Random(400);
  115. Y := Random(300);
  116. Diam := 50 + Random(100);
  117. t2 := Diam div 2;
  118. nCorners := 4 + Random(6);
  119. BitmapLayer.Bitmap.SetSize(Diam, Diam);
  120. Steps := nCorners * 2;
  121. Setlength(Poly, Steps + 1);
  122. Ang := PI / nCorners;
  123. for i := 0 to Steps do
  124. begin
  125. r := t2;
  126. if Odd(i) then
  127. r := t2 * 0.6;
  128. Poly[i] := FloatPoint(t2 + Sin(i * Ang) * r, t2 + Cos(i * Ang) * r);
  129. end;
  130. GR32_Polygons.PolygonFS(BitmapLayer.Bitmap, Poly, RandomColor());
  131. GR32_Polygons.PolyLineFS(BitmapLayer.Bitmap, Poly, clBlack32,True, 2);
  132. BitmapLayer.Bitmap.DrawMode := dmBlend;
  133. BitmapLayer.Bitmap.MasterAlpha := Opacity;
  134. BitmapLayer.Location := GR32.FloatRect(X, Y, X + Diam, Y + Diam);
  135. BitmapLayer.Scaled := True;
  136. end;
  137. procedure TFormMain.ButtonCompressionWarningClick(Sender: TObject);
  138. begin
  139. MessageDlg('Be aware that many applications only support reading RAW and RLE compressed PSD files', mtWarning, [mbOK], 0);
  140. end;
  141. procedure TFormMain.ButtonRandomClick(Sender: TObject);
  142. var
  143. i: Integer;
  144. begin
  145. ImgView.Layers.Clear;
  146. // Add a bunch of random shapes
  147. for i := 0 to 3 do
  148. Star($FF); // Solid shapes
  149. for i := 0 to 3 do
  150. Star($80); // Semi-transparent shapes
  151. end;
  152. procedure TFormMain.FormCreate(Sender: TObject);
  153. begin
  154. ImgView.Background.CheckersStyle := bcsMedium;
  155. ImgView.Background.FillStyle := bfsCheckers;
  156. ImgView.Bitmap.DrawMode := dmBlend;
  157. ImgView.Bitmap.MasterAlpha := 192;
  158. ImgView.Bitmap.DrawMode := dmBlend;
  159. if Graphics32Examples.MediaFileExists('Monalisa.jpg') then
  160. // Background is a static bitmap
  161. ImgView.Bitmap.LoadFromFile(Graphics32Examples.MediaFolder+'\Monalisa.jpg');
  162. ButtonRandom.Click;
  163. end;
  164. end.