DogLua - Lua filters for Project Dogwaffle:
 

Halftone (with GUI)

back

more Lua Scripts

 DogLua:   Overview - Downloading - Reference Guide - Sample Scripts - Lua Browser
  see also:  GUI Server - more SDK Info

 
the Halftone filter.......

This Lua script uses the GUI server for user-selectable parameters.

You can find the code shown to the right.

It is also available in a .lua file here:   HalftoneGui.lua 

 

You can simply save this file to the folder where Dogwaffle's Lua script files are located on your system.
















 
--HalftoneGui.lua

function DoHalftone(size,type)
-- Example of halftone filter
-- could use any mathematical means to generate the pattern


local x, y, r, g, b,v,v2,v3

local x2, y2
local scale
frequency=size/200


for y = 0, height - 1 do
  for x = 0, width - 1 do
    r, g, b = get_rgb(x, y)


    --generata a pattern...

    --sine version.  Classic halftone...
    if type==0 then
    v2=math.sin(math.rad(x)/size)*math.cos(math.rad(y)/size)
    v2=(v2+1)/2
    end

    --randomstipple
    if type==1 then
    v2=math.random()
    end

    --Cross contour
    if type==2 then
    v2=math.sin(math.rad(y)/size)
    v2=(v2+1)/2
    end

    --Diagonal Cross contour
    if type==3 then
    v2=math.sin(math.rad(x+y)/size)
    v2=(v2+1)/2
    end

if (type==4) then
     x2=((x/width)-.5)/frequency
     y2=((y/height)-.5)/frequency
     v2=(math.cos(math.sqrt(x2*x2+y2*y2)*.01745))
     v2=(v2+1)/2
end

if (type==5) then
--first
     x2=((x/width)-.13)/frequency
     y2=((y/height)-.88)/frequency
     v=(math.cos(math.sqrt(x2*x2+y2*y2)*.01745))
     v2=(v+1)/2
--second
     x2=((x/width)-.73)/frequency
     y2=((y/height)-.43)/frequency
     v=(math.cos(math.sqrt(x2*x2+y2*y2)*.01745))
     v3=(v+1)/2
     if (v3>v2) then v2=v3 end
--third
     x2=((x/width)-.45)/frequency
     y2=((y/height)-.86)/frequency
     v=(math.cos(math.sqrt(x2*x2+y2*y2)*.01745))
     v3=(v+1)/2
     if (v3>v2) then v2=v3 end

end

    --threshold it
      if (r>v2) then
      r=1
      else
      r=0
      end
      if (g>v2) then
      g=1
      else
      g=0
      end
      if (b>v2) then
      b=1
      else
      b=0
      end



     --display it
      set_rgb(x,y,r,g,b)

  end
  progress(y / height)
end

  progress(0)



end



-- the main program.

Dog_SaveUndo()

GUI_SetCaption("Halftone")
h1 = GUI_AddControl("Scroller", "Size", 50, 0, 100)
h3 = GUI_AddControl("Combobox", "Type")
GUI_SetList (h3,0,"Halftone")
GUI_SetList (h3,1,"Stipple")
GUI_SetList (h3,2,"Cross contour")
GUI_SetList (h3,3,"Diagonal contour")
GUI_SetList (h3,4,"Circular")
GUI_SetList (h3,5,"Confusion")
GUI_SetSettings (h3,0, "Halftone")


GUI_OpenPanel()



--event loop.  Repeat until ok or cancel pressed (index of -1 or -2)

repeat
idx, retval, retstr = GUI_WaitOnEvent()

size,string=GUI_GetSettings(h1)
size=size/1000
dummy,type=GUI_GetSettings(h3)

if (idx>-1) then

if type=="Halftone" then DoHalftone(size,0) end
if type=="Stipple" then DoHalftone(size,1) end
if type=="Cross contour" then DoHalftone(size,2) end
if type=="Diagonal contour" then DoHalftone(size,3) end
if type=="Circular" then DoHalftone(size,4) end
if type=="Confusion" then DoHalftone(size,5) end

Dog_Refresh()
end

until idx < 0 --repeat until


GUI_ClosePanel()


if idx == -2 then
--Restore the image
Dog_RestoreUndo()
Dog_GetBuffer()

end