Author Topic: Antialiasing using Gauss or Average filters  (Read 3271 times)

0 Members and 1 Guest are viewing this topic.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Antialiasing using Gauss or Average filters
« on: May 10, 2021, 05:10:45 pm »
This is a mini-version of my BinClock screen blanker. I made it to replace the old windows sidebar.exe based Clock, which is not available on Win10 anymore.

However, I mainly post it here to showcase how to achive antialiasing in QB64 programs by using Gauss or Average blurring filters.

Note you also need my Libraries Collection for this (see signature) and if you're not on Windows, then you may have to change the LOCALAPPDATA environment folder, which is used to save the current clock window position.

You may change the month abbreviations in lines 77-79 and assemble the date format (d$) in line 144 for your country if you want to.

Feel free to play with different filters in line 166 (see QB64Library\IMG-Support\docs\imageprocess.bm\ApplyFilter.html for available filters), the magnifying glass may help you to inspect the effects in detail.

Code: QB64: [Select]
  1. '+---------------+---------------------------------------------------+
  2. '| ###### ###### |     .--. .         .-.                            |
  3. '| ##  ## ##   # |     |   )|        (   ) o                         |
  4. '| ##  ##  ##    |     |--' |--. .-.  `-.  .  .-...--.--. .-.        |
  5. '| ######   ##   |     |  \ |  |(   )(   ) | (   ||  |  |(   )       |
  6. '| ##      ##    |     '   `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |
  7. '| ##     ##   # |                            ._.'                   |
  8. '| ##     ###### |  Sources & Documents placed in the Public Domain. |
  9. '+---------------+---------------------------------------------------+
  10. '|                                                                   |
  11. '| === NewClock.bas ===                                              |
  12. '|                                                                   |
  13. '| == A simple binary (BCD) clock inspired by the alien countdown    |
  14. '| == from the movie "Mission to Mars".                              |
  15. '| == For easy reading it also have digital and analog displays.     |
  16. '|                                                                   |
  17. '+-------------------------------------------------------------------+
  18. '| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |
  19. '| Find me in the QB64 Forum or mail to support&rhosigma-cw.net for  |
  20. '| any questions or suggestions. Thanx for your interest in my work. |
  21. '+-------------------------------------------------------------------+
  22.  
  23. 'get my local appdata folder
  24. lad$ = ENVIRON$("LOCALAPPDATA")
  25. IF lad$ <> "" THEN
  26.     IF RIGHT$(lad$, 1) <> "\" THEN lad$ = lad$ + "\"
  27. IF NOT _DIREXISTS(lad$ + "RhoSigma") THEN MKDIR lad$ + "RhoSigma"
  28. lad$ = lad$ + "RhoSigma\"
  29.  
  30. 'get old window position
  31. cfg$ = lad$ + "NewClock.bin"
  32.     OPEN "B", #1, cfg$
  33.     GET #1, , lft%
  34.     GET #1, , top%
  35.     CLOSE #1
  36.     lft% = -1: top% = -1
  37.  
  38. 'setup screen
  39. DIM SHARED scr&, tru&, col&
  40. scr& = _NEWIMAGE(300, 180, 32)
  41. SCREEN scr&
  42. tru& = _NEWIMAGE(300, 180, 32)
  43. col& = _NEWIMAGE(300, 180, 256)
  44. _SOURCE col&: _DEST col&
  45. _DELAY 0.2
  46. IF lft% < 0 OR top% < 0 THEN
  47.     _SCREENMOVE lft%, top%
  48. scrFont& = _LOADFONT("C:\Windows\Fonts\timesbd.ttf", 30)
  49. _FONT scrFont&
  50.  
  51. '3D space origin is on these screen coordinates
  52. DIM SHARED cx%: cx% = 8
  53. DIM SHARED cy%: cy% = 110
  54.  
  55. 'init BCD discs
  56. TYPE Disc
  57.     x AS INTEGER
  58.     y AS INTEGER
  59.     z AS INTEGER
  60.     r AS INTEGER
  61.     a AS INTEGER
  62. DIM SHARED Discs(23) AS Disc
  63. InitDiscs
  64. DIM SHARED curState&: curState& = 0
  65. DIM SHARED newState&: newState& = 0
  66.  
  67. 'init month names
  68. DIM SHARED month$(12)
  69. month$(1) = "Jan": month$(2) = "Feb": month$(3) = "Mar": month$(4) = "Apr"
  70. month$(5) = "May": month$(6) = "Jun": month$(7) = "Jul": month$(8) = "Aug"
  71. month$(9) = "Sep": month$(10) = "Oct": month$(11) = "Nov": month$(12) = "Dec"
  72.  
  73. 'draw hour/minute/seconds separators
  74. Line3D 52, 0, 0, 52, 210, 0, 2
  75. Line3D 52, 0, 0, 52, 0, -30, 2
  76. Line3D 128, 0, 0, 128, 210, 0, 2
  77. Line3D 128, 0, 0, 128, 0, -30, 2
  78.  
  79. 'mark binary factors
  80. _PRINTSTRING (8, 5), "8": Line3D -20, 180, 0, -70, 180, 0, 6
  81. _PRINTSTRING (8, 30), "4": Line3D -20, 130, 0, -45, 130, 0, 6
  82. _PRINTSTRING (280, 55), "2": Line3D 200, 80, 0, 225, 80, 0, 6
  83. _PRINTSTRING (280, 80), "1": Line3D 200, 30, 0, 250, 30, 0, 6
  84.  
  85. 'main loop
  86. _TITLE "Uhrzeit/Datum"
  87.     _LIMIT 1
  88.     FlipDiscs
  89.     IF _SCREENX <> -32000 AND _SCREENY <> -32000 THEN 'minimized?
  90.         IF _SCREENX <> lft% OR _SCREENY <> top% THEN 'position changed?
  91.             lft% = _SCREENX: top% = _SCREENY
  92.             OPEN "B", #1, cfg$
  93.             PUT #1, , lft%
  94.             PUT #1, , top%
  95.             CLOSE #1
  96.         END IF
  97.     END IF
  98.  
  99. 'cleanup
  100. _FREEFONT scrFont&
  101.  
  102. 'run the clock
  103. SUB FlipDiscs
  104. t$ = TIME$
  105. newState& = (VAL(MID$(t$, 1, 1)) * (2 ^ 20)) + (VAL(MID$(t$, 2, 1)) * (2 ^ 16)) +_
  106.             (VAL(MID$(t$, 4, 1)) * (2 ^ 12)) + (VAL(MID$(t$, 5, 1)) * (2 ^ 8)) +_
  107.             (VAL(MID$(t$, 7, 1)) * (2 ^ 4)) + (VAL(MID$(t$, 8, 1)) * (2 ^ 0))
  108. diff& = curState& XOR newState&
  109. curState& = newState&
  110. FOR rot% = 6 TO 90 STEP 6
  111.     FOR n% = 0 TO 23
  112.         IF (n% MOD 4) = 0 THEN AxisSegments Discs(n%).x
  113.         IF diff& AND (2 ^ n%) THEN
  114.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a, 0
  115.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a + 6, 15
  116.             Discs(n%).a = Discs(n%).a + 6
  117.             IF Discs(n%).a = 180 THEN Discs(n%).a = 0
  118.         ELSE
  119.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a, 15
  120.         END IF
  121.     NEXT n%
  122.     IF rot% = 54 THEN
  123.         'digital clock
  124.         COLOR 9
  125.         _PRINTSTRING (14, 117), MID$(t$, 1, 2)
  126.         _PRINTSTRING (84, 117), MID$(t$, 4, 2)
  127.         _PRINTSTRING (157, 117), MID$(t$, 7, 2)
  128.         d$ = MID$(DATE$, 4, 2) + ". " + month$(VAL(LEFT$(DATE$, 2))) + " " + RIGHT$(DATE$, 4)
  129.         _PRINTSTRING (98 - (_PRINTWIDTH(d$) / 2), 145), d$
  130.         'analog clock
  131.         LINE (242 - 34, 142 - 34)-(242 + 34, 142 + 34), 0, BF
  132.         CIRCLE (242, 142), 34, 14
  133.         CIRCLE (242, 142), 32, 6: t# = TIMER
  134.         x% = PolToCartX%((360 / 43200 * t#) - 90, 22)
  135.         y% = PolToCartY%((360 / 43200 * t#) - 90, 22)
  136.         LINE (242, 142)-(x% + 242, y% + 142), 14
  137.         x% = PolToCartX%((360 / 3600 * t#) - 90, 26)
  138.         y% = PolToCartY%((360 / 3600 * t#) - 90, 26)
  139.         LINE (242, 142)-(x% + 242, y% + 142), 10
  140.         x% = PolToCartX%((360 / 60 * t#) - 90, 28)
  141.         y% = PolToCartY%((360 / 60 * t#) - 90, 28)
  142.         LINE (242, 142)-(x% + 242, y% + 142), 12
  143.         x% = PolToCartX%((360 / 60 * t#) + 90, 7)
  144.         y% = PolToCartY%((360 / 60 * t#) + 90, 7)
  145.         LINE (242, 142)-(x% + 242, y% + 142), 12
  146.         CIRCLE (242, 142), 1, 12
  147.         CIRCLE (242, 142), 2, 12
  148.     END IF
  149.     _PUTIMAGE , col&, tru&
  150.     gau& = ApplyFilter&(tru&, "Gauss8", 0, 0, -1, -1, -1, -1, -1)
  151.     _PUTIMAGE , gau&, scr&
  152.     _FREEIMAGE gau&
  153.     _DISPLAY
  154. NEXT rot%
  155.  
  156. 'setup start values for all discs
  157. SUB InitDiscs
  158. n% = 0
  159. FOR i% = 180 TO 150 STEP -30
  160.     FOR j% = 30 TO 180 STEP 50
  161.         Discs(n%).x = i%
  162.         Discs(n%).y = j%
  163.         Discs(n%).z = 0
  164.         Discs(n%).r = 10
  165.         Discs(n%).a = 0
  166.         n% = n% + 1
  167.     NEXT j%
  168. NEXT i%
  169. FOR i% = 105 TO 75 STEP -30
  170.     FOR j% = 30 TO 180 STEP 50
  171.         Discs(n%).x = i%
  172.         Discs(n%).y = j%
  173.         Discs(n%).z = 0
  174.         Discs(n%).r = 10
  175.         Discs(n%).a = 0
  176.         n% = n% + 1
  177.     NEXT j%
  178. NEXT i%
  179. FOR i% = 30 TO 0 STEP -30
  180.     FOR j% = 30 TO 180 STEP 50
  181.         Discs(n%).x = i%
  182.         Discs(n%).y = j%
  183.         Discs(n%).z = 0
  184.         Discs(n%).r = 10
  185.         Discs(n%).a = 0
  186.         n% = n% + 1
  187.     NEXT j%
  188. NEXT i%
  189.  
  190. 'draw rotation axis segments between discs
  191. SUB AxisSegments (x%)
  192. Line3D x%, 0, 0, x%, 20, 0, 12
  193. Line3D x%, 40, 0, x%, 70, 0, 12
  194. Line3D x%, 90, 0, x%, 120, 0, 12
  195. Line3D x%, 140, 0, x%, 170, 0, 12
  196. Line3D x%, 190, 0, x%, 210, 0, 12
  197.  
  198. SUB Line3D (x1%, y1%, z1%, x2%, y2%, z2%, col%)
  199. 'x1%/y1%/z1% = start, x2%/y2%/z2% = end, col% = color pen
  200. x1# = (x1% + (y1% * 0.5)): z1# = (z1% + (y1% * 0.5))
  201. x2# = (x2% + (y2% * 0.5)): z2# = (z2% + (y2% * 0.5))
  202. LINE (x1# + cx%, -z1# + cy%)-(x2# + cx%, -z2# + cy%), col%
  203.  
  204. SUB Circle3D (x%, y%, z%, r%, ba%, col%)
  205. 'x%/y%/z% = center, r% = radius, ba% = B-Axis angle, col% = color pen
  206. mx# = (x% + (y% * 0.5)): mz# = (z% + (y% * 0.5))
  207. zx# = r% * COS(ba% * 0.017453292519943)
  208. zz# = r% * SIN(ba% * 0.017453292519943)
  209. FOR cir% = 0 TO 359 STEP 5
  210.     x# = zx# * COS(cir% * 0.017453292519943)
  211.     y# = r% * SIN(cir% * 0.017453292519943)
  212.     z# = zz# * COS(cir% * 0.017453292519943)
  213.     x# = (x# + (y# * 0.5)): z# = (z# + (y# * 0.5))
  214.     PSET (x# + mx# + cx%, -z# + -mz# + cy%), col%
  215. NEXT cir%
  216.  
  217. '$INCLUDE: 'QB64Library\IMG-Support\converthelper.bm'
  218. '$INCLUDE: 'QB64Library\IMG-Support\imageprocess.bm'
  219.  
  220.  
« Last Edit: October 09, 2021, 03:13:15 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack