1 /*
2  * Copyright (C) 1989-95 GROUPE BULL
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Except as contained in this notice, the name of GROUPE BULL shall not be
22  * used in advertising or otherwise to promote the sale, use or other dealings
23  * in this Software without prior written authorization from GROUPE BULL.
24  */
25 
26 /*****************************************************************************\
27 *  CrBufFrP.c:                                                                *
28 *                                                                             *
29 *  XPM library                                                                *
30 *  Scan a pixmap and possibly its mask and create an XPM buffer               *
31 *                                                                             *
32 *  Developed by Arnaud Le Hors                                                *
33 \*****************************************************************************/
34 
35 #include <LTconfig.h>
36 
37 /* amai: we can't include DebugUtil.h before system headers.
38    So we assume that system headers are idempotent, #include
39    all of those used below here and then try with our
40    DebugUtil.h! */
41 #include <stdio.h>
42 #include <string.h>
43 
44 #include <ctype.h>
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
48 #ifdef HAVE_SYS_STAT_H
49 #include <sys/stat.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 #ifdef HAVE_FCNTL_H
55 #include <fcntl.h>
56 #endif
57 #if defined(FOR_MSW) || defined(WIN32)
58 #include <io.h>
59 #endif
60 
61 #include <X11/Intrinsic.h> /* Avoid re-definition of Pixel-type */
62 
63 #include <Xm/XpmP.h>
64 #include <XmI/XpmI.h>
65 
66 /* #if !defined(WITH_DBMALLOC) && !defined(WITH_DMALLOC) */
67 #include <XmI/DebugUtil.h>
68 
69 #ifdef HAVE_STRLCAT
70 # define STRLCAT(dst, src, dstsize) { \
71  	if (strlcat(dst, src, dstsize) >= (dstsize)) \
72 	    return (XpmFileInvalid); }
73 # define STRLCPY(dst, src, dstsize) { \
74   	if (strlcpy(dst, src, dstsize) >= (dstsize)) \
75 	    return (XpmFileInvalid); }
76 #else
77 # define STRLCAT(dst, src, dstsize) { \
78 	if ((strlen(dst) + strlen(src)) < (dstsize)) \
79  	    strcat(dst, src); \
80 	else return (XpmFileInvalid); }
81 # define STRLCPY(dst, src, dstsize) { \
82 	if (strlen(src) < (dstsize)) \
83  	    strcpy(dst, src); \
84 	else return (XpmFileInvalid); }
85 #endif
86 
87 int
XpmCreateBufferFromPixmap(display,buffer_return,pixmap,shapemask,attributes)88 XpmCreateBufferFromPixmap(display, buffer_return, pixmap, shapemask,
89 			  attributes)
90     Display *display;
91     char **buffer_return;
92     Pixmap pixmap;
93     Pixmap shapemask;
94     XpmAttributes *attributes;
95 {
96     XImage *ximage = NULL;
97     XImage *shapeimage = NULL;
98     unsigned int width = 0;
99     unsigned int height = 0;
100     int ErrorStatus;
101 
102     /* get geometry */
103     if (attributes && attributes->valuemask & XpmSize) {
104 	width = attributes->width;
105 	height = attributes->height;
106     }
107     /* get the ximages */
108     if (pixmap)
109 	xpmCreateImageFromPixmap(display, pixmap, &ximage, &width, &height);
110     if (shapemask)
111 	xpmCreateImageFromPixmap(display, shapemask, &shapeimage,
112 				 &width, &height);
113 
114     /* create the buffer */
115     ErrorStatus = XpmCreateBufferFromImage(display, buffer_return, ximage,
116 					   shapeimage, attributes);
117 
118     /* destroy the ximages */
119     if (ximage)
120 	XDestroyImage(ximage);
121     if (shapeimage)
122 	XDestroyImage(shapeimage);
123 
124     return (ErrorStatus);
125 }
126