]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/CrBufFrI.c
2 * Copyright (C) 1989-95 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 LFUNC(WriteColors
, int, (char **dataptr
, unsigned int *data_size
,
38 unsigned int *used_size
, XpmColor
*colors
,
39 unsigned int ncolors
, unsigned int cpp
));
41 LFUNC(WritePixels
, void, (char *dataptr
, unsigned int *used_size
,
42 unsigned int width
, unsigned int height
,
43 unsigned int cpp
, unsigned int *pixels
,
46 LFUNC(WriteExtensions
, void, (char *dataptr
, unsigned int *used_size
,
47 XpmExtension
*ext
, unsigned int num
));
49 LFUNC(ExtensionsSize
, int, (XpmExtension
*ext
, unsigned int num
));
50 LFUNC(CommentsSize
, int, (XpmInfo
*info
));
53 XpmCreateBufferFromImage(display
, buffer_return
, image
, shapeimage
, attributes
)
58 XpmAttributes
*attributes
;
64 /* initialize return value */
66 *buffer_return
= NULL
;
68 /* create an XpmImage from the image */
69 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
70 &xpmimage
, attributes
);
71 if (ErrorStatus
!= XpmSuccess
)
74 /* create the buffer from the XpmImage */
76 xpmSetInfo(&info
, attributes
);
78 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, &info
);
81 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, NULL
);
83 /* free the XpmImage */
84 XpmFreeXpmImage(&xpmimage
);
91 #define RETURN(status) \
93 ErrorStatus = status; \
98 XpmCreateBufferFromXpmImage(buffer_return
, image
, info
)
103 /* calculation variables */
106 unsigned int cmts
, extensions
, ext_size
= 0;
107 unsigned int l
, cmt_size
= 0;
108 char *ptr
= NULL
, *p
;
109 unsigned int ptr_size
, used_size
;
111 *buffer_return
= NULL
;
113 cmts
= info
&& (info
->valuemask
& XpmComments
);
114 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
115 && info
->nextensions
;
117 /* compute the extensions and comments size */
119 ext_size
= ExtensionsSize(info
->extensions
, info
->nextensions
);
121 cmt_size
= CommentsSize(info
);
123 /* write the header line */
127 sprintf(buf
, "/* XPM */\nstatic char * image_name[] = {\n");
129 used_size
= strlen(buf
);
131 ptr_size
= used_size
+ ext_size
+ cmt_size
+ 1;
132 ptr
= (char *) XpmMalloc(ptr_size
);
137 /* write the values line */
138 if (cmts
&& info
->hints_cmt
) {
142 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->hints_cmt
);
144 used_size
+= strlen(info
->hints_cmt
) + 5;
150 sprintf(buf
, "\"%d %d %d %d", image
->width
, image
->height
,
151 image
->ncolors
, image
->cpp
);
156 if (info
&& (info
->valuemask
& XpmHotspot
)) {
160 sprintf(buf
+ l
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
169 sprintf(buf
+ l
, " XPMEXT");
177 sprintf(buf
+ l
, "\",\n");
182 p
= (char *) XpmRealloc(ptr
, ptr_size
);
186 strcpy(ptr
+ used_size
, buf
);
190 if (cmts
&& info
->colors_cmt
) {
194 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->colors_cmt
);
196 used_size
+= strlen(info
->colors_cmt
) + 5;
199 ErrorStatus
= WriteColors(&ptr
, &ptr_size
, &used_size
,
200 image
->colorTable
, image
->ncolors
, image
->cpp
);
202 if (ErrorStatus
!= XpmSuccess
)
206 * now we know the exact size we need, realloc the data
207 * 4 = 1 (for '"') + 3 (for '",\n')
208 * 1 = - 2 (because the last line does not end with ',\n') + 3 (for '};\n')
210 ptr_size
+= image
->height
* (image
->width
* image
->cpp
+ 4) + 1;
212 p
= (char *) XpmRealloc(ptr
, ptr_size
);
218 if (cmts
&& info
->pixels_cmt
) {
222 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->pixels_cmt
);
224 used_size
+= strlen(info
->pixels_cmt
) + 5;
227 WritePixels(ptr
+ used_size
, &used_size
, image
->width
, image
->height
,
228 image
->cpp
, image
->data
, image
->colorTable
);
230 /* print extensions */
232 WriteExtensions(ptr
+ used_size
, &used_size
,
233 info
->extensions
, info
->nextensions
);
235 /* close the array */
236 strcpy(ptr
+ used_size
, "};\n");
238 *buffer_return
= ptr
;
242 /* exit point in case of error, free only locally allocated variables */
246 return (ErrorStatus
);
250 WriteColors(dataptr
, data_size
, used_size
, colors
, ncolors
, cpp
)
252 unsigned int *data_size
;
253 unsigned int *used_size
;
255 unsigned int ncolors
;
259 unsigned int a
, key
, l
;
264 for (a
= 0; a
< ncolors
; a
++, colors
++) {
266 defaults
= (char **) colors
;
268 strncpy(s
, *defaults
++, cpp
);
271 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
272 if ((s2
= *defaults
)!=NULL
)
277 sprintf(s
, "\t%s %s", xpmColorKeys
[key
- 1], s2
);
285 s
= (char *) XpmRealloc(*dataptr
, *data_size
+ l
);
287 return (XpmNoMemory
);
289 strcpy(s
+ *used_size
, buf
);
297 WritePixels(dataptr
, used_size
, width
, height
, cpp
, pixels
, colors
)
299 unsigned int *used_size
;
303 unsigned int *pixels
;
307 unsigned int x
, y
, h
;
310 for (y
= 0; y
< h
; y
++) {
312 for (x
= 0; x
< width
; x
++, pixels
++) {
313 strncpy(s
, colors
[*pixels
].string
, cpp
);
319 /* duplicate some code to avoid a test in the loop */
321 for (x
= 0; x
< width
; x
++, pixels
++) {
322 strncpy(s
, colors
[*pixels
].string
, cpp
);
326 *used_size
+= s
- dataptr
;
330 ExtensionsSize(ext
, num
)
334 unsigned int x
, y
, a
, size
;
338 for (x
= 0; x
< num
; x
++, ext
++) {
339 /* 11 = 10 (for ',\n"XPMEXT ') + 1 (for '"') */
340 size
+= strlen(ext
->name
) + 11;
342 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++)
343 /* 4 = 3 (for ',\n"') + 1 (for '"') */
344 size
+= strlen(*line
) + 4;
346 /* 13 is for ',\n"XPMENDEXT"' */
351 WriteExtensions(dataptr
, used_size
, ext
, num
)
353 unsigned int *used_size
;
357 unsigned int x
, y
, a
;
361 for (x
= 0; x
< num
; x
++, ext
++) {
365 sprintf(s
, ",\n\"XPMEXT %s\"", ext
->name
);
367 s
+= strlen(ext
->name
) + 11;
370 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++) {
374 sprintf(s
, ",\n\"%s\"", *line
);
376 s
+= strlen(*line
) + 4;
380 strcpy(s
, ",\n\"XPMENDEXT\"");
381 *used_size
+= s
- dataptr
+ 13;
390 /* 5 = 2 (for "/_*") + 3 (for "*_/\n") */
392 size
+= 5 + strlen(info
->hints_cmt
);
394 if (info
->colors_cmt
)
395 size
+= 5 + strlen(info
->colors_cmt
);
397 if (info
->pixels_cmt
)
398 size
+= 5 + strlen(info
->pixels_cmt
);