tools: bmp2tiles init
[swan-dev] / tools / bmp2tiles / bmp2tiles.c
diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c
new file mode 100644 (file)
index 0000000..42a8805
--- /dev/null
@@ -0,0 +1,85 @@
+/**
+ * File              : bmp2tiles.c
+ * Author            : Robin Krens <robin@robinkrens.nl>
+ * Date              : 04.06.2022
+ * Last Modified Date: 12.06.2022
+ * Last Modified By  : Robin Krens <robin@robinkrens.nl>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <SDL2/SDL_image.h>
+
+#define TILE_SZ                32
+#define FOURBBP_ROW    8
+#define FOURBPP_COL    8
+
+void bmp_info(SDL_Surface * img)
+{
+       SDL_PixelFormat * fmt;
+       fmt = img->format;
+       fprintf(stdout,"WIDTH: %d, HEIGHT: %d, BPP: %d\n", img->w, img->h, fmt->BitsPerPixel);
+}
+
+
+void generate_4bpp_tile_planar(unsigned char *buf, void * userdata, int sz)
+{
+       unsigned char * ptr = (unsigned char *) userdata;
+       for (int i = 0; i < 32; i+=4) {
+               /* fprintf(stdout, "userdata: pos: %d - %d\n", i, *ptr++); */
+               for (int j = 0; j < 4; ++j) {
+                       for (int x = 0; x < 8; ++x) {
+                               buf[i + j] = ptr[x] << j; 
+                       }
+                       /* printf("%d: %x\n", i+j, buf[i+j]); */
+               }
+               ptr += 8;
+       }
+}
+
+void generate_4bpp_tile_packed(unsigned char *buf, void * userdata, int sz)
+{
+       unsigned char * ptr = (unsigned char *) userdata;
+       for (int i = 0; i < 32; i+=4) {
+               for (int j = 0; j < 4; ++j) {
+                       buf[i+j] = ptr[0] << 4;
+                       buf[i+j] |= ptr[1];
+                       ptr += 2;
+                       printf("%d: %x\n", i+j, buf[i+j]);
+               }
+       }
+}
+
+int main(void)
+{
+       SDL_Surface * rawbmp;
+       SDL_PixelFormat * fmt;
+       char filename[] = "test.bmp";
+       rawbmp = SDL_LoadBMP(filename);
+       
+       if (!rawbmp) {
+               fprintf(stderr, "can not load .bmp file\n");
+               exit(EXIT_FAILURE);
+       }
+
+       bmp_info(rawbmp);
+
+       SDL_LockSurface(rawbmp);
+       rawbmp->userdata = rawbmp->pixels;
+       SDL_UnlockSurface(rawbmp);
+
+       unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
+
+       generate_4bpp_tile_packed(tile_buf, rawbmp->userdata, 64);
+       
+       /* if (rawbmp->format->BitsPerPixel != 24) {
+               fprintf(stderr, "format %d not supported\n", rawbmp->format->BitsPerPixel);
+               exit(EXIT_FAILURE);
+       } */
+
+
+       SDL_FreeSurface(rawbmp);
+
+}