Blob


1 /*
2 * graphics file reading for page
3 */
5 #include <u.h>
6 #include <libc.h>
7 #include <draw.h>
8 #include <thread.h>
9 #include <bio.h>
10 #include <cursor.h>
11 #include "page.h"
13 typedef struct Convert Convert;
14 typedef struct GfxInfo GfxInfo;
16 struct Convert {
17 char *name;
18 char *cmd;
19 char *truecmd; /* cmd for true color */
20 };
22 struct GfxInfo {
23 Graphic *g;
24 };
26 /*
27 * N.B. These commands need to read stdin if %a is replaced
28 * with an empty string.
29 */
30 Convert cvt[] = {
31 [Ipic] { "plan9", "fb/3to1 rgbv %a |fb/pcp -tplan9" },
32 [Itiff] { "tiff", "fb/tiff2pic %a | fb/3to1 rgbv | fb/pcp -tplan9" },
33 [Iplan9bm] { "plan9bm", nil },
34 [Ijpeg] { "jpeg", "jpg -9 %a", "jpg -t9 %a" },
35 [Igif] { "gif", "gif -9 %a", "gif -t9 %a" },
36 [Iinferno] { "inferno", nil },
37 [Ifax] { "fax", "aux/g3p9bit -g %a" },
38 [Icvt2pic] { "unknown", "fb/cvt2pic %a |fb/3to1 rgbv" },
39 [Ippm] { "ppm", "ppm -9 %a", "ppm -t9 %a" },
40 /* ``temporary'' hack for hobby */
41 [Iccittg4] { "ccitt-g4", "cat %a|rx nslocum /usr/lib/ocr/bin/bcp -M|fb/pcp -tcompressed -l0" },
42 [Ipng] { "png", "png -9 %a", "png -t9 %a" },
43 [Iyuv] { "yuv", "yuv -9 %a", "yuv -t9 %a" },
44 [Ibmp] { "bmp", "bmp -9 %a", "bmp -t9 %a" },
45 };
47 static Image* gfxdrawpage(Document *d, int page);
48 static char* gfxpagename(Document*, int);
49 static int spawnrc(char*, Graphic*);
50 //static void waitrc(void);
51 //static int spawnpost(int);
52 static int addpage(Document*, char*);
53 static int rmpage(Document*, int);
54 static int genaddpage(Document*, char*, uchar*, int);
56 static char*
57 gfxpagename(Document *doc, int page)
58 {
59 GfxInfo *gfx = doc->extra;
60 return gfx->g[page].name;
61 }
63 static Image*
64 gfxdrawpage(Document *doc, int page)
65 {
66 GfxInfo *gfx = doc->extra;
67 return convert(gfx->g+page);
68 }
70 Document*
71 initgfx(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
72 {
73 GfxInfo *gfx;
74 Document *doc;
75 int i;
77 USED(b);
79 doc = emalloc(sizeof(*doc));
80 gfx = emalloc(sizeof(*gfx));
81 gfx->g = nil;
83 doc->npage = 0;
84 doc->drawpage = gfxdrawpage;
85 doc->pagename = gfxpagename;
86 doc->addpage = addpage;
87 doc->rmpage = rmpage;
88 doc->extra = gfx;
89 doc->fwdonly = 0;
91 fprint(2, "reading through graphics...\n");
92 if(argc==0 && buf)
93 genaddpage(doc, nil, buf, nbuf);
94 else{
95 for(i=0; i<argc; i++)
96 if(addpage(doc, argv[i]) < 0)
97 fprint(2, "warning: not including %s: %r\n", argv[i]);
98 }
100 return doc;
103 static int
104 genaddpage(Document *doc, char *name, uchar *buf, int nbuf)
106 Graphic *g;
107 GfxInfo *gfx;
108 Biobuf *b;
109 uchar xbuf[32];
110 int i, l;
112 l = 0;
113 gfx = doc->extra;
115 assert((name == nil) ^ (buf == nil));
116 assert(name != nil || doc->npage == 0);
118 for(i=0; i<doc->npage; i++)
119 if(strcmp(gfx->g[i].name, name) == 0)
120 return i;
122 if(name){
123 l = strlen(name);
124 if((b = Bopen(name, OREAD)) == nil) {
125 werrstr("Bopen: %r");
126 return -1;
129 if(Bread(b, xbuf, sizeof xbuf) != sizeof xbuf) {
130 werrstr("short read: %r");
131 return -1;
133 Bterm(b);
134 buf = xbuf;
135 nbuf = sizeof xbuf;
139 gfx->g = erealloc(gfx->g, (doc->npage+1)*(sizeof(*gfx->g)));
140 g = &gfx->g[doc->npage];
142 memset(g, 0, sizeof *g);
143 if(memcmp(buf, "GIF", 3) == 0)
144 g->type = Igif;
145 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
146 g->type = Itiff;
147 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
148 g->type = Itiff;
149 else if(memcmp(buf, "\377\330\377", 3) == 0)
150 g->type = Ijpeg;
151 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
152 g->type = Ipng;
153 else if(memcmp(buf, "compressed\n", 11) == 0)
154 g->type = Iinferno;
155 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
156 g->type = Ifax;
157 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
158 g->type = Ifax;
159 else if(memcmp(buf, "II*", 3) == 0)
160 g->type = Ifax;
161 else if(memcmp(buf, "TYPE=ccitt-g4", 13) == 0)
162 g->type = Iccittg4;
163 else if(memcmp(buf, "TYPE=", 5) == 0)
164 g->type = Ipic;
165 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
166 g->type = Ippm;
167 else if(memcmp(buf, "BM", 2) == 0)
168 g->type = Ibmp;
169 else if(memcmp(buf, " ", 10) == 0 &&
170 '0' <= buf[10] && buf[10] <= '9' &&
171 buf[11] == ' ')
172 g->type = Iplan9bm;
173 else if(strtochan((char*)buf) != 0)
174 g->type = Iplan9bm;
175 else if (l > 4 && strcmp(name + l -4, ".yuv") == 0)
176 g->type = Iyuv;
177 else
178 g->type = Icvt2pic;
180 if(name){
181 g->name = estrdup(name);
182 g->fd = -1;
183 }else{
184 g->name = estrdup("stdin"); /* so it can be freed */
185 g->fd = stdinpipe(buf, nbuf);
188 if(chatty) fprint(2, "classified \"%s\" as \"%s\"\n", g->name, cvt[g->type].name);
189 return doc->npage++;
192 static int
193 addpage(Document *doc, char *name)
195 return genaddpage(doc, name, nil, 0);
198 static int
199 rmpage(Document *doc, int n)
201 int i;
202 GfxInfo *gfx;
204 if(n < 0 || n >= doc->npage)
205 return -1;
207 gfx = doc->extra;
208 doc->npage--;
209 free(gfx->g[n].name);
211 for(i=n; i<doc->npage; i++)
212 gfx->g[i] = gfx->g[i+1];
214 if(n < doc->npage)
215 return n;
216 if(n == 0)
217 return 0;
218 return n-1;
222 Image*
223 convert(Graphic *g)
225 int fd;
226 Convert c;
227 char *cmd;
228 char *name, buf[1000];
229 Image *im;
230 int rcspawned = 0;
232 c = cvt[g->type];
233 if(c.cmd == nil) {
234 if(chatty) fprint(2, "no conversion for bitmap \"%s\"...\n", g->name);
235 if(g->fd < 0){ /* not stdin */
236 fd = open(g->name, OREAD);
237 if(fd < 0) {
238 fprint(2, "cannot open file: %r\n");
239 wexits("open");
241 }else
242 fd = g->fd;
243 } else {
244 cmd = c.cmd;
245 if(truecolor && c.truecmd)
246 cmd = c.truecmd;
248 if(g->fd >= 0) /* is pipe */
249 name = "";
250 else
251 name = g->name;
252 if(strlen(cmd)+strlen(name) > sizeof buf) {
253 fprint(2, "command too long\n");
254 wexits("convert");
256 snprint(buf, sizeof buf, cmd, name);
257 if(chatty) fprint(2, "using \"%s\" to convert \"%s\"...\n", buf, g->name);
258 fd = spawnrc(buf, g);
259 rcspawned++;
260 if(fd < 0) {
261 fprint(2, "cannot spawn converter: %r\n");
262 wexits("convert");
266 im = readimage(display, fd, 0);
267 if(im == nil) {
268 fprint(2, "warning: couldn't read image: %r\n");
271 close(fd);
272 return im;
275 static int
276 spawnrc(char *cmd, Graphic *g)
278 int pfd[2];
279 int fd[3];
281 if(chatty) fprint(2, "spawning(%s)...", cmd);
283 if(pipe(pfd) < 0)
284 return -1;
286 if(g->fd > 0)
287 fd[0] = dup(g->fd, -1);
288 else
289 fd[0] = open("/dev/null", OREAD);
290 fd[1] = pfd[1];
291 fd[2] = dup(2, -1);
293 if(threadspawnl(fd, "rc", "rc", "-c", cmd, nil) == -1)
294 return -1;
296 return pfd[0];