#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL_image.h>
#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);
-}
+typedef struct {
+ unsigned width;
+ unsigned height;
+ unsigned bpp;
+} swan_gfx_t;
-
-void generate_4bpp_tile_planar(unsigned char *buf, void * userdata, int sz)
+void set_info(swan_gfx_t * gfx, SDL_Surface * img)
{
- 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;
- }
+ gfx->width = img->w;
+ gfx->height = img->h;
+ gfx->bpp = img->format->BitsPerPixel;
+
+ /* set userdata to iterate */
+ SDL_LockSurface(img);
+ img->userdata = img->pixels;
+ SDL_UnlockSurface(img);
}
-void generate_4bpp_tile_packed(unsigned char *buf, void * userdata, int sz)
+void generate_4bpp_tile(unsigned char *buf, void * userdata,
+ bool packed)
{
unsigned char * ptr = (unsigned char *) userdata;
- for (int i = 0; i < 32; i+=4) {
+ for (int i = 0; i < TILE_SZ; 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]);
+ if (packed) { /* packed format */
+ buf[i+j] = ptr[0] << 4;
+ buf[i+j] |= ptr[1];
+ ptr += 2;
+ } else { /* planar format */
+ for (int x = 0; x < 8; ++x) {
+ buf[i + j] = ptr[x] << j;
+ }
+ }
}
+ if (!packed)
+ ptr += 8;
}
}
int main(void)
{
SDL_Surface * rawbmp;
- SDL_PixelFormat * fmt;
FILE * ostream;
char filename[] = "test.bmp";
char outname[] = "test.gfx";
exit(EXIT_FAILURE);
}
- bmp_info(rawbmp);
-
- SDL_LockSurface(rawbmp);
- rawbmp->userdata = rawbmp->pixels;
- SDL_UnlockSurface(rawbmp);
+ swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
+ set_info(gfx, rawbmp);
unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
- /* if (rawbmp->format->BitsPerPixel != 24) {
- fprintf(stderr, "format %d not supported\n", rawbmp->format->BitsPerPixel);
+ if (gfx->bpp != 8) {
+ fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel);
+ fprintf(stderr, "use --force in command to run\n");
exit(EXIT_FAILURE);
- } */
+ }
ostream = fopen(outname, "w");
if (!ostream) {
exit(EXIT_FAILURE);
}
- generate_4bpp_tile_packed(tile_buf, rawbmp->userdata, 64);
- int ret = fwrite(tile_buf, sizeof(unsigned char), 32, ostream);
- printf("written: %d\n", ret);
+ unsigned nr_tiles = 1;
- SDL_FreeSurface(rawbmp);
+ for (int i = 0; i < nr_tiles; ++i) {
+ generate_4bpp_tile(tile_buf, rawbmp->userdata, true);
+ int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, ostream);
+ if (ret != TILE_SZ) {
+ fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
+ goto cleanup;
+ }
+ }
+cleanup:
+ free(gfx);
+ SDL_FreeSurface(rawbmp);
+ fclose(ostream);
}