gfx.c 6.7 KB

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