]>
git.saurik.com Git - wxWidgets.git/blob - src/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 /* Visual Age cannot deal with old, non-ansi, code */
54 int XpmCreateBufferFromImage(
56 , char** buffer_return
59 , XpmAttributes
* attributes
63 XpmCreateBufferFromImage(display
, buffer_return
, image
, shapeimage
, attributes
)
68 XpmAttributes
*attributes
;
75 /* initialize return value */
77 *buffer_return
= NULL
;
79 /* create an XpmImage from the image */
80 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
81 &xpmimage
, attributes
);
82 if (ErrorStatus
!= XpmSuccess
)
85 /* create the buffer from the XpmImage */
87 xpmSetInfo(&info
, attributes
);
89 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, &info
);
92 XpmCreateBufferFromXpmImage(buffer_return
, &xpmimage
, NULL
);
94 /* free the XpmImage */
95 XpmFreeXpmImage(&xpmimage
);
102 #define RETURN(status) \
104 ErrorStatus = status; \
109 /* Visual Age cannot deal with old, non-ansi, code */
110 int XpmCreateBufferFromXpmImage(char** buffer_return
, XpmImage
* image
, XpmInfo
* info
)
113 XpmCreateBufferFromXpmImage(buffer_return
, image
, info
)
114 char **buffer_return
;
119 /* calculation variables */
122 unsigned int cmts
, extensions
, ext_size
= 0;
123 unsigned int l
, cmt_size
= 0;
124 char *ptr
= NULL
, *p
;
125 unsigned int ptr_size
, used_size
;
127 *buffer_return
= NULL
;
129 cmts
= info
&& (info
->valuemask
& XpmComments
);
130 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
131 && info
->nextensions
;
133 /* compute the extensions and comments size */
135 ext_size
= ExtensionsSize(info
->extensions
, info
->nextensions
);
137 cmt_size
= CommentsSize(info
);
139 /* write the header line */
143 sprintf(buf
, "/* XPM */\nstatic char * image_name[] = {\n");
145 used_size
= strlen(buf
);
147 ptr_size
= used_size
+ ext_size
+ cmt_size
+ 1;
148 ptr
= (char *) XpmMalloc(ptr_size
);
153 /* write the values line */
154 if (cmts
&& info
->hints_cmt
) {
158 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->hints_cmt
);
160 used_size
+= strlen(info
->hints_cmt
) + 5;
166 sprintf(buf
, "\"%d %d %d %d", image
->width
, image
->height
,
167 image
->ncolors
, image
->cpp
);
172 if (info
&& (info
->valuemask
& XpmHotspot
)) {
176 sprintf(buf
+ l
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
185 sprintf(buf
+ l
, " XPMEXT");
193 sprintf(buf
+ l
, "\",\n");
198 p
= (char *) XpmRealloc(ptr
, ptr_size
);
202 strcpy(ptr
+ used_size
, buf
);
206 if (cmts
&& info
->colors_cmt
) {
210 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->colors_cmt
);
212 used_size
+= strlen(info
->colors_cmt
) + 5;
215 ErrorStatus
= WriteColors(&ptr
, &ptr_size
, &used_size
,
216 image
->colorTable
, image
->ncolors
, image
->cpp
);
218 if (ErrorStatus
!= XpmSuccess
)
222 * now we know the exact size we need, realloc the data
223 * 4 = 1 (for '"') + 3 (for '",\n')
224 * 1 = - 2 (because the last line does not end with ',\n') + 3 (for '};\n')
226 ptr_size
+= image
->height
* (image
->width
* image
->cpp
+ 4) + 1;
228 p
= (char *) XpmRealloc(ptr
, ptr_size
);
234 if (cmts
&& info
->pixels_cmt
) {
238 sprintf(ptr
+ used_size
, "/*%s*/\n", info
->pixels_cmt
);
240 used_size
+= strlen(info
->pixels_cmt
) + 5;
243 WritePixels(ptr
+ used_size
, &used_size
, image
->width
, image
->height
,
244 image
->cpp
, image
->data
, image
->colorTable
);
246 /* print extensions */
248 WriteExtensions(ptr
+ used_size
, &used_size
,
249 info
->extensions
, info
->nextensions
);
251 /* close the array */
252 strcpy(ptr
+ used_size
, "};\n");
254 *buffer_return
= ptr
;
258 /* exit point in case of error, free only locally allocated variables */
262 return (ErrorStatus
);
266 /* Visual Age cannot deal with old, non-ansi, code */
267 static int WriteColors(
269 , unsigned int* data_size
270 , unsigned int* used_size
272 , unsigned int ncolors
277 WriteColors(dataptr
, data_size
, used_size
, colors
, ncolors
, cpp
)
279 unsigned int *data_size
;
280 unsigned int *used_size
;
282 unsigned int ncolors
;
287 unsigned int a
, key
, l
;
292 for (a
= 0; a
< ncolors
; a
++, colors
++) {
294 defaults
= (char **) colors
;
296 strncpy(s
, *defaults
++, cpp
);
299 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
300 if (s2
= *defaults
) {
304 sprintf(s
, "\t%s %s", xpmColorKeys
[key
- 1], s2
);
312 s
= (char *) XpmRealloc(*dataptr
, *data_size
+ l
);
314 return (XpmNoMemory
);
316 strcpy(s
+ *used_size
, buf
);
324 /* Visual Age cannot deal with old, non-ansi, code */
325 static void WritePixels(
327 , unsigned int* used_size
329 , unsigned int height
331 , unsigned int* pixels
336 WritePixels(dataptr
, used_size
, width
, height
, cpp
, pixels
, colors
)
338 unsigned int *used_size
;
342 unsigned int *pixels
;
347 unsigned int x
, y
, h
;
350 for (y
= 0; y
< h
; y
++) {
352 for (x
= 0; x
< width
; x
++, pixels
++) {
353 strncpy(s
, colors
[*pixels
].string
, cpp
);
359 /* duplicate some code to avoid a test in the loop */
361 for (x
= 0; x
< width
; x
++, pixels
++) {
362 strncpy(s
, colors
[*pixels
].string
, cpp
);
366 *used_size
+= s
- dataptr
;
370 /* Visual Age cannot deal with old, non-ansi, code */
372 ExtensionsSize(XpmExtension
* ext
, unsigned int num
)
375 ExtensionsSize(ext
, num
)
380 unsigned int x
, y
, a
, size
;
384 for (x
= 0; x
< num
; x
++, ext
++) {
385 /* 11 = 10 (for ',\n"XPMEXT ') + 1 (for '"') */
386 size
+= strlen(ext
->name
) + 11;
388 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++)
389 /* 4 = 3 (for ',\n"') + 1 (for '"') */
390 size
+= strlen(*line
) + 4;
392 /* 13 is for ',\n"XPMENDEXT"' */
397 /* Visual Age cannot deal with old, non-ansi, code */
398 static void WriteExtensions(
400 , unsigned int* used_size
406 WriteExtensions(dataptr
, used_size
, ext
, num
)
408 unsigned int *used_size
;
413 unsigned int x
, y
, a
;
417 for (x
= 0; x
< num
; x
++, ext
++) {
421 sprintf(s
, ",\n\"XPMEXT %s\"", ext
->name
);
423 s
+= strlen(ext
->name
) + 11;
426 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++) {
430 sprintf(s
, ",\n\"%s\"", *line
);
432 s
+= strlen(*line
) + 4;
436 strcpy(s
, ",\n\"XPMENDEXT\"");
437 *used_size
+= s
- dataptr
+ 13;
441 /* Visual Age cannot deal with old, non-ansi, code */
442 static int CommentsSize(XpmInfo
* info
)
444 static int CommentsSize(info
)
450 /* 5 = 2 (for "/_*") + 3 (for "*_/\n") */
452 size
+= 5 + strlen(info
->hints_cmt
);
454 if (info
->colors_cmt
)
455 size
+= 5 + strlen(info
->colors_cmt
);
457 if (info
->pixels_cmt
)
458 size
+= 5 + strlen(info
->pixels_cmt
);