Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. --Picture Tiler by Adrien Destugues
  2. --Extract unique tiles from the spare page
  3. --to the main one. Main page is erased.
  4. --
  5. -- Copyright 2011 Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
  6. --
  7. -- This program is free software; you can redistribute it and/or
  8. -- modify it under the terms of the GNU General Public License
  9. -- as published by the Free Software Foundation; version 2
  10. -- of the License. See <http://www.gnu.org/licenses/>
  11.  
  12. -- Copy palette from spare to main
  13. -- TODO
  14.  
  15. -- Grid size
  16. -- TODO : get it from GrafX2
  17. xgrid = 16;
  18. ygrid = 16;
  19.  
  20. -- picture size
  21. w, h = getpicturesize();
  22.  
  23. -- We may need less if there are duplicates
  24. setsparepicturesize(xgrid, w*h/xgrid);
  25.  
  26. tileid = 0;
  27.  
  28. -- blit part of the spare to picture
  29. function blitpicturetospare(srcx, srcy, dstx, dsty, width, height)
  30.         local x,y;
  31.         for y = 0, height - 1, 1 do
  32.                 for x = 0, width - 1, 1 do
  33.                         putsparepicturepixel(dstx+x, dsty+y, getpicturepixel(srcx + x, srcy + y));
  34.                 end
  35.         end
  36. end
  37.  
  38. function comparesparewithpicture(srcx, srcy, dstx, dsty, width, height)
  39.         local x,y,color
  40.         for y = 0, height - 1, 1 do
  41.                 for x = 0, width - 1, 1 do
  42.                         color = getsparepicturepixel(srcx + x, srcy + y);
  43.                         if color ~= getpicturepixel(dstx+x, dsty+y) then
  44.                                 -- they are different
  45.                                 return false;
  46.                         end
  47.                 end
  48.         end
  49.         -- they are identical
  50.         return true;
  51. end
  52.  
  53. -- compute checksum of a picture area
  54. -- it may not be unique, we use it as a key for an hashmap
  55. function checksum(srcx, srcy, width, height)
  56.         local sum,x,y
  57.         sum = 0;
  58.         for y = 0, height - 1, 1 do
  59.                 for x = 0, width - 1, 1 do
  60.                         sum = sum + getpicturepixel(srcx+x, srcy+y);
  61.                 end
  62.         end
  63.  
  64.         return sum;
  65. end
  66.  
  67. tilemap = {}
  68.  
  69. -- foreach tile
  70. for y = 0, h-1, ygrid do
  71.         for x = 0, w - 1, xgrid do
  72.                 -- existing one ?
  73.                 csum = checksum(x,y,xgrid,ygrid);
  74.                 if tilemap[csum] ~= nil then
  75.                         -- potential match
  76.                         -- Find matching tileid
  77.                         found = false;
  78.                         for id in pairs(tilemap[csum]) do
  79.                                 -- is it a match ?
  80.                                 if comparesparewithpicture(x,y,0,id*ygrid, xgrid, ygrid) then
  81.                                         -- found it !
  82.                                         tilemap[csum][id] = tilemap[csum][id] + 1;
  83.                                         found = true;
  84.                                         break;
  85.                                 end
  86.                         end
  87.                         -- Add tile anyway if needed
  88.                         if not found then
  89.                                 desty = tileid * ygrid;
  90.                                 blitpicturetospare(x, y, 0, desty, xgrid, ygrid);
  91.  
  92.                                 -- add it to the tilemap
  93.                                 tilemap[csum][tileid] = 1;
  94.                                 -- give it a tile id
  95.                                 tileid = tileid + 1;
  96.                         end
  97.                 else
  98.                         -- Copy to spare
  99.                         desty = tileid * ygrid;
  100.                         blitpicturetospare(x, y, 0, desty, xgrid, ygrid);
  101.  
  102.                         -- add it to the tilemap
  103.                         tilemap[csum] = {}
  104.                         tilemap[csum][tileid] = 1;
  105.                         -- give it a tile id
  106.                         tileid = tileid + 1;
  107.                 end
  108.         end
  109. end
  110.  
  111. setsparepicturesize(xgrid, (tileid-1)*ygrid)
  112. --updatescreen();
  113.