December 03, 2020, 08:01:24 PM

Author Topic: [bb] Antialias image by elias_t [ 1+ years ago ]  (Read 404 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Antialias image by elias_t [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Antialias image
Author : elias_t
Posted : 1+ years ago

Description : Antialias image function.

Code :
Code: BlitzBasic
  1. ;Image Antialiasing routine
  2. ;
  3. ;by elias_t
  4. ;
  5. ;[using also the rgb2hsv function by Ghost Dancer]
  6.  
  7. ;------------------------------
  8. ;Globals needed
  9. Global rgb_h#,rgb_s#,rgb_v#;
  10. Dim px(8),rav(8),gav(8),bav(8);
  11. ;------------------------------
  12.  
  13.  
  14.  
  15. ;==========================================================
  16. ;example
  17. Graphics 640,480,32,2
  18.  
  19. in=LoadImage("test.bmp")
  20.  
  21. DrawImage in,10,10
  22.  
  23. Text 0,0,"Press any key to antialias image"
  24.  
  25. WaitKey()
  26. FlushKeys()
  27.  
  28. ;the [.2] value represents the sensitivity on hue difference between pixels
  29. ;should be between .15-.5 [default=.2]
  30. ;the [25] value represents the brightness value between pixels
  31. ;should be between 15-75  [default=25]
  32.  
  33.  
  34. out=anti_alias(in,.2,25)
  35.  
  36.  
  37. DrawImage out,10+ImageWidth(in),10
  38.  
  39. WaitKey()
  40.  
  41. End
  42.  
  43. ;==========================================================
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. ;FUNCTIONS
  52.  
  53.  
  54. ;[img] is the input image
  55. ;[h#] value represents the sensitivity on hue difference between pixels
  56. ;should be between .15-.5 [default=.2]
  57. ;[v] value represents the brightness value between pixels to be checked
  58. ;should be between 15-75  [default=25]
  59.  
  60. Function anti_alias(img,h#,v)
  61.  
  62.  
  63. h#=Abs(h#)
  64. If h#<.15 Then h#=.15
  65. If h#>.5 Then h#=.5
  66. v=Abs(v)
  67. If v<15 Then v=15
  68. If v>75 Then v=75
  69.  
  70. out=CopyImage(img)
  71.  
  72. LockBuffer(ImageBuffer(out))
  73.  
  74. For x=0 To ImageWidth(out)
  75. For y=0 To ImageHeight(out)
  76.  
  77. ;--------------
  78. If y=0 Or y>ImageHeight(out)-2
  79. a=0
  80. Else
  81. a=1
  82. EndIf
  83. If x=0 Or x>ImageWidth(out)-2
  84. b=0
  85. Else
  86. b=1
  87. EndIf
  88. ;--------------
  89.  
  90. ;read the pixels      
  91. px(0)=ReadPixelFast(x,y-a,ImageBuffer(out) )
  92. px(1)=ReadPixelFast(x,y+a,ImageBuffer(out) )
  93. px(2)=ReadPixelFast(x+b,y,ImageBuffer(out) )  
  94. px(3)=ReadPixelFast(x-b,y,ImageBuffer(out) )
  95. px(4)=ReadPixelFast(x,y,ImageBuffer(out) );<-center pixel
  96. px(5)=ReadPixelFast(x+b,y-a,ImageBuffer(out) )
  97. px(6)=ReadPixelFast(x-b,y+a,ImageBuffer(out) )
  98. px(7)=ReadPixelFast(x+b,y-a,ImageBuffer(out) )  
  99. px(8)=ReadPixelFast(x-b,y+a,ImageBuffer(out) )
  100.  
  101.  
  102.  
  103. rgb_hsv(px(4))
  104. z4# = rgb_v#
  105. h4# = rgb_h#-180
  106. rgb_hsv(px(1))
  107. z1# = rgb_v#
  108. h1# = rgb_h#-180
  109. rgb_hsv(px(2))
  110. z2# = rgb_v#
  111. h2# = rgb_h#-180
  112.  
  113.  
  114. If ( px(4)<>px(1) And (Abs(z4#-z1#)>h#) Or Abs(h4#-h1#)>v ) Or (px(4)<>px(2) And (Abs(z4#-z2#)>h# Or Abs(h4#-h2#)>v ))
  115.  
  116.  
  117. For f=0 To 8
  118.     rav(f)=(px(f) Shr 16) And $ff
  119.     gav(f)=(px(f) Shr 8) And $ff
  120.     bav(f)=px(f) And $ff
  121. Next
  122.  
  123. For f=0 To 8
  124.     rax=rax+rav(f)
  125.     gax=gax+gav(f)
  126.     bax=bax+bav(f)
  127. Next
  128.  
  129. rax=rax/9
  130. gax=gax/9
  131. bax=bax/9
  132.    
  133. rgb=((rax Shl 16)+(gax Shl 8)+bax)
  134.  
  135. WritePixelFast x,y,rgb,ImageBuffer(out)
  136.  
  137. rax=0:gax=0:bax=0
  138.  
  139. EndIf;
  140.  
  141. Next
  142. Next
  143.  
  144. UnlockBuffer(ImageBuffer(out))
  145.  
  146. Return out
  147.  
  148. End Function
  149.  
  150.  
  151. ;------------------------------
  152.  
  153. Function rgb_hsv(rgb#);from the colour space library Ghost Dancer
  154.  
  155.         ;RGB components in  range 0 to 1
  156.         r# = (rgb# Shr 16 And $ff) / 255.0
  157.         g# = (rgb# Shr 8 And $ff) / 255.0
  158.         b# = (rgb# And $ff) / 255.0
  159.         ;min value     
  160.         If r < g Then minVal# = r Else minVal# = g
  161.         If b < minVal Then minVal = b
  162.         ;max value     
  163.         If r > g Then maxVal# = r Else maxVal# = g
  164.         If b > maxVal Then maxVal = b
  165.         ;calculate difference
  166.         diff# = maxVal - minVal
  167.        
  168.         rgb_v = maxVal
  169.        
  170.         If maxVal = 0 Then
  171.                 rgb_v = 0
  172.                 rgb_h = -1
  173.         Else
  174.                 rgb_s = diff / maxVal
  175.        
  176.                 If r = maxVal Then
  177.                         rgb_h = (g - b) / diff
  178.                 ElseIf g = maxVal Then
  179.                         rgb_h = 2 + (b - r) / diff
  180.                 Else
  181.                         rgb_h = 4 + (r - g) / diff
  182.                 EndIf
  183.        
  184.                 rgb_h = rgb_h * 60
  185.                 If rgb_h < 0 Then rgb_h = rgb_h + 360
  186.         EndIf
  187.  
  188. End Function
  189.  
  190. ;------------------------------


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal