Advertisement
Guest User

grafx2 remove chess pattern method 2 lua

a guest
Jan 17th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. -- Remove dither pattern with horizontal approach - 1 pixel up and own supportedd- will add this for two in future---
  2. -- Method 2 is different from method 1
  3. --
  4.  
  5. -- This variant needs image to only use colors < 16
  6.  
  7.  
  8. -- Determines which color should store the average of colors i and j.
  9. -- Only works if i and j are different and both < 16.
  10. function transp(i, j)
  11.   if i < j then
  12.     return i*15+j+15
  13.   else
  14.     return j*15+i+15
  15.   end
  16. end
  17.  
  18. -- Prepares the palette.
  19. -- Thanks to DawnBringer for gamma expertise
  20. -- Gamma factor should be set close to 1.6 for converting images designed on
  21. -- very low resolutions, up to 2.2 on very high resolutions.
  22. --
  23. gamma = 1.8 -- gamma factor.
  24. for i = 0, 15 do
  25.   for j = i+1, 15 do
  26.     r1, g1, b1 = getcolor(i);
  27.     r2, g2, b2 = getcolor(j);    
  28.     r3 = math.pow(((math.pow(r1/255,gamma) + math.pow(r2/255,gamma)) / 2),(1/gamma))*255
  29.     g3 = math.pow(((math.pow(g1/255,gamma) + math.pow(g2/255,gamma)) / 2),(1/gamma))*255
  30.     b3 = math.pow(((math.pow(b1/255,gamma) + math.pow(b2/255,gamma)) / 2),(1/gamma))*255            
  31.     setcolor(transp(i,j), r3, g3, b3)
  32.   end
  33. end
  34.  
  35.  
  36. -- Remove dither pattern with horizontal approach - 1 pixel up and own supportedd- will add  it for two in furue---
  37.  
  38. w, h = getpicturesize()
  39.  
  40. for y = 0, h - 1, 1 do
  41.  
  42.   for x = 0, w - 4, 1 do
  43.  
  44. get = getbackuppixel
  45. put = putpicturepixel  
  46. c1 = get(x,y);
  47. c2 = get(x+1,y);
  48. c3 = get(x+2,y);
  49. c4 = get(x+3,y);
  50.    
  51. if (c1 == c3) and (c2 == c4) and (c1 ~= c2) and (c2 ~= c3) and (get(x,y+1) ~= c1) and (get(x+1,y+1) ~= c2) and (get(x+2,y+1) ~= c3) and (get(x+3,y+1) ~= c4) and (get(x,y-1) ~= c1) and (get(x+1,y-1) ~= c2) and (get(x+2,y-1) ~= c3) and (get(x+3,y-1) ~= c4) then
  52.   r1, g1, b1 = getcolor(c1);
  53.   r2, g2, b2 = getcolor(c2);
  54.  
  55.  
  56.   avgcol = transp(c1, c2)
  57.  
  58.   put(x, y, avgcol);
  59.   put(x+1, y, avgcol);
  60.   put(x+2, y, avgcol);
  61.   put(x+3, y, avgcol);
  62.  
  63. -- expand on all 4 directions up --
  64.     if (get(x,y+1) == c2) and (get(x+1,y+1) ~= c2) and (get(x-1,y+1) ~= c2) and ((get(x+1,y+1) == c1) or (get(x-1,y+1) == c1)) then
  65. put(x, y+1, avgcol);
  66.   end
  67.   if (get(x+1,y+1) == c1) and (get(x+2,y+1) ~= c1) and (get(x,y+1) ~= c1) and ((get(x+2,y+1) == c2) or (get(x,y+1) == c2)) then
  68. put(x+1, y+1, avgcol);
  69.   end
  70.   if (get(x+2,y+1) == c2) and (get(x+3,y+1) ~= c2) and (get(x+1,y+1) ~= c2) and ((get(x+3,y+1) == c1) or (get(x+1,y+1) == c1)) then
  71. put(x+2, y+1, avgcol);
  72.   end
  73.   if (get(x+3,y+1) == c1) and  (get(x+4,y+1) ~= c1) and (get(x+2,y+1) ~= c1) and ((get(x+4,y+1) == c2) or (get(x+2,y+1) == c2)) then
  74. put(x+3, y+1, avgcol);
  75.   end
  76. -- expand on all 4 directions down --
  77.     if (get(x,y-1) == c2) and (get(x+1,y-1) ~= c2) and (get(x-1,y-1) ~= c2) and ((get(x+1,y-1) == c1) or (get(x-1,y-1) == c1)) then
  78. put(x, y-1, avgcol);
  79.   end
  80.   if (get(x+1,y-1) == c1) and (get(x+2,y-1) ~= c1) and (get(x,y-1) ~= c1) and ((get(x+2,y-1) == c2) or (get(x,y-1) == c2)) then
  81. put(x+1, y-1, avgcol);
  82.   end
  83.   if (get(x+2,y-1) == c2) and (get(x+3,y-1) ~= c2) and (get(x+1,y-1) ~= c2) and ((get(x+3,y-1) == c1) or (get(x+1,y-1) == c1)) then
  84. put(x+2, y-1, avgcol);
  85.   end
  86.   if (get(x+3,y-1) == c1) and  (get(x+4,y-1) ~= c1) and (get(x+2,y-1) ~= c1) and ((get(x+4,y-1) == c2) or (get(x+2,y-1) == c2)) then
  87. put(x+3, y-1, avgcol);
  88.   end
  89. end
  90.   end
  91. end
  92.  
  93. -- Remove left dither pattern with 4x1 vertical. Contains some important if then else conditions.--
  94.  
  95. w, h = getpicturesize()
  96.  
  97. for y = 0, h - 4, 1 do
  98.  
  99.   for x = 0, w - 1, 1 do
  100.  
  101. get = getbackuppixel
  102. put = putpicturepixel  
  103. c1 = get(x,y);
  104. c2 = get(x,y+1);
  105. c3 = get(x,y+2);
  106. c4 = get(x,y+3);
  107.    
  108. if (c1 == c3) and (c2 == c4) and  (c1 ~= c2) and (c2 ~= c3) and (get(x+1,y) ~= c1) and (get(x+1,y+1) ~= c2) and (get(x+1,y+2) ~= c3) and (get(x+1,y+3) ~= c4) and (get(x-1,y) ~= c1) and (get(x-1,y+1) ~= c2) and (get(x-1,y+2) ~= c3) and (get(x-1,y+3) ~= c4) and (get(x,y+4) ~=c4) and (get(x,y-1) ~=c1) then
  109.   r1, g1, b1 = getcolor(c1);
  110.   r2, g2, b2 = getcolor(c2);
  111.  
  112.  
  113.   avgcol = transp(c1, c2)
  114.  
  115.   put(x, y, avgcol);
  116.   put(x, y+1, avgcol);
  117.   put(x, y+2, avgcol);
  118.   put(x, y+3, avgcol);
  119.  
  120. -- expand on all 4 directions up --
  121.   if (get(x+1,y) == c2) and (((get(x+1,y+1) ~= c2) and (get(x+1,y-1) == c1)) or ((get(x+1,y+1) == c1) and (get(x+1,y-1) ~= c2))) then
  122. put(x+1, y, avgcol);
  123.   end
  124.   if (get(x+1,y+1) == c1) and (((get(x+1,y+2) ~= c1) and (get(x+1,y) == c2)) or ((get(x+1,y+2) == c2) and (get(x+1,y) ~= c1))) then
  125. put(x+1, y+1, avgcol);
  126.   end
  127.   if (get(x+1,y+2) == c2) and (((get(x+1,y+3) ~= c2) and (get(x+1,y+1) == c1)) or ((get(x+1,y+3) == c1) and (get(x+1,y+1) ~= c2))) then
  128. put(x+1, y+2, avgcol);
  129.   end
  130.   if (get(x+1,y+3) == c1) and  (((get(x+1,y+4) ~= c1) and (get(x+1,y+2) == c2)) or ((get(x+1,y+4) == c2) and (get(x+1,y+2) ~= c1))) then
  131. put(x+1, y+3, avgcol);
  132.   end
  133. -- expand on all 4 directions down --
  134.   if (get(x-1,y) == c2) and (((get(x-1,y+1) ~= c2) and (get(x-1,y-1) == c1)) or ((get(x-1,y+1) == c1) and (get(x-1,y-1) ~= c2))) then
  135. put(x-1, y, avgcol);
  136.   end
  137.   if (get(x-1,y+1) == c1) and (((get(x-1,y+2) ~= c1) and (get(x-1,y) == c2)) or ((get(x-1,y+2) == c2) and (get(x-1,y) ~= c1))) then
  138. put(x-1, y+1, avgcol);
  139.   end
  140.   if (get(x-1,y+2) == c2) and (((get(x-1,y+3) ~= c2) and (get(x-1,y+1) == c1)) or ((get(x-1,y+3) == c1) and (get(x-1,y+1) ~= c2))) then
  141. put(x-1, y+2, avgcol);
  142.   end
  143.   if (get(x-1,y+3) == c1) and  (((get(x-1,y+4) ~= c1) and (get(x-1,y+2) == c2)) or ((get(x-1,y+4) == c2) and (get(x-1,y+2) ~= c1))) then
  144. put(x-1, y+3, avgcol);
  145.   end
  146. end
  147.   end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement