Blob


1 /*
2 * graphics file reading for page
3 */
5 #include <u.h>
6 #include <libc.h>
7 #include <draw.h>
8 #include <cursor.h>
9 #include <event.h>
10 #include <bio.h>
11 #include "page.h"
13 typedef struct Convert Convert;
14 typedef struct GfxInfo GfxInfo;
15 typedef struct Graphic Graphic;
17 struct Convert {
18 char *name;
19 char *cmd;
20 char *truecmd; /* cmd for true color */
21 };
23 struct GfxInfo {
24 Graphic *g;
25 };
27 struct Graphic {
28 int type;
29 char *name;
30 uchar *buf; /* if stdin */
31 int nbuf;
32 };
34 enum {
35 Ipic,
36 Itiff,
37 Ijpeg,
38 Igif,
39 Iinferno,
40 Ifax,
41 Icvt2pic,
42 Iplan9bm,
43 Iccittg4,
44 Ippm,
45 Ipng,
46 Iyuv,
47 Ibmp
48 };
50 /*
51 * N.B. These commands need to read stdin if %a is replaced
52 * with an empty string.
53 */
54 Convert cvt[] = {
55 [Ipic] { "plan9", "fb/3to1 rgbv %a |fb/pcp -tplan9" },
56 [Itiff] { "tiff", "fb/tiff2pic %a | fb/3to1 rgbv | fb/pcp -tplan9" },
57 [Iplan9bm] { "plan9bm", nil },
58 [Ijpeg] { "jpeg", "jpg -9 %a", "jpg -t9 %a" },
59 [Igif] { "gif", "gif -9 %a", "gif -t9 %a" },
60 [Iinferno] { "inferno", nil },
61 [Ifax] { "fax", "aux/g3p9bit -g %a" },
62 [Icvt2pic] { "unknown", "fb/cvt2pic %a |fb/3to1 rgbv" },
63 [Ippm] { "ppm", "ppm -9 %a", "ppm -t9 %a" },
64 /* ``temporary'' hack for hobby */
65 [Iccittg4] { "ccitt-g4", "cat %a|rx nslocum /usr/lib/ocr/bin/bcp -M|fb/pcp -tcompressed -l0" },
66 [Ipng] { "png", "png -9 %a", "png -t9 %a" },
67 [Iyuv] { "yuv", "yuv -9 %a", "yuv -t9 %a" },
68 [Ibmp] { "bmp", "bmp -9 %a", "bmp -t9 %a" }
69 };
71 static Image* convert(Graphic*);
72 static Image* gfxdrawpage(Document *d, int page);
73 static char* gfxpagename(Document*, int);
74 static int spawnrc(char*, uchar*, int);
75 static void waitrc(void);
76 static int spawnpost(int);
77 static int addpage(Document*, char*);
78 static int rmpage(Document*, int);
79 static int genaddpage(Document*, char*, uchar*, int);
81 static char*
82 gfxpagename(Document *doc, int page)
83 {
84 GfxInfo *gfx = doc->extra;
85 return gfx->g[page].name;
86 }
88 static Image*
89 gfxdrawpage(Document *doc, int page)
90 {
91 GfxInfo *gfx = doc->extra;
92 return convert(gfx->g+page);
93 }
95 Document*
96 initgfx(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
97 {
98 GfxInfo *gfx;
99 Document *doc;
100 int i;
102 USED(b);
104 doc = emalloc(sizeof(*doc));
105 gfx = emalloc(sizeof(*gfx));
106 gfx->g = nil;
108 doc->npage = 0;
109 doc->drawpage = gfxdrawpage;
110 doc->pagename = gfxpagename;
111 doc->addpage = addpage;
112 doc->rmpage = rmpage;
113 doc->extra = gfx;
114 doc->fwdonly = 0;
116 fprint(2, "reading through graphics...\n");
117 if(argc==0 && buf)
118 genaddpage(doc, nil, buf, nbuf);
119 else{
120 for(i=0; i<argc; i++)
121 if(addpage(doc, argv[i]) < 0)
122 fprint(2, "warning: not including %s: %r\n", argv[i]);
125 return doc;
128 static int
129 genaddpage(Document *doc, char *name, uchar *buf, int nbuf)
131 Graphic *g;
132 GfxInfo *gfx;
133 Biobuf *b;
134 uchar xbuf[32];
135 int i, l;
137 l = 0;
138 gfx = doc->extra;
140 assert((name == nil) ^ (buf == nil));
141 assert(name != nil || doc->npage == 0);
143 for(i=0; i<doc->npage; i++)
144 if(strcmp(gfx->g[i].name, name) == 0)
145 return i;
147 if(name){
148 l = strlen(name);
149 if((b = Bopen(name, OREAD)) == nil) {
150 werrstr("Bopen: %r");
151 return -1;
154 if(Bread(b, xbuf, sizeof xbuf) != sizeof xbuf) {
155 werrstr("short read: %r");
156 return -1;
158 Bterm(b);
159 buf = xbuf;
160 nbuf = sizeof xbuf;
164 gfx->g = erealloc(gfx->g, (doc->npage+1)*(sizeof(*gfx->g)));
165 g = &gfx->g[doc->npage];
167 memset(g, 0, sizeof *g);
168 if(memcmp(buf, "GIF", 3) == 0)
169 g->type = Igif;
170 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
171 g->type = Itiff;
172 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
173 g->type = Itiff;
174 else if(memcmp(buf, "\377\330\377", 3) == 0)
175 g->type = Ijpeg;
176 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
177 g->type = Ipng;
178 else if(memcmp(buf, "compressed\n", 11) == 0)
179 g->type = Iinferno;
180 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
181 g->type = Ifax;
182 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
183 g->type = Ifax;
184 else if(memcmp(buf, "II*", 3) == 0)
185 g->type = Ifax;
186 else if(memcmp(buf, "TYPE=ccitt-g4", 13) == 0)
187 g->type = Iccittg4;
188 else if(memcmp(buf, "TYPE=", 5) == 0)
189 g->type = Ipic;
190 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
191 g->type = Ippm;
192 else if(memcmp(buf, "BM", 2) == 0)
193 g->type = Ibmp;
194 else if(memcmp(buf, " ", 10) == 0 &&
195 '0' <= buf[10] && buf[10] <= '9' &&
196 buf[11] == ' ')
197 g->type = Iplan9bm;
198 else if(strtochan((char*)buf) != 0)
199 g->type = Iplan9bm;
200 else if (l > 4 && strcmp(name + l -4, ".yuv") == 0)
201 g->type = Iyuv;
202 else
203 g->type = Icvt2pic;
205 if(name)
206 g->name = estrdup(name);
207 else{
208 g->name = estrdup("stdin"); /* so it can be freed */
209 g->buf = buf;
210 g->nbuf = nbuf;
213 if(chatty) fprint(2, "classified \"%s\" as \"%s\"\n", g->name, cvt[g->type].name);
214 return doc->npage++;
217 static int
218 addpage(Document *doc, char *name)
220 return genaddpage(doc, name, nil, 0);
223 static int
224 rmpage(Document *doc, int n)
226 int i;
227 GfxInfo *gfx;
229 if(n < 0 || n >= doc->npage)
230 return -1;
232 gfx = doc->extra;
233 doc->npage--;
234 free(gfx->g[n].name);
236 for(i=n; i<doc->npage; i++)
237 gfx->g[i] = gfx->g[i+1];
239 if(n < doc->npage)
240 return n;
241 if(n == 0)
242 return 0;
243 return n-1;
247 static Image*
248 convert(Graphic *g)
250 int fd;
251 Convert c;
252 char *cmd;
253 char *name, buf[1000];
254 Image *im;
255 int rcspawned = 0;
256 Waitmsg *w;
258 c = cvt[g->type];
259 if(c.cmd == nil) {
260 if(chatty) fprint(2, "no conversion for bitmap \"%s\"...\n", g->name);
261 if(g->buf == nil){ /* not stdin */
262 fd = open(g->name, OREAD);
263 if(fd < 0) {
264 fprint(2, "cannot open file: %r\n");
265 wexits("open");
267 }else
268 fd = stdinpipe(g->buf, g->nbuf);
269 } else {
270 cmd = c.cmd;
271 if(truecolor && c.truecmd)
272 cmd = c.truecmd;
274 if(g->buf != nil) /* is stdin */
275 name = "";
276 else
277 name = g->name;
278 if(strlen(cmd)+strlen(name) > sizeof buf) {
279 fprint(2, "command too long\n");
280 wexits("convert");
282 snprint(buf, sizeof buf, cmd, name);
283 if(chatty) fprint(2, "using \"%s\" to convert \"%s\"...\n", buf, g->name);
284 fd = spawnrc(buf, g->buf, g->nbuf);
285 rcspawned++;
286 if(fd < 0) {
287 fprint(2, "cannot spawn converter: %r\n");
288 wexits("convert");
292 im = readimage(display, fd, 0);
293 if(im == nil) {
294 fprint(2, "warning: couldn't read image: %r\n");
296 close(fd);
298 /* for some reason rx doesn't work well with wait */
299 /* for some reason 3to1 exits on success with a non-null status of |3to1 */
300 if(rcspawned && g->type != Iccittg4) {
301 if((w=wait())!=nil && w->msg[0] && !strstr(w->msg, "3to1"))
302 fprint(2, "slave wait error: %s\n", w->msg);
303 free(w);
305 return im;
308 static int
309 spawnrc(char *cmd, uchar *stdinbuf, int nstdinbuf)
311 int pfd[2];
312 int pid;
314 if(chatty) fprint(2, "spawning(%s)...", cmd);
316 if(pipe(pfd) < 0)
317 return -1;
318 if((pid = fork()) < 0)
319 return -1;
321 if(pid == 0) {
322 close(pfd[1]);
323 if(stdinbuf)
324 dup(stdinpipe(stdinbuf, nstdinbuf), 0);
325 else
326 dup(open("/dev/null", OREAD), 0);
327 dup(pfd[0], 1);
328 /*dup(pfd[0], 2); */
329 execl("/bin/rc", "rc", "-c", cmd, nil);
330 wexits("exec");
332 close(pfd[0]);
333 return pfd[1];