projects
/
swan-dev
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
6c2f3fe
)
tools: bmp2tiles image check on tile size and bpp
author
Robin Krens
<robin@robinkrens.nl>
Sun, 12 Jun 2022 18:52:15 +0000
(20:52 +0200)
committer
Robin Krens
<robin@robinkrens.nl>
Sun, 12 Jun 2022 18:52:15 +0000
(20:52 +0200)
tools/bmp2tiles/bmp2tiles.c
patch
|
blob
|
history
diff --git
a/tools/bmp2tiles/bmp2tiles.c
b/tools/bmp2tiles/bmp2tiles.c
index
96aaf2e
..
c64c252
100644
(file)
--- a/
tools/bmp2tiles/bmp2tiles.c
+++ b/
tools/bmp2tiles/bmp2tiles.c
@@
-22,25
+22,43
@@
typedef struct {
unsigned width;
unsigned height;
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;
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;
{
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);
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,
}
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));
}
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);
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 {
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");
if (!gfxstream) {
fprintf(stderr, "can not open file for writing!\n");
-
exit(EXIT_FAILURE)
;
+
goto close
;
}
unsigned nr_tiles = 1;
}
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");
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);
}
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);
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);
cleanup:
free(gfx);
SDL_FreeSurface(rawbmp);
- fclose(gfxstream);
- fclose(palstream);
}
}