load.c (1500B)
1 #include "u.h" 2 #include "lib.h" 3 #include "draw.h" 4 #include "memdraw.h" 5 6 int 7 _loadmemimage(Memimage *i, Rectangle r, uchar *data, int ndata) 8 { 9 int y, l, lpart, rpart, mx, m, mr; 10 uchar *q; 11 12 if(!rectinrect(r, i->r)) 13 return -1; 14 l = bytesperline(r, i->depth); 15 if(ndata < l*Dy(r)) 16 return -1; 17 ndata = l*Dy(r); 18 q = byteaddr(i, r.min); 19 mx = 7/i->depth; 20 lpart = (r.min.x & mx) * i->depth; 21 rpart = (r.max.x & mx) * i->depth; 22 m = 0xFF >> lpart; 23 /* may need to do bit insertion on edges */ 24 if(l == 1){ /* all in one byte */ 25 if(rpart) 26 m ^= 0xFF >> rpart; 27 for(y=r.min.y; y<r.max.y; y++){ 28 *q ^= (*data^*q) & m; 29 q += i->width*sizeof(uint32); 30 data++; 31 } 32 return ndata; 33 } 34 if(lpart==0 && rpart==0){ /* easy case */ 35 for(y=r.min.y; y<r.max.y; y++){ 36 memmove(q, data, l); 37 q += i->width*sizeof(uint32); 38 data += l; 39 } 40 return ndata; 41 } 42 mr = 0xFF ^ (0xFF >> rpart); 43 if(lpart!=0 && rpart==0){ 44 for(y=r.min.y; y<r.max.y; y++){ 45 *q ^= (*data^*q) & m; 46 if(l > 1) 47 memmove(q+1, data+1, l-1); 48 q += i->width*sizeof(uint32); 49 data += l; 50 } 51 return ndata; 52 } 53 if(lpart==0 && rpart!=0){ 54 for(y=r.min.y; y<r.max.y; y++){ 55 if(l > 1) 56 memmove(q, data, l-1); 57 q[l-1] ^= (data[l-1]^q[l-1]) & mr; 58 q += i->width*sizeof(uint32); 59 data += l; 60 } 61 return ndata; 62 } 63 for(y=r.min.y; y<r.max.y; y++){ 64 *q ^= (*data^*q) & m; 65 if(l > 2) 66 memmove(q+1, data+1, l-2); 67 q[l-1] ^= (data[l-1]^q[l-1]) & mr; 68 q += i->width*sizeof(uint32); 69 data += l; 70 } 71 return ndata; 72 }