]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/crbuffri.c
2 * Copyright (C) 1989-94 GROUPE BULL
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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.
26 /*****************************************************************************\
30 * Scan an image and possibly its mask and create an XPM buffer *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
37 #include "sys$library:string.h"
39 #if defined(SYSV) || defined(SVR4)
46 LFUNC(WriteColors
, int, (char **dataptr
, unsigned int *data_size
,
47 unsigned int *used_size
, XpmColor
*colors
,
48 unsigned int ncolors
, unsigned int cpp
));
50 LFUNC(WritePixels
, void, (char *dataptr
, unsigned int *used_size
,
51 unsigned int width
, unsigned int height
,
52 unsigned int cpp
, unsigned int *pixels
,
55 LFUNC(WriteExtensions
, void, (char *dataptr
, unsigned int *used_size
,
56 XpmExtension
*ext
, unsigned int num
));
58 LFUNC(ExtensionsSize
, int, (XpmExtension
*ext
, unsigned int num
));
59 LFUNC(CommentsSize
, int, (XpmInfo
*info
));
62 XpmCreateBufferFromImage(Display
*display
, char **buffer_return
, XImage
*image
,
63 XImage
*shapeimage
, XpmAttributes
*attributes
)
69 /* initialize return value */
71 *buffer_return
= NULL
;
73 /* create an XpmImage from the image */
74 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
75 &xpmimage
, attributes
);
76 if (ErrorStatus
!= XpmSuccess
)
79 /* create the buffer from the XpmImage */
81 xpmSetInfo(&info
, attributes
);
83 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, &info
);
86 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, NULL
);
88 /* free the XpmImage */
89 XpmFreeXpmImage(&xpmimage
);
96 #define RETURN(status) \
104 XpmCreateBufferFromXpmImage(char **buffer_return
, XpmImage
*image
, XpmInfo
*info
)
106 /* calculation variables */
109 unsigned int cmts
, extensions
, ext_size
= 0;
110 unsigned int l
, cmt_size
= 0;
111 char *ptr
= NULL
, *p
;
112 unsigned int ptr_size
, used_size
;
114 *buffer_return
= NULL
;
116 cmts
= info
&& (info
->valuemask
& XpmComments
);
117 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
118 && info
->nextensions
;
120 /* compute the extensions and comments size */
122 ext_size
= ExtensionsSize(info
->extensions
, info
->nextensions
);
124 cmt_size
= CommentsSize(info
);
126 /* write the header line */
127 sprintf(buf
, "/* XPM */\nstatic char * image_name[] = {\n");
128 used_size
= strlen(buf
);
129 ptr_size
= used_size
+ ext_size
+ cmt_size
+ 1;
130 ptr
= (char *) XpmMalloc(ptr_size
);
135 /* write the values line */
136 if (cmts
&& info
->hints_cmt
) {
137 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->hints_cmt
);
138 used_size
+= strlen(info
->hints_cmt
) + 5;
140 sprintf(buf
, "\"%d %d %d %d", image
->width
, image
->height
,
141 image
->ncolors
, image
->cpp
);
144 if (info
&& (info
->valuemask
& XpmHotspot
)) {
145 sprintf(buf
+ l
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
149 sprintf(buf
+ l
, " XPMEXT");
152 sprintf(buf
+ l
, "\",\n");
155 p
= (char *) XpmRealloc(ptr
, ptr_size
);
159 strcpy(ptr
+ used_size
, buf
);
163 if (cmts
&& info
->colors_cmt
) {
164 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->colors_cmt
);
165 used_size
+= strlen(info
->colors_cmt
) + 5;
167 ErrorStatus
= WriteColors(&ptr
, &ptr_size
, &used_size
,
168 image
->colorTable
, image
->ncolors
, image
->cpp
);
170 if (ErrorStatus
!= XpmSuccess
)
174 * now we know the exact size we needed, realloc the data 4 = 1 (for
175 * '"') + 3 (for '",\n') 1 = - 2 is because the last line does not end
176 * with ',\n' + 3 (for '};\n')
178 ptr_size
+= image
->height
* (image
->width
* image
->cpp
+ 4) + 1;
180 p
= (char *) XpmRealloc(ptr
, ptr_size
);
186 if (cmts
&& info
->pixels_cmt
) {
187 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->pixels_cmt
);
188 used_size
+= strlen(info
->pixels_cmt
) + 5;
190 WritePixels(ptr
+ used_size
, &used_size
, image
->width
, image
->height
,
191 image
->cpp
, image
->data
, image
->colorTable
);
193 /* print extensions */
195 WriteExtensions(ptr
+ used_size
, &used_size
,
196 info
->extensions
, info
->nextensions
);
198 /* close the array */
199 sprintf(ptr
+ used_size
, "};\n");
201 *buffer_return
= ptr
;
207 WriteColors(char **dataptr
, unsigned int *data_size
, unsigned int *used_size
,
208 XpmColor
*colors
, unsigned int ncolors
, unsigned int cpp
)
211 unsigned int a
, key
, l
;
216 for (a
= 0; a
< ncolors
; a
++, colors
++) {
218 defaults
= (char **) colors
;
220 strncpy(s
, *defaults
++, cpp
);
223 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
224 if (s2
= *defaults
) {
225 sprintf(s
, "\t%s %s", xpmColorKeys
[key
- 1], s2
);
231 s
= (char *) XpmRealloc(*dataptr
, *data_size
+ l
);
233 return (XpmNoMemory
);
235 strcpy(s
+ *used_size
, buf
);
243 WritePixels(char *dataptr
, unsigned int *used_size
, unsigned int width
, unsigned int height
,
244 unsigned int cpp
, unsigned int *pixels
, XpmColor
*colors
)
247 unsigned int x
, y
, h
;
250 for (y
= 0; y
< h
; y
++) {
252 for (x
= 0; x
< width
; x
++, pixels
++) {
253 strncpy(s
, colors
[*pixels
].string
, cpp
);
259 /* duplicate some code to avoid a test in the loop */
261 for (x
= 0; x
< width
; x
++, pixels
++) {
262 strncpy(s
, colors
[*pixels
].string
, cpp
);
266 *used_size
+= s
- dataptr
;
270 ExtensionsSize(XpmExtension
*ext
, unsigned int num
)
272 unsigned int x
, y
, a
, size
;
276 for (x
= 0; x
< num
; x
++, ext
++) {
277 /* 11 = 10 (for ',\n"XPMEXT ') + 1 (for '"') */
278 size
+= strlen(ext
->name
) + 11;
280 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++)
281 /* 4 = 3 (for ',\n"') + 1 (for '"') */
282 size
+= strlen(*line
) + 4;
284 /* 13 is for ',\n"XPMENDEXT"' */
289 WriteExtensions(char *dataptr
, unsigned int *used_size
, XpmExtension
*ext
, unsigned int num
)
291 unsigned int x
, y
, a
;
295 for (x
= 0; x
< num
; x
++, ext
++) {
296 sprintf(s
, ",\n\"XPMEXT %s\"", ext
->name
);
297 s
+= strlen(ext
->name
) + 11;
299 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++) {
300 sprintf(s
, ",\n\"%s\"", *line
);
301 s
+= strlen(*line
) + 4;
304 strcpy(s
, ",\n\"XPMENDEXT\"");
305 *used_size
+= s
- dataptr
+ 13;
309 CommentsSize(XpmInfo
*info
)
313 /* 5 = 2 (for "/_*") + 3 (for "*_/\n") */
315 size
+= 5 + strlen(info
->hints_cmt
);
317 if (info
->colors_cmt
)
318 size
+= 5 + strlen(info
->colors_cmt
);
320 if (info
->pixels_cmt
)
321 size
+= 5 + strlen(info
->pixels_cmt
);