Advertisement
b236

eg_jagger - simple ui

Oct 15th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | None | 0 0
  1.  
  2. local dlgWin = Dialog{ title = "eg jagger  " }  
  3. dlgWin
  4.     :color{
  5.         id = "sel",
  6.         color = app.fgColor
  7.         }
  8.    
  9.     :newrow()
  10.    
  11.     :color{
  12.         id = "del",
  13.         color = Color{ r=0, g=0, b=0, a=0 } --app.bgColor
  14.         }
  15.  
  16.     :button{
  17.         text = "clean up",
  18.         onclick = function()
  19.             Engy_Jagger(dlgWin)
  20.             --dlgWin:close()
  21.             app.refresh()
  22.         end
  23.     }
  24.  
  25. dlgWin:show{ wait = false }
  26.  
  27.  
  28. -- Enginya's Jagger
  29.  
  30. function Engy_Jagger(dlgWin)
  31.   local spr = app.activeSprite;
  32.   if not spr then
  33.     app.alert "No active sprite!"
  34.     return
  35.   end
  36.  
  37.   --if spr.colorMode ~= ColorMode.RGB then
  38.   --   app.alert "Not RGB mode!"
  39.   --   return
  40.   --end
  41.  
  42.   local sw = spr.width
  43.   local sh = spr.height
  44.  
  45.   local pc = app.pixelColor
  46.  
  47.   local fg = dlgWin.data.sel.rgbaPixel
  48.   local bg = dlgWin.data.del.rgbaPixel
  49.  
  50.   local cel = app.activeCel
  51.   if cel == nil then
  52.     app.alert "No image!"
  53.     return
  54.   end
  55.  
  56.   local img = cel.image:clone()
  57.  
  58.   local rx1 = cel.bounds.x
  59.   local rx2 = rx1 + cel.bounds.width - 1
  60.   local ry1 = cel.bounds.y
  61.   local ry2 = ry1 + cel.bounds.height - 1
  62.  
  63.   local cc = pc.rgba(fg) --pc.rgba(0,0,0,255)
  64.   local ct = bg --pc.rgba(255,0,0,0)
  65.   --local ct = pc.rgba(0,0,0,0)
  66.  
  67.   function ff(xx,yy) return img:getPixel(xx,yy) == cc end
  68.   function nn(xx,yy) return img:getPixel(xx,yy) ~= cc end
  69.  
  70.   local gx = {}
  71.   gx[0] = -1
  72.   gx[1] =  1
  73.   gx[2] =  1
  74.   gx[3] = -1
  75.   gx[4] = -1
  76.   gx[5] =  1
  77.   gx[6] =  0
  78.   gx[7] =  0
  79.  
  80.   local gy = {}
  81.   gy[0] = -1
  82.   gy[1] = -1
  83.   gy[2] =  1
  84.   gy[3] =  1
  85.   gy[4] =  0
  86.   gy[5] =  0
  87.   gy[6] = -1
  88.   gy[7] =  1
  89.  
  90.   --function gg(xx,yy,ii) return ff(xx+gx[ii],yy+gy[i]) end
  91.  
  92.   function clr(tab) for k,v in pairs(tab) do tab[k]=nil end end
  93.  
  94.   local pts = {}
  95.   local was = {}
  96.   local WW = sw*2
  97.  
  98.   function coll(xx,yy,kk)
  99.     table.insert(pts,{x=xx,y=yy})
  100.     if kk >= 3 then return end
  101.     was[xx+yy*WW] = 1
  102.     for ii=0,7 do
  103.       tx=xx+gx[ii]
  104.       ty=yy+gy[ii]
  105.       if ff(tx,ty) and was[tx+ty*WW] ~= 1 then coll(tx,ty,kk+1) end
  106.     end
  107.   end
  108.  
  109.   function avga(xx,yy)
  110.     mx = 0
  111.     my = 0
  112.     mn = 0
  113.    
  114.     for k,v in pairs(pts) do
  115.       --print( v.x..','..v.y )
  116.      
  117.       mx = mx + v.x
  118.       my = my + v.y
  119.       mn = mn + 1
  120.     end
  121.    
  122.     if mn <= 0 then return 0 end  
  123.    
  124.     mx = mx / mn
  125.     my = my / mn
  126.    
  127.     local aaa = math.atan( -(my-yy), mx-xx ) * 180 / 3.1415926
  128.     if aaa < 0 then aaa = aaa + 360 end
  129.    
  130.     --print( "ANG="..aaa )    
  131.     return aaa
  132.   end
  133.  
  134.   function ang(xx,yy,zx,zy,ax,ay,bx,by)
  135.     clr(pts)
  136.     clr(was)
  137.     was[xx+yy*WW] = 1
  138.     was[zx+zy*WW] = 1
  139.     coll(xx+ax,yy+ay,1)
  140.     local a1 = avga(xx,yy)
  141.    
  142.     clr(pts)
  143.     clr(was)
  144.     was[xx+yy*WW] = 1
  145.     was[zx+zy*WW] = 1
  146.     coll(xx+bx,yy+by,1)
  147.     local a2 = avga(xx,yy)
  148.    
  149.     local d1 = a2-a1; if d1 < 0 then d1 = 360+d1 end
  150.     local d2 = a1-a2; if d2 < 0 then d2 = 360+d2 end
  151.    
  152.     if d2 < d1 then d1 = d2 end
  153.     --print( "DA="..d1 )
  154.     return d1
  155.   end
  156.  
  157.   local ttt = 10
  158.   local any = 1
  159.   while any > 0 do
  160.     any = 0
  161.  
  162.     -- rule T: triple junctions
  163.  
  164.     for cy=ry1,ry2 do
  165.       for cx=rx1,rx2 do
  166.         local px = cx-rx1
  167.         local py = cy-ry1
  168.    
  169.         if ff(px,py) then
  170.          
  171.           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
  172.              any = 1
  173.              img:drawPixel( px, py, ct )
  174.           end
  175.  
  176.           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
  177.              any = 1
  178.              img:drawPixel( px, py, ct )
  179.           end
  180.  
  181.           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
  182.              any = 1
  183.              img:drawPixel( px, py, ct )
  184.           end
  185.  
  186.           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
  187.              any = 1
  188.              img:drawPixel( px, py, ct )
  189.           end
  190.         end
  191.       end
  192.     end
  193.  
  194.     -- rule P: pairs with diagonal 2-connects
  195.  
  196.     for cy=ry1,ry2 do
  197.       for cx=rx1,rx2 do
  198.         local px = cx-rx1
  199.         local py = cy-ry1
  200.    
  201.         if ff(px,py) then
  202.           if ff(px+1,py) and nn(px-1,py) and nn(px+2,py) then
  203.             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
  204.               any = 1
  205.               if ang(px,py,px+1,py,0,-1,1,1) > ang(px+1,py,px,py,-1,-1,0,1) then
  206.                 img:drawPixel( px+1, py, ct )
  207.               else
  208.                 img:drawPixel( px, py, ct )
  209.               end
  210.             end
  211.  
  212.             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
  213.               any = 1
  214.               if ang(px,py,px+1,py,0,1,1,-1) > ang(px+1,py,px,py,-1,1,0,-1) then
  215.                 img:drawPixel( px+1, py, ct )
  216.               else
  217.                 img:drawPixel( px, py, ct )
  218.               end
  219.             end
  220.           end
  221.    
  222.           if ff(px,py+1) and nn(px,py-1) and nn(px,py+2) then
  223.             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
  224.               any = 1
  225.               if ang(px,py,px,py+1,-1,0,1,1) > ang(px,py+1,px,py,-1,-1,1,0) then
  226.                 img:drawPixel( px, py+1, ct )
  227.               else
  228.                 img:drawPixel( px, py, ct )
  229.               end
  230.             end
  231.  
  232.             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
  233.               any = 1
  234.               if ang(px,py,px,py+1,1,0,-1,1) > ang(px,py+1,px,py,1,-1,-1,0) then
  235.                 img:drawPixel( px, py+1, ct )
  236.               else
  237.                 img:drawPixel( px, py, ct )
  238.               end
  239.             end
  240.           end
  241.         end
  242.       end
  243.     end
  244.  
  245.     -- rule C: lone corners
  246.    
  247.     for cy=ry1,ry2 do
  248.       for cx=rx1,rx2 do
  249.         local px = cx-rx1
  250.         local py = cy-ry1
  251.    
  252.         if ff(px,py) then
  253.           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
  254.             any = 1
  255.             img:drawPixel( px, py, ct )
  256.           end
  257.  
  258.           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
  259.             any = 1
  260.             img:drawPixel( px, py, ct )
  261.           end
  262.  
  263.           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
  264.             any = 1
  265.             img:drawPixel( px, py, ct )
  266.           end
  267.  
  268.           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
  269.             any = 1
  270.             img:drawPixel( px, py, ct )
  271.           end
  272.         end
  273.       end
  274.     end
  275.    
  276.     ttt = ttt - 1
  277.     if ttt <= 0 then break end
  278.   end
  279.  
  280.   cel.image = img
  281. end
  282.  
  283. app.transaction( function() Engy_Jagger() end )
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement