Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --A special gif library for LIKO-12
- --Written by Rami Sabbagh (RamiLego4Game)
- --Source: http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
- --Arguments: filename, width, height, 16 color palette
- local args = {...}
- local gif = {}
- local bit = require("bit")
- local zero = string.char(0)
- --Helping function
- local function bytes(b,len)
- if #b >= len then return b:sub(1,len) end
- return b..string.rep(zero,len-#b)
- end
- local function indian(str,len)
- if len then str = bytes(str,len) end
- local bytes = {}
- local counter = #str
- for char in string.gmatch(".",str) do
- bytes[counter] = char
- counter = counter-1
- end
- return table.concat(bytes)
- end
- local function tobytes(num,len)
- local str = ""
- while true do
- local byte = bit.band(num,255)
- num = bit.rshift(num,8)
- str = str..string.char(byte)
- if num == 0 then break end
- end
- return indian(str,len)
- end
- --It's better to use local variables for speed.
- local filename = args[1] or "NULL"
- local file = love.filesystem.newFile(filename,"w")
- --Header
- file:write("GIF87a",6)
- --Logical Screen Descriptor
- local LSD = ""
- local width = args[2] or 192
- local height = args[3] or 128
- LSD = LSD..tobytes(width,2)..tobytes(height,2)
- LSD = LSD..tobytes(tonumber(10110011,2),1) --Packed Byte, configured for 16 color, 4bits index
- LSD = LSD..tobytes(0,1) --Background color index
- LSD = LSD..tobytes(0,1) --Pixel aspect ratio
- file:write(LSD,7)
- --Global Color Table
- local palette = args[4]
- local paletteBytes = {}
- for k,v in ipairs(palette) do
- table.insert(paletteBytes, tobytes(v[1],1)..tobytes(v[2],1)..tobytes(v[3],1))
- end
- file:write(table.concat(paletteBytes),48)
- --Static Image Descriptor
- --No local palette:
- local ImgDescBase = tobytes(0x2C,1) .. tobytes(0x0000,2) .. tobytes(0x0000,2) .. tobytes(width,2) .. tobytes(height,2)
- local ImgDesc = ImgDescBase .. string.char(tonumber(00000000,2))
- --With local palette:
- local ImgDescLocal = ImgDescBase .. string.char(tonumber(10000011,2))
- --Special Variables
- local localpalette
- local rawbase = {}
- for i=1, width*height do table.insert(rawbase,zero) end
- function gif.setPalette(p)
- local flag = false
- for k,v in ipairs(p) do
- for k2,v2 in ipairs(v) do
- if v2 ~= palette[k][k2] then
- flag = true
- break
- end
- end
- if flag then break end
- end
- if not flag then localpalette = false end
- local pbytes = {}
- for k,v in ipairs(p) do
- table.insert(pbytes, tobytes(v[1],1)..tobytes(v[2],1)..tobytes(v[3],1))
- end
- localpalette = table.concat(pbytes)
- end
- local tconcat = table.concat
- local unpack = unpack
- function gif.frame(frame)
- --Image Descriptor
- if localpalette then
- file:write(ImgDescLocal,10)
- file:write(localpalette)
- else
- file:write(ImgDesc,10)
- end
- local rawdata = {unpack(rawbase)} --A trick to make the table with premade hash
- local counter = 1
- frame:mapPixel(function(x,y,r,g,b,a)
- rawdata[counter] = r
- counter = counter + 1
- return r,g,b,a
- end)
- rawdata = tconcat(rawdata)
- --LZW Compression
- end
- function gif.close()
- end
- return gif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement