2 * graphics file reading for page
13 typedef struct Convert Convert;
14 typedef struct GfxInfo GfxInfo;
19 char *truecmd; /* cmd for true color */
27 * N.B. These commands need to read stdin if %a is replaced
28 * with an empty string.
31 { "plan9", "fb/3to1 rgbv %a |fb/pcp -tplan9" },
32 { "tiff", "fb/tiff2pic %a | fb/3to1 rgbv | fb/pcp -tplan9" },
33 { "jpeg", "jpg -9 %a", "jpg -t9 %a" },
34 { "gif", "gif -9 %a", "gif -t9 %a" },
36 { "fax", "aux/g3p9bit -g %a" },
37 { "unknown", "fb/cvt2pic %a |fb/3to1 rgbv" },
39 { "ppm", "ppm -9 %a", "ppm -t9 %a" },
40 { "png", "png -9 %a", "png -t9 %a" },
41 { "yuv", "yuv -9 %a", "yuv -t9 %a" },
42 { "bmp", "bmp -9 %a", "bmp -t9 %a" },
45 static Image* gfxdrawpage(Document *d, int page);
46 static char* gfxpagename(Document*, int);
47 static int spawnrc(char*, Graphic*);
48 //static void waitrc(void);
49 //static int spawnpost(int);
50 static int addpage(Document*, char*);
51 static int rmpage(Document*, int);
52 static int genaddpage(Document*, char*, uchar*, int);
55 gfxpagename(Document *doc, int page)
57 GfxInfo *gfx = doc->extra;
58 return gfx->g[page].name;
62 gfxdrawpage(Document *doc, int page)
64 GfxInfo *gfx = doc->extra;
65 return convert(gfx->g+page);
69 initgfx(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
77 doc = emalloc(sizeof(*doc));
78 gfx = emalloc(sizeof(*gfx));
82 doc->drawpage = gfxdrawpage;
83 doc->pagename = gfxpagename;
84 doc->addpage = addpage;
90 fprint(2, "reading through graphics...\n");
92 genaddpage(doc, nil, buf, nbuf);
95 if(addpage(doc, argv[i]) < 0)
96 fprint(2, "warning: not including %s: %r\n", argv[i]);
103 genaddpage(Document *doc, char *name, uchar *buf, int nbuf)
114 assert((name == nil) ^ (buf == nil));
115 assert(name != nil || doc->npage == 0);
117 for(i=0; i<doc->npage; i++)
118 if(strcmp(gfx->g[i].name, name) == 0)
123 if((b = Bopen(name, OREAD)) == nil) {
124 werrstr("Bopen: %r");
128 if(Bread(b, xbuf, sizeof xbuf) != sizeof xbuf) {
129 werrstr("short read: %r");
138 gfx->g = erealloc(gfx->g, (doc->npage+1)*(sizeof(*gfx->g)));
139 g = &gfx->g[doc->npage];
141 memset(g, 0, sizeof *g);
142 if(memcmp(buf, "GIF", 3) == 0)
144 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
146 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
148 else if(memcmp(buf, "\377\330\377", 3) == 0)
150 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
152 else if(memcmp(buf, "compressed\n", 11) == 0)
154 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
156 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
158 else if(memcmp(buf, "II*", 3) == 0)
160 else if(memcmp(buf, "TYPE=", 5) == 0)
162 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
164 else if(memcmp(buf, "BM", 2) == 0)
166 else if(memcmp(buf, " ", 10) == 0 &&
167 '0' <= buf[10] && buf[10] <= '9' &&
170 else if(strtochan((char*)buf) != 0)
172 else if (l > 4 && strcmp(name + l -4, ".yuv") == 0)
178 g->name = estrdup(name);
181 g->name = estrdup("stdin"); /* so it can be freed */
182 g->fd = stdinpipe(buf, nbuf);
185 if(chatty) fprint(2, "classified \"%s\" as \"%s\"\n", g->name, cvt[g->type].name);
190 addpage(Document *doc, char *name)
192 return genaddpage(doc, name, nil, 0);
196 rmpage(Document *doc, int n)
201 if(n < 0 || n >= doc->npage)
206 free(gfx->g[n].name);
208 for(i=n; i<doc->npage; i++)
209 gfx->g[i] = gfx->g[i+1];
225 char *name, buf[1000];
231 if(chatty) fprint(2, "no conversion for bitmap \"%s\"...\n", g->name);
232 if(g->fd < 0){ /* not stdin */
233 fd = open(g->name, OREAD);
235 fprint(2, "cannot open file: %r\n");
242 if(truecolor && c.truecmd)
245 if(g->fd >= 0) /* is pipe */
249 if(strlen(cmd)+strlen(name) > sizeof buf) {
250 fprint(2, "command too long\n");
253 snprint(buf, sizeof buf, cmd, name);
254 if(chatty) fprint(2, "using \"%s\" to convert \"%s\"...\n", buf, g->name);
255 fd = spawnrc(buf, g);
258 fprint(2, "cannot spawn converter: %r\n");
263 im = readimage(display, fd, 0);
265 fprint(2, "warning: couldn't read image: %r\n");
273 spawnrc(char *cmd, Graphic *g)
278 if(chatty) fprint(2, "spawning(%s)...", cmd);
284 fd[0] = dup(g->fd, -1);
286 fd[0] = open("/dev/null", OREAD);
290 if(threadspawnl(fd, "rc", "rc", "-c", cmd, nil) == -1)