December 04, 2020, 11:30:37 AM

Author Topic: [bb] Bayer-Palbo Ordered Dithering by Popstar [ 1+ years ago ]  (Read 379 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Bayer-Palbo Ordered Dithering
Author : Popstar
Posted : 1+ years ago

Description : Converts an image into a black and white image with ordered dither.

When the image has been dithered, you can flip the screen by pressing any key.

IMHO this algorithm produces better dithering than the algorithm in Photoshop 6.0


Code :
Code: BlitzBasic
  1. ;-------------------------------;
  2. ;Bayer-Palbo Ordered Dithering  ;
  3. ;-------------------------------;
  4. ;A modified Bayer-method        ;
  5. ;ordered dithering algorithm.   ;
  6. ;-------------------------------;
  7. ;Apart from the normal ordered  ;
  8. ;dithering, this version also   ;
  9. ;adds contrast and minor error  ;
  10. ;diffusion.                     ;
  11. ;Try changing pct and ErrDif to ;
  12. ;find the best result.          ;
  13. ;-------------------------------;
  14.  
  15. Global B_Method=4
  16.  
  17. AppTitle "Bayer-Palbo Dithering Algorithm"
  18. SeedRnd MilliSecs()
  19. Graphics3D 1,1,32,2
  20.  
  21. pct=90      ;Try changing this value (0...100)
  22. ErrDif=10   ;Try changing this value (0...)
  23.  
  24. If pct>80 Then B_Method=8
  25.  
  26. resolution=446/100.0*pct
  27. Dim pattern(B_Method-1,B_Method-1)
  28. Dim c_table(resolution)
  29.  
  30. infile$="TEST.JPG"
  31.  
  32. in_image=LoadImage(infile$)
  33.  
  34. width=ImageWidth(in_image)
  35. height=ImageHeight(in_image)
  36.  
  37. Graphics3D width,height,32,2
  38.  
  39. in_image=LoadImage(infile$)
  40. out_image=CreateImage (ImageWidth(in_image),ImageHeight(in_image))
  41. show=True
  42.  
  43. Setup(resolution)
  44. BPDither(in_image,out_image,resolution,ErrDif,show)
  45. End
  46.  
  47. ;--Data-----------------------------------
  48.  
  49. .bayer4
  50. Data  0,  8,  2, 10
  51. Data 12,  4, 14,  6
  52. Data  3, 11,  1,  9
  53. Data 15,  7, 13,  5
  54.  
  55. .bayer8
  56. Data  0, 32,  8, 40,  2, 34, 10, 42   ; 8x8 Bayer ordered dithering
  57. Data 48, 16, 56, 24, 50, 18, 58, 26   ; pattern. Each Input pixel
  58. Data 12, 44,  4, 36, 14, 46,  6, 38   ; is scaled To the 0..63 range
  59. Data 60, 28, 52, 20, 62, 30, 54, 22   ; Before looking in this table
  60. Data  3, 35, 11, 43,  1, 33,  9, 41   ; To determine the action.
  61. Data 51, 19, 59, 27, 49, 17, 57, 25
  62. Data 15, 47,  7, 39, 13, 45,  5, 37
  63. Data 63, 31, 55, 23, 61, 29, 53, 21
  64.  
  65. ;--Functions------------------------------
  66.  
  67. Function Setup(resolution)
  68.  
  69. If B_Method=4
  70.        
  71.         Restore bayer4
  72.        
  73.         For y=0 To 3
  74.         For x=0 To 3
  75.                 Read num
  76.                 pattern(x,y)=num
  77.         Next
  78.         Next
  79.        
  80. Else
  81.        
  82.         Restore bayer8
  83.        
  84.         For y=0 To 7
  85.         For x=0 To 7
  86.                 Read num
  87.                 pattern(x,y)=num
  88.         Next
  89.         Next
  90.  
  91. EndIf
  92.  
  93. v#=180.0
  94. k#=resolution
  95. deg#=v#/k#
  96.  
  97. ;Store the Cos() calculation in an array to reduce time.
  98.  
  99. If B_Method=4
  100.  
  101.         For value=0 To resolution
  102.                 val#=(16+(Cos(value*deg#)*16))/2
  103.                 c_table(value)=Abs (val#-16)
  104.         Next
  105.        
  106. Else
  107.        
  108.         For value=0 To resolution
  109.                 val#=(64+(Cos(value*deg#)*64))/2
  110.                 c_table(value)=Abs (val#-64)
  111.         Next
  112.  
  113. End If
  114.  
  115. End Function
  116.  
  117. ;-----------------------------------------
  118.  
  119. Function BPDither(in_image,out_image,resolution,ErrDif,show)
  120.  
  121. a#=resolution/446.0
  122. newres#=(446.0-resolution)/2
  123.  
  124. SetBuffer BackBuffer()
  125. If show=True Then DrawImage in_image,0,0
  126. Flip
  127.  
  128. mil1=MilliSecs()
  129.  
  130. For y=0 To ImageHeight(in_image)-1
  131. For x=0 To ImageWidth(in_image)-1
  132.        
  133.         If KeyDown(1)=1 Then End
  134.        
  135.         SetBuffer ImageBuffer (in_image)
  136.         LockBuffer ImageBuffer(in_image)
  137.         cval=ReadPixelFast (x,y, ImageBuffer(in_image))
  138.         cval=cval And $FFFFFF
  139.         UnlockBuffer ImageBuffer(in_image)
  140.        
  141.         red=cval/256/256
  142.         green=(cval-(red*256*256))/256
  143.         blue=cval-(red*256*256)-(green*256)
  144.        
  145.         val#=((0.5*Red)+Green+(0.25*Blue))
  146.        
  147.         If val#<newres# : val#=0 : Goto jump : End If
  148.         If val#>446-newres# Then val#=resolution+newres#
  149.        
  150.         nv=val#-newres#
  151.         nv=nv+Rnd(-(ErrDif/2),(ErrDif/2))
  152.  
  153.         If nv<0 Then nv=0
  154.         If nv>resolution Then nv=resolution
  155.        
  156.         If B_Method=4
  157.                
  158.                 Xx=x And 3
  159.                 Yy=y And 3
  160.                
  161.         Else
  162.                
  163.                 Xx=x And 7
  164.                 Yy=y And 7
  165.                
  166.         End If 
  167.        
  168.         If c_table(nv)>pattern (Xx,Yy)
  169.                 SetBuffer ImageBuffer(out_image)
  170.                 LockBuffer ImageBuffer(out_image)
  171.                 WritePixelFast x,y,$000000FFFFFF,ImageBuffer(out_image)
  172.                 UnlockBuffer ImageBuffer(out_image)
  173.         End If
  174.         .jump
  175. Next
  176. Next
  177.  
  178. mil2=MilliSecs()
  179.  
  180. DebugLog "Seconds: "+(mil2-mil1)/1000
  181.  
  182. If show=True Then FlipScreens(in_image,out_image)
  183.  
  184. End Function
  185.  
  186. ;-----------------------------------------
  187.  
  188. Function FlipScreens(in_image,out_image)
  189.  
  190. While KeyHit(1)=0
  191.  
  192. SetBuffer BackBuffer()
  193. DrawBlock out_image,0,0
  194. Flip
  195. WaitKey()
  196.  
  197. If KeyDown(1)=1 Then End
  198.  
  199. SetBuffer BackBuffer()
  200. DrawBlock in_image,0,0
  201. Flip
  202. WaitKey()
  203. Wend
  204.  
  205. End Function
  206.  
  207. ;-----------------------------------------


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal