cellcolourreducer.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- cell colour reducer - jan'11, from Paulo Silva, with help from people from GrafX2 google group (DawnBringer, Adrien Destugues (PulkoMandy), and Yves Rizoud)
  2. -- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. See <http://www.gnu.org/licenses/>
  3. w,h=getpicturesize()
  4. ok,xcell,ycell=inputbox("Modify cell pixel size","xcell",8,1,16,4,"ycell",8,1,16,4);
  5. if ok==true then
  6. function grayscaleindexed(c)
  7. r,g,b=getcolor(c);return math.floor((b*11+r*30+g*59)/100);end
  8. celcnt={};for n=0,255,1 do celcnt[n+1]=0;end -- Arraycounter must have initial value
  9. for y1=0,h-1,ycell do
  10. for x1=0,w-1,xcell do
  11. for i=0,255,1 do
  12. celcnt[i+1]=0;end
  13. for y2=0,ycell-1,1 do
  14. for x2=0,xcell-1,1 do
  15. x=x1+x2;y=y1+y2;u=getpicturepixel(x,y)
  16. celcnt[u+1]=celcnt[u+1]+(1000*xcell*ycell)+math.random(0,950);end;end
  17. ikattr=0;paattr=0;ikcnt=0;pacnt=0
  18. for i=0,255,1 do
  19. if ikcnt<celcnt[i+1] then ikcnt=celcnt[i+1];ikattr=i;end;end
  20. celcnt[ikattr+1]=0
  21. for i=0,255,1 do
  22. if pacnt<celcnt[i+1] then pacnt=celcnt[i+1];paattr=i;end;end
  23. if grayscaleindexed(ikattr)>grayscaleindexed(paattr) then tmpr=ikattr;ikattr=paattr;paattr=tmpr;end
  24. wmid=math.floor((grayscaleindexed(paattr)+grayscaleindexed(ikattr))/2)
  25. for y2=0,ycell-1,1 do
  26. for x2=0,xcell-1,1 do
  27. x=x1+x2;y=y1+y2;u=getpicturepixel(x,y)
  28. if u==ikattr then
  29. idou=ikattr
  30. elseif u==paattr then
  31. idou=paattr
  32. else
  33. idou=ikattr
  34. if grayscaleindexed(u)>wmid then idou=paattr;end
  35. end
  36. putpicturepixel(x,y,idou)
  37. end;end;end;end;end