[WIP] initial gzip support
This commit is contained in:
@@ -253,8 +253,9 @@ end
|
|||||||
---@param groups? table<string, number> Groups that will be added to all peaks.
|
---@param groups? table<string, number> Groups that will be added to all peaks.
|
||||||
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
|
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
|
||||||
local modPath = core.get_modpath(core.get_current_modname())
|
local modPath = core.get_modpath(core.get_current_modname())
|
||||||
local peaks = ff2luat(modPath .. '/textures/maps/' .. map)
|
local peaks = ff2luat(modPath .. '/textures/maps/' .. 'img_easy.ff.gz')
|
||||||
|
|
||||||
|
print(dump(peaks))
|
||||||
local mapSize = peaks.size
|
local mapSize = peaks.size
|
||||||
peaks.size = nil
|
peaks.size = nil
|
||||||
|
|
||||||
|
|||||||
@@ -13,19 +13,72 @@
|
|||||||
---```
|
---```
|
||||||
---@alias ColoredDotsTable table<string,table>
|
---@alias ColoredDotsTable table<string,table>
|
||||||
|
|
||||||
|
---@param byte number
|
||||||
|
---@return table<boolean> 8 bits: true or false
|
||||||
|
local function byte2bits(byte)
|
||||||
|
local bits = {}
|
||||||
|
for _ = 1,8 do
|
||||||
|
table.insert(bits, 1, byte % 2 == 1)
|
||||||
|
byte = math.floor(byte / 2)
|
||||||
|
end
|
||||||
|
return bits
|
||||||
|
end
|
||||||
|
|
||||||
---@param uintb string unsigned int bytes
|
---@param uintb string unsigned int bytes
|
||||||
|
---@param littleEndian? boolean
|
||||||
---@return number
|
---@return number
|
||||||
local function uintb2num(uintb)
|
local function uintb2num(uintb, littleEndian)
|
||||||
local num = 0
|
local num = 0
|
||||||
local len = string.len(uintb)
|
local len = string.len(uintb)
|
||||||
|
|
||||||
for i = 1,len do
|
for i = 1,len do
|
||||||
num = num*256 + string.byte(uintb, i)
|
local j = i
|
||||||
|
if littleEndian then j = 1 + len-i end -- reverse order
|
||||||
|
num = num*256 + string.byte(uintb, j)
|
||||||
end
|
end
|
||||||
|
|
||||||
return num
|
return num
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Decompresses [GZIP](https://en.wikipedia.org/wiki/Gzip) format.
|
||||||
|
---@param zip string bytes
|
||||||
|
---@return string bytes
|
||||||
|
local function decompressGzip(zip)
|
||||||
|
local flags = byte2bits(string.byte(zip, 4)) -- checking flags (FLG field)
|
||||||
|
print('flags', dump(flags))
|
||||||
|
local cursor = 11
|
||||||
|
-- XLEN + Extra Field skip
|
||||||
|
if flags[3] then -- FEXTRA
|
||||||
|
local xlen = uintb2num(zip:sub(cursor, cursor+1), true)
|
||||||
|
cursor = cursor + 2 + xlen
|
||||||
|
end
|
||||||
|
-- file name skip
|
||||||
|
if flags[4] then -- FNAME
|
||||||
|
while string.byte(zip, cursor) ~= 0 and cursor < #zip do
|
||||||
|
cursor = cursor + 1
|
||||||
|
end
|
||||||
|
cursor = cursor + 1 -- also skipping zero byte
|
||||||
|
end
|
||||||
|
-- file comment skip
|
||||||
|
if flags[5] then -- FCOMMENT
|
||||||
|
while string.byte(zip, cursor) ~= 0 and cursor < #zip do
|
||||||
|
cursor = cursor + 1
|
||||||
|
end
|
||||||
|
cursor = cursor + 1 -- also skipping zero byte
|
||||||
|
end
|
||||||
|
--- CRC16 skip
|
||||||
|
if flags[2] then -- FHCRC
|
||||||
|
cursor = cursor + 2
|
||||||
|
end
|
||||||
|
|
||||||
|
local compressedData = zip:sub(cursor, #zip-8)
|
||||||
|
print('compressedData', compressedData)
|
||||||
|
print(dump(compressedData))
|
||||||
|
local data = compressedData -- getting compressed data without trailing 8 bytes (CRC32, ISIZE)
|
||||||
|
|
||||||
|
return core.decompress(data, 'deflate', 1)
|
||||||
|
end
|
||||||
|
|
||||||
---Converts (scaling) 16-bit unsigned integer to 8-bit unsigned integer.
|
---Converts (scaling) 16-bit unsigned integer to 8-bit unsigned integer.
|
||||||
---Why? Because hex codes of colors are 8-bit: #ffffff -> ff = 256 (8 bit). Not 65536 (16 bit).
|
---Why? Because hex codes of colors are 8-bit: #ffffff -> ff = 256 (8 bit). Not 65536 (16 bit).
|
||||||
---@param num number 16-bit number
|
---@param num number 16-bit number
|
||||||
@@ -54,15 +107,14 @@ end
|
|||||||
---@param backgroundColor string? ColorString, will be ignored while processing image. Default: `#000000FF`
|
---@param backgroundColor string? ColorString, will be ignored while processing image. Default: `#000000FF`
|
||||||
---@return ColoredDotsTable
|
---@return ColoredDotsTable
|
||||||
local function ff2luat(filepath, backgroundColor)
|
local function ff2luat(filepath, backgroundColor)
|
||||||
local file = io.open(filepath, 'rb')
|
local file = io.open(filepath, 'r')
|
||||||
if not file then error('Can\'t open farbfeld image: ' .. dump(filepath)) end
|
if not file then error('Can\'t open file: ' .. dump(filepath)) end
|
||||||
local ff = file:read('*a') -- binary!
|
local ff = file:read('*a') -- binary!
|
||||||
file:close()
|
file:close()
|
||||||
-- TODO: make it decompressing gzip format -> https://en.wikipedia.org/wiki/Gzip
|
|
||||||
-- I should skip 10 bytes in a beginning, read how many extra fields after it, skip them too, and skip last 8 bytes
|
print('ff', dump(ff))
|
||||||
-- (read wiki for information).
|
if ff:sub(1,2) == '\x1f\x8b' then ff = decompressGzip(ff) end
|
||||||
-- if filepath:sub(-3) == '.gz' then ff = core.decompress(ff, 'deflate') end
|
if not (ff:sub(1,8) == 'farbfeld') then error('File does not contain farbfeld image: '..dump(filepath)) end
|
||||||
if not ff:sub(1,8) == 'farbfeld' then error('Not farbfeld image: ' .. dump(filepath)) end
|
|
||||||
|
|
||||||
if not backgroundColor then backgroundColor = '#000000FF' end
|
if not backgroundColor then backgroundColor = '#000000FF' end
|
||||||
--HACK: normalizing colorstring for future string comparison ('#000' -> '#000000FF')
|
--HACK: normalizing colorstring for future string comparison ('#000' -> '#000000FF')
|
||||||
|
|||||||
Reference in New Issue
Block a user