Created
March 2, 2019 18:36
-
-
Save italomaia/83fe7da976ee7a36d95bc2192a59fe69 to your computer and use it in GitHub Desktop.
Decrypts mangarock images into webp using lua programming language
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 moses = require("moses") | |
local function parse_mri_data_to_webp_buffer (mri_data) | |
local size = #mri_data | |
local webp_size = size + 7 | |
local webp_size_as_byte = string.pack("<I", webp_size) | |
local webp_buffer = { | |
82, -- R | |
73, -- I | |
70, -- F | |
70, -- F | |
string.byte(webp_size_as_byte:sub(1, 1)), | |
string.byte(webp_size_as_byte:sub(2, 2)), | |
string.byte(webp_size_as_byte:sub(3, 3)), | |
string.byte(webp_size_as_byte:sub(4, 4)), | |
87, -- W | |
69, -- E | |
66, -- B | |
80, -- P | |
86, -- V | |
80, -- P | |
56, -- 8 | |
} | |
for i=1, size do | |
table.insert(webp_buffer, 101 ~ string.byte(mri_data:sub(i, i))) | |
end | |
return webp_buffer | |
end | |
local function buffer_to_data (buffer) | |
return table.concat(moses.map(buffer, moses.ary(string.char, 1)), '') | |
end | |
local function parse_mri_to_webp (mri_path, webp_path) | |
local fsi = io.open(mri_path, "rb") | |
local mri_data = fsi:read("*all") | |
fsi:close() | |
local webp_buffer = parse_mri_data_to_webp_buffer(mri_data) | |
local fso = io.open(webp_path, "wb") | |
fso:write(buffer_to_data(webp_buffer)) | |
fso:close() | |
end | |
local function main (path_list) | |
for _, path in pairs(path_list) do | |
local base_path = string.gsub(path, "(%.%w+)$", '') | |
parse_mri_to_webp(path, string.format("%s.webp", base_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