]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/unloadimage.c
remove debug print
[plan9front.git] / sys / src / libdraw / unloadimage.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 int
6 unloadimage(Image *i, Rectangle r, uchar *data, int ndata)
7 {
8         int bpl, n, chunk, dx, dy;
9         uchar *a, *start;
10         Display *d;
11
12         if(!rectinrect(r, i->r)){
13                 werrstr("unloadimage: bad rectangle");
14                 return -1;
15         }
16         bpl = bytesperline(r, i->depth);
17         if(ndata < bpl*Dy(r)){
18                 werrstr("unloadimage: buffer too small");
19                 return -1;
20         }
21         start = data;
22         d = i->display;
23         chunk = d->bufsize;
24         flushimage(d, 0);       /* make sure subsequent flush is for us only */
25         while(r.min.y < r.max.y){
26                 dx = Dx(r);
27                 dy = chunk/bpl;
28                 if(dy <= 0){
29                         dy = 1;
30                         dx = ((chunk*dx)/bpl) & ~7;
31                         n = bytesperline(Rect(r.min.x, r.min.y, r.min.x+dx, r.min.y+dy), i->depth);
32                         if(unloadimage(i, Rect(r.min.x+dx, r.min.y, r.max.x, r.min.y+dy), data+n, bpl-n) < 0)
33                                 return -1;
34                 } else {
35                         if(dy > Dy(r))
36                                 dy = Dy(r);
37                         n = bpl*dy;
38                 }
39                 a = bufimage(d, 1+4+4*4);
40                 if(a == nil){
41                         werrstr("unloadimage: %r");
42                         return -1;
43                 }
44                 a[0] = 'r';
45                 BPLONG(a+1, i->id);
46                 BPLONG(a+5, r.min.x);
47                 BPLONG(a+9, r.min.y);
48                 BPLONG(a+13, r.min.x+dx);
49                 BPLONG(a+17, r.min.y+dy);
50                 if(flushimage(d, 0) < 0)
51                         return -1;
52                 if(read(d->fd, data, n) < 0)
53                         return -1;
54                 data += bpl*dy;
55                 r.min.y += dy;
56         }
57         return data - start;
58 }