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, "error: %d bpp format detected\n", img->format->BitsPerPixel);
44 if (gfx->width % 8 | gfx->height % 8) {
45 fprintf(stderr, "error: 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 if (ptr[0] >= 8 || ptr[1] >= 8)
72 fprintf(stdout, "warning: too many colors detected\n");
73 gfx->outpal[ptr[0]] = (gfx->palette[ptr[0]].r >> 4) << 8 |
74 (gfx->palette[ptr[0]].g >> 4) << 4 |
75 gfx->palette[ptr[0]].b >> 4;
76 buf[i+j] = ptr[0] << 4;
79 } else { /* planar format */
80 for (int x = 0; x < 8; ++x) {
82 fprintf(stdout, "warning: too many colors detected\n");
83 buf[i + j] = ptr[x] << j;
92 int main(int argc, char *argv[])
102 while ((opt = getopt(argc, argv, "po:sv:")) != -1) {
105 printf("include palette");
108 printf("output file %s\n", optarg);
109 strcpy(outfile, optarg);
113 printf("generate gfx per 16x16 tile \n");
118 fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]);
123 if (optind >= argc) {
124 fprintf(stderr, "Expected infile\n");
125 fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]);
129 infile = argv[optind];
131 rawbmp = SDL_LoadBMP(infile);
133 fprintf(stderr, "can not load %s file\n", infile);
137 swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
138 int ret = set_info(gfx, rawbmp);
143 unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
146 gfxstream = fopen("out.gfx", "w");
148 printf("opening %s\n", outfile);
149 gfxstream = fopen(outfile, "w");
153 fprintf(stderr, "can not open file for writing!\n");
157 unsigned nr_tiles = 1;
159 for (int i = 0; i < nr_tiles; ++i) {
160 generate_4bpp_tile(tile_buf, gfx, true);
161 int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, gfxstream);
162 if (ret != TILE_SZ) {
163 fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
168 palstream = fopen("out.pal", "w");
170 fprintf(stderr, "can not open file for writing!\n");
175 fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
176 const short fill = 0xFFFF;
177 for (int i = 0; i < 8; ++i)
178 fwrite(&fill, sizeof(unsigned short), 1, palstream);
186 SDL_FreeSurface(rawbmp);