FreeCalypso > hg > freecalypso-hwlab
annotate lunalcd/ppmtoimg.c @ 165:7a4e8d23ab3a
eeproms/icestick: programming for Lattice Icestick
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 30 Jul 2023 18:10:51 +0000 |
parents | 1e6f05ede5ca |
children |
rev | line source |
---|---|
69
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This program converts a 176x220 pixel wallpaper image from PPM format |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * into a raw binary image file that will be written into an ad hoc flash |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * location with fc-loadtool; these images will then be displayed on our |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * Luna LCD by a standalone target-utils program. |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <ctype.h> |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 static char *ppm_filename; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 static FILE *ppmfile; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 static int ppm_is_ascii; /* P3 format instead of P6 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 /* |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 * This function reads one ASCII-encoded integer from a PPM file. |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 * It handles the white space and comment rules of the PPM format. |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 static int |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 ppm_get_ascii_number() |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 int accum, c; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 do { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 c = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (c < 0) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 badeof: fprintf(stderr, "%s: unexpected EOF\n", ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (c == '#') { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 do |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 c = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 while (c >= 0 && c != '\n'); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (c != '\n') |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 goto badeof; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } while (isspace(c)); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (!isdigit(c)) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 fprintf(stderr, |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 "%s: unexpected data where a number was expected\n", |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 for (accum = c - '0'; ; ) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 c = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 if (c < 0 || isspace(c)) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 break; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (!isdigit(c)) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 fprintf(stderr, |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 "%s: bad character in the middle of a number\n", |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 accum = accum * 10 + c - '0'; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 return accum; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 /* |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 * This function reads and parses the PPM header of our input file. |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 static int |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 ppm_read_header() |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 int magic1, magic2; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 int rd; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 magic1 = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 magic2 = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (magic1 == 'P' && magic2 == '3') |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 ppm_is_ascii = 1; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 else if (magic1 == 'P' && magic2 == '6') |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 ppm_is_ascii = 0; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 else { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 fprintf(stderr, "%s: P3 or P6 format expected\n", ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 rd = ppm_get_ascii_number(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 if (rd < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 if (rd != 176) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 fprintf(stderr, "%s: width is not 176\n", ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 rd = ppm_get_ascii_number(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 if (rd < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 if (rd != 220) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 fprintf(stderr, "%s: height is not 220\n", ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 rd = ppm_get_ascii_number(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 if (rd < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 if (rd != 255) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 fprintf(stderr, "%s: maxval is not 255\n", ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 return(0); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 /* |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 * This function reads a single R, G or B value from a PPM file. |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 static int |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 ppm_get_pixval() |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 int c; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 if (ppm_is_ascii) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 return ppm_get_ascii_number(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 c = getc(ppmfile); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 if (c < 0) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 fprintf(stderr, "%s: EOF while reading binary pixel data\n", |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 return c; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 static int |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 ppm_get_rgb() |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
125 int r, g, b; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
126 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
127 r = ppm_get_pixval(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
128 if (r < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
129 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
130 g = ppm_get_pixval(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
131 if (g < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
132 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
133 b = ppm_get_pixval(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
134 if (b < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
135 return(-1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
136 /* convert to 5:6:5 */ |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
137 r >>= 3; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
138 g >>= 2; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
139 b >>= 3; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
140 return (r << 11) | (g << 5) | b; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
141 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
142 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
143 main(argc, argv) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
144 char **argv; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
145 { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
146 FILE *of; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
147 int rc; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
148 unsigned n; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
149 |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
150 if (argc != 3) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
151 fprintf(stderr, "usage: %s ppmfile binfile\n", argv[0]); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
152 exit(1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
153 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
154 ppm_filename = argv[1]; |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
155 ppmfile = fopen(ppm_filename, "r"); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
156 if (!ppmfile) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
157 perror(ppm_filename); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
158 exit(1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
159 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
160 rc = ppm_read_header(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
161 if (rc < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
162 exit(1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
163 of = fopen(argv[2], "w"); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
164 if (!of) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
165 perror(argv[2]); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
166 exit(1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
167 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
168 for (n = 0; n < 176*220; n++) { |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
169 rc = ppm_get_rgb(); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
170 if (rc < 0) |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
171 exit(1); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
172 putc(rc & 0xFF, of); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
173 putc(rc >> 8, of); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
174 } |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
175 fclose(of); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
176 exit(0); |
1e6f05ede5ca
lunalcd: ppmtoimg utility added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
177 } |