aboutsummaryrefslogtreecommitdiff
path: root/src/libmemdraw/load.c
blob: 6788fa90b3a78b55526c106beac8620ff9269e7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

int
_loadmemimage(Memimage *i, Rectangle r, uchar *data, int ndata)
{
	int y, l, lpart, rpart, mx, m, mr;
	uchar *q;

	if(!rectinrect(r, i->r))
		return -1;
	l = bytesperline(r, i->depth);
	if(ndata < l*Dy(r))
		return -1;
	ndata = l*Dy(r);
	q = byteaddr(i, r.min);
	mx = 7/i->depth;
	lpart = (r.min.x & mx) * i->depth;
	rpart = (r.max.x & mx) * i->depth;
	m = 0xFF >> lpart;
	/* may need to do bit insertion on edges */
	if(l == 1){	/* all in one byte */
		if(rpart)
			m ^= 0xFF >> rpart;
		for(y=r.min.y; y<r.max.y; y++){
			*q ^= (*data^*q) & m;
			q += i->width*sizeof(u32int);
			data++;
		}
		return ndata;
	}
	if(lpart==0 && rpart==0){	/* easy case */
		for(y=r.min.y; y<r.max.y; y++){
			memmove(q, data, l);
			q += i->width*sizeof(u32int);
			data += l;
		}
		return ndata;
	}
	mr = 0xFF ^ (0xFF >> rpart);
	if(lpart!=0 && rpart==0){
		for(y=r.min.y; y<r.max.y; y++){
			*q ^= (*data^*q) & m;
			if(l > 1)
				memmove(q+1, data+1, l-1);
			q += i->width*sizeof(u32int);
			data += l;
		}
		return ndata;
	}
	if(lpart==0 && rpart!=0){
		for(y=r.min.y; y<r.max.y; y++){
			if(l > 1)
				memmove(q, data, l-1);
			q[l-1] ^= (data[l-1]^q[l-1]) & mr;
			q += i->width*sizeof(u32int);
			data += l;
		}
		return ndata;
	}
	for(y=r.min.y; y<r.max.y; y++){
		*q ^= (*data^*q) & m;
		if(l > 2)
			memmove(q+1, data+1, l-2);
		q[l-1] ^= (data[l-1]^q[l-1]) & mr;
		q += i->width*sizeof(u32int);
		data += l;
	}
	return ndata;
}