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>
30 unsigned short outpal[8];
33 int set_info(swan_gfx_t * gfx, SDL_Surface * img)
37 gfx->bpp = img->format->BitsPerPixel;
40 fprintf(stderr, "warning: %d bpp format detected\n", img->format->BitsPerPixel);
44 if (gfx->width % 8 | gfx->height % 8) {
45 fprintf(stderr, "warning: image not multiple of 16x16 tiles\n \
46 width: %d\theight: %d\n", gfx->width, gfx->height);
50 gfx->row_tiles = gfx->height / 8;
51 gfx->col_tiles = gfx->width / 8;
53 /* set userdata to iterate over
56 img->userdata = img->pixels;
57 gfx->data = (unsigned char *) img->userdata;
58 gfx->palette = img->format->palette->colors;
59 SDL_UnlockSurface(img);
64 void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx,
67 unsigned char * ptr = gfx->data;
68 for (int i = 0; i < TILE_SZ; i+=4) {
69 for (int j = 0; j < 4; ++j) {
70 if (packed) { /* packed format */
71 gfx->outpal[ptr[0]] = (gfx->palette[ptr[0]].r >> 4) << 8 |
72 (gfx->palette[ptr[0]].g >> 4) << 4 |
73 gfx->palette[ptr[0]].b >> 4;
74 buf[i+j] = ptr[0] << 4;
77 } else { /* planar format */
78 for (int x = 0; x < 8; ++x) {
79 buf[i + j] = ptr[x] << j;
88 int main(int argc, char *argv[])
98 while ((opt = getopt(argc, argv, "po:sv:")) != -1) {
101 printf("include palette");
104 printf("output file %s\n", optarg);
105 strcpy(outfile, optarg);
109 printf("generate gfx per 16x16 tile \n");
114 fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]);
119 if (optind >= argc) {
120 fprintf(stderr, "Expected infile\n");
121 fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]);
125 infile = argv[optind];
127 rawbmp = SDL_LoadBMP(infile);
129 fprintf(stderr, "can not load %s file\n", infile);
133 swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
134 int ret = set_info(gfx, rawbmp);
139 unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
142 gfxstream = fopen("out.gfx", "w");
144 printf("opening %s\n", outfile);
145 gfxstream = fopen(outfile, "w");
149 fprintf(stderr, "can not open file for writing!\n");
153 unsigned nr_tiles = 1;
155 for (int i = 0; i < nr_tiles; ++i) {
156 generate_4bpp_tile(tile_buf, gfx, true);
157 int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, gfxstream);
158 if (ret != TILE_SZ) {
159 fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
164 palstream = fopen("out.pal", "w");
166 fprintf(stderr, "can not open file for writing!\n");
171 fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
172 const short fill = 0xFFFF;
173 for (int i = 0; i < 8; ++i)
174 fwrite(&fill, sizeof(unsigned short), 1, palstream);
182 SDL_FreeSurface(rawbmp);