1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <ctype.h>
#include <bio.h>
#include "imagefile.h"
void
usage(void)
{
fprint(2, "usage: togif [-l loopcount] [-c 'comment'] [-d Δt (ms)] [-t transparency-index] [file ... [-d Δt] file ...]\n");
exits("usage");
}
#define UNSET (-12345678)
void
main(int argc, char *argv[])
{
Biobuf bout;
Memimage *i, *ni;
int fd, j, dt, trans, loop;
char buf[256];
char *err, *comment, *s;
comment = nil;
dt = -1;
trans = -1;
loop = UNSET;
ARGBEGIN{
case 'l':
s = ARGF();
if(s==nil || (!isdigit((uchar)s[0]) && s[0]!='-'))
usage();
loop = atoi(s);
break;
case 'c':
comment = ARGF();
if(comment == nil)
usage();
break;
case 'd':
s = ARGF();
if(s==nil || !isdigit((uchar)s[0]))
usage();
dt = atoi(s);
break;
case 't':
s = ARGF();
if(s==nil || !isdigit((uchar)s[0]))
usage();
trans = atoi(s);
if(trans > 255)
usage();
break;
default:
usage();
}ARGEND
if(Binit(&bout, 1, OWRITE) < 0)
sysfatal("Binit failed: %r");
memimageinit();
err = nil;
if(argc == 0){
i = readmemimage(0);
if(i == nil)
sysfatal("reading input: %r");
ni = memonechan(i);
if(ni == nil)
sysfatal("converting image to RGBV: %r");
if(i != ni){
freememimage(i);
i = ni;
}
err = memstartgif(&bout, i, -1);
if(err == nil){
if(comment)
err = memwritegif(&bout, i, comment, dt, trans);
else{
snprint(buf, sizeof buf, "Converted by Plan 9 from <stdin>");
err = memwritegif(&bout, i, buf, dt, trans);
}
}
}else{
if(loop == UNSET){
if(argc == 1)
loop = -1; /* no loop for single image */
else
loop = 0; /* the default case: 0 means infinite loop */
}
for(j=0; j<argc; j++){
if(argv[j][0] == '-' && argv[j][1]=='d'){
/* time change */
if(argv[j][2] == '\0'){
s = argv[++j];
if(j == argc)
usage();
}else
s = &argv[j][2];
if(!isdigit((uchar)s[0]))
usage();
dt = atoi(s);
if(j == argc-1) /* last argument must be file */
usage();
continue;
}
fd = open(argv[j], OREAD);
if(fd < 0)
sysfatal("can't open %s: %r", argv[j]);
i = readmemimage(fd);
if(i == nil)
sysfatal("can't readimage %s: %r", argv[j]);
close(fd);
ni = memonechan(i);
if(ni == nil)
sysfatal("converting image to RGBV: %r");
if(i != ni){
freememimage(i);
i = ni;
}
if(j == 0){
err = memstartgif(&bout, i, loop);
if(err != nil)
break;
}
if(comment)
err = memwritegif(&bout, i, comment, dt, trans);
else{
snprint(buf, sizeof buf, "Converted by Plan 9 from %s", argv[j]);
err = memwritegif(&bout, i, buf, dt, trans);
}
if(err != nil)
break;
freememimage(i);
comment = nil;
}
}
memendgif(&bout);
if(err != nil)
fprint(2, "togif: %s\n", err);
exits(err);
}
|