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