plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

loadimage.c (983B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <draw.h>
      4 
      5 int
      6 loadimage(Image *i, Rectangle r, uchar *data, int ndata)
      7 {
      8 	long dy;
      9 	int n, bpl;
     10 	uchar *a;
     11 	int chunk;
     12 
     13 	chunk = i->display->bufsize - 64;
     14 
     15 	if(!rectinrect(r, i->r)){
     16 		werrstr("loadimage: bad rectangle");
     17 		return -1;
     18 	}
     19 	bpl = bytesperline(r, i->depth);
     20 	n = bpl*Dy(r);
     21 	if(n > ndata){
     22 		werrstr("loadimage: insufficient data");
     23 		return -1;
     24 	}
     25 	ndata = 0;
     26 	while(r.max.y > r.min.y){
     27 		dy = r.max.y - r.min.y;
     28 		if(dy*bpl > chunk)
     29 			dy = chunk/bpl;
     30 		if(dy <= 0){
     31 			werrstr("loadimage: image too wide for buffer");
     32 			return -1;
     33 		}
     34 		n = dy*bpl;
     35 		a = bufimage(i->display, 21+n);
     36 		if(a == nil){
     37 			werrstr("bufimage failed");
     38 			return -1;
     39 		}
     40 		a[0] = 'y';
     41 		BPLONG(a+1, i->id);
     42 		BPLONG(a+5, r.min.x);
     43 		BPLONG(a+9, r.min.y);
     44 		BPLONG(a+13, r.max.x);
     45 		BPLONG(a+17, r.min.y+dy);
     46 		memmove(a+21, data, n);
     47 		ndata += n;
     48 		data += n;
     49 		r.min.y += dy;
     50 	}
     51 	if(flushimage(i->display, 0) < 0)
     52 		return -1;
     53 	return ndata;
     54 }