1 #include "xtama_createpixmap.h"
2
AllocColor(Display * dpy,Colormap cmap,char * color,XColor * xcol,Bool sw)3 unsigned long AllocColor( Display *dpy , Colormap cmap , char *color , XColor *xcol , Bool sw )
4 {
5 int cnum[3] , i ;
6 char tmpcolor[3] ;
7 static int AllocOfColor = 0 ;
8 static struct _colordata{
9 int red ;
10 int green ;
11 int blue ;
12
13 int rred ;
14 int rgreen ;
15 int rblue ;
16
17 int pixel ;
18 } colordata[255] ;
19 XColor rgb , hard ;
20
21 xcol->flags = DoRed|DoGreen|DoBlue ;
22
23 if ( !sw ){
24 if ( color[0] == '#' ){
25 color ++ ;
26 for ( i = 0 ; i < 3 ; i ++ ){
27 strncpy( tmpcolor , color , 2 ) ;
28 sscanf( tmpcolor , "%x" , &cnum[i] ) ;
29 cnum[i] *= 256 ;
30 color += 2 ;
31 }
32 xcol->red = cnum[0] ;
33 xcol->green = cnum[1] ;
34 xcol->blue = cnum[2] ;
35 }
36 else {
37 XLookupColor( dpy , cmap , color , &rgb , &hard ) ;
38 xcol->red = hard.red ;
39 xcol->green = hard.green ;
40 xcol->blue = hard.blue ;
41 }
42
43 for ( i = 0 ; i < AllocOfColor ; i ++ ){
44 if (( xcol->red == colordata[i].red &&
45 xcol->green == colordata[i].green &&
46 xcol->blue == colordata[i].blue ) ||
47 (xcol->red == colordata[i].red &&
48 xcol->green == colordata[i].green &&
49 xcol->blue == colordata[i].blue )){
50 return colordata[i].pixel ;
51 }
52 }
53 }
54
55 colordata[AllocOfColor].red = xcol->red ;
56 colordata[AllocOfColor].green = xcol->green ;
57 colordata[AllocOfColor].blue = xcol->blue ;
58
59 if ( !XAllocColor( dpy , cmap , xcol ) ){
60 printf("\033[41;37m\rCan't allocate color cell !\nTry with option \"-install\"\n\033[m\b") ;
61 exit(1) ;
62 }
63 colordata[AllocOfColor].rred = xcol->red ;
64 colordata[AllocOfColor].rgreen = xcol->green ;
65 colordata[AllocOfColor].rblue = xcol->blue ;
66 colordata[AllocOfColor].pixel = xcol->pixel ;
67 AllocOfColor ++ ;
68
69 return xcol->pixel ;
70 }
71
CreatePixmapFromData(Display * dpy,Window win,Colormap cmap,char ** xpmdata,Pixmap * pm1,Pixmap * pm2)72 void CreatePixmapFromData( Display *dpy , Window win , Colormap cmap , char **xpmdata , Pixmap *pm1 , Pixmap *pm2 )
73 {
74 int i , j , k , x , y , cforb , nofc ;
75 /* size is ( x , y ) : cforb means color for bytes
76 nofc means number of colors */
77
78 struct _ColorPallet {
79 unsigned long pixel ; /* pixel value for color pallet */
80 char *cpattern ;
81 } CP[255] ;
82
83 XColor tmpc ;
84 GC gc = DefaultGC( dpy , DefaultScreen( dpy )) ;
85 char buffer[20] , system , *mask ;
86
87 sscanf( xpmdata[0] , "%d %d %d %d", &x , &y , &nofc , &cforb ) ;
88
89 for ( i = 0 ; i < nofc ; i ++ ) {
90 CP[i].cpattern = malloc( cforb + 1 ) ;
91 if ( cforb != 1 ) sscanf( xpmdata[1+i] , "%s %c %s" , CP[i].cpattern , &system , buffer ) ;
92 else{
93 sscanf( &xpmdata[1+i][1] , " %c %s" , &system , buffer ) ;
94 CP[i].cpattern[0] = xpmdata[1+i][0] ;
95 }
96 if ( system == 'c' ) CP[i].pixel = AllocColor( dpy , cmap , buffer , &tmpc , False ) ;
97 else CP[i].pixel = -1 ;
98
99 }
100
101 *pm1 = XCreatePixmap( dpy , win , x , y , DefaultDepth(dpy,DefaultScreen(dpy))) ;
102 mask = malloc( y*(x/8+1)) ;
103 if ( mask == NULL )
104 printf("Can't allocate memory\n");
105 memset( mask , 0 , y*(x/8+1)) ;
106
107 for ( i = 0 ; i < y ; i ++ ){
108 for ( j = 0 ; j < x ; j ++ )
109 for ( k = 0 ; k < nofc ; k ++ ){
110 if ( strncmp( &xpmdata[1+nofc+i][j*cforb] , CP[k].cpattern , cforb )==0)
111 if ( CP[k].pixel != -1 ){
112 XSetForeground( dpy , gc , CP[k].pixel );
113 XDrawPoint( dpy , *pm1 , gc , j , i ) ;
114 MDrawPoint( mask , j , i , x , y ) ;
115 }
116 }
117 }
118 if ( pm2 != NULL ){
119 *pm2 = XCreateBitmapFromData( dpy , win , mask , x , y ) ;
120 }
121 free( mask ) ;
122
123 }
124
MDrawPoint(char * ptr,int x,int y,int xx,int yy)125 void MDrawPoint( char *ptr , int x , int y , int xx , int yy )
126 {
127 int i , j ;
128 int bpl ;
129
130 bpl = xx / 8 ; /* bytes per line */
131 if ( xx % 8 )
132 bpl ++ ;
133
134 i = x / 8 ;
135 j = x % 8 ;
136
137 ptr[bpl*y+i] |= 1 << j ;
138 }
139