diff --git a/asset/plugin/util_tm2.lua b/asset/plugin/util_tm2.lua new file mode 100644 index 0000000..f4c1f30 --- /dev/null +++ b/asset/plugin/util_tm2.lua @@ -0,0 +1,220 @@ +---@diagnostic disable : lowercase-global, missing-fields, undefined-global, duplicate-doc-field, undefined-field + +version = "v0.1" +description = "[lua_util_tm2::init] lua plugin to decode tim2 texture (including swizzle)" + +-- global declear +g_data = nil --- @type string +g_tilecfg = {} ---@type tilecfg_t +g_uijson = [[ +{ + "plugincfg" : [ + {"name": "swizzle", "type": "bool", "value": 0}, + {"name": "blockw", "type": "int", "value": 16}, + {"name": "blockh", "type": "int", "value": 8} + ] +}]] +g_plugincfg = {} + +-- tm2 declear +---@class tm2pic_t +---@field size_total integer +---@field size_palette integer +---@field size_image integer +---@field size_header integer +---@field count_color integer +---@field format integer +---@field count_mipmap integer +---@field type_clutcolor integer +---@field type_imagecolor integer +---@field width integer +---@field height integer +---@field reg_gstex1 integer +---@field reg_gstex2 integer +---@field reg_gsflag integer +---@field reg_gsclut integer + +---@class tm2_t +---@field magic string +---@field version integer +---@field format integer +---@field count integer +---@field reserved integer + +COLOR_TYPE = { + UNDEFINED = 0, + A1B5G5R5 = 1, + X8B8G8R8 = 2, + A8B8G8R8 = 3, + INDEX4 = 4, + INDEX8 = 5, +} + +g_tm2 = {} ---@type tm2_t +g_tm2pic = {} ---@type tm2pic_t +g_dataoffset = 0 ---@type integer +g_palatte = nil +g_swizzlemap = {} + +function swizzle_tm2(x, y, w, blockw, blockh) + idx = x + w*y + blocksize = blockw * blockh + blockline = w // blockw + blockidx = idx // blocksize + blocky = blockidx // blockline + blockx = blockidx % blockline + + blockinneridx = idx % blocksize + blockinnery = blockinneridx // blockw + blockinnerx = blockinneridx % blockw + + x2 = blockx * blockw + blockinnerx + y2 = blocky * blockh + blockinnery + return x2, y2 +end + +function deinterlace_palatte(palatte) + parts = (#palatte + 1 ) // 32 + stripes = 2 + colors = 8 + blocks = 2 + + newpallate = {} + i = 0 + for part = 0, parts - 1 do + for block = 0, blocks - 1 do + for stripe = 0, stripes - 1 do + for color = 0, colors - 1 do + i2 = part * colors * stripes * blocks + block * colors + stripe * stripes * colors + color + newpallate[i] = palatte[i2] + -- print(part, block, stripe, color, i, i2) + i = i + 1 + end + end + end + end + + return newpallate +end + +function parse_tm2(data) + TM2_FMT, TM2PIC_FMT = "> 4 + else d = d & 0xf end + pixel = g_palatte[d] + elseif (color_type == COLOR_TYPE.INDEX8) then + d = string.byte(g_data, g_dataoffset + idx + 1) + pixel = g_palatte[d] + elseif (color_type == COLOR_TYPE.A8B8G8R8) then -- not tested yet + pixel = string.unpack(g_data, ">I4", g_dataoffset + idx * 4 + 1) + end + + return pixel +end + +function decode_post() + log("[lua_util_tm2::post] decode finished") + set_tilenav({index=0, offset=-1}) + set_tilestyle({scale=1.5}) + g_data = nil + return true +end + +function decode_sendui() + return g_uijson +end + +function decode_recvui(cfg) + for i, t in ipairs(cfg.plugincfg) do + -- log(i, t.name, t.value) + if(t.name == "swizzle") then g_plugincfg.swizzle = t.value > 0 end + if(t.name == "blockw") then g_plugincfg.blockw = math.floor(t.value) end + if(t.name == "blockh") then g_plugincfg.blockh = math.floor(t.value) end + end + return true +end + +log(" ", description, version) \ No newline at end of file