11-04-2022, 01:48 AM
Dithering with an arbitrary diffusion matrix
Code: (Select All)
deflng a-z
img1 = _loadimage("nefertiti.jpg", 32)
w = _width(img1)
h = _height(img1)
img2 = _newimage(w, h, 32)
img3 = _newimage(w, h, 32)
img4 = _newimage(w, h, 32)
img5 = _newimage(w, h, 32)
img6 = _newimage(w, h, 32)
img7 = _newimage(w, h, 32)
img8 = _newimage(w, h, 32)
img9 = _newimage(w, h, 32)
img10 = _newimage(w, h, 32)
screen _newimage(w*3, h*3, 32)
redim h(2, 1) as single
h(0,0)=0:h(1,0)=-1:h(2,0)=7/16
h(0,1)=3/16:h(1,1)=5/16:h(2,1)=1/16
dither_bw img1, img2, 0.1, h()
dither img1, img3, 2, h()
dither img1, img4, 4, h()
redim h(4, 2) as single
h(0,0)=0:h(1,0)=0:h(2,0)=-1:h(3,0)=7/48:h(4,0)=5/48
h(0,1)=3/48:h(1,1)=5/48:h(2,1)=7/48:h(3,1)=5/48:h(4,1)=3/48
h(0,2)=1/48:h(1,2)=3/48:h(2,2)=5/48:h(3,2)=3/48:h(4,2)=1/48
dither_bw img1, img5, 0.1, h()
dither img1, img6, 2, h()
dither img1, img7, 4, h()
redim h(3, 2) as single
h(0,0)=0:h(1,0)=-1:h(2,0)=1/8:h(3,0)=1/8
h(0,1)=1/8:h(1,1)=1/8:h(2,1)=1/8:h(3,1)=0
h(0,2)=0:h(1,2)=1/8:h(2,2)=0:h(3,2)=0
dither_bw img1, img8, 0.1, h()
dither img1, img9, 2, h()
dither img1, img10, 4, h()
_dest 0
_putimage (0, 0), img2
_putimage (w, 0), img3
_putimage (2*w, 0), img4
_printstring (0,0),"Floyd-Steinberg"
_putimage (0, h), img5
_putimage (w, h), img6
_putimage (2*w, h), img7
_printstring (0,h),"Jarvis, Judice, and Ninke"
_putimage (0, 2*h), img8
_putimage (w, 2*h), img9
_putimage (2*w, 2*h), img10
_printstring (0,2*h),"Atkinson"
do
loop until _keyhit=27
system
'colour dither
'source image, destination image, number of colours per channel, diffusion matrix
sub dither(img1, img2, num, h() as single)
w = _width(img1)
h = _height(img1)
_dest img2
_source img2
_putimage , img1
for y=0 to h-1
for x=0 to w-1
z = point(x, y)
r = (_red(z)*num\255)*255\num
g = (_green(z)*num\255)*255\num
b = (_blue(z)*num\255)*255\num
pset (x, y), _rgb(r, g, b)
qr = _red(z) - r
qg = _green(z) - g
qb = _blue(z) - b
conv_ed img2, x, y, h(), qr, qg, qb
next
next
end sub
'black and white dither
'source image, destination image, bw threshold percent, diffusion matrix
sub dither_bw(img1, img2, t as double, h() as single)
w = _width(img1)
h = _height(img1)
_dest img2
_source img2
_putimage , img1
for y=0 to h-1
for x=0 to w-1
z = point(x, y)
c = -((_red(z)+_green(z)+_blue(z))/3 > 255*t)*255
pset (x, y), _rgb(c, c, c)
qr = _red(z) - c
qg = _green(z) - c
qb = _blue(z) - c
conv_ed img2, x, y, h(), qr, qg, qb
next
next
end sub
sub conv_ed(img, x0, y0, h() as single, qr, qg, qb)
for y=0 to ubound(h,2)
for x=0 to ubound(h,1)
if h(x,y)=-1 then
xx = x
yy = y
end if
next
next
_source img
_dest img
for y=0 to ubound(h,2)
for x=0 to ubound(h,1)
if h(x,y) > 0 then
r = _red(point(x0-xx+x, y0-yy+y)) + qr*h(x,y)
g = _green(point(x0-xx+x, y0-yy+y)) + qg*h(x,y)
b = _blue(point(x0-xx+x, y0-yy+y)) + qb*h(x,y)
pset (x0-xx+x, y0-yy+y), _rgb(r, g, b)
end if
next
next
end sub