Advertisement
b236

eg_jagger - no ui - selection

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