3 * Author : Robin Krens <robin@robinkrens.nl>
5 * Last Modified Date: 12.06.2022
6 * Last Modified By : Robin Krens <robin@robinkrens.nl>
16 #include <SDL2/SDL_image.h>
28 unsigned short outpal[8];
31 void set_info(swan_gfx_t * gfx, SDL_Surface * img)
35 gfx->bpp = img->format->BitsPerPixel;
37 /* set userdata to iterate */
39 img->userdata = img->pixels;
40 gfx->data = (unsigned char *) img->userdata;
41 gfx->palette = img->format->palette->colors;
42 SDL_UnlockSurface(img);
46 void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx,
49 unsigned char * ptr = gfx->data;
50 for (int i = 0; i < TILE_SZ; i+=4) {
51 for (int j = 0; j < 4; ++j) {
52 if (packed) { /* packed format */
53 gfx->outpal[ptr[0]] = (gfx->palette[ptr[0]].r >> 4) << 8 |
54 (gfx->palette[ptr[0]].g >> 4) << 4 |
55 gfx->palette[ptr[0]].b >> 4;
56 buf[i+j] = ptr[0] << 4;
59 } else { /* planar format */
60 for (int x = 0; x < 8; ++x) {
61 buf[i + j] = ptr[x] << j;
70 int main(int argc, char *argv[])
80 while ((opt = getopt(argc, argv, "po:sv:")) != -1) {
83 printf("include palette");
86 printf("output file %s\n", optarg);
87 strcpy(outfile, optarg);
91 printf("generate gfx per 16x16 tile \n");
96 fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]);
101 if (optind >= argc) {
102 fprintf(stderr, "Expected infile\n");
103 fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]);
107 infile = argv[optind];
109 rawbmp = SDL_LoadBMP(infile);
111 fprintf(stderr, "can not load %s file\n", infile);
115 swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
116 set_info(gfx, rawbmp);
118 unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
121 fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel);
122 fprintf(stderr, "use --force in command to run\n");
127 gfxstream = fopen("out.gfx", "w");
129 printf("opening %s\n", outfile);
130 gfxstream = fopen(outfile, "w");
134 fprintf(stderr, "can not open file for writing!\n");
138 unsigned nr_tiles = 1;
140 for (int i = 0; i < nr_tiles; ++i) {
141 generate_4bpp_tile(tile_buf, gfx, true);
142 int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, gfxstream);
143 if (ret != TILE_SZ) {
144 fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
149 palstream = fopen("out.pal", "w");
151 fprintf(stderr, "can not open file for writing!\n");
155 fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
156 const short fill = 0xFFFF;
157 for (int i = 0; i < 8; ++i)
158 fwrite(&fill, sizeof(unsigned short), 1, palstream);
162 SDL_FreeSurface(rawbmp);