colourreducer_zxspectrum_post.lua 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -- cell colour reducer - zxspectrum - from Paulo Silva, july 2012 - gpl licence
  2. w,h=getpicturesize()
  3. ok,halfb,timx=inputbox("Modify cell pixel size",
  4. "halfbright",180,0,255,1,
  5. "timex 8x1",0,0,1,0);
  6. -- print ("");print (halfb);print (timx);print ("")
  7. xcell=8;ycell=8
  8. if timx==1 then ycell=1;end
  9. function grayscaleindexed(c)
  10. r,g,b=getcolor(c);return math.floor((b*11+r*30+g*59)/100);end
  11. if ok==true then
  12. celcnt={};for n=0,255,1 do celcnt[n+1]=0;end
  13. for y1=0,h-1,ycell do
  14. --statusmessage( tostring(math.floor(y1/ycell*100)).."%")
  15. --statusmessage("testing...")
  16. --updatescreen()
  17. for x1=0,w-1,xcell do
  18. for i=0,255,1 do celcnt[i+1]=0;end
  19. for y2=0,ycell-1,1 do
  20. for x2=0,xcell-1,1 do
  21. x=x1+x2;y=y1+y2;u=getpicturepixel(x,y)
  22. celcnt[u+1]=celcnt[u+1]+(1000*xcell*ycell)+math.random(0,950);end;end
  23. ikattr=0;paattr=0;ikcnt=0;pacnt=0
  24. for i=0,255,1 do
  25. if ikcnt<celcnt[i+1] then ikcnt=celcnt[i+1];ikattr=i;end;end
  26. celcnt[ikattr+1]=0
  27. for i=0,255,1 do
  28. if pacnt<celcnt[i+1] then pacnt=celcnt[i+1];paattr=i;end;end
  29. if grayscaleindexed(ikattr)>grayscaleindexed(paattr) then tmpr=ikattr;ikattr=paattr;paattr=tmpr;end
  30. wmid=math.floor((grayscaleindexed(paattr)+grayscaleindexed(ikattr))/2)
  31. for y2=0,ycell-1,1 do
  32. for x2=0,xcell-1,1 do
  33. x=x1+x2;y=y1+y2;u=getpicturepixel(x,y)
  34. if u==ikattr then
  35. idou=ikattr
  36. elseif u==paattr then
  37. idou=paattr
  38. else
  39. idou=ikattr
  40. if grayscaleindexed(u)>wmid then idou=paattr;end
  41. end
  42. putpicturepixel(x,y,idou)
  43. end;end
  44. end;end
  45. for y=0,h-1,1 do
  46. for x=0,w-1,1 do
  47. c2=0;fb=1;c=getpicturepixel(x,y);r,g,b=getcolor(c)
  48. if (r<halfb) and ((g<halfb)and (b<halfb)) then fb=0;end
  49. if b>(halfb/2) then c2=c2+1;end
  50. if r>(halfb/2) then c2=c2+2;end
  51. if g>(halfb/2) then c2=c2+4;end
  52. c2=c2+(fb*8)
  53. putpicturepixel(x,y,c2)
  54. end;end
  55. for c=0,255,1 do setcolor(c,0x00,0x00,0x00);end
  56. setcolor( 0,0x00, 0x00,0x00);setcolor( 1,0x00, 0x00,halfb);setcolor( 2,halfb, 0x00,0x00);setcolor( 3,halfb,0x00,halfb)
  57. setcolor( 4,0x00,halfb,0x00);setcolor( 5,0x00,halfb,halfb);setcolor( 6,halfb,halfb,0x00);setcolor( 7,halfb,halfb,halfb)
  58. setcolor( 8,0x00, 0x00,0x00);setcolor( 9,0x00, 0x00, 0xFF);setcolor(10,0xFF, 0x00,0x00);setcolor(11,0xFF,0x00,0xFF)
  59. setcolor(12,0x00, 0xFF,0x00);setcolor(13,0x00, 0xFF, 0xFF);setcolor(14,0xFF, 0xFF,0x00);setcolor(15,0xFF,0xFF,0xFF)
  60. for y1=0,h-1,ycell do
  61. for x1=0,w-1,xcell do
  62. hbc=0
  63. for y2=0,ycell-1,1 do
  64. for x2=0,xcell-1,1 do
  65. x=x1+x2;y=y1+y2
  66. c=getpicturepixel(x,y)
  67. fb=math.floor(c/8)
  68. hbc=hbc+fb
  69. if c==0 then hbc=hbc+.5;end
  70. if c==8 then hbc=hbc-.5;end
  71. end;end
  72. hbc2=math.floor(hbc/(1+xcell*ycell/2))
  73. --print (hbc2)
  74. for y2=0,ycell-1,1 do
  75. for x2=0,xcell-1,1 do
  76. x=x1+x2;y=y1+y2;c=(getpicturepixel(x,y)%8)+(hbc2*8);putpicturepixel(x,y,c)
  77. end;end
  78. end;end
  79. end