25ea7d593f9503d06d1e5e58965c413a0097fb92
[swan-dev] / tools / bmp2tiles / bmp2tiles.c
1 /**
2  * File              : bmp2tiles.c
3  * Author            : Robin Krens <robin@robinkrens.nl>
4  * Date              : 04.06.2022
5  * Last Modified Date: 12.06.2022
6  * Last Modified By  : Robin Krens <robin@robinkrens.nl>
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <SDL2/SDL_image.h>
14
15 #define TILE_SZ         32
16 #define FOURBBP_ROW     8
17 #define FOURBPP_COL     8
18
19 void bmp_info(SDL_Surface * img)
20 {
21         SDL_PixelFormat * fmt;
22         fmt = img->format;
23         fprintf(stdout,"WIDTH: %d, HEIGHT: %d, BPP: %d\n", img->w, img->h, fmt->BitsPerPixel);
24 }
25
26
27 void generate_4bpp_tile_planar(unsigned char *buf, void * userdata, int sz)
28 {
29         unsigned char * ptr = (unsigned char *) userdata;
30         for (int i = 0; i < 32; i+=4) {
31                 /* fprintf(stdout, "userdata: pos: %d - %d\n", i, *ptr++); */
32                 for (int j = 0; j < 4; ++j) {
33                         for (int x = 0; x < 8; ++x) {
34                                 buf[i + j] = ptr[x] << j; 
35                         }
36                         /* printf("%d: %x\n", i+j, buf[i+j]); */
37                 }
38                 ptr += 8;
39         }
40 }
41
42 void generate_4bpp_tile_packed(unsigned char *buf, void * userdata, int sz)
43 {
44         unsigned char * ptr = (unsigned char *) userdata;
45         for (int i = 0; i < 32; i+=4) {
46                 for (int j = 0; j < 4; ++j) {
47                         buf[i+j] = ptr[0] << 4;
48                         buf[i+j] |= ptr[1];
49                         ptr += 2;
50                         printf("%d: %x\n", i+j, buf[i+j]);
51                 }
52         }
53 }
54
55 int main(void)
56 {
57         SDL_Surface * rawbmp;
58         SDL_PixelFormat * fmt;
59         FILE * ostream;
60         char filename[] = "test.bmp";
61         char outname[] = "test.gfx";
62         rawbmp = SDL_LoadBMP(filename);
63         
64         if (!rawbmp) {
65                 fprintf(stderr, "can not load .bmp file\n");
66                 exit(EXIT_FAILURE);
67         }
68
69         bmp_info(rawbmp);
70
71         SDL_LockSurface(rawbmp);
72         rawbmp->userdata = rawbmp->pixels;
73         SDL_UnlockSurface(rawbmp);
74
75         unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
76         
77         /* if (rawbmp->format->BitsPerPixel != 24) {
78                 fprintf(stderr, "format %d not supported\n", rawbmp->format->BitsPerPixel);
79                 exit(EXIT_FAILURE);
80         } */
81
82         ostream = fopen(outname, "w");
83         if (!ostream) {
84                 fprintf(stderr, "can not open file for writing!\n");
85                 exit(EXIT_FAILURE);
86         }
87
88         generate_4bpp_tile_packed(tile_buf, rawbmp->userdata, 64);
89         int ret = fwrite(tile_buf, sizeof(unsigned char), 32, ostream); 
90         printf("written: %d\n", ret);
91
92         SDL_FreeSurface(rawbmp);
93
94 }