2
0

MainUnit.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. unit MainUnit;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is PixelF Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Michael Hansen
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2005
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. uses
  36. {$IFNDEF FPC} Windows, {$ELSE} LResources, LCLType, Variants,{$ENDIF}
  37. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  38. Math, GR32, GR32_LowLevel, GR32_Image, GR32_RangeBars, GR32_Transforms,
  39. GR32_Blend;
  40. type
  41. { TMainForm }
  42. TMainForm = class(TForm)
  43. GbrTwist: TGaugeBar;
  44. Image32: TImage32;
  45. LblTwirlPower: TLabel;
  46. PnlSettings: TPanel;
  47. PnlTwirlDistortion: TPanel;
  48. RbxGetPixelFS: TRadioButton;
  49. RbxPixelS: TRadioButton;
  50. procedure FormCreate(Sender: TObject);
  51. procedure FormDestroy(Sender: TObject);
  52. procedure Image32PaintStage(Sender: TObject; Buffer: TBitmap32;
  53. StageNum: Cardinal);
  54. procedure GbrTwistChange(Sender: TObject);
  55. public
  56. Src: TBitmap32;
  57. procedure TwirlDistortion(Dst, Srcb: TBitmap32; const Value: Integer);
  58. end;
  59. var
  60. MainForm: TMainForm;
  61. implementation
  62. {$R *.dfm}
  63. uses
  64. {$IFDEF Darwin}
  65. MacOSAll,
  66. {$ENDIF}
  67. GR32_Math;
  68. procedure TMainForm.FormCreate(Sender: TObject);
  69. begin
  70. // load example image
  71. Image32.Bitmap.LoadFromResourceName(HInstance, 'STONES', RT_RCDATA);
  72. with Image32 do
  73. begin
  74. if PaintStages[0]^.Stage = PST_CLEAR_BACKGND then PaintStages[0]^.Stage := PST_CUSTOM;
  75. PaintStages.Add^.Stage := PST_CUSTOM;
  76. end;
  77. Image32.BufferOversize := 0;
  78. Src := TBitmap32.Create;
  79. with Src do
  80. begin
  81. SetBorderTransparent(Src, BoundsRect);
  82. Assign(Image32.Bitmap);
  83. OuterColor := clNone32;
  84. end;
  85. end;
  86. procedure TMainForm.FormDestroy(Sender: TObject);
  87. begin
  88. Src.Free;
  89. end;
  90. procedure TMainForm.TwirlDistortion(Dst, Srcb: TBitmap32; const Value: Integer);
  91. {twirl algoritm inspired by Patrick Quinn´s remap demo}
  92. var
  93. X, Y, DstR, DstB: Integer;
  94. Center: TFloatPoint;
  95. Radius, Angle, TwirlAngle, ScaledValue: TFloat;
  96. CosVal, SinVal: Single;
  97. begin
  98. Center.X := Srcb.Width * 0.5;
  99. Center.Y := Srcb.Height * 0.5;
  100. ScaledValue := -Value * 0.2 / Srcb.Height;
  101. DstR := Dst.Width - 1;
  102. DstB := Dst.Height - 1;
  103. if RbxGetPixelFS.Checked then
  104. for Y := 0 to DstB do
  105. for X := 0 to DstR do begin
  106. Radius := GR32_Math.Hypot(X - Center.X, Y - Center.Y);
  107. Angle := ArcTan2(Y - Center.Y, X - Center.X);
  108. TwirlAngle := Angle + Radius * ScaledValue;
  109. GR32_Math.SinCos(TwirlAngle, SinVal, CosVal);
  110. Dst.Pixel[X, Y] := Srcb.PixelFS[Center.X + Radius * CosVal,
  111. Center.Y + Radius * SinVal];
  112. end
  113. else if RbxPixelS.Checked then
  114. for Y := 0 to DstB do
  115. for X := 0 to DstR do begin
  116. Radius := GR32_Math.Hypot(X - Center.X, Y - Center.Y);
  117. Angle := ArcTan2(Y - Center.Y, X - Center.X);
  118. TwirlAngle := Angle + Radius * ScaledValue;
  119. GR32_Math.SinCos(TwirlAngle, SinVal, CosVal);
  120. Dst.Pixel[X, Y] := Srcb.PixelS[Round(Center.X + Radius * CosVal),
  121. Round(Center.Y + Radius * SinVal)];
  122. end;
  123. end;
  124. procedure TMainForm.Image32PaintStage(Sender: TObject; Buffer: TBitmap32;
  125. StageNum: Cardinal);
  126. const
  127. Colors: array [0..1] of TColor32 = ($FFFFFFFF, $FFB0B0B0);
  128. var
  129. W, I, J, Parity: Integer;
  130. Line1, Line2: TArrayOfColor32; // a buffer for a couple of scanlines
  131. begin
  132. with Image32.Buffer do
  133. if StageNum = 0 then
  134. begin
  135. W := Width;
  136. SetLength(Line1, W);
  137. SetLength(Line2, W);
  138. for I := 0 to W - 1 do
  139. begin
  140. Parity := I shr 3 and $1;
  141. Line1[I] := Colors[Parity];
  142. Line2[I] := Colors[1 - Parity];
  143. end;
  144. for J := 0 to Height - 1 do
  145. begin
  146. Parity := J shr 3 and $1;
  147. if Boolean(Parity) then
  148. MoveLongword(Line1[0], ScanLine[J]^, W)
  149. else
  150. MoveLongword(Line2[0], ScanLine[J]^, W);
  151. end;
  152. end
  153. else
  154. FrameRectS(BoundsRect , $FF000000);
  155. end;
  156. procedure TMainForm.GbrTwistChange(Sender: TObject);
  157. begin
  158. with Image32 do
  159. begin
  160. TwirlDistortion(Bitmap, Src, GbrTwist.Position);
  161. GbrTwist.Repaint;
  162. Repaint;
  163. end;
  164. end;
  165. end.