MainUnit.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Rotate Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  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} LCLIntf, LCLType, LResources, {$ENDIF}
  37. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, Math,
  38. GR32, GR32_Image, GR32_Transforms, GR32_RangeBars;
  39. type
  40. TFormRotateExample = class(TForm)
  41. Angle: TGaugeBar;
  42. Src: TImage32;
  43. Dst: TImage32;
  44. procedure FormCreate(Sender: TObject);
  45. procedure AngleChange(Sender: TObject);
  46. public
  47. procedure ScaleRot(Alpha: Single);
  48. end;
  49. var
  50. FormRotateExample: TFormRotateExample;
  51. implementation
  52. {$R *.dfm}
  53. uses
  54. Types,
  55. GR32_Math,
  56. GR32.ImageFormats.JPG;
  57. { TFormRotateExample }
  58. procedure TFormRotateExample.FormCreate(Sender: TObject);
  59. begin
  60. // load example image
  61. Src.Bitmap.LoadFromResourceName(HInstance, 'Delphi', RT_RCDATA);
  62. Dst.Bitmap.SetSize(Src.Bitmap.Width, Src.Bitmap.Height);
  63. // a workaround to the edge antialiasing problem
  64. SetBorderTransparent(Src.Bitmap, Src.Bitmap.BoundsRect);
  65. // show the picture
  66. ScaleRot(0);
  67. end;
  68. procedure TFormRotateExample.ScaleRot(Alpha: Single);
  69. var
  70. SrcR: Integer;
  71. SrcB: Integer;
  72. T: TAffineTransformation;
  73. Sn, Cn: TFloat;
  74. Sx, Sy, Scale: Single;
  75. begin
  76. SrcR := Src.Bitmap.Width - 1;
  77. SrcB := Src.Bitmap.Height - 1;
  78. T := TAffineTransformation.Create;
  79. try
  80. T.SrcRect := FloatRect(0, 0, SrcR + 1, SrcB + 1);
  81. // shift the origin
  82. T.Clear;
  83. // move the origin to a center for scaling and rotation
  84. T.Translate(-SrcR * 0.5, -SrcB * 0.5);
  85. T.Rotate(0, 0, Alpha);
  86. Alpha := Alpha * PI / 180;
  87. // get the width and height of rotated image (without scaling)
  88. GR32_Math.SinCos(Alpha, Sn, Cn);
  89. Sx := Abs(SrcR * Cn) + Abs(SrcB * Sn);
  90. Sy := Abs(SrcR * Sn) + Abs(SrcB * Cn);
  91. // calculate a new scale so that the image fits in original *bitmap* boundaries
  92. Sx := Src.Bitmap.Width / Sx;
  93. Sy := Src.Bitmap.Height / Sy;
  94. Scale := Min(Sx, Sy);
  95. T.Scale(Scale);
  96. // move the origin back
  97. T.Translate(SrcR * 0.5, SrcB * 0.5);
  98. // transform the bitmap
  99. Dst.BeginUpdate;
  100. try
  101. Dst.Bitmap.Clear(clBlack32);
  102. Transform(Dst.Bitmap, Src.Bitmap, T);
  103. finally
  104. Dst.EndUpdate;
  105. end;
  106. finally
  107. T.Free;
  108. end;
  109. end;
  110. procedure TFormRotateExample.AngleChange(Sender: TObject);
  111. begin
  112. ScaleRot(-Angle.Position);
  113. end;
  114. end.