November 28, 2020, 10:47:21 AM

Author Topic: [bmx] Rotate and flip pixmaps by Pineapple [ 1+ years ago ]  (Read 375 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Rotate and flip pixmaps
Author : Pineapple
Posted : 1+ years ago

Description : Rotates pixmaps CW, CCW, 180 degrees, and flips horizontally and vertically.

Code :
Code: BlitzMax
  1. '       --+-----------------------------------------------------------------------------------------+--
  2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '         |                because that would be a really shitty thing of you to do.                |
  6. '       --+-----------------------------------------------------------------------------------------+--
  7.  
  8.  
  9. SuperStrict
  10.  
  11. Import brl.pixmap
  12.  
  13. ' Example code
  14.  
  15. Rem
  16.  
  17. Graphics 280,398
  18.  
  19. ' make the test pixmap
  20. Local testpix:TPixmap=CreatePixmap(64,48,PF_RGB888)
  21. ClearPixels testpix,~0
  22. For Local i%=0 Until 16
  23.         For Local j%=0 Until 16
  24.                 testpix.WritePixel i,j,$ff0000
  25.                 testpix.WritePixel testpix.width-i-1,j,$00ff00
  26.                 testpix.WritePixel i,testpix.height-j-1,$0000ff
  27.         Next
  28. Next
  29.  
  30. ' draw the original and rotated/flipped pixmaps
  31. Local x%=2,y%=2,yinc%=66
  32. Local tx%=72,ty%=27
  33. DrawPixmap testpix,x,y+yinc*0
  34. DrawPixmap PixmapRotateCW(testpix),x,y+yinc*1
  35. DrawPixmap PixmapRotateCCW(testpix),x,y+yinc*2
  36. DrawPixmap PixmapRotate180(testpix),x,y+yinc*3
  37. DrawPixmap PixmapFlipH(testpix),x,y+yinc*4
  38. DrawPixmap PixmapFlipV(testpix),x,y+yinc*5
  39. DrawText "Original",tx,ty+yinc*0
  40. DrawText "Rotated Clockwise",tx,ty+yinc*1
  41. DrawText "Rotated Counterclockwise",tx,ty+yinc*2
  42. DrawText "Rotated 180",tx,ty+yinc*3
  43. DrawText "Flipped Horizontally",tx,ty+yinc*4
  44. DrawText "Flipped Vertically",tx,ty+yinc*5
  45.  
  46. ' render it to the screen in a loop
  47. Repeat
  48.         Flip
  49.         If KeyDown(27) Or AppTerminate() Then End
  50.         Delay 100
  51. Forever
  52.  
  53. EndRem
  54.  
  55. Function PixmapRotateCW:TPixmap(pix:TPixmap)
  56.         Local ret:TPixmap=CreatePixmap(pix.height,pix.width,pix.format)
  57.         For Local x%=0 Until ret.width
  58.                 Local gy%=pix.height-x-1
  59.                 For Local y%=0 Until ret.height
  60.                         ret.WritePixel x,y,pix.ReadPixel(y,gy)
  61.                 Next
  62.         Next
  63.         Return ret
  64. End Function
  65. Function PixmapRotateCCW:TPixmap(pix:TPixmap)
  66.         Local ret:TPixmap=CreatePixmap(pix.height,pix.width,pix.format)
  67.         For Local y%=0 Until ret.height
  68.                 Local gx%=pix.width-y-1
  69.                 For Local x%=0 Until ret.width
  70.                         ret.WritePixel x,y,pix.ReadPixel(gx,x)
  71.                 Next
  72.         Next
  73.         Return ret
  74. End Function
  75. Function PixmapRotate180:TPixmap(pix:TPixmap)
  76.         Local ret:TPixmap=CreatePixmap(pix.width,pix.height,pix.format)
  77.         For Local x%=0 Until ret.width
  78.                 Local gx%=pix.width-x-1
  79.                 For Local y%=0 Until ret.height
  80.                         Local gy%=pix.height-y-1
  81.                         ret.WritePixel x,y,pix.ReadPixel(gx,gy)
  82.                 Next
  83.         Next
  84.         Return ret
  85. End Function
  86. Function PixmapFlipH:TPixmap(pix:TPixmap)
  87.         Local ret:TPixmap=CreatePixmap(pix.width,pix.height,pix.format)
  88.         For Local x%=0 Until pix.width
  89.                 Local gx%=pix.width-x-1
  90.                 For Local y%=0 Until pix.height
  91.                         ret.WritePixel x,y,pix.ReadPixel(gx,y)
  92.                 Next
  93.         Next
  94.         Return ret
  95. End Function
  96. Function PixmapFlipV:TPixmap(pix:TPixmap)
  97.         Local ret:TPixmap=CreatePixmap(pix.width,pix.height,pix.format)
  98.         For Local y%=0 Until pix.height
  99.                 Local gy%=pix.height-y-1
  100.                 For Local x%=0 Until pix.width
  101.                         ret.WritePixel x,y,pix.ReadPixel(x,gy)
  102.                 Next
  103.         Next
  104.         Return ret
  105. End Function


Comments :


Polan(Posted 1+ years ago)

 For flipping there is XFlipPixmap nad YFlipPixmap function.


Pineapple(Posted 1+ years ago)

 Ah, I wasn't aware. Surprisingly, though, a speed test says that PixmapFlipH is slightly faster than XFlipPixmap.
Code: [Select]
SuperStrict

Framework brl.system
Import brl.standardio
Import "pixmaprot.bmx"

Const n%=2500,n2%=8
Global ms%,nms%,i%

Global pixfliph%,pixflipv%
Global pixflipx%,pixflipy%

Local pix:TPixmap=CreatePixmap(64,64,PF_RGBA8888)
ClearPixels pix,0
For i=0 Until pix.width
pix.WritePixel i,i,$ffff0000
Next

For Local t%=0 Until n2

ms=MilliSecs()
For i=0 Until n
pixmapfliph pix
Next
nms=MilliSecs()
Print "PixmapFlipH: "+(nms-ms)
pixfliph:+(nms-ms)

ms=MilliSecs()
For i=0 Until n
XFlipPixmap pix
Next
nms=MilliSecs()
Print "XFlipPixmap: "+(nms-ms)
pixflipx:+(nms-ms)

ms=MilliSecs()
For i=0 Until n
pixmapflipv pix
Next
nms=MilliSecs()
Print "PixmapFlipV: "+(nms-ms)
pixflipv:+(nms-ms)

ms=MilliSecs()
For i=0 Until n
YFlipPixmap pix
Next
nms=MilliSecs()
Print "YFlipPixmap: "+(nms-ms)
pixflipy:+(nms-ms)

Next

Print "Average PixmapFlipH ms: "+(pixfliph/n2)
Print "Average PixmapFlipV ms: "+(pixflipv/n2)
Print "Average XFlipPixmap ms: "+(pixflipx/n2)
Print "Average YFlipPixmap ms: "+(pixflipy/n2)
Code: [Select]
PixmapFlipH: 540
XFlipPixmap: 496
PixmapFlipV: 475
YFlipPixmap: 52
PixmapFlipH: 374
XFlipPixmap: 409
PixmapFlipV: 373
YFlipPixmap: 43
PixmapFlipH: 376
XFlipPixmap: 409
PixmapFlipV: 368
YFlipPixmap: 44
PixmapFlipH: 377
XFlipPixmap: 424
PixmapFlipV: 364
YFlipPixmap: 42
PixmapFlipH: 368
XFlipPixmap: 427
PixmapFlipV: 373
YFlipPixmap: 45
PixmapFlipH: 367
XFlipPixmap: 385
PixmapFlipV: 369
YFlipPixmap: 42
PixmapFlipH: 386
XFlipPixmap: 392
PixmapFlipV: 395
YFlipPixmap: 44
PixmapFlipH: 398
XFlipPixmap: 416
PixmapFlipV: 373
YFlipPixmap: 52
Average PixmapFlipH ms: 398
Average PixmapFlipV ms: 386
Average XFlipPixmap ms: 419
Average YFlipPixmap ms: 45



TomToad(Posted 1+ years ago)

 Found a bug.  In the last 3 functions, you need to exchange pix.height and pix.width in the CreatePixmap calls.


Pineapple(Posted 1+ years ago)

 Ah, whoops. It's fixed now. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal