Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' FreeBASIC Rotozoom Function for 32-bit images
- #include "fbgfx.bi"
- ' Function to perform rotozoom on a 32-bit image
- function Rotozoom(src as FB.Image Ptr, angle as Single, zoom as Single) as FB.Image Ptr
- dim as Integer src_width = src->width
- dim as Integer src_height = src->height
- dim as Integer dst_width = cint(src_width * zoom)
- dim as Integer dst_height = cint(src_height * zoom)
- dim as FB.Image Ptr dst = imagecreate(dst_width, dst_height, 0)
- dim as Integer src_pitch = src_width * sizeof(UInteger)
- dim as Integer dst_pitch = dst_width * sizeof(UInteger)
- dim as UInteger Ptr src_pixels = cast(UInteger Ptr, src + 1)
- dim as UInteger Ptr dst_pixels = cast(UInteger Ptr, dst + 1)
- dim as Single rad = angle * (3.14159265358979323846 / 180.0)
- dim as Single cos_angle = cos(rad) / zoom
- dim as Single sin_angle = sin(rad) / zoom
- dim as Single cx = (src_width - 1) / 2.0
- dim as Single cy = (src_height - 1) / 2.0
- dim as Single dx = (dst_width - 1) / 2.0
- dim as Single dy = (dst_height - 1) / 2.0
- for y as Integer = 0 to dst_height - 1
- for x as Integer = 0 to dst_width - 1
- dim as Single src_x = cos_angle * (x - dx) + sin_angle * (y - dy) + cx
- dim as Single src_y = -sin_angle * (x - dx) + cos_angle * (y - dy) + cy
- dim as Integer ix = cint(src_x)
- dim as Integer iy = cint(src_y)
- if ix >= 0 and ix < src_width and iy >= 0 and iy < src_height then
- dst_pixels[y * dst_width + x] = src_pixels[iy * src_width + ix]
- else
- dst_pixels[y * dst_width + x] = &h00000000 ' Transparent or black
- end if
- next
- next
- return dst
- end function
- ' Main program to demonstrate the rotozoom function
- screenres 800, 600, 32
- ' Load or create a source image (for example, loading from a file or drawing something)
- dim as FB.Image Ptr src_image = imagecreate(200, 200, 0)
- line src_image, (0, 0)-(199, 199), &hFFFF0000, BF ' Draw a red square
- ' Rotate and zoom the image
- dim as Single angle = 45.0
- dim as Single zoom = 1.5
- dim as FB.Image Ptr dst_image = Rotozoom(src_image, angle, zoom)
- ' Display the resulting image
- put (300, 200), dst_image, alpha
- ' Wait for a key press before exiting
- sleep
- ' Clean up
- imagedestroy(src_image)
- imagedestroy(dst_image)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement