Discussion:
Rotate image 45 degrees
Richard Bleeker
2002-11-25 07:48:28 UTC
Permalink
I am looking for a piece of code which can take a TBitmap (at least 16bpp) and rotate it 45 degrees.

90 degrees is easy to do as it is just a case of swopping X and Y values.
But with all the algorithms I've found and tried, I cannot get a smoothly rotated image - it always has a couple of pixels which are out of place...

Anybody have an algorithm or piece of code for this?

TIA

Richard

[Non-text portions of this message have been removed]


The Delphi Collection Web Site!
http://www.delphicollection.com/public/index.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
h***@quintiles.com
2002-11-25 07:56:55 UTC
Permalink
Hi Richard,
I got this following code from the Delphi Codebook. Try it out and let us know if this one works!
Best regards
Hardy

function RotateImage(Src : TBitmap; Anglo : Real) : TBitmap;
var
i, j, W, H : Integer;
X1, X2, Y1, Y2 : Integer;
C : TColor;
Rx, Ry : Integer;
Side3, FX, FY : Extended;
SDC, DDC : Integer;
begin
W := Src.Width;
H := Src.Height;
TmpMem.Height := Round(H * 2.5);
TmpMem.Width := Round(W * 2.5);
SDC := Src.Canvas.Handle;
DDC := TmpMem.Canvas.Handle;
for i := 0 to W - 1 do
for j := 0 to H - 1 do
begin
C := GetPixel(SDC, i, j);
Side3 := Sqrt(Sqr(i) + Sqr(j));
SinCos((ArcSin(I / Side3) + Anglo), FX, FY);
Rx := Round(FX * Side3) + W;
Ry := Round(FY * Side3) + H;
if j = 1 then
begin
X1 := Rx;
Y1 := Ry;
X2 := Rx;
Y2 := Ry;
end
else
begin
if Rx < X1 then X1 := Rx;
if Ry < Y1 then Y1 := Ry;
if Rx > X2 then X2 := Rx;
if Ry > Y2 then Y2 := Ry;
end;
SetPixel(DDC, Rx , Ry, C);
end;
Result := TmpMem;
end;






"Richard Bleeker" <***@rich.za.net> on 25/11/2002 09:48:28 AM

Please respond to delphi-***@yahoogroups.com

To: <delphi-***@yahoogroups.com>
cc: (bcc: Hardy le Roux/QBLM/Quintiles)

Subject: [Delphi] Rotate image 45 degrees


I am looking for a piece of code which can take a TBitmap (at least 16bpp)
and rotate it 45 degrees.

90 degrees is easy to do as it is just a case of swopping X and Y values.
But with all the algorithms I've found and tried, I cannot get a smoothly
rotated image - it always has a couple of pixels which are out of place...

Anybody have an algorithm or piece of code for this?

TIA

Richard

[Non-text portions of this message have been removed]


The Delphi Collection Web Site!
http://www.delphicollection.com/public/index.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/








**************************************************************************************************
The contents of this email and any attachments are confidential.
It is intended for the named recipient(s) only.
If you have received this email in error please notify the system manager or the
sender immediately and do not disclose the contents to any one or make copies.

** eSafe scanned this email for viruses, vandals and malicious content **
**************************************************************************************************


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/CBxunD/vN2EAA/xGHJAA/i7folB/TM
---------------------------------------------------------------------~->

The Delphi Collection Web Site!
http://www.delphicollection.com/public/index.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Rory Daulton
2002-11-25 15:10:47 UTC
Permalink
Richard,
Post by Richard Bleeker
I am looking for a piece of code which can take a TBitmap (at least 16bpp)
and rotate it 45 degrees.
Post by Richard Bleeker
90 degrees is easy to do as it is just a case of swopping X and Y values.
But with all the algorithms I've found and tried, I cannot get a smoothly
rotated image - it always has a couple of pixels which are out of place...
Post by Richard Bleeker
Anybody have an algorithm or piece of code for this?
I can give you an algorithm that is "smooth" but it also expands the area of
the visible bitmap by a factor of two (meaning that the edge is expanded by
the square root of two). And this "visible bitmap" would be a diamond
shape, meaning that the entire enclosing bitmap would be twice again as
large in area, so the total resulting bitmap would be four times larger in
area (twice larger in height and depth). Is this acceptable for you?

The solution given by ***@quintiles.com will keep the surface area
the same but it is not "smooth." This means that sometimes two distinct
pixels from the source bitmap will go to the same pixel in the destination
bitmap, and you will (probably) get some "empty" pixels in the destination
bitmap. This is due to the fact that a 45 degree rotation with no size
expansion involves multiplication by the square root of 2, which will not
fit comfortably into the integral coordinates used by bitmaps.

Let me know if you want my algorithm.

Rory Daulton
***@email.com
Campus Crusade for Christ


---
[This E-mail scanned for viruses by Declude Virus and Nmax "Your MAXimum Connection!"]
[This E-mail scanned for spam by Declude JunkMail and Nmax "Your MAXimum Connection!"]


The Delphi Collection Web Site!
http://www.delphicollection.com/public/index.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Rory Daulton
2002-11-26 20:10:00 UTC
Permalink
Richard,

I replied to your private request for code to rotate a bitmap by 45 degrees,
but my reply bounced back. Since you first asked on this list, I'll now
reply on the list.
Yes please could you send it to me. I am looking for something which can
do anti-aliasing, and this sounds like it...

Sorry, but there's no anti-aliasing here and I don't even know how to do it.
My algorithm is very simple. Here's the idea. Suppose you start with this
3-by-4 bitmap (you need a monospace font to see this well):

abcd
efgh
ijkl

You can rotate it by 45 degrees smoothly with simple linear transformation
if you expand the area by a factor of two, like this:

..a...
.e*b..
i*f*c.
.j*g*d
..k*h.
...l..

The periods '.' are the background, needed to get a rectangular result. The
astericks '*' are filler. You could get a "smooth" result if you take each
filler pixel to be the average color of its four adjacent pixels. A simpler
approach is to use the color of just one pixel, say the one to its left. If
we also add filler to one side of the resulting diamond, this will make the
result exactly twice the area of the original, and we get:

..aa...
.eebb..
iiffcc.
.jjggdd
..kkhh.
...ll..

There are yet other ways to set those 'filler' pixels, I suppose.

My 'algorithm' is to take the pixel at location (X, Y) [zero-based] in a
bitmap of height H and width W and to copy its color to the new bitmap of
height H + W - 2 and width H + W - 1 at location (X + Y, H - 1 - X + Y).
The 'filler' pixel, the same color according to my second graphic above, is
at location (X + Y, H - X + Y). Before all this copying is done, all pixels
in the destination bitmap should be set to the desired background color, so
the corners are taken care of.

Simple, but it works and it is 'smooth,' and using averaged filler pixels
would be even smoother. Its main disadvantage is that the size of the
bitmap was increased.

I hope this helps.
What is Campus Crusade for Christ?
It is an interdenominational Christian organization, of which I am a staff
member. We now have about 26,000 full-time staff and over 100,000 associate
staff and volunteers. Our purpose is to help fulfill the Great Commission
that Jesus gave in Matthew 28:18-20, which in summary is to "make disciples
of all nations." We do that by winning people to Jesus Christ (evangelism),
building them in their faith, training them to do the work of Christian
ministry, and sending them out to do the same with others. We are probably
most famous for our evangelistic tools such as 'The Four Spiritual Laws'
booklet and the 'JESUS' Film.

The name "Campus Crusade for Christ" comes from our origin on university
campuses in the United States in 1951, but we now have many more ministries.
I have been a missionary to East Africa (Kenya and Tanzania) for a little
more than 25 years, doing a variety of ministries. I am now on furlough in
the United States raising funds, but when I return to Kenya I will be a
half-time computer supervisor at the Nairobi International School of
Theology and a half-time student at the same school to complete my Master of
Divinity degree.

I hope that wasn't more than you wanted!


Rory Daulton
***@email.com
Campus Crusade for Christ


---
[This E-mail scanned for viruses by Declude Virus and Nmax "Your MAXimum Connection!"]
[This E-mail scanned for spam by Declude JunkMail and Nmax "Your MAXimum Connection!"]


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/CBxunD/vN2EAA/xGHJAA/i7folB/TM
---------------------------------------------------------------------~->

The Delphi Collection Web Site!
http://www.delphicollection.com/public/index.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Continue reading on narkive:
Loading...