img2gba

changeset 0:c359fcbd2422

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Jun 2014 18:19:52 +0300
parents
children eb9334da7c80
files .hgignore Makefile src/imgconv.c
diffstat 3 files changed, 106 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Wed Jun 25 18:19:52 2014 +0300
     1.3 @@ -0,0 +1,8 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     1.7 +\.png$
     1.8 +\.jpg$
     1.9 +\.ppm$
    1.10 +\.tga$
    1.11 +img2gba$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Wed Jun 25 18:19:52 2014 +0300
     2.3 @@ -0,0 +1,20 @@
     2.4 +PREFIX = /usr/local
     2.5 +
     2.6 +src = $(wildcard src/*.c)
     2.7 +obj = $(src:.c=.o)
     2.8 +bin = img2gba
     2.9 +
    2.10 +CFLAGS = -pedantic -Wall -g
    2.11 +LDFLAGS = -limago
    2.12 +
    2.13 +$(bin): $(obj)
    2.14 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.15 +
    2.16 +
    2.17 +.PHONY: clean
    2.18 +clean:
    2.19 +	rm $(obj) $(bin)
    2.20 +
    2.21 +.PHONY: install
    2.22 +install:
    2.23 +	cp $(bin) $(DESTDIR)$(PREFIX)/bin/$(bin)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/imgconv.c	Wed Jun 25 18:19:52 2014 +0300
     3.3 @@ -0,0 +1,78 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <string.h>
     3.7 +#include <imago2.h>
     3.8 +
     3.9 +int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name);
    3.10 +
    3.11 +int main(int argc, char **argv)
    3.12 +{
    3.13 +	int i;
    3.14 +
    3.15 +	if(argc < 2) {
    3.16 +		fprintf(stderr, "not enough arguments\n");
    3.17 +		return -1;
    3.18 +	}
    3.19 +
    3.20 +	for(i=1; i<argc; i++) {
    3.21 +		char fname[512], *cptr;
    3.22 +		int x, y;
    3.23 +		unsigned int *img = img_load_pixels(argv[i], &x, &y, IMG_FMT_RGBA32);
    3.24 +		if(!img) continue;
    3.25 +
    3.26 +		strcpy(fname, argv[i]);
    3.27 +		cptr = strrchr(fname, '.');
    3.28 +		if(cptr) {
    3.29 +			strcpy(cptr, ".c");
    3.30 +		} else {
    3.31 +			cptr = fname + strlen(fname);
    3.32 +			strcpy(cptr, ".c");
    3.33 +		}
    3.34 +
    3.35 +		if(save_image15(img, x, y, fname, "img") == -1) {
    3.36 +			fprintf(stderr, "could not save as %s\n", fname);
    3.37 +		}
    3.38 +
    3.39 +		img_free_pixels(img);
    3.40 +	}
    3.41 +	return 0;
    3.42 +}
    3.43 +
    3.44 +#define GET_R(pixel)	(((pixel) >> 16) & 0xff)
    3.45 +#define GET_G(pixel)	(((pixel) >> 8) & 0xff)
    3.46 +#define GET_B(pixel)	(((pixel) >> 0) & 0xff)
    3.47 +
    3.48 +#define PACK_COLOR15(r, g, b) ((((r) & 0x1f) << 10) | (((g) & 0x1f) << 5) | ((b) & 0x1f))
    3.49 +
    3.50 +int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name)
    3.51 +{
    3.52 +	int i, j;
    3.53 +	FILE *fp;
    3.54 +
    3.55 +	if(!(fp = fopen(fname, "w"))) {
    3.56 +		return -1;
    3.57 +	}
    3.58 +
    3.59 +	fprintf(fp, "\nconst unsigned short %s[] = {\n", array_name);
    3.60 +
    3.61 +	for(j=0; j<y; j++) {
    3.62 +		fprintf(fp, "\t");
    3.63 +		for(i=0; i<x; i++) {
    3.64 +			unsigned short r = GET_R(*img) >> 3;
    3.65 +			unsigned short g = GET_G(*img) >> 3;
    3.66 +			unsigned short b = GET_B(*img) >> 3;
    3.67 +			unsigned short pixel15 = PACK_COLOR15(r, g, b);
    3.68 +			fprintf(fp, "%u", pixel15);
    3.69 +			if(i < x-1 || j < y-1) {
    3.70 +				fprintf(fp, ", ");
    3.71 +			}
    3.72 +			img++;
    3.73 +		}
    3.74 +		fprintf(fp, "\n");
    3.75 +	}
    3.76 +
    3.77 +	fprintf(fp, "};\n");
    3.78 +
    3.79 +	fclose(fp);
    3.80 +	return 0;
    3.81 +}