Created
March 6, 2021 02:51
-
-
Save cheapie/e4edc2ea274750c235c984d972703803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local imlib2 = require("imlib2") | |
local img = imlib2.image.load(arg[1]) | |
local xsize = img:get_width() | |
local ysize = img:get_height() | |
local function explodebits(input,count) | |
local output = {} | |
if not count then count = 8 end | |
for i=0,count-1,1 do | |
output[i] = input%(2^(i+1)) >= 2^i | |
end | |
return output | |
end | |
local function implodebits(input,count) | |
local output = 0 | |
if not count then count = 8 end | |
for i=0,count-1,1 do | |
output = output + (input[i] and 2^i or 0) | |
end | |
return output | |
end | |
local packtable = {} | |
local unpacktable = {} | |
for i=0,25,1 do | |
packtable[i] = string.char(i+65) | |
packtable[i+26] = string.char(i+97) | |
unpacktable[string.char(i+65)] = i | |
unpacktable[string.char(i+97)] = i+26 | |
end | |
for i=0,9,1 do | |
packtable[i+52] = tostring(i) | |
unpacktable[tostring(i)] = i+52 | |
end | |
packtable[62] = "+" | |
packtable[63] = "/" | |
unpacktable["+"] = 62 | |
unpacktable["/"] = 63 | |
local function packpixel(pixel) | |
pixel = tonumber(pixel,16) | |
if not pixel then return "AAAA" end | |
local bits = explodebits(pixel,24) | |
local block1 = {} | |
local block2 = {} | |
local block3 = {} | |
local block4 = {} | |
for i=0,5,1 do | |
block1[i] = bits[i] | |
block2[i] = bits[i+6] | |
block3[i] = bits[i+12] | |
block4[i] = bits[i+18] | |
end | |
local char1 = packtable[implodebits(block1,6)] or "A" | |
local char2 = packtable[implodebits(block2,6)] or "A" | |
local char3 = packtable[implodebits(block3,6)] or "A" | |
local char4 = packtable[implodebits(block4,6)] or "A" | |
return char1..char2..char3..char4 | |
end | |
local function unpackpixel(pack) | |
local block1 = unpacktable[pack:sub(1,1)] or 0 | |
local block2 = unpacktable[pack:sub(2,2)] or 0 | |
local block3 = unpacktable[pack:sub(3,3)] or 0 | |
local block4 = unpacktable[pack:sub(4,4)] or 0 | |
local out = block1+(2^6*block2)+(2^12*block3)+(2^18*block4) | |
return string.format("%06X",out) | |
end | |
local b64 = "" | |
print("{") | |
for y=0,ysize-1,1 do | |
io.write(" {") | |
for x=0,xsize-1,1 do | |
local color = img:get_pixel(x,y) | |
if color.alpha == 0 then | |
io.write("\"000000\",") | |
b64 = b64..packpixel("000000") | |
else | |
io.write(string.format("\"%02X%02X%02X\",",color.red,color.green,color.blue)) | |
b64 = b64..packpixel(string.format("%02X%02X%02X",color.red,color.green,color.blue)) | |
end | |
end | |
print("},") | |
end | |
print("}") | |
print() | |
print() | |
print(b64) | |
print() | |
print(string.format("X Size: %d Y Size: %d",xsize,ysize)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment