Advertisement
Dr_Davenstein

Semi-functioning ChatGPT Rotozoom

May 20th, 2024
1,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
FreeBasic 2.36 KB | Science | 0 0
  1. ' FreeBASIC Rotozoom Function for 32-bit images
  2. #include "fbgfx.bi"
  3.  
  4. ' Function to perform rotozoom on a 32-bit image
  5. function Rotozoom(src as FB.Image Ptr, angle as Single, zoom as Single) as FB.Image Ptr
  6.     dim as Integer src_width = src->width
  7.     dim as Integer src_height = src->height
  8.     dim as Integer dst_width = cint(src_width * zoom)
  9.     dim as Integer dst_height = cint(src_height * zoom)
  10.     dim as FB.Image Ptr dst = imagecreate(dst_width, dst_height, 0)
  11.  
  12.     dim as Integer src_pitch = src_width * sizeof(UInteger)
  13.     dim as Integer dst_pitch = dst_width * sizeof(UInteger)
  14.     dim as UInteger Ptr src_pixels = cast(UInteger Ptr, src + 1)
  15.     dim as UInteger Ptr dst_pixels = cast(UInteger Ptr, dst + 1)
  16.  
  17.     dim as Single rad = angle * (3.14159265358979323846 / 180.0)
  18.     dim as Single cos_angle = cos(rad) / zoom
  19.     dim as Single sin_angle = sin(rad) / zoom
  20.  
  21.     dim as Single cx = (src_width - 1) / 2.0
  22.     dim as Single cy = (src_height - 1) / 2.0
  23.     dim as Single dx = (dst_width - 1) / 2.0
  24.     dim as Single dy = (dst_height - 1) / 2.0
  25.  
  26.     for y as Integer = 0 to dst_height - 1
  27.         for x as Integer = 0 to dst_width - 1
  28.             dim as Single src_x = cos_angle * (x - dx) + sin_angle * (y - dy) + cx
  29.             dim as Single src_y = -sin_angle * (x - dx) + cos_angle * (y - dy) + cy
  30.  
  31.             dim as Integer ix = cint(src_x)
  32.             dim as Integer iy = cint(src_y)
  33.  
  34.             if ix >= 0 and ix < src_width and iy >= 0 and iy < src_height then
  35.                 dst_pixels[y * dst_width + x] = src_pixels[iy * src_width + ix]
  36.             else
  37.                 dst_pixels[y * dst_width + x] = &h00000000 ' Transparent or black
  38.             end if
  39.         next
  40.     next
  41.  
  42.     return dst
  43. end function
  44.  
  45. ' Main program to demonstrate the rotozoom function
  46. screenres 800, 600, 32
  47.  
  48. ' Load or create a source image (for example, loading from a file or drawing something)
  49. dim as FB.Image Ptr src_image = imagecreate(200, 200, 0)
  50. line src_image, (0, 0)-(199, 199), &hFFFF0000, BF ' Draw a red square
  51.  
  52. ' Rotate and zoom the image
  53. dim as Single angle = 45.0
  54. dim as Single zoom = 1.5
  55. dim as FB.Image Ptr dst_image = Rotozoom(src_image, angle, zoom)
  56.  
  57. ' Display the resulting image
  58. put (300, 200), dst_image, alpha
  59.  
  60. ' Wait for a key press before exiting
  61. sleep
  62.  
  63. ' Clean up
  64. imagedestroy(src_image)
  65. imagedestroy(dst_image)
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement