Vince's Corner Takeout
#34
Dithering with an arbitrary diffusion matrix

[Image: 5nM20e2.png]

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
Reply


Messages In This Thread
Vince's Corner Takeout - by bplus - 04-29-2022, 02:12 PM
RE: Vince's Corner Takeout - by vince - 04-29-2022, 09:34 PM
RE: Vince's Corner Takeout - by vince - 05-02-2022, 03:10 AM
RE: Vince's Corner Takeout - by bplus - 05-02-2022, 04:25 AM
RE: Vince's Corner Takeout - by vince - 05-02-2022, 11:16 PM
RE: Vince's Corner Takeout - by vince - 05-03-2022, 01:10 AM
RE: Vince's Corner Takeout - by bplus - 05-03-2022, 01:15 AM
RE: Vince's Corner Takeout - by vince - 05-03-2022, 04:26 AM
RE: Vince's Corner Takeout - by bplus - 05-03-2022, 03:32 PM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 03:41 AM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 03:57 AM
RE: Vince's Corner Takeout - by dcromley - 05-10-2022, 02:57 PM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 08:14 PM
RE: Vince's Corner Takeout - by SMcNeill - 05-10-2022, 02:59 PM
RE: Vince's Corner Takeout - by vince - 05-11-2022, 01:13 AM
RE: Vince's Corner Takeout - by dcromley - 05-11-2022, 01:58 AM
RE: Vince's Corner Takeout - by vince - 06-01-2022, 09:05 AM
RE: Vince's Corner Takeout - by vince - 08-11-2022, 02:51 AM
RE: Vince's Corner Takeout - by bplus - 06-03-2022, 02:47 PM
RE: Vince's Corner Takeout - by triggered - 06-04-2022, 02:00 AM
RE: Vince's Corner Takeout - by vince - 06-07-2022, 02:02 AM
RE: Vince's Corner Takeout - by bplus - 06-07-2022, 02:15 AM
RE: Vince's Corner Takeout - by vince - 07-13-2022, 05:23 AM
RE: Vince's Corner Takeout - by BSpinoza - 07-14-2022, 04:54 AM
RE: Vince's Corner Takeout - by bplus - 07-14-2022, 04:35 PM
RE: Vince's Corner Takeout - by aurel - 08-11-2022, 01:02 PM
RE: Vince's Corner Takeout - by bplus - 08-11-2022, 04:22 PM
RE: Vince's Corner Takeout - by aurel - 08-11-2022, 05:33 PM
RE: Vince's Corner Takeout - by BSpinoza - 08-12-2022, 03:44 AM
RE: Vince's Corner Takeout - by vince - 08-11-2022, 08:42 PM
RE: Vince's Corner Takeout - by vince - 08-19-2022, 05:00 AM
RE: Vince's Corner Takeout - by bplus - 08-19-2022, 06:33 PM
RE: Vince's Corner Takeout - by vince - 08-23-2022, 10:04 PM
RE: Vince's Corner Takeout - by vince - 11-04-2022, 01:48 AM
RE: Vince's Corner Takeout - by vince - 03-31-2023, 11:07 PM



Users browsing this thread: 14 Guest(s)