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 */
16 static int skipcomment(FILE *fp
)
24 while (ch
!= '\n' && ch
!= EOF
)
28 static int readentry(FILE *fp
,int format
,int depth
)
33 while ((ch
= getc(fp
)) == ' ' || ch
== '\t' || ch
== '\r' || ch
== '\n');
34 if (ch
< '0' || ch
> '9') {
36 fprintf(stderr
,"Format error in input file\n");
38 for (; ch
>= '0' && ch
<= '9'; ch
= getc(fp
))
39 i
= 10*i
+ ch
- '0'; }
41 if ((i
= getc(fp
)) > depth
|| i
< 0)
43 return((i
*256)/(depth
+1));
46 static void packpixel(char *data
,int c
)
51 i
= (i
<< 3) | (*data
++ & 0x7);
53 putchar((i
>> (n
-= 8)) & 0xFF); }
55 putchar(i
<< (8 - n
));
63 for (d
= 0; i
; d
++, i
/= 10);
67 static char *i2s(char *buf
,int i
)
76 static void flushdata(int x
,int y
,int c
,char *data
)
78 char b1
[10],b2
[10],b3
[10],b4
[10];
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
]))) {
88 printf("\e[%s-",i2s(b1
,i
-j
));
90 printf("\e[%s;%s;%s-",i2s(b1
,x
+j
),i2s(b2
,y
),i2s(b3
,i
-j
));
91 packpixel(data
+j
,i
-j
); }
93 printf("\e[%s;%s+",i2s(b1
,rle
),i2s(b2
,data
[i
]));
95 printf("\e[%s;%s;%s;%s+",i2s(b1
,x
+i
),i2s(b2
,y
),
96 i2s(b3
,rle
),i2s(b4
,data
[i
]));
102 printf("\e[%s-",i2s(b1
,c
-j
));
104 printf("\e[%s;%s;%s-",i2s(b1
,x
+j
),i2s(b2
,y
),i2s(b3
,c
-j
));
105 packpixel(data
+j
,c
-j
); }
109 int main(int argc
,char *argv
[])
115 int format
,width
,height
,depth
;
116 int bg
= 0,bgred
= 0,bggreen
= 0,bgblue
= 0;
117 int xoffset
= 0,yoffset
= 0;
122 while ((i
= getopt(argc
,argv
,"b:t:x:y:")) >= 0) switch(i
) {
125 for (i
= bgred
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
126 bgred
= 10*bgred
+ optarg
[i
++] - '0');
127 if (optarg
[i
++] != '/')
129 for (bggreen
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
130 bggreen
= 10*bggreen
+ optarg
[i
++] - '0');
131 if (optarg
[i
++] != '/')
133 for (bgblue
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
134 bgblue
= 10*bgblue
+ optarg
[i
++] - '0');
139 if ((tp
= malloc(sizeof(struct trans
))) == NULL
)
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
++] != '/')
145 for (tp
->g
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
146 tp
->g
= 10*tp
->g
+ optarg
[i
++] - '0');
147 if (optarg
[i
++] != '/')
149 for (tp
->b
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
150 tp
->b
= 10*tp
->b
+ optarg
[i
++] - '0');
151 if (optarg
[i
++] != ':')
153 if (optarg
[i
] == '-') {
156 for (tp
->idx
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
157 tp
->idx
= 10*tp
->idx
+ optarg
[i
++] - '0');
159 if (tp
->idx
< -1 || tp
->idx
>= 8)
167 for (i
= xoffset
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
168 xoffset
= 10*xoffset
+ optarg
[i
++] - '0');
173 for (i
= yoffset
= 0; optarg
[i
] >= '0' && optarg
[i
] <= '9';
174 yoffset
= 10*yoffset
+ optarg
[i
++] - '0');
180 fprintf(stderr
,"Usage: %s [-b r/g/b] [-t r/g/b:idx] "
181 "[-x offset] [-y offset] [ppmfile]\n",argv
[0]);
183 if (argc
-optind
== 0)
185 else if (argc
-optind
== 1)
186 infile
= fopen(argv
[optind
],"r");
189 if ((ch
= skipcomment(infile
)) != 'P' ||
190 ((format
= getc(infile
)) != '3' && format
!= '6') ||
191 ((ch
= getc(infile
)) != '\n' && ch
!= '\r' && getc(infile
) != '\n'))
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')
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')
204 if (!width
|| !height
|| !depth
/* || depth > 255 */)
206 if ((buf
= malloc(width
)) == NULL
)
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
);
214 if (bg
&& bgred
== r
&&
215 bggreen
== g
&& bgblue
== b
)
217 else for (tp
= trans
; tp
; tp
= tp
->next
)
218 if (tp
->r
== r
&& tp
->g
== g
&& tp
->b
== b
) {
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
) {
230 flushdata(w
-c
+xoffset
,h
+yoffset
,c
,buf
);
233 flushdata(w
-c
+xoffset
,h
+yoffset
,c
,buf
); }