]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/WrFFrI.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 * Write an image and possibly its mask to an XPM file *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
36 * The code related to AMIGA has been added by
37 * Lorens Younes (d93-hyo@nada.kth.se) 4/96
41 #if !defined(NO_ZPIPE) && defined(WIN32)
43 # define pclose _pclose
46 /* MS Windows define a function called WriteFile @#%#&!!! */
47 LFUNC(xpmWriteFile
, int, (FILE *file
, XpmImage
*image
, char *name
,
50 LFUNC(WriteColors
, void, (FILE *file
, XpmColor
*colors
, unsigned int ncolors
));
52 LFUNC(WritePixels
, int, (FILE *file
, unsigned int width
, unsigned int height
,
53 unsigned int cpp
, unsigned int *pixels
,
56 LFUNC(WriteExtensions
, void, (FILE *file
, XpmExtension
*ext
,
59 LFUNC(OpenWriteFile
, int, (char *filename
, xpmData
*mdata
));
60 LFUNC(xpmDataClose
, void, (xpmData
*mdata
));
63 /* Visual Age cannot deal with old, non-ansi, code */
64 int XpmWriteFileFromImage(
69 , XpmAttributes
* attributes
73 XpmWriteFileFromImage(display
, filename
, image
, shapeimage
, attributes
)
78 XpmAttributes
*attributes
;
85 /* create an XpmImage from the image */
86 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
87 &xpmimage
, attributes
);
88 if (ErrorStatus
!= XpmSuccess
)
91 /* write the file from the XpmImage */
93 xpmSetInfo(&info
, attributes
);
94 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, &info
);
96 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, NULL
);
98 /* free the XpmImage */
99 XpmFreeXpmImage(&xpmimage
);
101 return (ErrorStatus
);
105 /* Visual Age cannot deal with old, non-ansi, code */
106 int XpmWriteFileFromXpmImage(
113 XpmWriteFileFromXpmImage(filename
, image
, info
)
120 char *name
, *dot
, *s
, new_name
[BUFSIZ
];
123 /* open file to write */
124 if ((ErrorStatus
= OpenWriteFile(filename
, &mdata
)) != XpmSuccess
)
125 return (ErrorStatus
);
127 /* figure out a name */
132 if (!(name
= rindex(filename
, '/'))
134 && !(name
= rindex(filename
, ':'))
141 /* let's try to make a valid C syntax name */
142 if (dot
= index(name
, '.')) {
143 strcpy(new_name
, name
);
144 /* change '.' to '_' */
146 while (dot
= index(s
, '.')) {
151 if (dot
= index(name
, '-')) {
152 if (name
!= new_name
) {
153 strcpy(new_name
, name
);
156 /* change '-' to '_' */
158 while (dot
= index(s
, '-')) {
166 /* write the XpmData from the XpmImage */
167 if (ErrorStatus
== XpmSuccess
)
168 ErrorStatus
= xpmWriteFile(mdata
.stream
.file
, image
, name
, info
);
170 xpmDataClose(&mdata
);
172 return (ErrorStatus
);
176 /* Visual Age cannot deal with old, non-ansi, code */
186 xpmWriteFile(file
, image
, name
, info
)
193 /* calculation variables */
194 unsigned int cmts
, extensions
;
197 cmts
= info
&& (info
->valuemask
& XpmComments
);
198 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
199 && info
->nextensions
;
201 /* print the header line */
202 fprintf(file
, "/* XPM */\nstatic char * %s[] = {\n", name
);
204 /* print the hints line */
205 if (cmts
&& info
->hints_cmt
)
206 fprintf(file
, "/*%s*/\n", info
->hints_cmt
);
208 fprintf(file
, "\"%d %d %d %d", image
->width
, image
->height
,
209 image
->ncolors
, image
->cpp
);
211 if (info
&& (info
->valuemask
& XpmHotspot
))
212 fprintf(file
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
215 fprintf(file
, " XPMEXT");
217 fprintf(file
, "\",\n");
220 if (cmts
&& info
->colors_cmt
)
221 fprintf(file
, "/*%s*/\n", info
->colors_cmt
);
223 WriteColors(file
, image
->colorTable
, image
->ncolors
);
226 if (cmts
&& info
->pixels_cmt
)
227 fprintf(file
, "/*%s*/\n", info
->pixels_cmt
);
229 ErrorStatus
= WritePixels(file
, image
->width
, image
->height
, image
->cpp
,
230 image
->data
, image
->colorTable
);
231 if (ErrorStatus
!= XpmSuccess
)
232 return (ErrorStatus
);
234 /* print extensions */
236 WriteExtensions(file
, info
->extensions
, info
->nextensions
);
238 /* close the array */
239 fprintf(file
, "};\n");
245 /* Visual Age cannot deal with old, non-ansi, code */
250 , unsigned int ncolors
254 WriteColors(file
, colors
, ncolors
)
257 unsigned int ncolors
;
264 for (a
= 0; a
< ncolors
; a
++, colors
++) {
266 defaults
= (char **) colors
;
267 fprintf(file
, "\"%s", *defaults
++);
269 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
271 fprintf(file
, "\t%s %s", xpmColorKeys
[key
- 1], s
);
273 fprintf(file
, "\",\n");
278 /* Visual Age cannot deal with old, non-ansi, code */
279 static int WritePixels(
282 , unsigned int height
284 , unsigned int* pixels
289 WritePixels(file
, width
, height
, cpp
, pixels
, colors
)
294 unsigned int *pixels
;
299 unsigned int x
, y
, h
;
302 p
= buf
= (char *) XpmMalloc(width
* cpp
+ 3);
304 return (XpmNoMemory
);
307 for (y
= 0; y
< h
; y
++) {
309 for (x
= 0; x
< width
; x
++, pixels
++) {
310 strncpy(s
, colors
[*pixels
].string
, cpp
);
315 fprintf(file
, "%s,\n", buf
);
317 /* duplicate some code to avoid a test in the loop */
319 for (x
= 0; x
< width
; x
++, pixels
++) {
320 strncpy(s
, colors
[*pixels
].string
, cpp
);
325 fprintf(file
, "%s", buf
);
332 /* Visual Age cannot deal with old, non-ansi, code */
333 static void WriteExtensions(
340 WriteExtensions(file
, ext
, num
)
346 unsigned int x
, y
, n
;
349 for (x
= 0; x
< num
; x
++, ext
++) {
350 fprintf(file
, ",\n\"XPMEXT %s\"", ext
->name
);
352 for (y
= 0, line
= ext
->lines
; y
< n
; y
++, line
++)
353 fprintf(file
, ",\n\"%s\"", *line
);
355 fprintf(file
, ",\n\"XPMENDEXT\"");
359 * open the given file to be written as an xpmData which is returned
363 #define pclose fclose
364 /* Visual Age cannot deal with old, non-ansi, code */
365 static int OpenWriteFile(
371 OpenWriteFile(filename
, mdata
)
382 mdata
->stream
.file
= (stdout
);
383 mdata
->type
= XPMFILE
;
386 int len
= strlen(filename
);
387 if (len
> 2 && !strcmp(".Z", filename
+ (len
- 2))) {
388 sprintf(buf
, "compress > \"%s\"", filename
);
389 if (!(mdata
->stream
.file
= popen(buf
, "w")))
390 return (XpmOpenFailed
);
392 mdata
->type
= XPMPIPE
;
393 } else if (len
> 3 && !strcmp(".gz", filename
+ (len
- 3))) {
394 sprintf(buf
, "gzip -q > \"%s\"", filename
);
395 if (!(mdata
->stream
.file
= popen(buf
, "w")))
396 return (XpmOpenFailed
);
398 mdata
->type
= XPMPIPE
;
401 if (!(mdata
->stream
.file
= fopen(filename
, "w")))
402 return (XpmOpenFailed
);
404 mdata
->type
= XPMFILE
;
413 * close the file related to the xpmData if any
416 /* Visual Age cannot deal with old, non-ansi, code */
417 static void xpmDataClose(xpmData
* mdata
)
424 switch (mdata
->type
) {
426 if (mdata
->stream
.file
!= (stdout
))
427 fclose(mdata
->stream
.file
);
431 pclose(mdata
->stream
.file
);