Re: How to rotate a bitmap?



"Heinrich Wolf" <invalid@xxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:3h2ac0Ferac5U1@xxxxxxxxxxxxxxxxx
> Hi,
>
> here is my code.
Or with Integer Math
> It is german. Some translations:
>
> german - english
> Drehen - Rotate
> Winkel - Angle
> Fortschritt - Progress
> MussSpeichern - MustSave

Function GaussTrunc(r : Extended) : longestint;
Begin
If r < 0 Then
GaussTrunc := Pred(Trunc(r))
Else
GaussTrunc := Trunc(r);
End;

Function GaussRound(r : Extended) : longestint;
Begin
GaussRound := GaussTrunc(r + 0.5);
End;

procedure TMainForm.Drehen(xFaktorX, xFaktorY, yFaktorX, yFaktorY : int64;
Aktion : String);
var x, y,
xMax,
yMax,
xHidden,
yHidden,
xMin,
yMin,
xx, xy : Int64;
yx, yy : Array of Int64;
i : Integer;
begin
Enable(False);
xMin := 0;
xMax := 0;
yMin := 0;
yMax := 0;
x := (VisibleImage.Picture.Bitmap.Width - 1) * xFaktorX
+ (VisibleImage.Picture.Bitmap.Height - 1) * yFaktorX
+ $80000000;
y := (VisibleImage.Picture.Bitmap.Width - 1) * xFaktorY
+ (VisibleImage.Picture.Bitmap.Height - 1) * yFaktorY
+ $80000000;
if x < xMin then
xMin := x;
if x > xMax then
xMax := x;
if y < yMin then
yMin := y;
if y > yMax then
yMax := y;
x := (VisibleImage.Picture.Bitmap.Width - 1) * xFaktorX + $80000000;
y := (VisibleImage.Picture.Bitmap.Width - 1) * xFaktorY + $80000000;
if x < xMin then
xMin := x;
if x > xMax then
xMax := x;
if y < yMin then
yMin := y;
if y > yMax then
yMax := y;
x := (VisibleImage.Picture.Bitmap.Height - 1) * yFaktorX + $80000000;
y := (VisibleImage.Picture.Bitmap.Height - 1) * yFaktorY + $80000000;
if x < xMin then
xMin := x;
if x > xMax then
xMax := x;
if y < yMin then
yMin := y;
if y > yMax then
yMax := y;
HiddenImage.Picture.Bitmap.Width := (xMax - xMin + 1) shr 32;
HiddenImage.Picture.Bitmap.Height := (yMax - yMin + 1) shr 32;
HiddenImage.Picture.Bitmap.Canvas.Brush.Style := bsSolid;
HiddenImage.Picture.Bitmap.Canvas.Brush.Color := clWhite;
HiddenImage.Picture.Bitmap.Canvas.FillRect(
Rect(0, 0, HiddenImage.Picture.Bitmap.Width,
HiddenImage.Picture.Bitmap.Height));
SetLength(yx, VisibleImage.Picture.Bitmap.Height);
SetLength(yy, VisibleImage.Picture.Bitmap.Height);
yx[0] := 0;
yy[0] := 0;
for i := 1 to VisibleImage.Picture.Bitmap.Height - 1 do
begin
yx[i] := yx[i - 1] + yFaktorX;
yy[i] := yy[i - 1] + yFaktorY;
end;
FortschrittForm.Start(clWhite, clBlue, Aktion);
x := 0;
while (x < VisibleImage.Picture.Bitmap.Width)
and FortschrittForm.Continue(
(x * 100) div (VisibleImage.Picture.Bitmap.Width - 1)) do
begin
xx := x * xFaktorX;
xy := x * xFaktorY;
y := 0;
while (y < VisibleImage.Picture.Bitmap.Height)
and FortschrittForm.Continue(
(x * 100) div (VisibleImage.Picture.Bitmap.Width - 1))
do
begin
xHidden := (xx + yx[y] - xMin + $80000000) shr 32;
yHidden := (xy + yy[y] - yMin + $80000000) shr 32;
HiddenImage.Picture.Bitmap.Canvas.Pixels[xHidden, yHidden]
:=
VisibleImage.Picture.Bitmap.Canvas.Pixels[x, y];
HiddenImage.Picture.Bitmap.Canvas.Pixels[xHidden + 1, yHidden]
:=
VisibleImage.Picture.Bitmap.Canvas.Pixels[x, y];
HiddenImage.Picture.Bitmap.Canvas.Pixels[xHidden, yHidden +
1] :=
VisibleImage.Picture.Bitmap.Canvas.Pixels[x, y];
inc(y);
end;
inc(x);
end;
if FortschrittForm.Complete then
begin
VisibleImage.Height := HiddenImage.Picture.Bitmap.Height;
VisibleImage.Width := HiddenImage.Picture.Bitmap.Width;
VisibleImage.Picture.Bitmap.Assign(HiddenImage.Picture.Bitmap);
MussSpeichern := True;
end;
Enable(True);
end;

procedure TMainForm.MnuDrehenClick(Sender: TObject);
var w : extended;
begin
ParamForm.Param.Caption := 'Winkel';
ParamForm.SpinEdit.MinValue := -179;
ParamForm.SpinEdit.MaxValue := 180;
ParamForm.SpinEdit.Value := 90;
if ParamForm.ShowModal = mrOk then
if ParamForm.SpinEdit.Value <> 0 then
begin
w := ParamForm.SpinEdit.Value * pi / 180;
Drehen(GaussRound( cos(w) * $100000000),
GaussRound( sin(w) * $100000000),
GaussRound(-sin(w) * $100000000),
GaussRound( cos(w) * $100000000), 'Drehen');
end;
end;


.



Relevant Pages