From b3db8c33b32bd8c2246e297081a00a0a4e040210 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 12 Jun 2022 20:52:15 +0200 Subject: [PATCH] tools: bmp2tiles image check on tile size and bpp --- tools/bmp2tiles/bmp2tiles.c | 48 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c index 96aaf2e..c64c252 100644 --- a/tools/bmp2tiles/bmp2tiles.c +++ b/tools/bmp2tiles/bmp2tiles.c @@ -22,25 +22,43 @@ typedef struct { unsigned width; unsigned height; + unsigned row_tiles; + unsigned col_tiles; unsigned bpp; unsigned char * data; SDL_Colour * palette; unsigned short outpal[8]; } swan_gfx_t; -void set_info(swan_gfx_t * gfx, SDL_Surface * img) +int set_info(swan_gfx_t * gfx, SDL_Surface * img) { gfx->width = img->w; gfx->height = img->h; gfx->bpp = img->format->BitsPerPixel; - - /* set userdata to iterate */ + + if (gfx->bpp != 8) { + fprintf(stderr, "warning: %d bpp format detected\n", img->format->BitsPerPixel); + return -1; + } + + if (gfx->width % 8 | gfx->height % 8) { + fprintf(stderr, "warning: image not multiple of 16x16 tiles\n \ + width: %d\theight: %d\n", gfx->width, gfx->height); + return -1; + } + + gfx->row_tiles = gfx->height / 8; + gfx->col_tiles = gfx->width / 8; + + /* set userdata to iterate over + * pixels */ SDL_LockSurface(img); img->userdata = img->pixels; gfx->data = (unsigned char *) img->userdata; gfx->palette = img->format->palette->colors; SDL_UnlockSurface(img); - + + return 0; } void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx, @@ -113,16 +131,13 @@ int main(int argc, char *argv[]) } swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t)); - set_info(gfx, rawbmp); + int ret = set_info(gfx, rawbmp); + + if (ret != 0) + goto cleanup; unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ); - if (gfx->bpp != 8) { - fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel); - fprintf(stderr, "use --force in command to run\n"); - exit(EXIT_FAILURE); - } - if (!bout) gfxstream = fopen("out.gfx", "w"); else { @@ -132,7 +147,7 @@ int main(int argc, char *argv[]) if (!gfxstream) { fprintf(stderr, "can not open file for writing!\n"); - exit(EXIT_FAILURE); + goto close; } unsigned nr_tiles = 1; @@ -149,7 +164,8 @@ int main(int argc, char *argv[]) palstream = fopen("out.pal", "w"); if (!palstream) { fprintf(stderr, "can not open file for writing!\n"); - exit(EXIT_FAILURE); + fclose(palstream); + goto close; } fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream); @@ -157,9 +173,11 @@ int main(int argc, char *argv[]) for (int i = 0; i < 8; ++i) fwrite(&fill, sizeof(unsigned short), 1, palstream); + fclose(palstream); + +close: + fclose(gfxstream); cleanup: free(gfx); SDL_FreeSurface(rawbmp); - fclose(gfxstream); - fclose(palstream); } -- 2.7.4