3 * Author : Robin Krens <robin@robinkrens.nl>
5 * Last Modified Date: 12.06.2022
6 * Last Modified By : Robin Krens <robin@robinkrens.nl>
14 #include <SDL2/SDL_image.h>
26 void set_info(swan_gfx_t * gfx, SDL_Surface * img)
30 gfx->bpp = img->format->BitsPerPixel;
32 /* set userdata to iterate */
34 img->userdata = img->pixels;
35 SDL_UnlockSurface(img);
38 void generate_4bpp_tile(unsigned char *buf, void * userdata,
41 unsigned char * ptr = (unsigned char *) userdata;
42 for (int i = 0; i < TILE_SZ; i+=4) {
43 for (int j = 0; j < 4; ++j) {
44 if (packed) { /* packed format */
45 buf[i+j] = ptr[0] << 4;
48 } else { /* planar format */
49 for (int x = 0; x < 8; ++x) {
50 buf[i + j] = ptr[x] << j;
63 char filename[] = "test.bmp";
64 char outname[] = "test.gfx";
65 rawbmp = SDL_LoadBMP(filename);
68 fprintf(stderr, "can not load .bmp file\n");
72 swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
73 set_info(gfx, rawbmp);
75 unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
78 fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel);
79 fprintf(stderr, "use --force in command to run\n");
83 ostream = fopen(outname, "w");
85 fprintf(stderr, "can not open file for writing!\n");
89 unsigned nr_tiles = 1;
91 for (int i = 0; i < nr_tiles; ++i) {
92 generate_4bpp_tile(tile_buf, rawbmp->userdata, true);
93 int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, ostream);
95 fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
102 SDL_FreeSurface(rawbmp);