xref: /plan9-contrib/sys/src/libmemdraw/addr.c (revision 6f1f24dd981116678e9fd1c5fb4c6ae548570725)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <memdraw.h>
5 
6 /*
7  * Wordaddr is deprecated.
8  */
9 ulong*
wordaddr(Memimage * i,Point p)10 wordaddr(Memimage *i, Point p)
11 {
12 	return (ulong*) ((uintptr)byteaddr(i, p) & ~(sizeof(ulong)-1));
13 }
14 
15 uchar*
byteaddr(Memimage * i,Point p)16 byteaddr(Memimage *i, Point p)
17 {
18 	uchar *a;
19 
20 	a = i->data->bdata+i->zero+sizeof(ulong)*p.y*i->width;
21 
22 	if(i->depth < 8){
23 		/*
24 		 * We need to always round down,
25 		 * but C rounds toward zero.
26 		 */
27 		int np;
28 		np = 8/i->depth;
29 		if(p.x < 0)
30 			return a+(p.x-np+1)/np;
31 		else
32 			return a+p.x/np;
33 	}
34 	else
35 		return a+p.x*(i->depth/8);
36 }
37