January 15, 2021, 06:41:08 PM

Author Topic: [bb] antialias/blend by smitty [ 1+ years ago ]  (Read 474 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] antialias/blend by smitty [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : antialias/blend
Author : smitty
Posted : 1+ years ago

Description : a very simple way of blending between pixels..nothing new or complex here but it seems to work quite well.

Code :
Code: BlitzBasic
  1. ; anti alias/blend example (for sprite editor maybe?)
  2. Graphics 800,600,32
  3. Global backgnd=CreateImage(800,600);
  4. Global mx,my ;global mouse coords
  5. Dim zz(7),rt(7),gt(7),bt(7) ;holds colour values
  6. ; draw a cursor
  7. Global cursor=CreateImage(32,32)
  8. SetBuffer ImageBuffer(cursor)
  9. Color $aa,$aa,$aa
  10. Oval 0,0,32,32,0
  11. MidHandle cursor
  12.  
  13. SetBuffer BackBuffer()
  14. ;draw the large grid
  15. drawgrid()
  16. ;draw a random colour palette
  17. For x=0 To 31
  18.         For y=0 To 3
  19.                 Color Rnd(255),Rnd(255),Rnd(255)
  20.                 Rect (x*16)+16,(y*16)+528,16,16,1
  21.         Next
  22. Next
  23. ; draw a frame for the pixel grid
  24. Color $ff,$ff,$ff
  25. Rect 598,198,36,36,0
  26. ;copy screen to background
  27. CopyRect(0,0,800,600,0,0,BackBuffer(),ImageBuffer(backgnd))
  28.  
  29.  
  30. ;main loop
  31. While Not KeyHit(1)
  32. CopyRect(0,0,800,600,0,0,ImageBuffer(backgnd),BackBuffer())
  33. mx=MouseX()
  34. my=MouseY()
  35. tmx=mx/16
  36. tmy=my/16
  37. DrawImage cursor,mx,my
  38. If mx<512 And my<512 ;check if within the grid area
  39.         If MouseDown(1); normal draw
  40.                 SetBuffer ImageBuffer(backgnd)
  41.                 Rect  tmx*16,tmy*16,16,16,1
  42.                 Plot tmx+600,tmy+200
  43.         EndIf
  44.         If MouseDown(2);anti alias
  45.                 SetBuffer ImageBuffer(backgnd)
  46.                 ;get the surrounding colour values             
  47.                 zz(0)=ReadPixel(tmx+600,tmy+200-1) ;get color of pixel top
  48.                 zz(1)=ReadPixel(tmx+600,tmy+200+1) ;get color of pixel bottom
  49.                 zz(2)=ReadPixel(tmx+600+1,tmy+200); pixel to right     
  50.                 zz(3)=ReadPixel(tmx+600-1,tmy+200); pixel to left
  51.                 zz(4)=ReadPixel(tmx+600-1,tmy+200-1); pixel to topleft
  52.                 zz(5)=ReadPixel(tmx+600+1,tmy+200-1); pixel to topright
  53.                 zz(6)=ReadPixel(tmx+600-1,tmy+200+1); pixel to leftbottom
  54.                 zz(7)=ReadPixel(tmx+600+1,tmy+200+1); pixel to rightbottom
  55.                                
  56.                 ;calculate  color value for the anti-aliased pixel
  57.                 antialias()
  58.                 ;draw with the anti-aliased Color
  59.                 Rect  tmx*16,tmy*16,16,16,1 ;draw on grid
  60.                 Plot tmx+600,tmy+200 ;draw at pixel size
  61.         EndIf
  62. Else
  63.         ; change drawing color
  64.         ; this  checks any area outside the grid
  65.         ; not just the palette bar
  66.         If MouseDown(1)
  67.                 GetColor mx,my
  68.         EndIf
  69. EndIf
  70. drawgrid()
  71. SetBuffer BackBuffer()
  72. Flip
  73. Wend
  74.  
  75. End
  76.  
  77. Function antialias()
  78. ; mask out alpha
  79. For f=0 To 7
  80.         zz(f)=zz(f) And $ffffff
  81. Next
  82.  
  83. ;seperate r,g,b values
  84. For f=0 To 7
  85.         rt(f)=(zz(f) Shr 16) And $ff
  86.         gt(f)=(zz(f) Shr 8) And $ff
  87.         bt(f)=zz(f) And $ff
  88. Next
  89.        
  90. ; add samples then  average.
  91. For f=0 To 7
  92.         rx=rx+rt(f)
  93.         gx=gx+gt(f)
  94.         bx=bx+bt(f)
  95. Next
  96. rx=rx Shr 3
  97. gx=gx Shr 3
  98. bx=bx Shr 3
  99. ; assign new color
  100. Color rx,gx,bx
  101.        
  102. End Function
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. Function drawgrid()
  110.         r=ColorRed():g=ColorGreen():b=ColorBlue()
  111.         Color $80,$80,$80
  112.         For x=0 To 31
  113.                 For y=0 To 31
  114.                         Rect x*16,y*16,16,16,0
  115.                 Next
  116.         Next
  117.         Color r,g,b
  118. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal