MainUnit.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. unit MainUnit;
  2. interface
  3. uses
  4. {$IFNDEF FPC} Windows, {$ELSE} LCLIntf, LCLType, {$ENDIF}
  5. SysUtils, Classes, Graphics, Controls, Forms, Menus, Dialogs, ComCtrls,
  6. ExtCtrls, StdCtrls, Math,
  7. GR32, GR32_Image, GR32_Layers, GR32_RangeBars;
  8. type
  9. TFormGammaBlur = class(TForm)
  10. PaintBoxIncorrect: TPaintBox32;
  11. LabelIncorrect: TLabel;
  12. LabelCorrect: TLabel;
  13. PaintBoxCorrect: TPaintBox32;
  14. GaugeBarGamma: TGaugeBar;
  15. LabelGamma: TLabel;
  16. LabelGammaValue: TLabel;
  17. GaugeBarBlurRadius: TGaugeBar;
  18. LabelBlur: TLabel;
  19. LabelBlurValue: TLabel;
  20. Panel1: TPanel;
  21. LabelTestImage: TLabel;
  22. RadioButtonRedGreen: TRadioButton;
  23. RadioButtonCircles: TRadioButton;
  24. CheckBoxUseNew: TCheckBox;
  25. CheckBoxGammaSRGB: TCheckBox;
  26. procedure PaintBoxIncorrectPaintBuffer(Sender: TObject);
  27. procedure PaintBoxCorrectPaintBuffer(Sender: TObject);
  28. procedure GaugeBarGammaChange(Sender: TObject);
  29. procedure FormCreate(Sender: TObject);
  30. procedure GaugeBarBlurRadiusChange(Sender: TObject);
  31. procedure PaintBoxResize(Sender: TObject);
  32. procedure RadioButtonTestImageClick(Sender: TObject);
  33. procedure CheckBoxUseNewClick(Sender: TObject);
  34. procedure CheckBoxGammaSRGBClick(Sender: TObject);
  35. private
  36. FTestBitmap: TBitmap32;
  37. procedure ComposeTestImage;
  38. procedure UpdateGamma;
  39. public
  40. constructor Create(AOwner: TComponent); override;
  41. destructor Destroy; override;
  42. end;
  43. var
  44. FormGammaBlur: TFormGammaBlur;
  45. implementation
  46. {$IFDEF FPC}
  47. {$R *.lfm}
  48. {$ELSE}
  49. {$R *.dfm}
  50. {$ENDIF}
  51. uses
  52. GR32_Math,
  53. GR32_Polygons,
  54. GR32_VectorUtils,
  55. GR32_Gamma,
  56. GR32_System,
  57. GR32_Blurs,
  58. GR32.Blur,
  59. GR32_Resamplers;
  60. { TFrmGammaBlur }
  61. constructor TFormGammaBlur.Create(AOwner: TComponent);
  62. begin
  63. inherited;
  64. PaintBoxIncorrect.BufferOversize := 0;
  65. PaintBoxCorrect.BufferOversize := 0;
  66. FTestBitmap := TBitmap32.Create;
  67. end;
  68. destructor TFormGammaBlur.Destroy;
  69. begin
  70. FTestBitmap.Free;
  71. inherited;
  72. end;
  73. procedure TFormGammaBlur.FormCreate(Sender: TObject);
  74. begin
  75. GaugeBarGammaChange(nil);
  76. GaugeBarBlurRadiusChange(nil);
  77. // Ensure controls are same size in case we messed up at design-time
  78. PaintBoxIncorrect.Width := PaintBoxCorrect.Width;
  79. PaintBoxIncorrect.Height := PaintBoxCorrect.Height;
  80. end;
  81. procedure TFormGammaBlur.GaugeBarBlurRadiusChange(Sender: TObject);
  82. var
  83. BlurRadius: Double;
  84. begin
  85. BlurRadius := 0.1 * GaugeBarBlurRadius.Position;
  86. LabelBlurValue.Caption := Format('%.1n px', [BlurRadius]);
  87. PaintBoxIncorrect.Invalidate;
  88. PaintBoxCorrect.Invalidate;
  89. end;
  90. procedure TFormGammaBlur.GaugeBarGammaChange(Sender: TObject);
  91. begin
  92. UpdateGamma;
  93. end;
  94. procedure ComposeTestImageRedGreen(Bitmap: TBitmap32);
  95. begin
  96. Bitmap.Clear(clRed32);
  97. Bitmap.FillRect(0, 0, Bitmap.Width, Bitmap.Height div 2, clLime32);
  98. end;
  99. procedure ComposeTestImageCircles(Bitmap: TBitmap32);
  100. var
  101. Points: TArrayOfFloatPoint;
  102. Index: Integer;
  103. begin
  104. Bitmap.Clear(clBlack32);
  105. RandSeed := integer($DEADBABE);
  106. for Index := 0 to 70 do
  107. begin
  108. Points := Circle(Bitmap.Width * Random, Bitmap.Height * Random,
  109. 0.5 * Min(Bitmap.Width, Bitmap.Height) * Random);
  110. PolygonFS(Bitmap, Points, HSLtoRGB(Random, 1, 0.5));
  111. end;
  112. end;
  113. procedure TFormGammaBlur.CheckBoxGammaSRGBClick(Sender: TObject);
  114. begin
  115. UpdateGamma;
  116. end;
  117. procedure TFormGammaBlur.CheckBoxUseNewClick(Sender: TObject);
  118. begin
  119. PaintBoxCorrect.Invalidate;
  120. PaintBoxIncorrect.Invalidate;
  121. end;
  122. procedure TFormGammaBlur.ComposeTestImage;
  123. begin
  124. if RadioButtonCircles.Checked then
  125. ComposeTestImageCircles(FTestBitmap)
  126. else
  127. ComposeTestImageRedGreen(FTestBitmap);
  128. end;
  129. procedure TFormGammaBlur.PaintBoxResize(Sender: TObject);
  130. begin
  131. Assert(PaintBoxCorrect.Width = PaintBoxIncorrect.Width);
  132. Assert(PaintBoxCorrect.Height = PaintBoxIncorrect.Height);
  133. FTestBitmap.SetSize(PaintBoxCorrect.Width, PaintBoxCorrect.Height);
  134. ComposeTestImage;
  135. end;
  136. procedure TFormGammaBlur.RadioButtonTestImageClick(Sender: TObject);
  137. begin
  138. ComposeTestImage;
  139. PaintBoxCorrect.Invalidate;
  140. PaintBoxIncorrect.Invalidate;
  141. end;
  142. procedure TFormGammaBlur.UpdateGamma;
  143. var
  144. GammaValue: Double;
  145. begin
  146. GaugeBarGamma.Enabled := (not CheckBoxGammaSRGB.Checked);
  147. if (CheckBoxGammaSRGB.Checked) then
  148. begin
  149. Set_sRGB;
  150. LabelGammaValue.Caption := 'sRGB';
  151. end else
  152. begin
  153. GammaValue := 0.001 * GaugeBarGamma.Position;
  154. LabelGammaValue.Caption := Format('%.3n', [GammaValue]);
  155. SetGamma(GammaValue);
  156. end;
  157. PaintBoxIncorrect.Invalidate;
  158. PaintBoxCorrect.Invalidate;
  159. end;
  160. procedure TFormGammaBlur.PaintBoxCorrectPaintBuffer(Sender: TObject);
  161. begin
  162. if CheckBoxUseNew.Checked then
  163. GammaBlur32(FTestBitmap, PaintBoxCorrect.Buffer, 0.1 * GaugeBarBlurRadius.Position)
  164. else
  165. begin
  166. FTestBitmap.DrawTo(PaintBoxCorrect.Buffer);
  167. GaussianBlurGamma(PaintBoxCorrect.Buffer, 0.1 * GaugeBarBlurRadius.Position);
  168. end;
  169. end;
  170. procedure TFormGammaBlur.PaintBoxIncorrectPaintBuffer(Sender: TObject);
  171. begin
  172. if CheckBoxUseNew.Checked then
  173. Blur32(FTestBitmap, PaintBoxIncorrect.Buffer, 0.1 * GaugeBarBlurRadius.Position)
  174. else
  175. begin
  176. FTestBitmap.DrawTo(PaintBoxIncorrect.Buffer);
  177. GaussianBlur(PaintBoxIncorrect.Buffer, 0.1 * GaugeBarBlurRadius.Position);
  178. end;
  179. end;
  180. end.