2
0

test_formatui_main.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {
  3. Created by BGRA Controls Team
  4. (c) 2025 Massimo Magnano
  5. Test Save File Format Settings Form
  6. }
  7. unit test_formatui_main;
  8. {$mode ObjFPC}{$H+}
  9. interface
  10. uses
  11. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, EditBtn, Buttons,
  12. FpImage,
  13. BGRABitmapTypes,
  14. BCPanel, BCRoundedImage, BCButton, BGRADialogs;
  15. type
  16. { TSaveFile_Settings }
  17. TSaveFile_Settings = class(TForm)
  18. BCLabel10: TLabel;
  19. BCLabel9: TLabel;
  20. btLoad: TBCButton;
  21. btSave: TBCButton;
  22. edFileName: TEdit;
  23. lbExt: TLabel;
  24. lbDetails: TLabel;
  25. openPictBGRA: TBGRAOpenPictureDialog;
  26. panelRead: TBCPanel;
  27. bcImage: TBCRoundedImage;
  28. panelWrite: TBCPanel;
  29. cbSaveFormat: TComboBox;
  30. dirDestination: TDirectoryEdit;
  31. Label6: TLabel;
  32. procedure btLoadClick(Sender: TObject);
  33. procedure btSaveClick(Sender: TObject);
  34. procedure cbSaveFormatChange(Sender: TObject);
  35. procedure dirDestinationChange(Sender: TObject);
  36. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  37. procedure FormShow(Sender: TObject);
  38. private
  39. SaveFormat: TBGRAImageFormat;
  40. SaveWriter: TFPCustomImageWriter;
  41. SavePath: String;
  42. panelFormatUI: TBCPanel;
  43. procedure AdjustFormatPanel;
  44. public
  45. end;
  46. var
  47. SaveFile_Settings: TSaveFile_Settings;
  48. implementation
  49. {$R *.lfm}
  50. uses Math, BGRAFormatUI, BGRAReadLzp, BGRAWriteLzp, BGRAPaintNet;
  51. { TSaveFile_Settings }
  52. procedure TSaveFile_Settings.AdjustFormatPanel;
  53. begin
  54. lbExt.Caption:= '.'+SuggestImageExtension(SaveFormat);
  55. if (panelFormatUI <> nil)
  56. then begin
  57. panelFormatUI.Top:= 150; panelFormatUI.Left:= 10;
  58. Width:= Max(780, panelFormatUI.Width+130);
  59. Height:= Max(450, panelFormatUI.Height+150);
  60. panelFormatUI.Parent:= panelWrite;
  61. panelFormatUI.Visible:= True;
  62. end
  63. else begin
  64. Width:= 780;
  65. Height:= 450;
  66. end;
  67. end;
  68. procedure TSaveFile_Settings.cbSaveFormatChange(Sender: TObject);
  69. begin
  70. SaveFormat:= TBGRAImageFormat(PTRUInt(cbSaveFormat.Items.Objects[cbSaveFormat.ItemIndex]));
  71. SaveWriter.Free;
  72. SaveWriter:= CreateBGRAImageWriter(SaveFormat, True);
  73. if (panelFormatUI <> nil) then panelFormatUI.Visible:= False;
  74. TBGRAFormatUIContainer.GetUI(SaveFormat, SaveWriter, panelFormatUI);
  75. AdjustFormatPanel;
  76. end;
  77. procedure TSaveFile_Settings.btLoadClick(Sender: TObject);
  78. begin
  79. try
  80. if openPictBGRA.Execute then
  81. begin
  82. bcImage.Bitmap.LoadFromFile(openPictBGRA.FileName); //'c:\tmp\Acquisitions Book 1.03.01, Byzantine.jpg'
  83. bcImage.Invalidate;
  84. lbDetails.Caption:= 'image: '+IntToStr(bcImage.Bitmap.Width)+' x '+IntToStr(bcImage.Bitmap.Height);
  85. end;
  86. finally
  87. end;
  88. end;
  89. procedure TSaveFile_Settings.btSaveClick(Sender: TObject);
  90. var
  91. ext: String;
  92. begin
  93. if not(bcImage.Bitmap.Empty) then
  94. begin
  95. if (BGRAFormatUIContainer <> nil) and
  96. (panelFormatUI <> nil)
  97. then BGRAFormatUIContainer.SetWriterProperties(SaveWriter);
  98. ext:= '.'+SuggestImageExtension(SaveFormat);
  99. bcImage.Bitmap.SaveToFile(SavePath+DirectorySeparator+edFileName.Text+ext,
  100. SaveWriter);
  101. MessageDlg('Saved as '#13#10+edFileName.Text+ext+#13#10'Class='+SaveWriter.ClassName, mtInformation, [mbOk], 0);
  102. end;
  103. end;
  104. procedure TSaveFile_Settings.dirDestinationChange(Sender: TObject);
  105. begin
  106. SavePath :=dirDestination.Directory;
  107. end;
  108. procedure TSaveFile_Settings.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  109. begin
  110. if (SaveWriter<>nil) then SaveWriter.Free;
  111. if (BGRAFormatUIContainer <> nil) then BGRAFormatUIContainer.Free;
  112. end;
  113. procedure TSaveFile_Settings.FormShow(Sender: TObject);
  114. begin
  115. try
  116. panelFormatUI:= nil;
  117. dirDestination.Directory:= ExtractFileDir(ParamStr(0));
  118. //Select JPeg as Current Format
  119. if (TBGRAFormatUIContainer.BuildSaveFormats(cbSaveFormat, ifJpeg) > 0) then
  120. begin
  121. SaveFormat:= ifJpeg;
  122. SaveWriter:= CreateBGRAImageWriter(SaveFormat, True);
  123. TBGRAFormatUIContainer.GetUI(SaveFormat, SaveWriter, panelFormatUI);
  124. end
  125. else raise Exception.Create('No Writers Registered...');
  126. finally
  127. AdjustFormatPanel;
  128. end;
  129. end;
  130. initialization
  131. RegisterPaintNetFormat;
  132. end.