foo
[mdlife] / tools / pngdump / tiles.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "tiles.h"
5 #include "image.h"
6
7 int img2tiles(struct tilemap *tmap, struct image *img, int tw, int th, int dedup)
8 {
9         int i, j, x, y, tx, ty, tileoffs, xtiles, ytiles, ntiles;
10         struct image orig;
11         unsigned int pix;
12
13         if(alloc_image(&orig, img->width, img->height, img->bpp) == -1) {
14                 fprintf(stderr, "img2tiles: failed to allocate temporary image\n");
15                 return -1;
16         }
17         memcpy(orig.pixels, img->pixels, img->scansz * img->height);
18
19         xtiles = (img->width + tw - 1) / tw;
20         ytiles = (img->height + th - 1) / th;
21         ntiles = xtiles * ytiles;
22
23         img->width = tw;
24         img->height = ntiles * th;
25         img->pitch = img->scansz = tw * img->bpp / 8;
26
27         if(tmap) {
28                 tmap->width = xtiles;
29                 tmap->height = ytiles;
30                 if(!(tmap->map = malloc(ntiles * sizeof *tmap->map))) {
31                         fprintf(stderr, "failed to allocate tilemap\n");
32                         free(orig.pixels);
33                         return -1;
34                 }
35         }
36
37         tileoffs = 0;
38         y = 0;
39         for(i=0; i<ytiles; i++) {
40                 x = 0;
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);
46                                 }
47                         }
48                         tileoffs += th; /* destination Y offset, inc by th for every tile */
49                         x += tw;
50                 }
51                 y += th;
52         }
53
54         free(orig.pixels);
55         return 0;
56 }
57
58 int dump_tilemap(struct tilemap *tmap, const char *fname)
59 {
60         return -1;
61 }