A 4 bit sprite image converter

not sure where to put this but i made a 16 color pallet image converter in c
im just assuming how the bitmap works in the library (heaving width, height and then pixel data)
currently it has the pico8/default pallet hardcoded in it.

since there is no simulator yet i am not sure if this will work at all

source code
/* 4 bit sprite format - Public Domain */
#include <stdio.h>

/* STB image library from https://github.com/nothings/stb */
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
unsigned char pallet[][3] = {
    {  0,  0,  0},
    { 29, 43, 83},
    {126, 37, 83},
    {  0,135, 81},
    {171, 82, 54},
    { 95, 87, 79},
    {194,195,199},
    {255,241,232},
    {255,  0, 77},
    {255,163,  0},
    {255,255, 39},
    {  0,231, 86},
    { 41,173,255},
    {131,118,156},
    {255,119,168},
    {255,204,170}
};
int check(unsigned char r,unsigned char g,unsigned char b,int x, int y){
    for (int i=0; i<16; i++) {
        if (pallet[i][0]==r &&pallet[i][1]==g &&pallet[i][2]==b) {
            return i;
        }
    }
    printf("pixel at x%i,y%i not in pallet color:%i,%i,%i\n",x,y,r,g,b);
    return 0;
}
int main(int argc, const char * argv[]) {
    if (argc != 2) {
         printf("no image suplied\n");
    }
    else{
        int sx,sy,n;
        unsigned char *data = stbi_load(argv[1], &sx, &sy, &n, 3);
        int last = 0;
        FILE *output = NULL;
        output = fopen("output.4BS", "w+");
        if (output != NULL) {
            fprintf(output,"{%i, %i, /*width, height*/\n",sx,sy);
            for (int y=0; y<sy; y++) {
                for (int x=0; x<sx; x++) {
                    if(x%2){
                    fprintf(output,"0x%x%x,",last, check(data[((y*sx)+x)*3] , data[((y*sx)+x)*3 + 1], data[((y*sx)+x)*3 +2],x,y) );
                    }else{
                        last = check(data[((y*sx)+x)*3] , data[((y*sx)+x)*3 + 1], data[((y*sx)+x)*3 +2],x,y);
                    }
                }
                fprintf(output,"\n");
            }
            fprintf(output,"};");
            
        }
        stbi_image_free(data);
        fclose(output);
    }
    return 0;
}

2 Likes

Tool with palette extraction coming up later today

oh ok, thought this was helpfull…

1 Like

@adekto but how were you to know? Its cool! You will need 4-bit tricks in any case, better get some practise!

lol yea ok, first time really doing this file read and writing in c, its was fun to learn new stuff

@adekto wait till you see my tool. You will want to improve that code. I will start work on all this stuff as soon as I have gotten some breakfast.

1 Like