[WIP] initial gzip support

This commit is contained in:
2026-01-28 18:17:14 +03:00
parent 426fea3691
commit 3146c7865d
2 changed files with 65 additions and 12 deletions

View File

@@ -253,8 +253,9 @@ end
---@param groups? table<string, number> Groups that will be added to all peaks.
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
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
peaks.size = nil

View File

@@ -13,19 +13,72 @@
---```
---@alias ColoredDotsTable table<string,table>
---@param uintb string unsigned int bytes
---@return number
local function uintb2num(uintb)
---@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 littleEndian? boolean
---@return number
local function uintb2num(uintb, littleEndian)
local num = 0
local len = string.len(uintb)
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
return num
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.
---Why? Because hex codes of colors are 8-bit: #ffffff -> ff = 256 (8 bit). Not 65536 (16 bit).
---@param num number 16-bit number
@@ -54,15 +107,14 @@ end
---@param backgroundColor string? ColorString, will be ignored while processing image. Default: `#000000FF`
---@return ColoredDotsTable
local function ff2luat(filepath, backgroundColor)
local file = io.open(filepath, 'rb')
if not file then error('Can\'t open farbfeld image: ' .. dump(filepath)) end
local file = io.open(filepath, 'r')
if not file then error('Can\'t open file: ' .. dump(filepath)) end
local ff = file:read('*a') -- binary!
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
-- (read wiki for information).
-- if filepath:sub(-3) == '.gz' then ff = core.decompress(ff, 'deflate') end
if not ff:sub(1,8) == 'farbfeld' then error('Not farbfeld image: ' .. dump(filepath)) end
print('ff', dump(ff))
if ff:sub(1,2) == '\x1f\x8b' then ff = decompressGzip(ff) end
if not (ff:sub(1,8) == 'farbfeld') then error('File does not contain farbfeld image: '..dump(filepath)) end
if not backgroundColor then backgroundColor = '#000000FF' end
--HACK: normalizing colorstring for future string comparison ('#000' -> '#000000FF')