megadrive_test2

diff tools/ppm2md.c @ 4:72ab63f262bf

tiles
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jun 2017 08:02:51 +0300
parents
children ea70f3da150f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/ppm2md.c	Mon Jun 19 08:02:51 2017 +0300
     1.3 @@ -0,0 +1,97 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <ctype.h>
     1.8 +
     1.9 +unsigned char *read_image(int *xsz, int *ysz, int *maxval);
    1.10 +
    1.11 +int main(void)
    1.12 +{
    1.13 +	int i, j, width, height, maxval;
    1.14 +	unsigned char *pixels;
    1.15 +
    1.16 +	if(!(pixels = read_image(&width, &height, &maxval))) {
    1.17 +		return 1;
    1.18 +	}
    1.19 +	fprintf(stderr, "read image %dx%d (maxval: %d)\n", width, height, maxval);
    1.20 +
    1.21 +	/* reduce colors */
    1.22 +
    1.23 +	/* slice tiles */
    1.24 +	for(i=0; i<height / 8; i++) {
    1.25 +		for(j=0; j<width / 8; j++) {
    1.26 +		}
    1.27 +	}
    1.28 +
    1.29 +	return 0;
    1.30 +}
    1.31 +
    1.32 +char *clean_line(char *s)
    1.33 +{
    1.34 +	char *tmp;
    1.35 +
    1.36 +	while(*s && isspace(*s)) {
    1.37 +		++s;
    1.38 +	}
    1.39 +
    1.40 +	tmp = strchr(s, '#');
    1.41 +	if(tmp) *tmp = 0;
    1.42 +	tmp = strchr(s, '\n');
    1.43 +	if(tmp) *tmp = 0;
    1.44 +	tmp = strchr(s, '\r');
    1.45 +	if(tmp) *tmp = 0;
    1.46 +
    1.47 +	return s;
    1.48 +}
    1.49 +
    1.50 +unsigned char *read_image(int *width, int *height, int *maxval)
    1.51 +{
    1.52 +	char buf[256];
    1.53 +	int i, xsz, ysz, hdrline = 0;
    1.54 +	unsigned char *pixels;
    1.55 +
    1.56 +	while(hdrline < 3 && fgets(buf, sizeof buf, stdin)) {
    1.57 +		char *line = clean_line(buf);
    1.58 +		if(!*line) continue;
    1.59 +
    1.60 +		switch(hdrline) {
    1.61 +		case 0:
    1.62 +			if(strcmp(line, "P6") != 0) {
    1.63 +				goto inval;
    1.64 +			}
    1.65 +			break;
    1.66 +
    1.67 +		case 1:
    1.68 +			if(sscanf(line, "%d %d", &xsz, &ysz) != 2) {
    1.69 +				goto inval;
    1.70 +			}
    1.71 +			break;
    1.72 +
    1.73 +		case 2:
    1.74 +			if(sscanf(line, "%d", maxval) != 1) {
    1.75 +				goto inval;
    1.76 +			}
    1.77 +			break;
    1.78 +		}
    1.79 +		++hdrline;
    1.80 +	}
    1.81 +
    1.82 +	if(!(pixels = malloc(xsz * ysz * 3))) {
    1.83 +		fprintf(stderr, "failed to allocate image: %dx%d\n", xsz, ysz);
    1.84 +		return 0;
    1.85 +	}
    1.86 +
    1.87 +	for(i=0; i<xsz * ysz * 3; i++) {
    1.88 +		int c = getchar();
    1.89 +		if(c == -1) goto inval;
    1.90 +		pixels[i] = c;
    1.91 +	}
    1.92 +
    1.93 +	*width = xsz;
    1.94 +	*height = ysz;
    1.95 +	return pixels;
    1.96 +
    1.97 +inval:
    1.98 +	fprintf(stderr, "invalid input\n");
    1.99 +	return 0;
   1.100 +}