From 51c1a6de09f1256335d06a2dc9554626bb00f65a Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sat, 24 Sep 2011 11:15:27 +0000 Subject: [PATCH] Add Tiler, a factory script that extracts (unique) tiles from spare to main page. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1823 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- .../scripts/samples_2.4/picture/Tiler.lua | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 share/grafx2/scripts/samples_2.4/picture/Tiler.lua diff --git a/share/grafx2/scripts/samples_2.4/picture/Tiler.lua b/share/grafx2/scripts/samples_2.4/picture/Tiler.lua new file mode 100644 index 00000000..92467887 --- /dev/null +++ b/share/grafx2/scripts/samples_2.4/picture/Tiler.lua @@ -0,0 +1,119 @@ +--PICTURE: Tiler +--By Adrien Destugues +-- +--Extract unique tiles from the spare page to the main one +--Main page is erased. +-- +-- Copyright 2010 Adrien Destugues +-- +-- This program is free software; you can redistribute it and/or +-- modify it under the terms of the GNU General Public License +-- as published by the Free Software Foundation; version 2 +-- of the License. See + +-- Copy palette from spare to main +-- TODO + +-- Grid size +-- TODO : get it from GrafX2 +xgrid = 8 +ygrid = 16 + +-- picture size +w, h = getsparepicturesize() + +-- We may need less if there are duplicates +setpicturesize(xgrid, w*h/xgrid) + +tileid = 0 + +-- blit part of the spare to picture +function blitsparetopicture(srcx, srcy, dstx, dsty, width, height) + local x,y,color + for y = 0, height - 1, 1 do + for x = 0, width - 1, 1 do + color = getsparepicturepixel(srcx + x, srcy + y); + putpicturepixel(dstx+x, dsty+y, color); + end + end +end + +function comparesparetopicture(srcx, srcy, dstx, dsty, width, height) + local x,y,color + for y = 0, height - 1, 1 do + for x = 0, width - 1, 1 do + color = getsparepicturepixel(srcx + x, srcy + y); + if color ~= getpicturepixel(dstx+x, dsty+y) then + -- they are different + return false; + end + end + end + -- they are identical + return true; +end + +-- compute checksum of a picture area +-- it may not be unique, we use it as a key for an hashmap +function checksum(srcx, srcy, width, height) + local sum,x,y + sum = 0; + for y = 0, height - 1, 1 do + for x = 0, width - 1, 1 do + sum = sum + getsparepicturepixel(srcx+x, srcy+y); + end + end + + return sum; +end + +tilemap = {} + +-- foreach tile +for y = 0, h-1, ygrid do + for x = 0, w - 1, xgrid do + --TODO - existing one ? + csum = checksum(x,y,xgrid,ygrid); + if tilemap[csum] ~= nil then + -- potential match + -- Find matching tileid + found = false; + for id in pairs(tilemap[csum]) do + -- is it a match ? + if comparesparetopicture(x,y,0,id*ygrid, xgrid, ygrid) then + -- found it ! + tilemap[csum][id] = tilemap[csum][id] + 1; + found = true; + break; + end + end + -- Add tile anyway if needed + if not found then + desty = tileid * ygrid; + blitsparetopicture(x, y, 0, desty, xgrid, ygrid); + + -- add it to the tilemap + tilemap[csum][tileid] = 1; + -- give it a tile id + tileid = tileid + 1; + end + else + -- Copy to spare + desty = tileid * ygrid; + blitsparetopicture(x, y, 0, desty, xgrid, ygrid); + + -- add it to the tilemap + tilemap[csum] = {} + tilemap[csum][tileid] = 1; + -- give it a tile id + tileid = tileid + 1; + end + --statusmessage("processed " .. tileid .. " tiles"); + --updatescreen(); + end +end + +setpicturesize(xgrid, (tileid-1)*ygrid) + +finalizepicture() +updatescreen()