b236

eg_jagger - no ui

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