2 #-# Copyright © 2021 Eric Bina, Dave Black, TJ Phan,
3 #-# Vincent Renardias, Willem Vermin
5 #-# Permission is hereby granted, free of charge, to any person
6 #-# obtaining a copy of this software and associated documentation
7 #-# files (the “Software”), to deal in the Software without
8 #-# restriction, including without limitation the rights to use,
9 #-# copy, modify, merge, publish, distribute, sublicense, and/or
10 #-# sell copies of the Software, and to permit persons to whom
11 #-# the Software is furnished to do so, subject to the following
14 #-# The above copyright notice and this permission notice shall
15 #-# be included in all copies or substantial portions of the Software.
17 #-# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18 #-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 #-# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 #-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 #-# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 #-# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 #-# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 #-# OTHER DEALINGS IN THE SOFTWARE.
34 // from the xpm package:
35 static void xpmCreatePixmapFromImage(
39 Pixmap
*pixmap_return
)
44 *pixmap_return
= XCreatePixmap(display
, d
, ximage
->width
,
45 ximage
->height
, ximage
->depth
);
46 /* set fg and bg in case we have an XYBitmap */
47 values
.foreground
= 1;
48 values
.background
= 0;
49 gc
= XCreateGC(display
, *pixmap_return
,
50 GCForeground
| GCBackground
, &values
);
52 XPutImage(display
, *pixmap_return
, gc
, ximage
, 0, 0, 0, 0,
53 ximage
->width
, ximage
->height
);
58 void paintit(XImage
*img
, long int color
)
61 for (y
=0; y
<img
->height
; y
++)
62 for (x
=0; x
<img
->width
; x
++)
64 XPutPixel(img
, x
,y
,color
);
69 // reverse characters in string, characters taken in chunks of l
70 // if you know what I mean
71 static void strrevert(char*s
, size_t l
)
74 size_t n
= strlen(s
)/l
;
76 char *c
= (char *)malloc(l
*sizeof(*c
));
78 char *b
= s
+strlen(s
)-l
;
91 // equal to XpmCreatePixmapFromData, with extra flags:
92 // flop: if 1, reverse the data horizontally
93 // Extra: 0xff000000 is added to the pixmap data
95 int iXpmCreatePixmapFromData(Display
*display
, Drawable d
,
96 const char *data
[], Pixmap
*p
, Pixmap
*s
, XpmAttributes
*attr
, int flop
)
98 int rc
, lines
, i
, ncolors
, height
, w
;
101 sscanf(data
[0],"%*s %d %d %d", &height
, &ncolors
, &w
);
102 lines
= height
+ncolors
+1;
104 idata
= (char **)malloc(lines
*sizeof(*idata
));
105 for (i
=0; i
<lines
; i
++)
106 idata
[i
] = strdup(data
[i
]);
108 // flop the image data
109 for (i
=1+ncolors
; i
<lines
; i
++)
110 strrevert(idata
[i
],w
);
112 XImage
*ximage
,*shapeimage
;
113 rc
= XpmCreateImageFromData(display
,idata
,&ximage
,&shapeimage
,attr
);
116 I("rc from XpmCreateImageFromData: ");
120 printf("XpmColorError\n");
121 for (i
=0; i
<lines
; i
++)
122 printf("\"%s\",\n",idata
[i
]);
125 printf("XpmOpenFailed\n");
128 printf("XpmFileInvalid\n");
131 printf("XpmNoMemory: maybe issue with width of data: w=%d\n",w
);
134 printf("XpmColorFailed\n");
135 for (i
=0; i
<lines
; i
++)
136 printf("\"%s\",\n",idata
[i
]);
146 XAddPixel(ximage
,0xff000000);
148 xpmCreatePixmapFromImage(display
, d
, ximage
, p
);
150 xpmCreatePixmapFromImage(display
, d
, shapeimage
, s
);
151 XDestroyImage(ximage
);
152 XDestroyImage(shapeimage
);
153 for(i
=0; i
<lines
; i
++) free(idata
[i
]);
158 // given xpmdata **data, add the non-transparent pixels to Region r
159 Region
regionfromxpm(const char **data
, int flop
)
162 Region r
= XCreateRegion();
163 // width, height, #colors, $chars to code color
164 sscanf(*data
,"%d %d %d %d",&w
,&h
,&nc
,&n
);
165 // find color "None":
167 char *code
= (char *)"";
173 sscanf(data
[i
]+n
,"%*s %100s",s
);
175 if(!strcasecmp(s
,"None"))
177 code
= strndup(data
[i
],n
);
188 char*s
= strdup(data
[y
+offset
]);
193 if (strncmp(s
+n
*x
,code
,n
))
197 XUnionRectWithRegion(&rect
,r
,r
);
207 * converts xpm data to bitmap
208 * xpm: input xpm data
209 * bitsreturn: output bitmap, allocated by this function
210 * wreturn: width of bitmap
211 * hreturn: height of bitmap
212 * lreturn: length of bitmap
214 * Return value: 1: OK, 0: not OK.
215 * BUGS: this code has not been tested on a big endian system
217 int xpmtobits(char *xpm
[],unsigned char **bitsreturn
, int *wreturn
, int *hreturn
, int *lreturn
)
221 unsigned char *bits
= (unsigned char*) malloc(sizeof(unsigned char)*1);
222 if (sscanf(xpm
[0],"%d %d %d %d",&w
,&h
,&nc
,&cpp
)!=4)
224 if(cpp
<=0 || w
<0 || h
<0 || nc
<0)
228 int l
= ((int)w
+ 7)/8; // # chars needed for this w
230 // l*h+1: we do not want allocate 0 bytes
231 bits
= (unsigned char*) realloc(bits
,sizeof(unsigned char)*(l
*h
+1));
237 char *code
= (char *)malloc(sizeof(char)*cpp
);
238 for (i
=0; i
<cpp
; i
++)
245 if (strlen(xpm
[i
]) > (size_t)cpp
+ 6)
247 sscanf(xpm
[i
]+cpp
,"%*s %100s",s
);
248 if(!strcasecmp(s
,"none"))
251 code
= strndup(xpm
[i
],cpp
);
259 if (is_little_endian())
260 for (y
=0; y
<h
; y
++) // little endian
263 const char *s
= xpm
[y
+offset
];
268 if (cpp
*x
+ cpp
<= l
)
270 if (strncmp(s
+cpp
*x
,code
,cpp
))
281 bits
[j
++] = c
>>(8-k
);
284 for (y
=0; y
<h
; y
++) // big endian NOT tested
287 const char *s
= xpm
[y
+offset
];
292 if (cpp
*x
+ cpp
<= l
)
294 if (strncmp(s
+cpp
*x
,code
,cpp
))
305 bits
[j
++] = c
<<(8-k
);
313 // given color and xmpdata **data of a monocolored picture like:
315 //XPM_TYPE *snow06_xpm[] = {
316 ///* columns rows colors chars-per-pixel */
325 // change the second color to color and put the result in out.
326 // lines will become the number of lines in out, comes in handy
327 // when wanteing to free out.
328 void xpm_set_color(char **data
, char ***out
, int *lines
, const char *color
)
331 sscanf(data
[0],"%*d %d",&n
);
333 *out
= (char**)malloc(sizeof(char *)*(n
+3));
337 x
[j
] = strdup(data
[j
]);
338 x
[2] = (char *)malloc(5+strlen(color
));
344 for (j
=3; j
<n
+3; j
++)
346 x
[j
] = strdup(data
[j
]);
352 void xpm_destroy(char **data
)
355 sscanf(data
[0],"%*d %d %d",&h
,&nc
);
357 for (i
=0; i
<h
+nc
+1; i
++)
362 void xpm_print(char **xpm
)
365 sscanf(xpm
[0],"%d %d %d",&w
,&h
,&nc
);
367 printf("%s\n",xpm
[0]);
368 for (i
=1; i
<1+nc
; i
++)
369 printf("%s\n",xpm
[i
]);
370 for (i
=0; i
<2*w
+2; i
++)
377 printf("%2c",xpm
[i
+nc
+1][j
]);
381 for (i
=0; i
<2*w
+2; i
++)
This page took 0.032514 seconds and 4 git commands to generate.