1 /*
2 **  Copyright 1994, Home Pages, Inc.
3 **
4 **    Please read the file COPYRIGHT for specific information.
5 **
6 **    Home Pages, Inc.
7 **    257 Castro St. Suite 219
8 **    Mountain View, CA 94041
9 **
10 **    Phone: 1 415 903 5353
11 **      Fax: 1 415 903 5345
12 **
13 **    EMail: support@homepages.com
14 **
15 */
16 
17 #include <stdio.h>
18 #include <ctype.h>
19 extern char *strchr();
20 #define TRUE 1
21 #define FALSE 0
22 #include "gif.h"
23 
24 struct colorTable_s {
25 	int	r,g,b;
26 	char	*name;
27 } colorTable [] = {
28 	{255,250,250,"snow"},
29 	{248,248,255,"ghost white"},
30 	{248,248,255,"GhostWhite"},
31 	{245,245,245,"white smoke"},
32 	{245,245,245,"WhiteSmoke"},
33 	{220,220,220,"gainsboro"},
34 	{255,250,240,"floral white"},
35 	{255,250,240,"FloralWhite"},
36 	{253,245,230,"old lace"},
37 	{253,245,230,"OldLace"},
38 	{250,240,230,"linen"},
39 	{250,235,215,"antique white"},
40 	{250,235,215,"AntiqueWhite"},
41 	{255,239,213,"papaya whip"},
42 	{255,239,213,"PapayaWhip"},
43 	{255,235,205,"blanched almond"},
44 	{255,235,205,"BlanchedAlmond"},
45 	{255,228,196,"bisque"},
46 	{255,218,185,"peach puff"},
47 	{255,218,185,"PeachPuff"},
48 	{255,222,173,"navajo white"},
49 	{255,222,173,"NavajoWhite"},
50 	{255,228,181,"moccasin"},
51 	{255,248,220,"cornsilk"},
52 	{255,255,240,"ivory"},
53 	{255,250,205,"lemon chiffon"},
54 	{255,250,205,"LemonChiffon"},
55 	{255,245,238,"seashell"},
56 	{240,255,240,"honeydew"},
57 	{245,255,250,"mint cream"},
58 	{245,255,250,"MintCream"},
59 	{240,255,255,"azure"},
60 	{240,248,255,"alice blue"},
61 	{240,248,255,"AliceBlue"},
62 	{230,230,250,"lavender"},
63 	{255,240,245,"lavender blush"},
64 	{255,240,245,"LavenderBlush"},
65 	{255,228,225,"misty rose"},
66 	{255,228,225,"MistyRose"},
67 	{255,255,255,"white"},
68 	{0,0,0,"black"},
69 	{47,79,79,"dark slate gray"},
70 	{47,79,79,"DarkSlateGray"},
71 	{47,79,79,"dark slate grey"},
72 	{47,79,79,"DarkSlateGrey"},
73 	{105,105,105,"dim gray"},
74 	{105,105,105,"DimGray"},
75 	{105,105,105,"dim grey"},
76 	{105,105,105,"DimGrey"},
77 	{112,128,144,"slate gray"},
78 	{112,128,144,"SlateGray"},
79 	{112,128,144,"slate grey"},
80 	{112,128,144,"SlateGrey"},
81 	{119,136,153,"light slate gray"},
82 	{119,136,153,"LightSlateGray"},
83 	{119,136,153,"light slate grey"},
84 	{119,136,153,"LightSlateGrey"},
85 	{192,192,192,"gray"},
86 	{192,192,192,"grey"},
87 	{211,211,211,"light grey"},
88 	{211,211,211,"LightGrey"},
89 	{211,211,211,"light gray"},
90 	{211,211,211,"LightGray"},
91 	{25,25,112,"midnight blue"},
92 	{25,25,112,"MidnightBlue"},
93 	{0,0,128,"navy"},
94 	{0,0,128,"navy blue"},
95 	{0,0,128,"NavyBlue"},
96 	{100,149,237,"cornflower blue"},
97 	{100,149,237,"CornflowerBlue"},
98 	{72,61,139,"dark slate blue"},
99 	{72,61,139,"DarkSlateBlue"},
100 	{106,90,205,"slate blue"},
101 	{106,90,205,"SlateBlue"},
102 	{123,104,238,"medium slate blue"},
103 	{123,104,238,"MediumSlateBlue"},
104 	{132,112,255,"light slate blue"},
105 	{132,112,255,"LightSlateBlue"},
106 	{0,0,205,"medium blue"},
107 	{0,0,205,"MediumBlue"},
108 	{65,105,225,"royal blue"},
109 	{65,105,225,"RoyalBlue"},
110 	{0,0,255,"blue"},
111 	{30,144,255,"dodger blue"},
112 	{30,144,255,"DodgerBlue"},
113 	{0,191,255,"deep sky blue"},
114 	{0,191,255,"DeepSkyBlue"},
115 	{135,206,235,"sky blue"},
116 	{135,206,235,"SkyBlue"},
117 	{135,206,250,"light sky blue"},
118 	{135,206,250,"LightSkyBlue"},
119 	{70,130,180,"steel blue"},
120 	{70,130,180,"SteelBlue"},
121 	{176,196,222,"light steel blue"},
122 	{176,196,222,"LightSteelBlue"},
123 	{173,216,230,"light blue"},
124 	{173,216,230,"LightBlue"},
125 	{176,224,230,"powder blue"},
126 	{176,224,230,"PowderBlue"},
127 	{175,238,238,"pale turquoise"},
128 	{175,238,238,"PaleTurquoise"},
129 	{0,206,209,"dark turquoise"},
130 	{0,206,209,"DarkTurquoise"},
131 	{72,209,204,"medium turquoise"},
132 	{72,209,204,"MediumTurquoise"},
133 	{64,224,208,"turquoise"},
134 	{0,255,255,"cyan"},
135 	{224,255,255,"light cyan"},
136 	{224,255,255,"LightCyan"},
137 	{95,158,160,"cadet blue"},
138 	{95,158,160,"CadetBlue"},
139 	{102,205,170,"medium aquamarine"},
140 	{102,205,170,"MediumAquamarine"},
141 	{127,255,212,"aquamarine"},
142 	{0,100,0,"dark green"},
143 	{0,100,0,"DarkGreen"},
144 	{85,107,47,"dark olive green"},
145 	{85,107,47,"DarkOliveGreen"},
146 	{143,188,143,"dark sea green"},
147 	{143,188,143,"DarkSeaGreen"},
148 	{46,139,87,"sea green"},
149 	{46,139,87,"SeaGreen"},
150 	{60,179,113,"medium sea green"},
151 	{60,179,113,"MediumSeaGreen"},
152 	{32,178,170,"light sea green"},
153 	{32,178,170,"LightSeaGreen"},
154 	{152,251,152,"pale green"},
155 	{152,251,152,"PaleGreen"},
156 	{0,255,127,"spring green"},
157 	{0,255,127,"SpringGreen"},
158 	{124,252,0,"lawn green"},
159 	{124,252,0,"LawnGreen"},
160 	{0,255,0,"green"},
161 	{127,255,0,"chartreuse"},
162 	{0,250,154,"medium spring green"},
163 	{0,250,154,"MediumSpringGreen"},
164 	{173,255,47,"green yellow"},
165 	{173,255,47,"GreenYellow"},
166 	{50,205,50,"lime green"},
167 	{50,205,50,"LimeGreen"},
168 	{154,205,50,"yellow green"},
169 	{154,205,50,"YellowGreen"},
170 	{34,139,34,"forest green"},
171 	{34,139,34,"ForestGreen"},
172 	{107,142,35,"olive drab"},
173 	{107,142,35,"OliveDrab"},
174 	{189,183,107,"dark khaki"},
175 	{189,183,107,"DarkKhaki"},
176 	{240,230,140,"khaki"},
177 	{238,232,170,"pale goldenrod"},
178 	{238,232,170,"PaleGoldenrod"},
179 	{250,250,210,"light goldenrod yellow"},
180 	{250,250,210,"LightGoldenrodYellow"},
181 	{255,255,224,"light yellow"},
182 	{255,255,224,"LightYellow"},
183 	{255,255,0,"yellow"},
184 	{255,215,0,"gold"},
185 	{238,221,130,"light goldenrod"},
186 	{238,221,130,"LightGoldenrod"},
187 	{218,165,32,"goldenrod"},
188 	{184,134,11,"dark goldenrod"},
189 	{184,134,11,"DarkGoldenrod"},
190 	{188,143,143,"rosy brown"},
191 	{188,143,143,"RosyBrown"},
192 	{205,92,92,"indian red"},
193 	{205,92,92,"IndianRed"},
194 	{139,69,19,"saddle brown"},
195 	{139,69,19,"SaddleBrown"},
196 	{160,82,45,"sienna"},
197 	{205,133,63,"peru"},
198 	{222,184,135,"burlywood"},
199 	{245,245,220,"beige"},
200 	{245,222,179,"wheat"},
201 	{244,164,96,"sandy brown"},
202 	{244,164,96,"SandyBrown"},
203 	{210,180,140,"tan"},
204 	{210,105,30,"chocolate"},
205 	{178,34,34,"firebrick"},
206 	{165,42,42,"brown"},
207 	{233,150,122,"dark salmon"},
208 	{233,150,122,"DarkSalmon"},
209 	{250,128,114,"salmon"},
210 	{255,160,122,"light salmon"},
211 	{255,160,122,"LightSalmon"},
212 	{255,165,0,"orange"},
213 	{255,140,0,"dark orange"},
214 	{255,140,0,"DarkOrange"},
215 	{255,127,80,"coral"},
216 	{240,128,128,"light coral"},
217 	{240,128,128,"LightCoral"},
218 	{255,99,71,"tomato"},
219 	{255,69,0,"orange red"},
220 	{255,69,0,"OrangeRed"},
221 	{255,0,0,"red"},
222 	{255,105,180,"hot pink"},
223 	{255,105,180,"HotPink"},
224 	{255,20,147,"deep pink"},
225 	{255,20,147,"DeepPink"},
226 	{255,192,203,"pink"},
227 	{255,182,193,"light pink"},
228 	{255,182,193,"LightPink"},
229 	{219,112,147,"pale violet red"},
230 	{219,112,147,"PaleVioletRed"},
231 	{176,48,96,"maroon"},
232 	{199,21,133,"medium violet red"},
233 	{199,21,133,"MediumVioletRed"},
234 	{208,32,144,"violet red"},
235 	{208,32,144,"VioletRed"},
236 	{255,0,255,"magenta"},
237 	{238,130,238,"violet"},
238 	{221,160,221,"plum"},
239 	{218,112,214,"orchid"},
240 	{186,85,211,"medium orchid"},
241 	{186,85,211,"MediumOrchid"},
242 	{153,50,204,"dark orchid"},
243 	{153,50,204,"DarkOrchid"},
244 	{148,0,211,"dark violet"},
245 	{148,0,211,"DarkViolet"},
246 	{138,43,226,"blue violet"},
247 	{138,43,226,"BlueViolet"},
248 	{160,32,240,"purple"},
249 	{147,112,219,"medium purple"},
250 	{147,112,219,"MediumPurple"},
251 	{216,191,216,"thistle"},
252 	{255,250,250,"snow1"},
253 	{238,233,233,"snow2"},
254 	{205,201,201,"snow3"},
255 	{139,137,137,"snow4"},
256 	{255,245,238,"seashell1"},
257 	{238,229,222,"seashell2"},
258 	{205,197,191,"seashell3"},
259 	{139,134,130,"seashell4"},
260 	{255,239,219,"AntiqueWhite1"},
261 	{238,223,204,"AntiqueWhite2"},
262 	{205,192,176,"AntiqueWhite3"},
263 	{139,131,120,"AntiqueWhite4"},
264 	{255,228,196,"bisque1"},
265 	{238,213,183,"bisque2"},
266 	{205,183,158,"bisque3"},
267 	{139,125,107,"bisque4"},
268 	{255,218,185,"PeachPuff1"},
269 	{238,203,173,"PeachPuff2"},
270 	{205,175,149,"PeachPuff3"},
271 	{139,119,101,"PeachPuff4"},
272 	{255,222,173,"NavajoWhite1"},
273 	{238,207,161,"NavajoWhite2"},
274 	{205,179,139,"NavajoWhite3"},
275 	{139,121,94,"NavajoWhite4"},
276 	{255,250,205,"LemonChiffon1"},
277 	{238,233,191,"LemonChiffon2"},
278 	{205,201,165,"LemonChiffon3"},
279 	{139,137,112,"LemonChiffon4"},
280 	{255,248,220,"cornsilk1"},
281 	{238,232,205,"cornsilk2"},
282 	{205,200,177,"cornsilk3"},
283 	{139,136,120,"cornsilk4"},
284 	{255,255,240,"ivory1"},
285 	{238,238,224,"ivory2"},
286 	{205,205,193,"ivory3"},
287 	{139,139,131,"ivory4"},
288 	{240,255,240,"honeydew1"},
289 	{224,238,224,"honeydew2"},
290 	{193,205,193,"honeydew3"},
291 	{131,139,131,"honeydew4"},
292 	{255,240,245,"LavenderBlush1"},
293 	{238,224,229,"LavenderBlush2"},
294 	{205,193,197,"LavenderBlush3"},
295 	{139,131,134,"LavenderBlush4"},
296 	{255,228,225,"MistyRose1"},
297 	{238,213,210,"MistyRose2"},
298 	{205,183,181,"MistyRose3"},
299 	{139,125,123,"MistyRose4"},
300 	{240,255,255,"azure1"},
301 	{224,238,238,"azure2"},
302 	{193,205,205,"azure3"},
303 	{131,139,139,"azure4"},
304 	{131,111,255,"SlateBlue1"},
305 	{122,103,238,"SlateBlue2"},
306 	{105,89,205,"SlateBlue3"},
307 	{71,60,139,"SlateBlue4"},
308 	{72,118,255,"RoyalBlue1"},
309 	{67,110,238,"RoyalBlue2"},
310 	{58,95,205,"RoyalBlue3"},
311 	{39,64,139,"RoyalBlue4"},
312 	{0,0,255,"blue1"},
313 	{0,0,238,"blue2"},
314 	{0,0,205,"blue3"},
315 	{0,0,139,"blue4"},
316 	{30,144,255,"DodgerBlue1"},
317 	{28,134,238,"DodgerBlue2"},
318 	{24,116,205,"DodgerBlue3"},
319 	{16,78,139,"DodgerBlue4"},
320 	{99,184,255,"SteelBlue1"},
321 	{92,172,238,"SteelBlue2"},
322 	{79,148,205,"SteelBlue3"},
323 	{54,100,139,"SteelBlue4"},
324 	{0,191,255,"DeepSkyBlue1"},
325 	{0,178,238,"DeepSkyBlue2"},
326 	{0,154,205,"DeepSkyBlue3"},
327 	{0,104,139,"DeepSkyBlue4"},
328 	{135,206,255,"SkyBlue1"},
329 	{126,192,238,"SkyBlue2"},
330 	{108,166,205,"SkyBlue3"},
331 	{74,112,139,"SkyBlue4"},
332 	{176,226,255,"LightSkyBlue1"},
333 	{164,211,238,"LightSkyBlue2"},
334 	{141,182,205,"LightSkyBlue3"},
335 	{96,123,139,"LightSkyBlue4"},
336 	{198,226,255,"SlateGray1"},
337 	{185,211,238,"SlateGray2"},
338 	{159,182,205,"SlateGray3"},
339 	{108,123,139,"SlateGray4"},
340 	{202,225,255,"LightSteelBlue1"},
341 	{188,210,238,"LightSteelBlue2"},
342 	{162,181,205,"LightSteelBlue3"},
343 	{110,123,139,"LightSteelBlue4"},
344 	{191,239,255,"LightBlue1"},
345 	{178,223,238,"LightBlue2"},
346 	{154,192,205,"LightBlue3"},
347 	{104,131,139,"LightBlue4"},
348 	{224,255,255,"LightCyan1"},
349 	{209,238,238,"LightCyan2"},
350 	{180,205,205,"LightCyan3"},
351 	{122,139,139,"LightCyan4"},
352 	{187,255,255,"PaleTurquoise1"},
353 	{174,238,238,"PaleTurquoise2"},
354 	{150,205,205,"PaleTurquoise3"},
355 	{102,139,139,"PaleTurquoise4"},
356 	{152,245,255,"CadetBlue1"},
357 	{142,229,238,"CadetBlue2"},
358 	{122,197,205,"CadetBlue3"},
359 	{83,134,139,"CadetBlue4"},
360 	{0,245,255,"turquoise1"},
361 	{0,229,238,"turquoise2"},
362 	{0,197,205,"turquoise3"},
363 	{0,134,139,"turquoise4"},
364 	{0,255,255,"cyan1"},
365 	{0,238,238,"cyan2"},
366 	{0,205,205,"cyan3"},
367 	{0,139,139,"cyan4"},
368 	{151,255,255,"DarkSlateGray1"},
369 	{141,238,238,"DarkSlateGray2"},
370 	{121,205,205,"DarkSlateGray3"},
371 	{82,139,139,"DarkSlateGray4"},
372 	{127,255,212,"aquamarine1"},
373 	{118,238,198,"aquamarine2"},
374 	{102,205,170,"aquamarine3"},
375 	{69,139,116,"aquamarine4"},
376 	{193,255,193,"DarkSeaGreen1"},
377 	{180,238,180,"DarkSeaGreen2"},
378 	{155,205,155,"DarkSeaGreen3"},
379 	{105,139,105,"DarkSeaGreen4"},
380 	{84,255,159,"SeaGreen1"},
381 	{78,238,148,"SeaGreen2"},
382 	{67,205,128,"SeaGreen3"},
383 	{46,139,87,"SeaGreen4"},
384 	{154,255,154,"PaleGreen1"},
385 	{144,238,144,"PaleGreen2"},
386 	{124,205,124,"PaleGreen3"},
387 	{84,139,84,"PaleGreen4"},
388 	{0,255,127,"SpringGreen1"},
389 	{0,238,118,"SpringGreen2"},
390 	{0,205,102,"SpringGreen3"},
391 	{0,139,69,"SpringGreen4"},
392 	{0,255,0,"green1"},
393 	{0,238,0,"green2"},
394 	{0,205,0,"green3"},
395 	{0,139,0,"green4"},
396 	{127,255,0,"chartreuse1"},
397 	{118,238,0,"chartreuse2"},
398 	{102,205,0,"chartreuse3"},
399 	{69,139,0,"chartreuse4"},
400 	{192,255,62,"OliveDrab1"},
401 	{179,238,58,"OliveDrab2"},
402 	{154,205,50,"OliveDrab3"},
403 	{105,139,34,"OliveDrab4"},
404 	{202,255,112,"DarkOliveGreen1"},
405 	{188,238,104,"DarkOliveGreen2"},
406 	{162,205,90,"DarkOliveGreen3"},
407 	{110,139,61,"DarkOliveGreen4"},
408 	{255,246,143,"khaki1"},
409 	{238,230,133,"khaki2"},
410 	{205,198,115,"khaki3"},
411 	{139,134,78,"khaki4"},
412 	{255,236,139,"LightGoldenrod1"},
413 	{238,220,130,"LightGoldenrod2"},
414 	{205,190,112,"LightGoldenrod3"},
415 	{139,129,76,"LightGoldenrod4"},
416 	{255,255,224,"LightYellow1"},
417 	{238,238,209,"LightYellow2"},
418 	{205,205,180,"LightYellow3"},
419 	{139,139,122,"LightYellow4"},
420 	{255,255,0,"yellow1"},
421 	{238,238,0,"yellow2"},
422 	{205,205,0,"yellow3"},
423 	{139,139,0,"yellow4"},
424 	{255,215,0,"gold1"},
425 	{238,201,0,"gold2"},
426 	{205,173,0,"gold3"},
427 	{139,117,0,"gold4"},
428 	{255,193,37,"goldenrod1"},
429 	{238,180,34,"goldenrod2"},
430 	{205,155,29,"goldenrod3"},
431 	{139,105,20,"goldenrod4"},
432 	{255,185,15,"DarkGoldenrod1"},
433 	{238,173,14,"DarkGoldenrod2"},
434 	{205,149,12,"DarkGoldenrod3"},
435 	{139,101,8,"DarkGoldenrod4"},
436 	{255,193,193,"RosyBrown1"},
437 	{238,180,180,"RosyBrown2"},
438 	{205,155,155,"RosyBrown3"},
439 	{139,105,105,"RosyBrown4"},
440 	{255,106,106,"IndianRed1"},
441 	{238,99,99,"IndianRed2"},
442 	{205,85,85,"IndianRed3"},
443 	{139,58,58,"IndianRed4"},
444 	{255,130,71,"sienna1"},
445 	{238,121,66,"sienna2"},
446 	{205,104,57,"sienna3"},
447 	{139,71,38,"sienna4"},
448 	{255,211,155,"burlywood1"},
449 	{238,197,145,"burlywood2"},
450 	{205,170,125,"burlywood3"},
451 	{139,115,85,"burlywood4"},
452 	{255,231,186,"wheat1"},
453 	{238,216,174,"wheat2"},
454 	{205,186,150,"wheat3"},
455 	{139,126,102,"wheat4"},
456 	{255,165,79,"tan1"},
457 	{238,154,73,"tan2"},
458 	{205,133,63,"tan3"},
459 	{139,90,43,"tan4"},
460 	{255,127,36,"chocolate1"},
461 	{238,118,33,"chocolate2"},
462 	{205,102,29,"chocolate3"},
463 	{139,69,19,"chocolate4"},
464 	{255,48,48,"firebrick1"},
465 	{238,44,44,"firebrick2"},
466 	{205,38,38,"firebrick3"},
467 	{139,26,26,"firebrick4"},
468 	{255,64,64,"brown1"},
469 	{238,59,59,"brown2"},
470 	{205,51,51,"brown3"},
471 	{139,35,35,"brown4"},
472 	{255,140,105,"salmon1"},
473 	{238,130,98,"salmon2"},
474 	{205,112,84,"salmon3"},
475 	{139,76,57,"salmon4"},
476 	{255,160,122,"LightSalmon1"},
477 	{238,149,114,"LightSalmon2"},
478 	{205,129,98,"LightSalmon3"},
479 	{139,87,66,"LightSalmon4"},
480 	{255,165,0,"orange1"},
481 	{238,154,0,"orange2"},
482 	{205,133,0,"orange3"},
483 	{139,90,0,"orange4"},
484 	{255,127,0,"DarkOrange1"},
485 	{238,118,0,"DarkOrange2"},
486 	{205,102,0,"DarkOrange3"},
487 	{139,69,0,"DarkOrange4"},
488 	{255,114,86,"coral1"},
489 	{238,106,80,"coral2"},
490 	{205,91,69,"coral3"},
491 	{139,62,47,"coral4"},
492 	{255,99,71,"tomato1"},
493 	{238,92,66,"tomato2"},
494 	{205,79,57,"tomato3"},
495 	{139,54,38,"tomato4"},
496 	{255,69,0,"OrangeRed1"},
497 	{238,64,0,"OrangeRed2"},
498 	{205,55,0,"OrangeRed3"},
499 	{139,37,0,"OrangeRed4"},
500 	{255,0,0,"red1"},
501 	{238,0,0,"red2"},
502 	{205,0,0,"red3"},
503 	{139,0,0,"red4"},
504 	{255,20,147,"DeepPink1"},
505 	{238,18,137,"DeepPink2"},
506 	{205,16,118,"DeepPink3"},
507 	{139,10,80,"DeepPink4"},
508 	{255,110,180,"HotPink1"},
509 	{238,106,167,"HotPink2"},
510 	{205,96,144,"HotPink3"},
511 	{139,58,98,"HotPink4"},
512 	{255,181,197,"pink1"},
513 	{238,169,184,"pink2"},
514 	{205,145,158,"pink3"},
515 	{139,99,108,"pink4"},
516 	{255,174,185,"LightPink1"},
517 	{238,162,173,"LightPink2"},
518 	{205,140,149,"LightPink3"},
519 	{139,95,101,"LightPink4"},
520 	{255,130,171,"PaleVioletRed1"},
521 	{238,121,159,"PaleVioletRed2"},
522 	{205,104,137,"PaleVioletRed3"},
523 	{139,71,93,"PaleVioletRed4"},
524 	{255,52,179,"maroon1"},
525 	{238,48,167,"maroon2"},
526 	{205,41,144,"maroon3"},
527 	{139,28,98,"maroon4"},
528 	{255,62,150,"VioletRed1"},
529 	{238,58,140,"VioletRed2"},
530 	{205,50,120,"VioletRed3"},
531 	{139,34,82,"VioletRed4"},
532 	{255,0,255,"magenta1"},
533 	{238,0,238,"magenta2"},
534 	{205,0,205,"magenta3"},
535 	{139,0,139,"magenta4"},
536 	{255,131,250,"orchid1"},
537 	{238,122,233,"orchid2"},
538 	{205,105,201,"orchid3"},
539 	{139,71,137,"orchid4"},
540 	{255,187,255,"plum1"},
541 	{238,174,238,"plum2"},
542 	{205,150,205,"plum3"},
543 	{139,102,139,"plum4"},
544 	{224,102,255,"MediumOrchid1"},
545 	{209,95,238,"MediumOrchid2"},
546 	{180,82,205,"MediumOrchid3"},
547 	{122,55,139,"MediumOrchid4"},
548 	{191,62,255,"DarkOrchid1"},
549 	{178,58,238,"DarkOrchid2"},
550 	{154,50,205,"DarkOrchid3"},
551 	{104,34,139,"DarkOrchid4"},
552 	{155,48,255,"purple1"},
553 	{145,44,238,"purple2"},
554 	{125,38,205,"purple3"},
555 	{85,26,139,"purple4"},
556 	{171,130,255,"MediumPurple1"},
557 	{159,121,238,"MediumPurple2"},
558 	{137,104,205,"MediumPurple3"},
559 	{93,71,139,"MediumPurple4"},
560 	{255,225,255,"thistle1"},
561 	{238,210,238,"thistle2"},
562 	{205,181,205,"thistle3"},
563 	{139,123,139,"thistle4"},
564 	{0,0,0,"gray0"},
565 	{0,0,0,"grey0"},
566 	{3,3,3,"gray1"},
567 	{3,3,3,"grey1"},
568 	{5,5,5,"gray2"},
569 	{5,5,5,"grey2"},
570 	{8,8,8,"gray3"},
571 	{8,8,8,"grey3"},
572 	{10,10,10,"gray4"},
573 	{10,10,10,"grey4"},
574 	{13,13,13,"gray5"},
575 	{13,13,13,"grey5"},
576 	{15,15,15,"gray6"},
577 	{15,15,15,"grey6"},
578 	{18,18,18,"gray7"},
579 	{18,18,18,"grey7"},
580 	{20,20,20,"gray8"},
581 	{20,20,20,"grey8"},
582 	{23,23,23,"gray9"},
583 	{23,23,23,"grey9"},
584 	{26,26,26,"gray10"},
585 	{26,26,26,"grey10"},
586 	{28,28,28,"gray11"},
587 	{28,28,28,"grey11"},
588 	{31,31,31,"gray12"},
589 	{31,31,31,"grey12"},
590 	{33,33,33,"gray13"},
591 	{33,33,33,"grey13"},
592 	{36,36,36,"gray14"},
593 	{36,36,36,"grey14"},
594 	{38,38,38,"gray15"},
595 	{38,38,38,"grey15"},
596 	{41,41,41,"gray16"},
597 	{41,41,41,"grey16"},
598 	{43,43,43,"gray17"},
599 	{43,43,43,"grey17"},
600 	{46,46,46,"gray18"},
601 	{46,46,46,"grey18"},
602 	{48,48,48,"gray19"},
603 	{48,48,48,"grey19"},
604 	{51,51,51,"gray20"},
605 	{51,51,51,"grey20"},
606 	{54,54,54,"gray21"},
607 	{54,54,54,"grey21"},
608 	{56,56,56,"gray22"},
609 	{56,56,56,"grey22"},
610 	{59,59,59,"gray23"},
611 	{59,59,59,"grey23"},
612 	{61,61,61,"gray24"},
613 	{61,61,61,"grey24"},
614 	{64,64,64,"gray25"},
615 	{64,64,64,"grey25"},
616 	{66,66,66,"gray26"},
617 	{66,66,66,"grey26"},
618 	{69,69,69,"gray27"},
619 	{69,69,69,"grey27"},
620 	{71,71,71,"gray28"},
621 	{71,71,71,"grey28"},
622 	{74,74,74,"gray29"},
623 	{74,74,74,"grey29"},
624 	{77,77,77,"gray30"},
625 	{77,77,77,"grey30"},
626 	{79,79,79,"gray31"},
627 	{79,79,79,"grey31"},
628 	{82,82,82,"gray32"},
629 	{82,82,82,"grey32"},
630 	{84,84,84,"gray33"},
631 	{84,84,84,"grey33"},
632 	{87,87,87,"gray34"},
633 	{87,87,87,"grey34"},
634 	{89,89,89,"gray35"},
635 	{89,89,89,"grey35"},
636 	{92,92,92,"gray36"},
637 	{92,92,92,"grey36"},
638 	{94,94,94,"gray37"},
639 	{94,94,94,"grey37"},
640 	{97,97,97,"gray38"},
641 	{97,97,97,"grey38"},
642 	{99,99,99,"gray39"},
643 	{99,99,99,"grey39"},
644 	{102,102,102,"gray40"},
645 	{102,102,102,"grey40"},
646 	{105,105,105,"gray41"},
647 	{105,105,105,"grey41"},
648 	{107,107,107,"gray42"},
649 	{107,107,107,"grey42"},
650 	{110,110,110,"gray43"},
651 	{110,110,110,"grey43"},
652 	{112,112,112,"gray44"},
653 	{112,112,112,"grey44"},
654 	{115,115,115,"gray45"},
655 	{115,115,115,"grey45"},
656 	{117,117,117,"gray46"},
657 	{117,117,117,"grey46"},
658 	{120,120,120,"gray47"},
659 	{120,120,120,"grey47"},
660 	{122,122,122,"gray48"},
661 	{122,122,122,"grey48"},
662 	{125,125,125,"gray49"},
663 	{125,125,125,"grey49"},
664 	{127,127,127,"gray50"},
665 	{127,127,127,"grey50"},
666 	{130,130,130,"gray51"},
667 	{130,130,130,"grey51"},
668 	{133,133,133,"gray52"},
669 	{133,133,133,"grey52"},
670 	{135,135,135,"gray53"},
671 	{135,135,135,"grey53"},
672 	{138,138,138,"gray54"},
673 	{138,138,138,"grey54"},
674 	{140,140,140,"gray55"},
675 	{140,140,140,"grey55"},
676 	{143,143,143,"gray56"},
677 	{143,143,143,"grey56"},
678 	{145,145,145,"gray57"},
679 	{145,145,145,"grey57"},
680 	{148,148,148,"gray58"},
681 	{148,148,148,"grey58"},
682 	{150,150,150,"gray59"},
683 	{150,150,150,"grey59"},
684 	{153,153,153,"gray60"},
685 	{153,153,153,"grey60"},
686 	{156,156,156,"gray61"},
687 	{156,156,156,"grey61"},
688 	{158,158,158,"gray62"},
689 	{158,158,158,"grey62"},
690 	{161,161,161,"gray63"},
691 	{161,161,161,"grey63"},
692 	{163,163,163,"gray64"},
693 	{163,163,163,"grey64"},
694 	{166,166,166,"gray65"},
695 	{166,166,166,"grey65"},
696 	{168,168,168,"gray66"},
697 	{168,168,168,"grey66"},
698 	{171,171,171,"gray67"},
699 	{171,171,171,"grey67"},
700 	{173,173,173,"gray68"},
701 	{173,173,173,"grey68"},
702 	{176,176,176,"gray69"},
703 	{176,176,176,"grey69"},
704 	{179,179,179,"gray70"},
705 	{179,179,179,"grey70"},
706 	{181,181,181,"gray71"},
707 	{181,181,181,"grey71"},
708 	{184,184,184,"gray72"},
709 	{184,184,184,"grey72"},
710 	{186,186,186,"gray73"},
711 	{186,186,186,"grey73"},
712 	{189,189,189,"gray74"},
713 	{189,189,189,"grey74"},
714 	{191,191,191,"gray75"},
715 	{191,191,191,"grey75"},
716 	{194,194,194,"gray76"},
717 	{194,194,194,"grey76"},
718 	{196,196,196,"gray77"},
719 	{196,196,196,"grey77"},
720 	{199,199,199,"gray78"},
721 	{199,199,199,"grey78"},
722 	{201,201,201,"gray79"},
723 	{201,201,201,"grey79"},
724 	{204,204,204,"gray80"},
725 	{204,204,204,"grey80"},
726 	{207,207,207,"gray81"},
727 	{207,207,207,"grey81"},
728 	{209,209,209,"gray82"},
729 	{209,209,209,"grey82"},
730 	{212,212,212,"gray83"},
731 	{212,212,212,"grey83"},
732 	{214,214,214,"gray84"},
733 	{214,214,214,"grey84"},
734 	{217,217,217,"gray85"},
735 	{217,217,217,"grey85"},
736 	{219,219,219,"gray86"},
737 	{219,219,219,"grey86"},
738 	{222,222,222,"gray87"},
739 	{222,222,222,"grey87"},
740 	{224,224,224,"gray88"},
741 	{224,224,224,"grey88"},
742 	{227,227,227,"gray89"},
743 	{227,227,227,"grey89"},
744 	{229,229,229,"gray90"},
745 	{229,229,229,"grey90"},
746 	{232,232,232,"gray91"},
747 	{232,232,232,"grey91"},
748 	{235,235,235,"gray92"},
749 	{235,235,235,"grey92"},
750 	{237,237,237,"gray93"},
751 	{237,237,237,"grey93"},
752 	{240,240,240,"gray94"},
753 	{240,240,240,"grey94"},
754 	{242,242,242,"gray95"},
755 	{242,242,242,"grey95"},
756 	{245,245,245,"gray96"},
757 	{245,245,245,"grey96"},
758 	{247,247,247,"gray97"},
759 	{247,247,247,"grey97"},
760 	{250,250,250,"gray98"},
761 	{250,250,250,"grey98"},
762 	{252,252,252,"gray99"},
763 	{252,252,252,"grey99"},
764 	{255,255,255,"gray100"},
765 	{255,255,255,"grey100"},
766 	{0,0,0,"sgi gray 0"},
767 	{0,0,0,"SGIGray0"},
768 	{0,0,0,"sgi grey 0"},
769 	{0,0,0,"SGIGrey0"},
770 	{10,10,10,"sgi gray 4"},
771 	{10,10,10,"SGIGray4"},
772 	{10,10,10,"sgi grey 4"},
773 	{10,10,10,"SGIGrey4"},
774 	{20,20,20,"sgi gray 8"},
775 	{20,20,20,"SGIGray8"},
776 	{20,20,20,"sgi grey 8"},
777 	{20,20,20,"SGIGrey8"},
778 	{30,30,30,"sgi gray 12"},
779 	{30,30,30,"SGIGray12"},
780 	{30,30,30,"sgi grey 12"},
781 	{30,30,30,"SGIGrey12"},
782 	{40,40,40,"sgi gray 16"},
783 	{40,40,40,"SGIGray16"},
784 	{40,40,40,"sgi grey 16"},
785 	{40,40,40,"SGIGrey16"},
786 	{51,51,51,"sgi gray 20"},
787 	{51,51,51,"SGIGray20"},
788 	{51,51,51,"sgi grey 20"},
789 	{51,51,51,"SGIGrey20"},
790 	{61,61,61,"sgi gray 24"},
791 	{61,61,61,"SGIGray24"},
792 	{61,61,61,"sgi grey 24"},
793 	{61,61,61,"SGIGrey24"},
794 	{71,71,71,"sgi gray 28"},
795 	{71,71,71,"SGIGray28"},
796 	{71,71,71,"sgi grey 28"},
797 	{71,71,71,"SGIGrey28"},
798 	{81,81,81,"sgi gray 32"},
799 	{81,81,81,"SGIGray32"},
800 	{81,81,81,"sgi grey 32"},
801 	{81,81,81,"SGIGrey32"},
802 	{91,91,91,"sgi gray 36"},
803 	{91,91,91,"SGIGray36"},
804 	{91,91,91,"sgi grey 36"},
805 	{91,91,91,"SGIGrey36"},
806 	{102,102,102,"sgi gray 40"},
807 	{102,102,102,"SGIGray40"},
808 	{102,102,102,"sgi grey 40"},
809 	{102,102,102,"SGIGrey40"},
810 	{112,112,112,"sgi gray 44"},
811 	{112,112,112,"SGIGray44"},
812 	{112,112,112,"sgi grey 44"},
813 	{112,112,112,"SGIGrey44"},
814 	{122,122,122,"sgi gray 48"},
815 	{122,122,122,"SGIGray48"},
816 	{122,122,122,"sgi grey 48"},
817 	{122,122,122,"SGIGrey48"},
818 	{132,132,132,"sgi gray 52"},
819 	{132,132,132,"SGIGray52"},
820 	{132,132,132,"sgi grey 52"},
821 	{132,132,132,"SGIGrey52"},
822 	{142,142,142,"sgi gray 56"},
823 	{142,142,142,"SGIGray56"},
824 	{142,142,142,"sgi grey 56"},
825 	{142,142,142,"SGIGrey56"},
826 	{153,153,153,"sgi gray 60"},
827 	{153,153,153,"SGIGray60"},
828 	{153,153,153,"sgi grey 60"},
829 	{153,153,153,"SGIGrey60"},
830 	{163,163,163,"sgi gray 64"},
831 	{163,163,163,"SGIGray64"},
832 	{163,163,163,"sgi grey 64"},
833 	{163,163,163,"SGIGrey64"},
834 	{173,173,173,"sgi gray 68"},
835 	{173,173,173,"SGIGray68"},
836 	{173,173,173,"sgi grey 68"},
837 	{173,173,173,"SGIGrey68"},
838 	{183,183,183,"sgi gray 72"},
839 	{183,183,183,"SGIGray72"},
840 	{183,183,183,"sgi grey 72"},
841 	{183,183,183,"SGIGrey72"},
842 	{193,193,193,"sgi gray 76"},
843 	{193,193,193,"SGIGray76"},
844 	{193,193,193,"sgi grey 76"},
845 	{193,193,193,"SGIGrey76"},
846 	{204,204,204,"sgi gray 80"},
847 	{204,204,204,"SGIGray80"},
848 	{204,204,204,"sgi grey 80"},
849 	{204,204,204,"SGIGrey80"},
850 	{214,214,214,"sgi gray 84"},
851 	{214,214,214,"SGIGray84"},
852 	{214,214,214,"sgi grey 84"},
853 	{214,214,214,"SGIGrey84"},
854 	{224,224,224,"sgi gray 88"},
855 	{224,224,224,"SGIGray88"},
856 	{224,224,224,"sgi grey 88"},
857 	{224,224,224,"SGIGrey88"},
858 	{234,234,234,"sgi gray 92"},
859 	{234,234,234,"SGIGray92"},
860 	{234,234,234,"sgi grey 92"},
861 	{234,234,234,"SGIGrey92"},
862 	{244,244,244,"sgi gray 96"},
863 	{244,244,244,"SGIGray96"},
864 	{244,244,244,"sgi grey 96"},
865 	{244,244,244,"SGIGrey96"},
866 	{255,255,255,"sgi gray 100"},
867 	{255,255,255,"SGIGray100"},
868 	{255,255,255,"sgi grey 100"},
869 	{255,255,255,"SGIGrey100"},
870 	{125,158,192,"sgi light blue"},
871 	{125,158,192,"SGILightBlue"},
872 	{85,85,85,"sgi dark gray"},
873 	{85,85,85,"SGIDarkGray"},
874 	{85,85,85,"sgi dark grey"},
875 	{85,85,85,"SGIDarkGrey"},
876 	{198,113,113,"sgi salmon"},
877 	{198,113,113,"SGISalmon"},
878 	{113,198,113,"sgi chartreuse"},
879 	{113,198,113,"SGIChartreuse"},
880 	{142,142,56,"sgi olive drab"},
881 	{142,142,56,"SGIOliveDrab"},
882 	{113,113,198,"sgi slate blue"},
883 	{113,113,198,"SGISlateBlue"},
884 	{142,56,142,"sgi beet"},
885 	{142,56,142,"SGIBeet"},
886 	{56,142,142,"sgi teal"},
887 	{56,142,142,"SGITeal"},
888 	{170,170,170,"sgi light gray"},
889 	{170,170,170,"SGILightGray"},
890 	{170,170,170,"sgi light grey"},
891 	{170,170,170,"SGILightGrey"},
892 	{214,214,214,"sgi very light gray"},
893 	{214,214,214,"SGIVeryLightGray"},
894 	{214,214,214,"sgi very light grey"},
895 	{214,214,214,"SGIVeryLightGrey"},
896 	{132,132,132,"sgi medium gray"},
897 	{132,132,132,"SGIMediumGray"},
898 	{132,132,132,"sgi medium grey"},
899 	{132,132,132,"SGIMediumGrey"},
900 	{40,40,40,"sgi very dark gray"},
901 	{40,40,40,"SGIVeryDarkGray"},
902 	{40,40,40,"sgi very dark grey"},
903 	{40,40,40,"SGIVeryDarkGrey"},
904 	{197,193,170,"sgi bright gray"},
905 	{197,193,170,"SGIBrightGray"},
906 	{197,193,170,"sgi bright grey"},
907 	{197,193,170,"SGIBrightGrey"},
908 	{75,0,130,"Indigo"},
909 	{220,20,60,"Crimson"},
910 };
911 
912 #define NUMBER(x)	(sizeof(x) / sizeof(x[0]))
913 
dumpCMAP(char * title,char * start,int size,unsigned char data[GIF_MAXCOLORS][3])914 static void dumpCMAP(char *title, char *start,
915 			int size, unsigned char data[GIF_MAXCOLORS][3])
916 {
917 	int	i;
918 	int	j;
919 
920 	if (size == 0)
921 		return;
922 	printf("%s\n", title);
923 
924 	for (i = 0; i < size; i++) {
925 		int	r = data[i][0];
926 		int	g = data[i][1];
927 		int	b = data[i][2];
928 		char	*name = NULL;
929 
930 		for (j = 0; j < NUMBER(colorTable); j++) {
931 			if (colorTable[j].r == r &&
932 			    colorTable[j].g == g &&
933 			    colorTable[j].b == b) {
934 				name = colorTable[j].name;
935 				break;
936 			}
937 		}
938 
939 		printf("%s%3d: %3d %3d %3d (0x%02x 0x%02x 0x%02x) %s\n",
940 			start, i+1,
941 			data[i][0], data[i][1], data[i][2],
942 			data[i][0], data[i][1], data[i][2],
943 			name == NULL ? "" : name);
944 	}
945 }
946 
GIFDump(char * name,GIFStream * stream,int commentsOnly)947 static void GIFDump(char *name, GIFStream *stream, int commentsOnly)
948 {
949 	GIFData	*cur;
950 
951 	if (stream == NULL) {
952 		printf("GIF Image %s -- BAD Image\n", name);
953 		return;
954 	}
955 
956 	printf("GIF Image %s (%dx%d)\n",
957 		name == NULL ? "STDIN" : name,
958 		stream->width, stream->height);
959 	if (!commentsOnly) {
960 		if (stream->cmapSize != 0)
961 			dumpCMAP("Image Colormap", "",
962 				stream->cmapSize, stream->cmapData);
963 	}
964 
965 	for (cur = stream->data; cur != NULL; cur = cur->next) {
966 		if (commentsOnly && cur->type != gif_comment)
967 			continue;
968 		switch (cur->type) {
969 		case gif_image:
970 			printf("  %sImage at %d,%d size %dx%d\n",
971 				cur->data.image.interlaced ?
972 					"Interlaced " : "",
973 				cur->x, cur->y, cur->width, cur->height);
974 			if (cur->info.transparent >= 0) {
975 				printf("\tTransparent pixel = %d\n",
976 					cur->info.transparent + 1);
977 			}
978 			if (cur->data.image.cmapSize != 0) {
979 				dumpCMAP("  Image Colormap", "\t",
980 					cur->data.image.cmapSize,
981 					cur->data.image.cmapData);
982 			}
983 			break;
984 		case gif_comment:
985 			printf("  Comment: %*.*s\n",
986 				cur->data.comment.len,
987 				cur->data.comment.len,
988 				cur->data.comment.text);
989 			break;
990 		case gif_text:
991 			printf("  Image Text (at %d,%d): %*.*s\n",
992 				cur->x, cur->y,
993 				cur->data.text.len,
994 				cur->data.text.len,
995 				cur->data.text.text);
996 			break;
997 		}
998 	}
999 }
1000 
GIFFree(GIFStream * stream)1001 static void GIFFree(GIFStream *stream)
1002 {
1003 	GIFData	*cur, *nxt;
1004 
1005 	if (stream == NULL)
1006 		return;
1007 	cur = stream->data;
1008 
1009 	while (cur != NULL) {
1010 		nxt = cur->next;
1011 		switch (cur->type) {
1012 		case gif_image:
1013 			free(cur->data.image.data);
1014 			break;
1015 		case gif_comment:
1016 			free(cur->data.comment.text);
1017 			break;
1018 		case gif_text:
1019 			free(cur->data.text.text);
1020 			break;
1021 		}
1022 		free(cur);
1023 		cur = nxt;
1024 	}
1025 	free(stream);
1026 }
1027 
setInterlace(GIFStream * stream,int flg)1028 static void setInterlace(GIFStream *stream, int flg)
1029 {
1030 	GIFData	*cur = stream->data;
1031 
1032 	for ( ; cur != NULL; cur = cur->next) {
1033 		if (cur->type == gif_image)
1034 			cur->data.image.interlaced = flg > 0;
1035 	}
1036 }
toval(char * s)1037 static int toval(char *s)
1038 {
1039 	if (s[0] == '0' && s[1] == 'x') {
1040 		int	v;
1041 		sscanf(s, "0x%x", &v);
1042 		return v;
1043 	} else {
1044 		return atoi(s);
1045 	}
1046 }
1047 
matchCMAP(char * tpix,int size,unsigned char data[256][3])1048 static int matchCMAP(char *tpix, int size, unsigned char data[256][3])
1049 {
1050 	char	buf[80], *cp;
1051 	char	c[3][20];
1052 	int	r, g, b;
1053 	int	i;
1054 	int	min, dist;
1055 
1056 	if (strchr(tpix, ',') == NULL) {
1057 		int	v = atoi(tpix);
1058 
1059 		if (v != 0) {
1060 			if (v <= size && v > 0)
1061 				return v - 1;
1062 			return -1;
1063 		}
1064 		for (v = 0; v < NUMBER(colorTable); v++) {
1065 			if (strcasecmp(colorTable[v].name, tpix) == 0)
1066 				break;
1067 		}
1068 		if (v == NUMBER(colorTable))
1069 			return -2;
1070 
1071 		sprintf(buf, "%d,%d,%d",
1072 			colorTable[v].r, colorTable[v].g, colorTable[v].b);
1073 		tpix = buf;
1074 	}
1075 
1076 	for (cp = tpix, i = 0; i < 3; i++, cp = strchr(cp, ',')) {
1077 		char	*x;
1078 
1079 		if (cp == NULL)
1080 			break;
1081 		if (*cp == ',')
1082 			cp++;
1083 
1084 		strncpy(c[i], cp, 10);
1085 		if ((x = strchr(c[i], ',')) != NULL)
1086 			*x = '\0';
1087 	}
1088 
1089 	if (i != 3)
1090 		return -3;
1091 
1092 	r = toval(c[0]);
1093 	g = toval(c[1]);
1094 	b = toval(c[2]);
1095 
1096 	min = -3;
1097 	dist = 255 * 255 * 3;
1098 
1099 	for (i = 0; i < size && dist != 0; i++) {
1100 		int	d;
1101 		int	dr = data[i][0] - r;
1102 		int	dg = data[i][1] - g;
1103 		int	db = data[i][2] - b;
1104 
1105 		if (dr == 0 && dg == 0 && db == 0)
1106 			return i;
1107 
1108 		d = dr * dr + dg * dg + db * b;
1109 
1110 		if (d < dist) {
1111 			dist = d;
1112 			min = i;
1113 		}
1114 	}
1115 	if (dist > 0)
1116 		fprintf(stderr, "No exact match using pixel %d\n", min + 1);
1117 	return min;
1118 }
1119 
setTransparent(GIFStream * stream,char * tpix)1120 static void setTransparent(GIFStream *stream, char *tpix)
1121 {
1122 	GIFData	*cur = stream->data;
1123 	int	cnt = 0;
1124 
1125 	for ( ; cur != NULL; cur = cur->next) {
1126 		int	idx;
1127 
1128 		if (cur->type != gif_image)
1129 			continue;
1130 		cnt++;
1131 		if (cur->info.transparent != -1) {
1132 			fprintf(stderr,"Warning: image %d was already transparent\n");
1133 		}
1134 
1135 		if (cur->data.image.cmapSize != 0) {
1136 			idx = matchCMAP(tpix, cur->data.image.cmapSize,
1137 					cur->data.image.cmapData);
1138 		} else {
1139 			idx = matchCMAP(tpix, stream->cmapSize,
1140 					stream->cmapData);
1141 		}
1142 		if (idx == -1) {
1143 			fprintf(stderr,"Warning: Pixel index larger than colormap size\n");
1144 		} else if (idx == -2) {
1145 			fprintf(stderr,"Warning: No such color known (%s)\n", tpix);
1146 		} else {
1147 			cur->info.transparent = idx;
1148 		}
1149 	}
1150 }
1151 
addComments(GIFStream * stream,char * text)1152 static void addComments(GIFStream *stream, char *text)
1153 {
1154 	GIFData	*n;
1155 
1156 	if (text == NULL)
1157 		return;
1158 
1159 	n = (GIFData *)malloc(sizeof(GIFData));
1160 	n->type = gif_comment;
1161 	n->info.transparent = -1;
1162 	n->info.delayTime = 0;
1163 	n->info.inputFlag = 0;
1164 	n->info.disposal = gif_no_disposal;
1165 	n->width = n->height = n->x = n->y = 0;
1166 	n->data.comment.len  = strlen(text);
1167 	n->data.comment.text = (char*)malloc(strlen(text));
1168 	strcpy(n->data.comment.text, text);
1169 
1170 	n->next = stream->data;
1171 	stream->data = n;
1172 }
1173 
stripComment(GIFStream * stream)1174 static void stripComment(GIFStream *stream)
1175 {
1176 	GIFData	*nxt, *cur, *prev = NULL;
1177 
1178 	for (cur = stream->data; cur != NULL; cur = nxt) {
1179 		nxt = cur->next;
1180 
1181 		if (cur->type == gif_comment) {
1182 			if (prev == NULL)
1183 				stream->data = cur->next;
1184 			else
1185 				prev->next = cur->next;
1186 			free(cur->data.comment.text);
1187 			free(cur);
1188 		} else {
1189 			prev = cur;
1190 		}
1191 	}
1192 }
1193 
info()1194 static void info()
1195 {
1196 	printf("Copyright 1994, Home Pages, Inc.\n");
1197 	printf("\n");
1198 	printf("Permission to use, copy, and distribute GIFTOOL in its entirety, for\n");
1199 	printf("non-commercial purposes, is hereby granted without fee, provided that\n");
1200 	printf("this license information and copyright notice appear in all copies.\n");
1201 	printf("\n");
1202 	printf("Note that distributing GIFTOOL 'bundled' in with ANY product is considered\n");
1203 	printf("to be a 'commercial purpose'.\n");
1204 	printf("\n");
1205 	printf("Also note that any copies of GIFTOOL that are distributed MUST be built\n");
1206 	printf("and/or configured to be in their 'unregistered copy' mode, so that it\n");
1207 	printf("is made obvious to the user that GIFTOOL is shareware, and that they should\n");
1208 	printf("consider donating, or at least reading this License Info.\n");
1209 	printf("\n");
1210 	printf("Permission to use, copy, and distribute GIFTOOL in its entirety, for\n");
1211 	printf("non-commercial purposes, is hereby granted without fee, provided that\n");
1212 	printf("this license information and copyright notice appear in all copies.\n");
1213 	printf("\n");
1214 	printf("Note that distributing GIFTOOL 'bundled' in with ANY product is considered\n");
1215 	printf("to be a 'commercial purpose'.\n");
1216 	printf("\n");
1217 	printf("Also note that any copies of GIFTOOL that are distributed MUST be built\n");
1218 	printf("and/or configured to be in their 'unregistered copy' mode, so that it\n");
1219 	printf("is made obvious to the user that GIFTOOL is shareware, and that they should\n");
1220 	printf("consider donating, or at least reading this License Info.\n");
1221 	printf("\n");
1222 	printf("The software may be modified for your own purposes, but modified\n");
1223 	printf("versions may NOT be distributed without prior consent of the author.\n");
1224 	printf("\n");
1225 	printf("This software is provided 'as-is', without any express or implied\n");
1226 	printf("warranty.  In no event will the author be held liable for any damages\n");
1227 	printf("arising from the use of this software.\n");
1228 	printf("\n");
1229 	printf("If you would like to do something with GIFTOOL that this copyright\n");
1230 	printf("prohibits (such as distributing it with a commercial product,\n");
1231 	printf("using portions of the source in some other program, etc.), please\n");
1232 	printf("contact the author.  Arrangements can probably easily be worked out.\n");
1233 	printf("\n");
1234 	printf("GIFTOOL is shareware for PERSONAL USE only.  You may use GIFTOOL for \n");
1235 	printf("your own amusement, and if you find it nifty, useful, generally cool, \n");
1236 	printf("or of some value to you, your non-deductable donation would be greatly\n");
1237 	printf("appreciated.  $10 is the suggested donation, though, of course,\n");
1238 	printf("larger donations are quite welcome.  \n");
1239 	printf("\n");
1240 	printf("Commercial, government, and institutional users MUST register their\n");
1241 	printf("copies of GIFTOOL, for the exceedingly REASONABLE price of just $10 per\n");
1242 	printf("workstation.  Site licenses are available for those who wish to run \n");
1243 	printf("GIFTOOL on a large number of machines.  Please contact Home Pages Inc\n");
1244 	printf("for more details.\n");
1245 	printf("\n");
1246 	printf("   US Mail:  Home Pages, Inc.\n");
1247 	printf("             257 Castro St, Suite 219\n");
1248 	printf("             Mountain View, CA 94041\n");
1249 	printf("\n");
1250 	printf("   Phone:    (415) 903-5353\n");
1251 	printf("   EMail:    support@homepages.com\n");
1252 	exit(0);
1253 }
1254 
usage()1255 static void usage()
1256 {
1257 	fprintf(stderr,"Usage: giftool [options] [file]\n");
1258 	fprintf(stderr,"       giftool (-p|-c|-B) [options] [files...]\n");
1259 	fprintf(stderr,"\t-B\tBatch Mode, read and write the same filename\n");
1260 	fprintf(stderr,"\t-i\tSet GIF Interlace mode ON\n");
1261 	fprintf(stderr,"\t+i\tSet GIF Interlace mode OFF\n");
1262 	fprintf(stderr,"\t-p\tPrint information about file(s)\n");
1263 	fprintf(stderr,"\t-c\tPrint comment information\n");
1264 	fprintf(stderr,"\t+c\tAdd comments to file(s)\n");
1265 	fprintf(stderr,"\t-C\tStrip comment from file(s)\n");
1266 	fprintf(stderr,"\t-o file\tSend output to 'file'\n");
1267 	fprintf(stderr,"\t-rgb name\tUse 'name' as the transparent pixel\n");
1268 	fprintf(stderr,"\t-rgb ##,##,##\ttUse rgb-value as the transparent pixel\n");
1269 	fprintf(stderr,"\t-###\tUsed pixel index as transparent (1 == first colormap entry)\n");
1270 	fprintf(stderr,"\n");
1271 	fprintf(stderr,"For instance you could say 'giftool -B -i *.gif' to convert all your images\nto interlaced GIF files in one easy step.\n");
1272 	fprintf(stderr,"\n");
1273 	fprintf(stderr,"giftool version 1.0 is Copyright 1994 Home Pages, Inc and is shareware.\n");
1274 	fprintf(stderr,"Please, use the -info option to read the licensing information.\n");
1275 	exit(1);
1276 }
1277 
main(int argc,char * argv[])1278 int main(int argc, char *argv[])
1279 {
1280 	int		c, i;
1281 	int		batchMode = 0;
1282 	int		interlace = 0, batchInterlace = 0;
1283 	int		print     = 0;
1284 	int		stripComments = 0, printComments = 0;
1285 	int		addTrans  = 0, optimize = 0;
1286 	char		*transPixel = NULL, *outFile = NULL;
1287 	char		*addComment = NULL;
1288 	GIFStream	*stream;
1289 	int		idx;
1290 	int		stdinMode = 0;
1291 
1292 	for (idx = 1; idx < argc &&
1293 			(argv[idx][0] == '-' || argv[idx][0] == '+'); idx++) {
1294 		char		*arg = argv[idx];
1295 		if (strcmp(arg, "-O") == 0) {
1296 			optimize++;
1297 		} else if (strcmp(arg, "-B") == 0) {
1298 			batchMode = 1;
1299 		} else if (strcmp(arg, "+i") == 0) {
1300 			interlace = -1;
1301 		} else if (strcmp(arg, "-i") == 0) {
1302 			interlace = 1;
1303 		} else if (strcmp(arg, "-c") == 0) {
1304 			printComments = 1;
1305 			print++;
1306 		} else if (strncmp(arg, "+c", 2) == 0) {
1307 			if (strlen(arg) != 2)
1308 				arg += 2;
1309 			else
1310 				arg = argv[++idx];
1311 			if (idx == argc) usage();
1312 			addComment = arg;
1313 		} else if (strcmp(arg, "-C") == 0) {
1314 			stripComments++;
1315 		} else if (strcmp(arg, "-p") == 0) {
1316 			print++;
1317 		} else if (strncmp(arg, "-o", 2) == 0) {
1318 			if (strlen(arg) != 2)
1319 				arg += 2;
1320 			else
1321 				arg = argv[++idx];
1322 			if (idx == argc) usage();
1323 			outFile = arg;
1324 		} else if (strncmp(arg, "-rgb", 4) == 0) {
1325 			if (strlen(arg) != 4)
1326 				arg += 4;
1327 			else
1328 				arg = argv[++idx];
1329 			if (idx == argc) usage();
1330 			if (isdigit(*arg) && strchr(arg, ',') == NULL) {
1331 				static char	buf[24];
1332 				if (idx == argc-2) usage();
1333 				sprintf(buf, "%d,%d,%d", arg, argv[idx+1], argv[idx+2]);
1334 				idx += 2;
1335 				transPixel = buf;
1336 			} else {
1337 				transPixel = arg;
1338 			}
1339 		} else if (isdigit(arg[1])) {
1340 			transPixel = arg+1;
1341 		} else if (strcmp(arg, "--") == 0) {
1342 			idx++;
1343 			break;
1344 		} else if (strcmp(arg, "-info") == 0) {
1345 			info();
1346 		} else {
1347 			if (strcmp(arg, "-help") != 0)
1348 				printf("Unrecognized option %s\n", arg);
1349 			usage();
1350 		}
1351 	}
1352 
1353 	if (outFile != NULL && batchMode) {
1354 		fprintf(stderr,"Error: You can't use both the -o and -B options\n");
1355 		usage();
1356 	}
1357 
1358 	if (!batchMode && !print) {
1359 		if (argc - idx > 1)
1360 			usage();
1361 	}
1362 
1363 	if (idx == argc) {
1364 		argc++;
1365 		stdinMode = 1;
1366 		batchMode = 0;
1367 	}
1368 
1369 	for (i = idx; i < argc; i++) {
1370 		GIFStream	*stream;
1371 		char		*frm;
1372 
1373 		if (stdinMode) {
1374 			stream = GIFReadFP(stdin);
1375 			frm    = NULL;
1376 		} else {
1377 			frm    = argv[i];
1378 			stream = GIFRead(frm);
1379 		}
1380 
1381 		if (print) {
1382 			GIFDump(frm, stream, printComments);
1383 		} else {
1384 			int	status;
1385 			char	*out;
1386 
1387 			if (stripComments)
1388 				stripComment(stream);
1389 			addComments(stream, addComment);
1390 			if (transPixel != NULL)
1391 				setTransparent(stream, transPixel);
1392 			if (interlace != 0)
1393 				setInterlace(stream, interlace);
1394 
1395 			if (batchMode) {
1396 				status = GIFWrite(argv[i], stream, optimize);
1397 				out = argv[i];
1398 			} else {
1399 				if (outFile == NULL) {
1400 					status = GIFWriteFP(stdout, stream, optimize);
1401 					out = "<STDOUT>";
1402 				} else {
1403 					status = GIFWrite(outFile, stream, optimize);
1404 					out = outFile;
1405 				}
1406 			}
1407 			if (status)
1408 				fprintf(stderr, "error writing %s\n", out);
1409 
1410 			GIFFree(stream);
1411 		}
1412 	}
1413 	return 0;
1414 }
1415