Advertisement
Ningow

Screentone

Dec 14th, 2020
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.44 KB | None | 0 0
  1. function init()
  2.     setName("Screentone")
  3.     setDesc("Dithering machine")
  4.     setSize(85,16+8+24+64+8+8+7+4)
  5.     addOutput(24+32)
  6.     addInput("",24+32)
  7.     addParameter("Intensity","Intensity",24+64+9,100,0,200,true)
  8.     addParameter("Style","Dithering style",16+24+64+9,1,0,7)
  9.     addParameter("Steps","Dithering steps",16+16+24+64+9,3,1,3)
  10. end
  11. -- Ugly way to use channels
  12. function checker(x,y,sel)
  13.     if sel == 0 then
  14.         if (x+y)%2 == 0 then
  15.             return 0
  16.         end
  17.         return 1
  18.     elseif sel == 1 then
  19.         if (x+y)%4 == 0 or (x-y)%4 == 0 then
  20.             return 0
  21.         end
  22.         return 1
  23.     elseif sel == 2 then
  24.         if (x+y)%4 == 0 and x%2 == 0 and y%2 == 0 then
  25.             return 0
  26.         end
  27.         return 1
  28.     elseif sel == 3 then
  29.         if (x+y+1)%4 == 0 or (x-y+1)%4 == 0 then
  30.             return 1
  31.         end
  32.         return 0
  33.     elseif sel == 4 then
  34.         if (x+y)%4 == 0 and x%2 == 0 and y%2 ==0 then
  35.             return 1
  36.         end
  37.         return 0
  38.     end
  39. end
  40.  
  41. function lined(x,y,sel)
  42.     if sel == 0 then
  43.         if y%2 == 0 then
  44.             return 0
  45.         end
  46.         return 1
  47.     elseif sel == 1 then
  48.         if y%2 == 0 and (x+y)%4 ~= 1 then
  49.             return 0
  50.         end
  51.         return 1
  52.     elseif sel == 2 then
  53.         if (y+1)%2 == 0 and (x+y+1)%4 ~= 1 then
  54.             return 1
  55.         end
  56.         return 0
  57.     end
  58. end
  59.  
  60. function crossd(x,y,sel)
  61.     if sel == 0 then
  62.         if (x+y)%4 == 0 then
  63.             return 0
  64.         end
  65.         return 1
  66.     elseif sel == 1 then
  67.         if (x-y+1)%4 == 0 then
  68.             return 1
  69.         end
  70.         return 0
  71.     end
  72. end
  73.  
  74. function block(x,y,sel,inv)
  75.     if sel == 0 then
  76.         if (x*y)%2 == 0 then
  77.             return 0+inv
  78.         end
  79.         return 1-inv
  80.     elseif sel == 1 then
  81.         if (x*y)%4 == 0 then
  82.             return 1-inv
  83.         end
  84.         return 0+inv
  85.     elseif sel == 2 then
  86.         if (x*y)%4 == 0 then
  87.             return 1-inv
  88.         end
  89.         return 0+inv
  90.     end
  91. end
  92.  
  93. function fourline(x,y,sel,inv)
  94.     if sel == 0 then
  95.         if x%2 == 0 then
  96.             if x%4 == 0 and y%16 < 8 then
  97.                 return 1-inv
  98.             else
  99.                 return 0+inv
  100.             end
  101.         end
  102.         return 1-inv
  103.     elseif sel == 1 then
  104.         if x%4 == 0 and y%16 < 8 then
  105.             return 0+inv
  106.         end
  107.         return 1-inv
  108.     elseif sel == 2 then
  109.         if y%8 < 4 then
  110.             return 0+inv
  111.         end
  112.         return 1-inv
  113.     end
  114. end
  115.  
  116. function apply()
  117.     math.randomseed (getValue(5,0,0,1))
  118.     x = 0
  119.     y = 0
  120.     tileSize = getTileSize()
  121.     intensity = getValue(1,0,0,100)
  122.     style = getValue(2,0,0,1)
  123.     steps = getValue(3,0,0,1)
  124.     for i=0, tileSize*tileSize-1 do
  125.         x = i%tileSize
  126.         y = math.floor(i/tileSize)
  127.         input = getValue(0,x,y,1)*intensity
  128.        
  129.         if style == 0 then --No Dithering
  130.             if input > 0.5 then
  131.                 out = 1
  132.             else
  133.                 out = 0
  134.             end
  135.         elseif style == 1 then --Default Dither
  136.             if steps == 3 then
  137.                 if input > 6/7 then out = 1
  138.                 elseif input > 5/7 then
  139.                     out = checker(x,y,2)
  140.                 elseif input > 4/7 then
  141.                     out = checker(x,y,1)
  142.                 elseif input > 3/7 then
  143.                     out = checker(x,y,0)
  144.                 elseif input > 2/7 then
  145.                     out = checker(x,y,3)
  146.                 elseif input > 1/7 then
  147.                     out = checker(x,y,4)
  148.                 else out = 0
  149.                 end
  150.             elseif steps == 2 then
  151.                 if input > 4/5 then out = 1
  152.                 elseif input > 3/5 then
  153.                     out = checker(x,y,2)
  154.                 elseif input > 2/5 then
  155.                     out = checker(x,y,0)
  156.                 elseif input > 1/5 then
  157.                     out = checker(x,y+1,4)
  158.                 else out = 0
  159.                 end
  160.             else
  161.                 if input > 2/3 then out = 1
  162.                 elseif input > 1/3 then
  163.                     out = checker(x,y,0)
  164.                 else out = 0
  165.                 end
  166.             end
  167.         elseif style == 2 then --H Lined
  168.             if steps == 3 then
  169.                 if input > 6/7 then out = 1
  170.                 elseif input > 5/7 then
  171.                     out = checker(x+1,y,2)
  172.                 elseif input > 4/7 then
  173.                     out = lined(x,y,1)
  174.                 elseif input > 3/7 then
  175.                     out = lined(x,y,0)
  176.                 elseif input > 2/7 then
  177.                     out = lined(x,y,2)
  178.                 elseif input > 1/7 then
  179.                     out = checker(x+1,y+1,4)
  180.                 else out = 0
  181.                 end
  182.             elseif steps == 2 then
  183.                 if input > 4/5 then out = 1
  184.                 elseif input > 3/5 then
  185.                     out = lined(x,y,1)
  186.                 elseif input > 2/5 then
  187.                     out = lined(x,y,0)
  188.                 elseif input > 1/5 then
  189.                     out = lined(x,y,2)
  190.                 else out = 0
  191.                 end
  192.             else
  193.                 if input > 2/3 then out = 1
  194.                 elseif input > 1/3 then
  195.                     out = lined(x,y,0)
  196.                 else out = 0
  197.                 end
  198.             end
  199.         elseif style == 3 then --V Lined
  200.             if steps == 3 then
  201.                 if input > 6/7 then out = 1
  202.                 elseif input > 5/7 then
  203.                     out = checker(y+1,x,2)
  204.                 elseif input > 4/7 then
  205.                     out = lined(y,x,1)
  206.                 elseif input > 3/7 then
  207.                     out = lined(y,x,0)
  208.                 elseif input > 2/7 then
  209.                     out = lined(y,x,2)
  210.                 elseif input > 1/7 then
  211.                     out = checker(y+1,x+1,4)
  212.                 else out = 0
  213.                 end
  214.             elseif steps == 2 then
  215.                 if input > 4/5 then out = 1
  216.                 elseif input > 3/5 then
  217.                     out = checker(y+1,x,2)
  218.                 elseif input > 2/5 then
  219.                     out = lined(y,x,1)
  220.                 elseif input > 1/5 then
  221.                     out = checker(y+1,x+1,4)
  222.                 else out = 0
  223.                 end
  224.             else
  225.                 if input > 2/3 then out = 1
  226.                 elseif input > 1/3 then
  227.                     out = lined(y,x,1)
  228.                 else out = 0
  229.                 end
  230.             end
  231.         elseif style == 4 then --Crossed
  232.             if steps == 3 then
  233.                 if input > 6/7 then out = 1
  234.                 elseif input > 5/7 then
  235.                     out = checker(y,x,2)
  236.                 elseif input > 4/7 then
  237.                     out = crossd(y,x,0)
  238.                 elseif input > 3/7 then
  239.                     out = checker(y,x,1)
  240.                 elseif input > 2/7 then
  241.                     out = crossd(y,x,1)
  242.                 elseif input > 1/7 then
  243.                     out = checker(y+2,x+1,4)
  244.                 else out = 0
  245.                 end
  246.             elseif steps == 2 then
  247.                 if input > 4/5 then out = 1
  248.                 elseif input > 3/5 then
  249.                     out = crossd(y,x,0)
  250.                 elseif input > 2/5 then
  251.                     out = checker(y,x,1)
  252.                 elseif input > 1/5 then
  253.                     out = crossd(y,x,1)
  254.                 else out = 0
  255.                 end
  256.             else
  257.                 if input > 2/3 then out = 1
  258.                 elseif input > 1/3 then
  259.                     out = checker(y,x,1)
  260.                 else out = 0
  261.                 end
  262.             end
  263.         elseif style == 5 then --Blocks
  264.             if steps == 3 then
  265.                 if input > 6/7 then out = 1
  266.                 elseif input > 5/7 then
  267.                     out = block(x,y,0,1)
  268.                 elseif input > 4/7 then
  269.                     out = block(x,y,1,0)
  270.                 elseif input > 3/7 then
  271.                     out = lined(y,x,0)
  272.                 elseif input > 2/7 then
  273.                     out = block(x,y,1,1)
  274.                 elseif input > 1/7 then
  275.                     out = block(x,y,0,0)
  276.                 else out = 0
  277.                 end
  278.             elseif steps == 2 then
  279.                 if input > 4/5 then out = 1
  280.                 elseif input > 3/5 then
  281.                     out = block(x,y,0,1)
  282.                 elseif input > 2/5 then
  283.                     out = lined(y,x,0)
  284.                 elseif input > 1/5 then
  285.                     out = block(x,y,0,0)
  286.                 else out = 0
  287.                 end
  288.             else
  289.                 if input > 2/3 then out = 1
  290.                 elseif input > 1/3 then
  291.                     out = block(x,y,0,0)
  292.                 else out = 0
  293.                 end
  294.             end
  295.         elseif style == 6 then --4x4 Vline
  296.             if steps == 3 then
  297.                 if input > 6/7 then out = 1
  298.                 elseif input > 5/7 then
  299.                     out = fourline(x,y,1,0)
  300.                 elseif input > 4/7 then
  301.                     out = fourline(x,y,0,0)
  302.                 elseif input > 3/7 then
  303.                     out = lined(y,x,0)
  304.                 elseif input > 2/7 then
  305.                     out = fourline(x+1,y+8,0,1)
  306.                 elseif input > 1/7 then
  307.                     out = fourline(x+1,y+8,1,1)
  308.                 else out = 0
  309.                 end
  310.             elseif steps == 2 then
  311.                 if input > 4/5 then out = 1
  312.                 elseif input > 3/5 then
  313.                     out = fourline(x,y,0,0)
  314.                 elseif input > 2/5 then
  315.                     out = lined(y,x,0)
  316.                 elseif input > 1/5 then
  317.                     out = fourline(x+1,y+8,0,1)
  318.                 else out = 0
  319.                 end
  320.             else
  321.                 if input > 2/3 then out = 1
  322.                 elseif input > 1/3 then
  323.                     out = lined(y,x,0)
  324.                 else out = 0
  325.                 end
  326.             end
  327.         else --Random
  328.             if steps == 3 then
  329.                 if input > (math.random(100,600)/700) then out = 1
  330.                 else out = 0
  331.                 end
  332.             elseif steps == 2 then
  333.                 if input > (math.random(100,400)/500) then out = 1
  334.                 else out = 0
  335.                 end
  336.             else
  337.                 if input > (math.random(100,200)/300) then out = 1
  338.                 else out = 0
  339.                 end
  340.             end
  341.         end
  342.        
  343.         setPixel(0,x,y,out,out,out)
  344.     end
  345.    
  346.    
  347. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement