RotateImage - an API+native procedure

Share your advanced PureBasic knowledge/code with the community.
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

RotateImage - an API+native procedure

Post by kenmo »

Here is a versatile procedure for rotating images 0, 90, 180, or 270 degrees (counted in 0-3 quarter turns clockwise)

Code: Select all

RotateImage(SourceImage.i, QuarterTurnsCW.i, DestImage.i = #PB_Ignore)
The full IncludeFile is on GitHub here:
https://raw.githubusercontent.com/kenmo ... eImage.pbi
https://github.com/kenmo-pb/includes/bl ... eImage.pbi

I've used pixel-by-pixel rotation code for a while. But this thread pointed me to the PlgBlt_() Windows API for 90 and 270 rotations, which also led me to StretchBlt_() for faster 180 rotations (at least in my testing).
viewtopic.php?f=12&t=75279

The benefits of this procedure are:
- rotate an image "in-place" (keep the same number), OR to another image, OR to a new #PB_Any image
- uses API functions when possible (only Windows, for now) otherwise falls back to native rotation code
- it has a few smart optimizations, like if you're rotating a square image it won't create an intermediate image since the dimensions stay the same
- Windows/Mac/Linux compatible
- packaged into one portable procedure
- EnableExplicit safe, as always 8)

Cheers