Posterize

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Posterize

Post by nimdays »

A simple way to reduce the colors

Code: Select all

const pic = "c:\blunt.bmp" 'change this (optional)
const w = 800,h = 600,d = 32
const t = 100 'change the timer if necessary
const l = 32  'change the level if necessary

function pst(src as ubyte,lv as ubyte)as ubyte
   dim as ubyte v = 256\lv 
   ''level = 2 to 128
   if lv < 2  or lv > 128 then return src
   
   if src < v then return 0
   dim as uinteger v1 = (src \ v)* v + v
   if v1 > 255 then v1 = 255 'ie 256
   
   return v1
end function

screenres w,h,d

dim as any ptr img = imagecreate(w,h)

if (bload(pic,img)) then 'no picture then create some boxes
   for i as integer = 1 to 300
      dim as integer x = rnd*(w-i),y = rnd*(h-i)
      line img,(x,y)-(x+100,y+100),rgb(rnd*255,rnd*255,rnd*255),BF
   next i
end if

dim as ubyte ptr pixels
imageinfo img,,,,,pixels
dim as ubyte ptr p = screenptr()

for i as integer = l to 2 step -1
    if len(inkey()) then exit for
    windowtitle "Level " & i
    screenlock()
    for j as integer = 0 to w * h * 4 -1 step 4
        p[j]  = pst(pixels[j],i)
        p[j+1]  = pst(pixels[j+1],i)
        p[j+2]  = pst(pixels[j+2],i)
    next j
    screenunlock()
    sleep t
next

? "Press any key to quit"
imagedestroy(img)
sleep

Post Reply