7 int img2tiles(struct tilemap *tmap, struct image *img, int tw, int th, int dedup)
9 int i, j, x, y, tx, ty, tileoffs, xtiles, ytiles, ntiles;
13 if(alloc_image(&orig, img->width, img->height, img->bpp) == -1) {
14 fprintf(stderr, "img2tiles: failed to allocate temporary image\n");
17 memcpy(orig.pixels, img->pixels, img->scansz * img->height);
19 xtiles = (img->width + tw - 1) / tw;
20 ytiles = (img->height + th - 1) / th;
21 ntiles = xtiles * ytiles;
24 img->height = ntiles * th;
25 img->pitch = img->scansz = tw * img->bpp / 8;
29 tmap->height = ytiles;
30 if(!(tmap->map = malloc(ntiles * sizeof *tmap->map))) {
31 fprintf(stderr, "failed to allocate tilemap\n");
39 for(i=0; i<ytiles; i++) {
41 for(j=0; j<xtiles; j++) {
42 for(ty=0; ty<th; ty++) {
43 for(tx=0; tx<tw; tx++) {
44 pix = get_pixel(&orig, x + tx, y + ty);
45 put_pixel(img, tx, ty + tileoffs, pix);
48 tileoffs += th; /* destination Y offset, inc by th for every tile */
58 int dump_tilemap(struct tilemap *tmap, const char *fname)