[ath5k] Update for changes in kernel 2.6.31
[gpxe.git] / contrib / ppmtoansi / ppmtoansi.c
blob71d95cba7d4687059d164fff1dc1147d98c0bbc1
1 #include <getopt.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 static int palette[8][3] = {
6 /* black, red, green, yellow, */
7 { 0, 0, 0},{255, 0, 0},{ 0,255, 0},{255,255, 0},
8 { 0, 0,255},{255, 0,255},{ 0,255,255},{255,255,255}};
9 /* blue, magenta, cyan, white */
11 static struct trans {
12 struct trans *next;
13 int idx,r,g,b;
14 } *trans = NULL;
16 static int skipcomment(FILE *fp)
18 int ch;
20 for (;;) {
21 ch = getc(fp);
22 if (ch != '#')
23 return(ch);
24 while (ch != '\n' && ch != EOF)
25 ch = getc(fp); }
28 static int readentry(FILE *fp,int format,int depth)
30 int ch,i = 0;
32 if (format == '3') {
33 while ((ch = getc(fp)) == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
34 if (ch < '0' || ch > '9') {
35 error:
36 fprintf(stderr,"Format error in input file\n");
37 exit(1); }
38 for (; ch >= '0' && ch <= '9'; ch = getc(fp))
39 i = 10*i + ch - '0'; }
40 else {
41 if ((i = getc(fp)) > depth || i < 0)
42 goto error; }
43 return((i*256)/(depth+1));
46 static void packpixel(char *data,int c)
48 int i = 0, n = 0;
50 while (c--) {
51 i = (i << 3) | (*data++ & 0x7);
52 if ((n += 3) >= 8)
53 putchar((i >> (n -= 8)) & 0xFF); }
54 if (n)
55 putchar(i << (8 - n));
56 return;
59 static int dg(int i)
61 int d;
63 for (d = 0; i; d++, i /= 10);
64 return(d);
67 static char *i2s(char *buf,int i)
69 /* if (!i)
70 *buf = '\000';
71 else*/
72 sprintf(buf,"%d",i);
73 return(buf);
76 static void flushdata(int x,int y,int c,char *data)
78 char b1[10],b2[10],b3[10],b4[10];
79 int i,j,rle,v;
81 for (i = j = v = 0; i < c; ) {
82 for (rle = 0; i+rle < c && data[i] == data[i+rle]; rle++);
83 if (rle > (i != j ? (v ? 4 : 6) : 0) +
84 ((v || (i != j)) ? 4+dg(rle)+dg(data[i])
85 : 6+dg(x+i)+dg(y)+dg(rle)+dg(data[i]))) {
86 if (i != j) {
87 if (v)
88 printf("\e[%s-",i2s(b1,i-j));
89 else
90 printf("\e[%s;%s;%s-",i2s(b1,x+j),i2s(b2,y),i2s(b3,i-j));
91 packpixel(data+j,i-j); }
92 if (v++ || (i != j))
93 printf("\e[%s;%s+",i2s(b1,rle),i2s(b2,data[i]));
94 else
95 printf("\e[%s;%s;%s;%s+",i2s(b1,x+i),i2s(b2,y),
96 i2s(b3,rle),i2s(b4,data[i]));
97 j = i += rle; }
98 else
99 i++; }
100 if (j != c) {
101 if (v)
102 printf("\e[%s-",i2s(b1,c-j));
103 else
104 printf("\e[%s;%s;%s-",i2s(b1,x+j),i2s(b2,y),i2s(b3,c-j));
105 packpixel(data+j,c-j); }
106 return;
109 int main(int argc,char *argv[])
111 extern int optind;
112 extern char *optarg;
113 FILE *infile = NULL;
114 int ch,i,j,dist,idx;
115 int format,width,height,depth;
116 int bg = 0,bgred = 0,bggreen = 0,bgblue = 0;
117 int xoffset = 0,yoffset = 0;
118 int w,h,r,g,b,c;
119 struct trans *tp;
120 char *buf;
122 while ((i = getopt(argc,argv,"b:t:x:y:")) >= 0) switch(i) {
123 case 'b':
124 bg++;
125 for (i = bgred = 0; optarg[i] >= '0' && optarg[i] <= '9';
126 bgred = 10*bgred + optarg[i++] - '0');
127 if (optarg[i++] != '/')
128 goto usage;
129 for (bggreen = 0; optarg[i] >= '0' && optarg[i] <= '9';
130 bggreen = 10*bggreen + optarg[i++] - '0');
131 if (optarg[i++] != '/')
132 goto usage;
133 for (bgblue = 0; optarg[i] >= '0' && optarg[i] <= '9';
134 bgblue = 10*bgblue + optarg[i++] - '0');
135 if (optarg[i])
136 goto usage;
137 break;
138 case 't':
139 if ((tp = malloc(sizeof(struct trans))) == NULL)
140 goto usage;
141 for (i = tp->r = 0; optarg[i] >= '0' && optarg[i] <= '9';
142 tp->r = 10*tp->r + optarg[i++] - '0');
143 if (optarg[i++] != '/')
144 goto usage;
145 for (tp->g = 0; optarg[i] >= '0' && optarg[i] <= '9';
146 tp->g = 10*tp->g + optarg[i++] - '0');
147 if (optarg[i++] != '/')
148 goto usage;
149 for (tp->b = 0; optarg[i] >= '0' && optarg[i] <= '9';
150 tp->b = 10*tp->b + optarg[i++] - '0');
151 if (optarg[i++] != ':')
152 goto usage;
153 if (optarg[i] == '-') {
154 j = -1; i++; }
155 else j = 1;
156 for (tp->idx = 0; optarg[i] >= '0' && optarg[i] <= '9';
157 tp->idx = 10*tp->idx + optarg[i++] - '0');
158 tp->idx *= j;
159 if (tp->idx < -1 || tp->idx >= 8)
160 goto usage;
161 if (optarg[i])
162 goto usage;
163 tp->next = trans;
164 trans = tp;
165 break;
166 case 'x':
167 for (i = xoffset = 0; optarg[i] >= '0' && optarg[i] <= '9';
168 xoffset = 10*xoffset + optarg[i++] - '0');
169 if (optarg[i])
170 goto usage;
171 break;
172 case 'y':
173 for (i = yoffset = 0; optarg[i] >= '0' && optarg[i] <= '9';
174 yoffset = 10*yoffset + optarg[i++] - '0');
175 if (optarg[i])
176 goto usage;
177 break;
178 default:
179 usage:
180 fprintf(stderr,"Usage: %s [-b r/g/b] [-t r/g/b:idx] "
181 "[-x offset] [-y offset] [ppmfile]\n",argv[0]);
182 exit(1); }
183 if (argc-optind == 0)
184 infile = stdin;
185 else if (argc-optind == 1)
186 infile = fopen(argv[optind],"r");
187 if (!infile)
188 goto usage;
189 if ((ch = skipcomment(infile)) != 'P' ||
190 ((format = getc(infile)) != '3' && format != '6') ||
191 ((ch = getc(infile)) != '\n' && ch != '\r' && getc(infile) != '\n'))
192 goto usage;
193 for (width = 0; (ch = skipcomment(infile)) >= '0' && ch <= '9';
194 width = 10*width + ch - '0');
195 while (ch == ' ') ch = getc(infile);
196 for (height = 0; ch >= '0' && ch <= '9'; ch = getc(infile))
197 height = 10*height + ch - '0';
198 if (ch != '\n' && ch != '\r' && getc(infile) != '\n')
199 goto usage;
200 for (depth = 0; (ch = skipcomment(infile)) >= '0' && ch <= '9';
201 depth = 10*depth + ch - '0');
202 if (ch != '\n' && ch != '\r' && getc(infile) != '\n')
203 goto usage;
204 if (!width || !height || !depth /* || depth > 255 */)
205 goto usage;
206 if ((buf = malloc(width)) == NULL)
207 goto usage;
208 for (h = 0; h < height; h++) {
209 for (w = c = 0; w < width; w++) {
210 r = readentry(infile,format,depth);
211 g = readentry(infile,format,depth);
212 b = readentry(infile,format,depth);
213 idx = 255;
214 if (bg && bgred == r &&
215 bggreen == g && bgblue == b)
216 idx = -1;
217 else for (tp = trans; tp; tp = tp->next)
218 if (tp->r == r && tp->g == g && tp->b == b) {
219 idx = tp->idx;
220 break; }
221 if (idx == 255)
222 for (idx = -1, dist = 3*255*255, i = 8; i--;)
223 if ((j = (r-palette[i][0])*(r-palette[i][0]) +
224 (g-palette[i][1])*(g-palette[i][1]) +
225 (b-palette[i][2])*(b-palette[i][2])) < dist) {
226 dist = j; idx = i; }
227 if (idx >= 0)
228 buf[c++] = idx;
229 else if (c) {
230 flushdata(w-c+xoffset,h+yoffset,c,buf);
231 c = 0; } }
232 if (c)
233 flushdata(w-c+xoffset,h+yoffset,c,buf); }
234 exit(0);