finished the tile deduplication and tilemap generation in pngdump
[mdlife] / tools / pngdump / tiles.c
index 92f404f..9421a06 100644 (file)
@@ -50,18 +50,30 @@ int img2tiles(struct tilemap *tmap, struct image *img, int tw, int th, int dedup
                                }
                        }
 
-                       if(!dedup || (tid = matchtile(img, tileoffs, th)) == -1) {
-                               tmap->map[tileno] = tileno;
-                               tileoffs += th; /* destination Y offset, inc by th for every tile */
+                       if(dedup) {
+                               if((tid = matchtile(img, tileoffs, th)) == -1) {
+                                       if(tmap) {
+                                               tmap->map[tileno++] = tileoffs / th;
+                                       }
+                                       tileoffs += th; /* destination Y offset, inc by th for every tile */
+                               } else {
+                                       if(tmap) {
+                                               tmap->map[tileno++] = tid;
+                                       }
+                               }
                        } else {
-                               tmap->map[tileno] = tid;
+                               tileoffs += th; /* destination Y offset, inc by th for every tile */
                        }
-                       tileno++;
                        x += tw;
                }
                y += th;
        }
 
+       if(dedup) {
+               putchar('\n');
+               img->height = tileoffs;
+       }
+
        free(orig.pixels);
        return 0;
 }
@@ -74,7 +86,7 @@ static int matchtile(struct image *img, int toffs, int th)
 
        tilesz = img->pitch * th;
        pa = (unsigned char*)img->pixels;
-       pb = (unsigned char*)img->pixels + toffs * tilesz;
+       pb = (unsigned char*)img->pixels + toffs * img->pitch;
 
        for(i=0; i<ntiles; i++) {
                if(memcmp(pa, pb, tilesz) == 0) {