megadrive_test2

annotate 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
rev   line source
nuclear@4 1 #include <stdio.h>
nuclear@4 2 #include <stdlib.h>
nuclear@4 3 #include <string.h>
nuclear@4 4 #include <ctype.h>
nuclear@4 5
nuclear@4 6 unsigned char *read_image(int *xsz, int *ysz, int *maxval);
nuclear@4 7
nuclear@4 8 int main(void)
nuclear@4 9 {
nuclear@4 10 int i, j, width, height, maxval;
nuclear@4 11 unsigned char *pixels;
nuclear@4 12
nuclear@4 13 if(!(pixels = read_image(&width, &height, &maxval))) {
nuclear@4 14 return 1;
nuclear@4 15 }
nuclear@4 16 fprintf(stderr, "read image %dx%d (maxval: %d)\n", width, height, maxval);
nuclear@4 17
nuclear@4 18 /* reduce colors */
nuclear@4 19
nuclear@4 20 /* slice tiles */
nuclear@4 21 for(i=0; i<height / 8; i++) {
nuclear@4 22 for(j=0; j<width / 8; j++) {
nuclear@4 23 }
nuclear@4 24 }
nuclear@4 25
nuclear@4 26 return 0;
nuclear@4 27 }
nuclear@4 28
nuclear@4 29 char *clean_line(char *s)
nuclear@4 30 {
nuclear@4 31 char *tmp;
nuclear@4 32
nuclear@4 33 while(*s && isspace(*s)) {
nuclear@4 34 ++s;
nuclear@4 35 }
nuclear@4 36
nuclear@4 37 tmp = strchr(s, '#');
nuclear@4 38 if(tmp) *tmp = 0;
nuclear@4 39 tmp = strchr(s, '\n');
nuclear@4 40 if(tmp) *tmp = 0;
nuclear@4 41 tmp = strchr(s, '\r');
nuclear@4 42 if(tmp) *tmp = 0;
nuclear@4 43
nuclear@4 44 return s;
nuclear@4 45 }
nuclear@4 46
nuclear@4 47 unsigned char *read_image(int *width, int *height, int *maxval)
nuclear@4 48 {
nuclear@4 49 char buf[256];
nuclear@4 50 int i, xsz, ysz, hdrline = 0;
nuclear@4 51 unsigned char *pixels;
nuclear@4 52
nuclear@4 53 while(hdrline < 3 && fgets(buf, sizeof buf, stdin)) {
nuclear@4 54 char *line = clean_line(buf);
nuclear@4 55 if(!*line) continue;
nuclear@4 56
nuclear@4 57 switch(hdrline) {
nuclear@4 58 case 0:
nuclear@4 59 if(strcmp(line, "P6") != 0) {
nuclear@4 60 goto inval;
nuclear@4 61 }
nuclear@4 62 break;
nuclear@4 63
nuclear@4 64 case 1:
nuclear@4 65 if(sscanf(line, "%d %d", &xsz, &ysz) != 2) {
nuclear@4 66 goto inval;
nuclear@4 67 }
nuclear@4 68 break;
nuclear@4 69
nuclear@4 70 case 2:
nuclear@4 71 if(sscanf(line, "%d", maxval) != 1) {
nuclear@4 72 goto inval;
nuclear@4 73 }
nuclear@4 74 break;
nuclear@4 75 }
nuclear@4 76 ++hdrline;
nuclear@4 77 }
nuclear@4 78
nuclear@4 79 if(!(pixels = malloc(xsz * ysz * 3))) {
nuclear@4 80 fprintf(stderr, "failed to allocate image: %dx%d\n", xsz, ysz);
nuclear@4 81 return 0;
nuclear@4 82 }
nuclear@4 83
nuclear@4 84 for(i=0; i<xsz * ysz * 3; i++) {
nuclear@4 85 int c = getchar();
nuclear@4 86 if(c == -1) goto inval;
nuclear@4 87 pixels[i] = c;
nuclear@4 88 }
nuclear@4 89
nuclear@4 90 *width = xsz;
nuclear@4 91 *height = ysz;
nuclear@4 92 return pixels;
nuclear@4 93
nuclear@4 94 inval:
nuclear@4 95 fprintf(stderr, "invalid input\n");
nuclear@4 96 return 0;
nuclear@4 97 }