Author Topic: Dithering samples  (Read 6192 times)

0 Members and 1 Guest are viewing this topic.

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Dithering samples
« on: July 06, 2018, 08:33:17 am »
Post Deleted
« Last Edit: April 14, 2022, 08:46:52 am by RCcola1987 »
I am from a Kazakhstan, we follow the hawk.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dithering samples
« Reply #1 on: July 08, 2018, 12:48:57 pm »
For a more moving experience, I diddled with dither:
Code: QB64: [Select]
  1. _TITLE "Dither by keybone diddled by bplus 2018-07-08"
  2. SCREEN _NEWIMAGE(640, 480, 32)
  3. m = 11
  4.     r = (r + 1) MOD m
  5.     ditherCheckerbox r, m, _RGBA32(255, 0, 0, 255), _RGBA32(0, 0, 0, 255)
  6.     'ditherColumns r, m, _RGBA32(255, 0, 0, 255), _RGBA32(0, 0, 255, 255)
  7.     'ditherLines r, m, _RGBA32(255, 0, 0, 255), _RGBA32(0, 0, 0, 255)
  8.     _DISPLAY
  9.     _LIMIT 20
  10.  
  11. SUB ditherCheckerbox (remainder AS INTEGER, modulus AS INTEGER, color1 AS _UNSIGNED LONG, color2 AS _UNSIGNED LONG)
  12.     startLine = 1
  13.     FOR j = 1 TO _HEIGHT
  14.         yr = (j + remainder) MOD modulus
  15.         IF yr MOD modulus = 0 THEN
  16.             FOR i = 1 TO _WIDTH
  17.                 xr = (i + remainder) MOD modulus
  18.                 IF xr MOD modulus THEN
  19.                     PSET (i, j), color2
  20.                 ELSE
  21.                     PSET (i, j), color1
  22.                 END IF
  23.             NEXT i
  24.         ELSE
  25.             FOR i = 1 TO _WIDTH
  26.                 xr = (i + remainder) MOD modulus
  27.                 IF xr MOD modulus = 0 THEN
  28.                     PSET (i, j), color2
  29.                 ELSE
  30.                     PSET (i, j), color1
  31.                 END IF
  32.             NEXT i
  33.         END IF
  34.     NEXT j
  35.  
  36. SUB ditherColumns (remainder AS INTEGER, modulus AS INTEGER, color1 AS _UNSIGNED LONG, color2 AS _UNSIGNED LONG)
  37.     FOR j = 1 TO _HEIGHT
  38.         FOR i = 1 TO _WIDTH
  39.             xr = (i + remainder) MOD modulus
  40.             IF xr MOD modulus = 0 THEN
  41.                 PSET (i, j), color2
  42.             ELSE
  43.                 PSET (i, j), color1
  44.             END IF
  45.         NEXT i
  46.     NEXT j
  47.  
  48. SUB ditherLines (remainder AS INTEGER, modulus AS INTEGER, color1 AS _UNSIGNED LONG, color2 AS _UNSIGNED LONG)
  49.     FOR j = 1 TO _HEIGHT
  50.         yr = (j + remainder) MOD modulus
  51.         FOR i = 1 TO _WIDTH
  52.             IF yr MOD modulus = 0 THEN
  53.                 PSET (i, j), color2
  54.             ELSE
  55.                 PSET (i, j), color1
  56.             END IF
  57.         NEXT i
  58.     NEXT j
  59.  

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Dithering samples
« Reply #2 on: July 16, 2018, 05:33:43 am »
gradient example
* gradient.bas (Filesize: 2.14 KB, Downloads: 237)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dithering samples
« Reply #3 on: July 16, 2018, 11:56:18 pm »
I guess I will get to dithering later, first I wanted to make the gradient.

Quiz: Can anyone explain what's happening in the first column?
Code: QB64: [Select]
  1. _TITLE "Making the Gradient"
  2.  
  3. DEFLNG A-Z
  4. CONST w = 800
  5. CONST h = 600
  6. SCREEN _NEWIMAGE(w, h, 32)
  7. d = 1
  8. k1 = _RGB32(128, 0, 0)
  9. k2 = _RGB(0, 128, 255)
  10. stepper = 300
  11.     FOR y = 0 TO h - 1 STEP stepper
  12.         FOR x = 0 TO w - 1 STEP stepper
  13.             IF y MOD 2 * stepper THEN
  14.                 IF x MOD 2 * stepper THEN
  15.                     IF x MOD 4 * stepper = 3 * stepper THEN c1 = k1: c2 = k2 ELSE c1 = k2: c2 = k1
  16.                     horzGradRec x, y, stepper, stepper, c1, c2
  17.                     gradCirc x + stepper / 2, y + stepper / 2, stepper \ 3, k2, k1
  18.                 ELSE
  19.                     vertGradRec x, y, stepper, stepper, c2, c1
  20.                     gradCirc x + stepper / 2, y + stepper / 2, stepper \ 3, k1, k2
  21.                 END IF
  22.             ELSE
  23.                 IF x MOD 2 * stepper THEN
  24.                     vertGradRec x, y, stepper, stepper, c2, c1
  25.                     gradCirc x + stepper / 2, y + stepper / 2, stepper \ 3, k1, k2
  26.                 ELSE
  27.                     IF x MOD 4 * stepper = 2 * stepper THEN c1 = k1: c2 = k2 ELSE c1 = k2: c2 = k1
  28.                     horzGradRec x, y, stepper, stepper, c2, c1
  29.                     gradCirc x + stepper / 2, y + stepper / 2, stepper \ 3, k2, k1
  30.                 END IF
  31.             END IF
  32.         NEXT
  33.     NEXT
  34.     stepper = stepper - 2 * d
  35.     IF stepper < 10 OR stepper > 300 THEN d = d * -1
  36.     _DISPLAY
  37.     _LIMIT 8
  38.  
  39. 'c1 color to left, c2 color to right
  40. SUB horzGradRec (x0, y0, w, h, c1, c2)
  41.     FOR cx = 0 TO w
  42.         midInk c1, c2, cx / w
  43.         LINE (x0 + cx, y0)-STEP(0, h), , BF
  44.     NEXT
  45.  
  46. SUB vertGradRec (x0, y0, w, h, c1, c2)
  47.     FOR cy = 0 TO h
  48.         midInk c1, c2, cy / h
  49.         LINE (x0, y0 + cy)-STEP(w, 0), , BF
  50.     NEXT
  51.  
  52. 'let c1 be outer most color c2 the inner most
  53. SUB gradCirc (x0, y0, r, c1, c2)
  54.     FOR cr = r TO 0 STEP -1
  55.         midInk c2, c1, cr / r
  56.         fcirc x0, y0, cr
  57.     NEXT
  58.  
  59. ' let fr## be the fraction from 1st color to 2nd color 0 means all color 1, 1 means all color 2
  60. SUB midInk (c1, c2, fr##)
  61.     r1 = _RED(c1): g1 = _GREEN(c1): b1 = _BLUE(c1)
  62.     r2 = _RED(c2): g2 = _GREEN(c2): b2 = _BLUE(c2)
  63.     COLOR _RGB(r1 + (r2 - r1) * fr##, g1 + (g2 - g1) * fr##, b1 + (b2 - b1) * fr##)
  64.  
  65. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  66.     DIM Radius AS LONG, RadiusError AS LONG
  67.     DIM X AS LONG, Y AS LONG
  68.  
  69.     Radius = ABS(R)
  70.     RadiusError = -Radius
  71.     X = Radius
  72.     Y = 0
  73.  
  74.     IF Radius = 0 THEN PSET (CX, CY): EXIT SUB
  75.  
  76.     ' Draw the middle span here so we don't draw it twice in the main loop,
  77.     ' which would be a problem with blending turned on.
  78.     LINE (CX - X, CY)-(CX + X, CY), , BF
  79.  
  80.     WHILE X > Y
  81.         RadiusError = RadiusError + Y * 2 + 1
  82.         IF RadiusError >= 0 THEN
  83.             IF X <> Y + 1 THEN
  84.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  85.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  86.             END IF
  87.             X = X - 1
  88.             RadiusError = RadiusError - X * 2
  89.         END IF
  90.         Y = Y + 1
  91.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  92.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  93.     WEND
  94.  
  95.  
  96.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dithering samples
« Reply #4 on: July 17, 2018, 10:40:11 am »
Dang, I just noticed, I am missing a couple of 32's for _rgb32(...) not _rgb(...).

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: Dithering samples
« Reply #5 on: July 31, 2018, 03:46:16 am »
gradient example

I was bored so I made a test pattern using these routines.

I overlaid a grayscale gradient with a few translucent LINE … BF statements to get this effects. Afterwards the whole image is dithered.
I might use this effect in my gui ive been programming. :)
* gradient-testpattern.bas (Filesize: 4.48 KB, Downloads: 260)
« Last Edit: July 31, 2018, 03:53:05 am by keybone »
I am from a Kazakhstan, we follow the hawk.