megadrive_test2

diff tools/ppm2md.c @ 5:ea70f3da150f

color cycling tunnel
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 20 Jun 2017 06:08:58 +0300
parents 72ab63f262bf
children
line diff
     1.1 --- a/tools/ppm2md.c	Mon Jun 19 08:02:51 2017 +0300
     1.2 +++ b/tools/ppm2md.c	Tue Jun 20 06:08:58 2017 +0300
     1.3 @@ -3,26 +3,92 @@
     1.4  #include <string.h>
     1.5  #include <ctype.h>
     1.6  
     1.7 +#define RODATA_STR	"__attribute__((section(\".rodata\")))"
     1.8 +
     1.9  unsigned char *read_image(int *xsz, int *ysz, int *maxval);
    1.10  
    1.11 -int main(void)
    1.12 +int main(int argc, char **argv)
    1.13  {
    1.14 -	int i, j, width, height, maxval;
    1.15 -	unsigned char *pixels;
    1.16 +	int i, j, width, height, xtiles, ytiles, maxval;
    1.17 +	unsigned char *pixels, *tiles, *src, *dest;
    1.18 +	const char *prefix = "img_";
    1.19 +
    1.20 +	if(argv[1]) {
    1.21 +		prefix = argv[1];
    1.22 +	}
    1.23  
    1.24  	if(!(pixels = read_image(&width, &height, &maxval))) {
    1.25  		return 1;
    1.26  	}
    1.27  	fprintf(stderr, "read image %dx%d (maxval: %d)\n", width, height, maxval);
    1.28  
    1.29 -	/* reduce colors */
    1.30 +	xtiles = width / 8;
    1.31 +	ytiles = height / 8;
    1.32 +	/* each tile is 32 bytes, 8 rows of 4 bytes each */
    1.33 +	if(!(tiles = malloc(xtiles * ytiles * 32))) {
    1.34 +		free(pixels);
    1.35 +		return 1;
    1.36 +	}
    1.37  
    1.38 -	/* slice tiles */
    1.39 -	for(i=0; i<height / 8; i++) {
    1.40 -		for(j=0; j<width / 8; j++) {
    1.41 +	/* if we have 15 as max val, we'll assume they are straight palette indices
    1.42 +	 * otherwise, do color quantization and palette allocation
    1.43 +	 */
    1.44 +	if(maxval == 15) {
    1.45 +		/* just drop green/blue components in-place, keep red as index */
    1.46 +		dest = src = pixels;
    1.47 +		for(i=0; i<width * height; i++) {
    1.48 +			*dest++ = *src;
    1.49 +			src += 3;
    1.50 +		}
    1.51 +	} else {
    1.52 +		/* TODO */
    1.53 +	}
    1.54 +
    1.55 +	/* after processing, we have 1 color index per byte */
    1.56 +
    1.57 +	/* slice tiles and pack indices as nibbles */
    1.58 +	dest = tiles;
    1.59 +	for(i=0; i<ytiles; i++) {
    1.60 +		for(j=0; j<xtiles; j++) {
    1.61 +			int x, y;
    1.62 +			int tilestart = (i * xtiles * 8 + j) * 8;
    1.63 +
    1.64 +			for(y=0; y<8; y++) {
    1.65 +				for(x=0; x<4; x++) {
    1.66 +					src = pixels + tilestart + y * width + x * 2;
    1.67 +					*dest++ = (src[0] << 4) | (src[1] & 0xf);
    1.68 +				}
    1.69 +			}
    1.70 +
    1.71  		}
    1.72  	}
    1.73  
    1.74 +	/* output as C array */
    1.75 +	printf("/* generated by ppm2md */\n");
    1.76 +	printf("int %sxtiles " RODATA_STR " = %d;\n", prefix, xtiles);
    1.77 +	printf("int %sytiles " RODATA_STR " = %d;\n", prefix, ytiles);
    1.78 +	printf("unsigned char %stiles[][32] " RODATA_STR " = {", prefix);
    1.79 +
    1.80 +	src = tiles;
    1.81 +	for(i=0; i<xtiles * ytiles; i++) {
    1.82 +		if(i > 0) {
    1.83 +			printf(",\n");
    1.84 +		} else {
    1.85 +			putchar('\n');
    1.86 +		}
    1.87 +		printf("\t{");
    1.88 +		for(j=0; j<32; j++) {
    1.89 +			if(j == 0) {
    1.90 +				printf("%u", (unsigned int)*src++);
    1.91 +			} else {
    1.92 +				printf(", %u", (unsigned int)*src++);
    1.93 +			}
    1.94 +		}
    1.95 +		printf(" }");
    1.96 +	}
    1.97 +	printf("\n};\n");
    1.98 +
    1.99 +
   1.100  	return 0;
   1.101  }
   1.102