Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dlgWin = Dialog{ title = "eg jagger " }
- dlgWin
- :color{
- id = "sel",
- color = app.fgColor
- }
- :newrow()
- :color{
- id = "del",
- color = Color{ r=0, g=0, b=0, a=0 } --app.bgColor
- }
- :button{
- text = "clean up",
- onclick = function()
- Engy_Jagger(dlgWin)
- --dlgWin:close()
- app.refresh()
- end
- }
- dlgWin:show{ wait = false }
- -- Enginya's Jagger
- function Engy_Jagger(dlgWin)
- local spr = app.activeSprite;
- if not spr then
- app.alert "No active sprite!"
- return
- end
- --if spr.colorMode ~= ColorMode.RGB then
- -- app.alert "Not RGB mode!"
- -- return
- --end
- local sw = spr.width
- local sh = spr.height
- local pc = app.pixelColor
- local fg = dlgWin.data.sel.rgbaPixel
- local bg = dlgWin.data.del.rgbaPixel
- local cel = app.activeCel
- if cel == nil then
- app.alert "No image!"
- return
- end
- local img = cel.image:clone()
- local rx1 = cel.bounds.x
- local rx2 = rx1 + cel.bounds.width - 1
- local ry1 = cel.bounds.y
- local ry2 = ry1 + cel.bounds.height - 1
- local cc = pc.rgba(fg) --pc.rgba(0,0,0,255)
- local ct = bg --pc.rgba(255,0,0,0)
- --local ct = pc.rgba(0,0,0,0)
- function ff(xx,yy) return img:getPixel(xx,yy) == cc end
- function nn(xx,yy) return img:getPixel(xx,yy) ~= cc end
- local gx = {}
- gx[0] = -1
- gx[1] = 1
- gx[2] = 1
- gx[3] = -1
- gx[4] = -1
- gx[5] = 1
- gx[6] = 0
- gx[7] = 0
- local gy = {}
- gy[0] = -1
- gy[1] = -1
- gy[2] = 1
- gy[3] = 1
- gy[4] = 0
- gy[5] = 0
- gy[6] = -1
- gy[7] = 1
- --function gg(xx,yy,ii) return ff(xx+gx[ii],yy+gy[i]) end
- function clr(tab) for k,v in pairs(tab) do tab[k]=nil end end
- local pts = {}
- local was = {}
- local WW = sw*2
- function coll(xx,yy,kk)
- table.insert(pts,{x=xx,y=yy})
- if kk >= 3 then return end
- was[xx+yy*WW] = 1
- for ii=0,7 do
- tx=xx+gx[ii]
- ty=yy+gy[ii]
- if ff(tx,ty) and was[tx+ty*WW] ~= 1 then coll(tx,ty,kk+1) end
- end
- end
- function avga(xx,yy)
- mx = 0
- my = 0
- mn = 0
- for k,v in pairs(pts) do
- --print( v.x..','..v.y )
- mx = mx + v.x
- my = my + v.y
- mn = mn + 1
- end
- if mn <= 0 then return 0 end
- mx = mx / mn
- my = my / mn
- local aaa = math.atan( -(my-yy), mx-xx ) * 180 / 3.1415926
- if aaa < 0 then aaa = aaa + 360 end
- --print( "ANG="..aaa )
- return aaa
- end
- function ang(xx,yy,zx,zy,ax,ay,bx,by)
- clr(pts)
- clr(was)
- was[xx+yy*WW] = 1
- was[zx+zy*WW] = 1
- coll(xx+ax,yy+ay,1)
- local a1 = avga(xx,yy)
- clr(pts)
- clr(was)
- was[xx+yy*WW] = 1
- was[zx+zy*WW] = 1
- coll(xx+bx,yy+by,1)
- local a2 = avga(xx,yy)
- local d1 = a2-a1; if d1 < 0 then d1 = 360+d1 end
- local d2 = a1-a2; if d2 < 0 then d2 = 360+d2 end
- if d2 < d1 then d1 = d2 end
- --print( "DA="..d1 )
- return d1
- end
- local ttt = 10
- local any = 1
- while any > 0 do
- any = 0
- -- rule T: triple junctions
- for cy=ry1,ry2 do
- for cx=rx1,rx2 do
- local px = cx-rx1
- local py = cy-ry1
- if ff(px,py) then
- if ff(px-1,py) and ff(px+1,py) and ff(px,py-1) and nn(px,py+1) and nn(px-1,py-1) and nn(px+1,py-1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px-1,py) and ff(px+1,py) and ff(px,py+1) and nn(px,py-1) and nn(px-1,py+1) and nn(px+1,py+1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px,py-1) and ff(px,py+1) and ff(px+1,py) and nn(px-1,py) and nn(px+1,py-1) and nn(px+1,py+1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px,py-1) and ff(px,py+1) and ff(px-1,py) and nn(px+1,py) and nn(px-1,py-1) and nn(px-1,py+1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- end
- end
- end
- -- rule P: pairs with diagonal 2-connects
- for cy=ry1,ry2 do
- for cx=rx1,rx2 do
- local px = cx-rx1
- local py = cy-ry1
- if ff(px,py) then
- if ff(px+1,py) and nn(px-1,py) and nn(px+2,py) then
- if ff(px,py-1) and ff(px+1,py+1) and nn(px+2,py-1) and nn(px-1,py+1) and nn(px+1,py-1) and nn(px,py+1) then
- any = 1
- if ang(px,py,px+1,py,0,-1,1,1) > ang(px+1,py,px,py,-1,-1,0,1) then
- img:drawPixel( px+1, py, ct )
- else
- img:drawPixel( px, py, ct )
- end
- end
- if ff(px+1,py-1) and ff(px,py+1) and nn(px-1,py-1) and nn(px+2,py+1) and nn(px,py-1) and nn(px+1,py+1) then
- any = 1
- if ang(px,py,px+1,py,0,1,1,-1) > ang(px+1,py,px,py,-1,1,0,-1) then
- img:drawPixel( px+1, py, ct )
- else
- img:drawPixel( px, py, ct )
- end
- end
- end
- if ff(px,py+1) and nn(px,py-1) and nn(px,py+2) then
- if ff(px-1,py) and ff(px+1,py+1) and nn(px-1,py+2) and nn(px+1,py-1) and nn(px-1,py+1) and nn(px+1,py) then
- any = 1
- if ang(px,py,px,py+1,-1,0,1,1) > ang(px,py+1,px,py,-1,-1,1,0) then
- img:drawPixel( px, py+1, ct )
- else
- img:drawPixel( px, py, ct )
- end
- end
- if ff(px-1,py+1) and ff(px+1,py) and nn(px-1,py-1) and nn(px+1,py+2) and nn(px-1,py) and nn(px+1,py+1) then
- any = 1
- if ang(px,py,px,py+1,1,0,-1,1) > ang(px,py+1,px,py,1,-1,-1,0) then
- img:drawPixel( px, py+1, ct )
- else
- img:drawPixel( px, py, ct )
- end
- end
- end
- end
- end
- end
- -- rule C: lone corners
- for cy=ry1,ry2 do
- for cx=rx1,rx2 do
- local px = cx-rx1
- local py = cy-ry1
- if ff(px,py) then
- if ff(px,py-1) and ff(px+1,py) and nn(px-1,py) and nn(px-1,py+1) and nn(px,py+1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px,py-1) and ff(px-1,py) and nn(px+1,py) and nn(px+1,py+1) and nn(px,py+1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px,py+1) and ff(px+1,py) and nn(px-1,py) and nn(px-1,py-1) and nn(px,py-1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- if ff(px,py+1) and ff(px-1,py) and nn(px+1,py) and nn(px+1,py-1) and nn(px,py-1) then
- any = 1
- img:drawPixel( px, py, ct )
- end
- end
- end
- end
- ttt = ttt - 1
- if ttt <= 0 then break end
- end
- cel.image = img
- end
- app.transaction( function() Engy_Jagger() end )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement