Advertisement
Cralant

Compression techniques for arrays.

May 20th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. {{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},
  2. {r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},
  3. {r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},{r=0,g=0,b=1,a=1},{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},{r=1,g=0,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1},{r=0,g=1,b=0,a=1}}
  4. --489 Chars, Uncompressed values
  5.  
  6.  
  7. c1 = {r=1,g=0,b=0,a=1}
  8. c2 = {r=0,g=1,b=0,a=1}
  9. c3 = {r=0,g=0,b=1,a=1}
  10. --Pre set variables for the rgba values.
  11.  
  12.  
  13. {c1,c1,c1,c2,c2,c2,c3,c3,c3,
  14. c2,c2,c2,c3,c3,c3,c1,c1,c1,
  15. c3,c3,c3,c1,c1,c1,c2,c2,c2}
  16. --84 Chars
  17. --Each rgba colour value is compressed into a pre set variable for the colour.
  18. --E.g: c1 = {r=1,g=0,b=0,a=1}
  19.  
  20. {{c1,3},{c2,3},{c3,3},
  21. {c2,3},{c3,3},{c1,3},
  22. {c3,3},{c1,3},{c2,3}}
  23. --66 Chars
  24. --Stores value of colour and how many times it has repeated.
  25. --E.g: {c1,3} = c1,c1,c1
  26.  
  27. while k <= imgWidth*imgHeight do
  28. arr = image[s]
  29. num = arr[2]
  30. col = arr[1]
  31. for i = 1, num do
  32. x = x+1
  33. if x > imgWidth then
  34. y = y+1
  35. x = 1
  36. end
  37. this:set_sprite_texel(x-1,imgHeight-y,col.r/255,col.g/255,col.b/255,col.a/255)
  38. end
  39. k = k + num
  40. s = s + 1
  41. end
  42.  
  43.  
  44. {1,0,0,1,0,0,1,0,0,
  45. -1,0,0,1,0,0,-2,0,0,
  46. 2,0,0,-2,0,0,1,0,0}
  47. --60 Chars
  48. --Takes the first number of the string and converts it to an integer step from the previous number.
  49. --E.g: image[1] = c1, image[0] = nil/c0 compressed array has the first step as 1.
  50. --image[2] = c1, image[1] = c1 compressed array has the second step as 0. etc... All steps are from the previous entry.
  51.  
  52. while k <= imgWidth*imgHeight do
  53. colCode = image[k] + (prev ~= nil and prev or 0)
  54. x = x + 1
  55. if x > imgWidth then
  56. y = y + 1
  57. x = 1
  58. end
  59. col = "c"..colCode
  60. k = k + 1
  61. prev = colCode
  62. this:set_sprite_texel(x-1,imgHeight-y,col.r,col.g,col.b,col.a)
  63. end
  64.  
  65.  
  66. --[[
  67. Future ideas:
  68. Use combination of shortening arrays by method {c1,3} and apply shortening method to values that only repeat 3 or more times. Use script to detect if the value in the table is another table or a colour variable and act accordingly.
  69. Use same method as above for the integer step method, may work for more repetitive arrays. (This method will probably take up more space the just using variables for the rgba values if the image is grainy and has few repeating colour sets, same with the above method.)
  70. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement