Advertisement
RamiLego

LIKOGif.lua

Jul 12th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. --A special gif library for LIKO-12
  2. --Written by Rami Sabbagh (RamiLego4Game)
  3.  
  4. --Source: http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
  5.  
  6. --Arguments: filename, width, height, 16 color palette
  7.  
  8. local args = {...}
  9. local gif = {}
  10. local bit = require("bit")
  11.  
  12. local zero = string.char(0)
  13.  
  14. --Helping function
  15. local function bytes(b,len)
  16.   if #b >= len then return b:sub(1,len) end
  17.   return b..string.rep(zero,len-#b)
  18. end
  19.  
  20. local function indian(str,len)
  21.   if len then str = bytes(str,len) end
  22.   local bytes = {}
  23.   local counter = #str
  24.   for char in string.gmatch(".",str) do
  25.     bytes[counter] = char
  26.     counter = counter-1
  27.   end
  28.   return table.concat(bytes)
  29. end
  30.  
  31. local function tobytes(num,len)
  32.   local str = ""
  33.   while true do
  34.     local byte = bit.band(num,255)
  35.     num = bit.rshift(num,8)
  36.     str = str..string.char(byte)
  37.     if num == 0 then break end
  38.   end
  39.   return indian(str,len)
  40. end
  41.  
  42. --It's better to use local variables for speed.
  43. local filename = args[1] or "NULL"
  44. local file = love.filesystem.newFile(filename,"w")
  45.  
  46. --Header
  47. file:write("GIF87a",6)
  48.  
  49. --Logical Screen Descriptor
  50. local LSD = ""
  51. local width = args[2] or 192
  52. local height = args[3] or 128
  53. LSD = LSD..tobytes(width,2)..tobytes(height,2)
  54. LSD = LSD..tobytes(tonumber(10110011,2),1) --Packed Byte, configured for 16 color, 4bits index
  55. LSD = LSD..tobytes(0,1) --Background color index
  56. LSD = LSD..tobytes(0,1) --Pixel aspect ratio
  57.  
  58. file:write(LSD,7)
  59.  
  60. --Global Color Table
  61. local palette = args[4]
  62. local paletteBytes = {}
  63.  
  64. for k,v in ipairs(palette) do
  65.   table.insert(paletteBytes, tobytes(v[1],1)..tobytes(v[2],1)..tobytes(v[3],1))
  66. end
  67.  
  68. file:write(table.concat(paletteBytes),48)
  69.  
  70. --Static Image Descriptor
  71. --No local palette:
  72. local ImgDescBase = tobytes(0x2C,1) .. tobytes(0x0000,2) .. tobytes(0x0000,2) .. tobytes(width,2) .. tobytes(height,2)
  73. local ImgDesc = ImgDescBase .. string.char(tonumber(00000000,2))
  74.  
  75. --With local palette:
  76. local ImgDescLocal = ImgDescBase .. string.char(tonumber(10000011,2))
  77.  
  78. --Special Variables
  79. local localpalette
  80. local rawbase = {}
  81. for i=1, width*height do table.insert(rawbase,zero) end
  82.  
  83. function gif.setPalette(p)
  84.   local flag = false
  85.   for k,v in ipairs(p) do
  86.     for k2,v2 in ipairs(v) do
  87.       if v2 ~= palette[k][k2] then
  88.         flag = true
  89.         break
  90.       end
  91.     end
  92.     if flag then break end
  93.   end
  94.  
  95.   if not flag then localpalette = false end
  96.  
  97.   local pbytes = {}
  98.   for k,v in ipairs(p) do
  99.     table.insert(pbytes, tobytes(v[1],1)..tobytes(v[2],1)..tobytes(v[3],1))
  100.   end
  101.  
  102.   localpalette = table.concat(pbytes)
  103. end
  104.  
  105. local tconcat = table.concat
  106. local unpack = unpack
  107.  
  108. function gif.frame(frame)
  109.   --Image Descriptor
  110.   if localpalette then
  111.     file:write(ImgDescLocal,10)
  112.     file:write(localpalette)
  113.   else
  114.     file:write(ImgDesc,10)
  115.   end
  116.  
  117.   local rawdata = {unpack(rawbase)} --A trick to make the table with premade hash
  118.   local counter = 1
  119.  
  120.   frame:mapPixel(function(x,y,r,g,b,a)
  121.     rawdata[counter] = r
  122.     counter = counter + 1
  123.     return r,g,b,a
  124.   end)
  125.  
  126.   rawdata = tconcat(rawdata)
  127.  
  128.   --LZW Compression
  129.  
  130. end
  131.  
  132. function gif.close()
  133.  
  134. end
  135.  
  136. return gif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement