]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/mothra/getpix.c
add mothra
[plan9front.git] / sys / src / cmd / mothra / getpix.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5 #include <panel.h>
6 #include "mothra.h"
7
8 typedef struct Pix Pix;
9 struct Pix{
10         Pix *next;
11         Image *b;
12         int width;
13         int height;
14         char name[NNAME];
15 };
16
17 char *pixcmd[]={
18 [GIF]   "gif -9t",
19 [JPEG]  "jpg -9t",
20 [PNG]   "png -9t",
21 [PIC]   "fb/3to1 /lib/fb/cmap/rgbv",
22 [TIFF]  "/sys/lib/mothra/tiffcvt",
23 [XBM]   "fb/xbm2pic",
24 };
25
26 void storebitmap(Rtext *t, Image *b){
27         t->b=b;
28         free(t->text);
29         t->text=0;
30 }
31
32 void getimage(Rtext *t, Www *w){
33         int pfd[2];
34         Action *ap;
35         Url url;
36         Image *b;
37         int fd;
38         char err[512];
39         Pix *p;
40
41         ap=t->user;
42         crackurl(&url, ap->image, w->base);
43         for(p=w->pix;p!=nil; p=p->next)
44                 if(strcmp(ap->image, p->name)==0 && ap->width==p->width && ap->height==p->height){
45                         storebitmap(t, p->b);
46                         free(ap->image);
47                         ap->image=0;
48                         w->changed=1;
49                         return;
50                 }
51         fd=urlopen(&url, GET, 0);
52         if(fd==-1){
53         Err:
54                 snprint(err, sizeof(err), "[%s: %r]", url.fullname);
55                 free(t->text);
56                 t->text=strdup(err);
57                 free(ap->image);
58                 ap->image=0;
59                 w->changed=1;
60                 close(fd);
61                 return;
62         }
63         if(url.type!=GIF
64         && url.type!=JPEG
65         && url.type!=PNG
66         && url.type!=PIC
67         && url.type!=TIFF
68         && url.type!=XBM){
69                 werrstr("unknown image type");
70                 goto Err;
71         }
72
73         if((fd = pipeline(pixcmd[url.type], fd)) < 0)
74                 goto Err;
75         if(ap->width>0 || ap->height>0){
76                 char buf[80];
77                 char *p;
78
79                 p = buf;
80                 p += sprint(p, "resize");
81                 if(ap->width>0)
82                         p += sprint(p, " -x %d", ap->width);
83                 if(ap->height>0)
84                         p += sprint(p, " -y %d", ap->height);
85                 if((fd = pipeline(buf, fd)) < 0)
86                         goto Err;
87         }
88         b=readimage(display, fd, 1);
89         if(b==0){
90                 werrstr("can't read image");
91                 goto Err;
92         }
93         close(fd);
94         p = emallocz(sizeof(Pix), 1);
95         strncpy(p->name, ap->image, sizeof(p->name));
96         p->b=b;
97         p->width=ap->width;
98         p->height=ap->height;
99         p->next=w->pix;
100         w->pix=p;
101         storebitmap(t, b);
102         free(ap->image);
103         ap->image=0;
104         w->changed=1;
105 }
106
107 void getpix(Rtext *t, Www *w){
108         Action *ap;
109
110         for(;t!=0;t=t->next){
111                 ap=t->user;
112                 if(ap && ap->image)
113                         getimage(t, w);
114         }
115 }
116
117 void freepix(void *p)
118 {
119         Pix *x, *xx;
120         xx = p;
121         while(x = xx){
122                 xx = x->next;
123                 freeimage(x->b);
124                 free(x);
125         }
126 }