Skip to content

Instantly share code, notes, and snippets.

@italomaia
Created March 1, 2019 22:14
Show Gist options
  • Save italomaia/3a39e8dc6a564ba05e511bfc7bcc414d to your computer and use it in GitHub Desktop.
Save italomaia/3a39e8dc6a564ba05e511bfc7bcc414d to your computer and use it in GitHub Desktop.
local moses = require("moses")
local function parse_mri_to_webp(mri_path)
local name = string.match(mri_path, "(%w+)%.")
local fsi = io.open(mri_path, "rb")
local idata = fsi:read("*all")
fsi:close()
local size = #idata
local n = size + 7
local odata = {
82, -- R
73, -- I
70, -- F
70, -- F
(n >> 24) & 255,
(n >> 16) & 255,
(n >> 8) & 255,
255 & n,
87, -- W
69, -- E
66, -- B
80, -- P
86, -- V
80, -- P
56, -- 8
}
for i=1, size do
local char = idata:sub(i, i)
local byte = string.byte(char)
table.insert(odata, 101 ~ byte)
end
local fso = io.open(name .. "_l.webp", "wb")
fso:write(table.concat(moses.map(odata, function (v)
return string.char(v)
end), ''))
fso:close()
end
local function main(path_list)
for _, path in pairs(path_list) do
parse_mri_to_webp(path)
end
end
if arg[-1] then
main(moses.slice(arg, 1))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment