]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/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
45 #if !defined(NO_ZPIPE) && defined(WIN32)
47 # define pclose _pclose
50 /* MS Windows define a function called WriteFile @#%#&!!! */
51 LFUNC(xpmWriteFile
, int, (FILE *file
, XpmImage
*image
, char *name
,
54 LFUNC(WriteColors
, void, (FILE *file
, XpmColor
*colors
, unsigned int ncolors
));
56 LFUNC(WritePixels
, int, (FILE *file
, unsigned int width
, unsigned int height
,
57 unsigned int cpp
, unsigned int *pixels
,
60 LFUNC(WriteExtensions
, void, (FILE *file
, XpmExtension
*ext
,
63 LFUNC(OpenWriteFile
, int, (char *filename
, xpmData
*mdata
));
64 LFUNC(xpmDataClose
, void, (xpmData
*mdata
));
67 XpmWriteFileFromImage(display
, filename
, image
, shapeimage
, attributes
)
72 XpmAttributes
*attributes
;
78 /* create an XpmImage from the image */
79 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
80 &xpmimage
, attributes
);
81 if (ErrorStatus
!= XpmSuccess
)
84 /* write the file from the XpmImage */
86 xpmSetInfo(&info
, attributes
);
87 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, &info
);
89 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, NULL
);
91 /* free the XpmImage */
92 XpmFreeXpmImage(&xpmimage
);
98 XpmWriteFileFromXpmImage(filename
, image
, info
)
104 char *name
, *dot
, *s
, new_name
[BUFSIZ
];
107 /* open file to write */
108 if ((ErrorStatus
= OpenWriteFile(filename
, &mdata
)) != XpmSuccess
)
109 return (ErrorStatus
);
111 /* figure out a name */
116 if (!(name
= rindex(filename
, '/'))
118 && !(name
= rindex(filename
, ':'))
125 /* let's try to make a valid C syntax name */
126 if (dot
= index(name
, '.')) {
127 strcpy(new_name
, name
);
128 /* change '.' to '_' */
130 while (dot
= index(s
, '.')) {
135 if (dot
= index(name
, '-')) {
136 if (name
!= new_name
) {
137 strcpy(new_name
, name
);
140 /* change '-' to '_' */
142 while (dot
= index(s
, '-')) {
150 /* write the XpmData from the XpmImage */
151 if (ErrorStatus
== XpmSuccess
)
152 ErrorStatus
= xpmWriteFile(mdata
.stream
.file
, image
, name
, info
);
154 xpmDataClose(&mdata
);
156 return (ErrorStatus
);
160 xpmWriteFile(file
, image
, name
, info
)
166 /* calculation variables */
167 unsigned int cmts
, extensions
;
170 cmts
= info
&& (info
->valuemask
& XpmComments
);
171 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
172 && info
->nextensions
;
174 /* print the header line */
175 fprintf(file
, "/* XPM */\nstatic char * %s[] = {\n", name
);
177 /* print the hints line */
178 if (cmts
&& info
->hints_cmt
)
179 fprintf(file
, "/*%s*/\n", info
->hints_cmt
);
181 fprintf(file
, "\"%d %d %d %d", image
->width
, image
->height
,
182 image
->ncolors
, image
->cpp
);
184 if (info
&& (info
->valuemask
& XpmHotspot
))
185 fprintf(file
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
188 fprintf(file
, " XPMEXT");
190 fprintf(file
, "\",\n");
193 if (cmts
&& info
->colors_cmt
)
194 fprintf(file
, "/*%s*/\n", info
->colors_cmt
);
196 WriteColors(file
, image
->colorTable
, image
->ncolors
);
199 if (cmts
&& info
->pixels_cmt
)
200 fprintf(file
, "/*%s*/\n", info
->pixels_cmt
);
202 ErrorStatus
= WritePixels(file
, image
->width
, image
->height
, image
->cpp
,
203 image
->data
, image
->colorTable
);
204 if (ErrorStatus
!= XpmSuccess
)
205 return (ErrorStatus
);
207 /* print extensions */
209 WriteExtensions(file
, info
->extensions
, info
->nextensions
);
211 /* close the array */
212 fprintf(file
, "};\n");
218 WriteColors(file
, colors
, ncolors
)
221 unsigned int ncolors
;
227 for (a
= 0; a
< ncolors
; a
++, colors
++) {
229 defaults
= (char **) colors
;
230 fprintf(file
, "\"%s", *defaults
++);
232 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
234 fprintf(file
, "\t%s %s", xpmColorKeys
[key
- 1], s
);
236 fprintf(file
, "\",\n");
242 WritePixels(file
, width
, height
, cpp
, pixels
, colors
)
247 unsigned int *pixels
;
251 unsigned int x
, y
, h
;
254 p
= buf
= (char *) XpmMalloc(width
* cpp
+ 3);
256 return (XpmNoMemory
);
259 for (y
= 0; y
< h
; y
++) {
261 for (x
= 0; x
< width
; x
++, pixels
++) {
262 strncpy(s
, colors
[*pixels
].string
, cpp
);
267 fprintf(file
, "%s,\n", buf
);
269 /* duplicate some code to avoid a test in the loop */
271 for (x
= 0; x
< width
; x
++, pixels
++) {
272 strncpy(s
, colors
[*pixels
].string
, cpp
);
277 fprintf(file
, "%s", buf
);
284 WriteExtensions(file
, ext
, num
)
289 unsigned int x
, y
, n
;
292 for (x
= 0; x
< num
; x
++, ext
++) {
293 fprintf(file
, ",\n\"XPMEXT %s\"", ext
->name
);
295 for (y
= 0, line
= ext
->lines
; y
< n
; y
++, line
++)
296 fprintf(file
, ",\n\"%s\"", *line
);
298 fprintf(file
, ",\n\"XPMENDEXT\"");
302 * open the given file to be written as an xpmData which is returned
305 OpenWriteFile(filename
, mdata
)
315 mdata
->stream
.file
= (stdout
);
316 mdata
->type
= XPMFILE
;
319 int len
= strlen(filename
);
320 if (len
> 2 && !strcmp(".Z", filename
+ (len
- 2))) {
321 sprintf(buf
, "compress > \"%s\"", filename
);
322 if (!(mdata
->stream
.file
= popen(buf
, "w")))
323 return (XpmOpenFailed
);
325 mdata
->type
= XPMPIPE
;
326 } else if (len
> 3 && !strcmp(".gz", filename
+ (len
- 3))) {
327 sprintf(buf
, "gzip -q > \"%s\"", filename
);
328 if (!(mdata
->stream
.file
= popen(buf
, "w")))
329 return (XpmOpenFailed
);
331 mdata
->type
= XPMPIPE
;
334 if (!(mdata
->stream
.file
= fopen(filename
, "w")))
335 return (XpmOpenFailed
);
337 mdata
->type
= XPMFILE
;
346 * close the file related to the xpmData if any
352 switch (mdata
->type
) {
354 if (mdata
->stream
.file
!= (stdout
))
355 fclose(mdata
->stream
.file
);
359 pclose(mdata
->stream
.file
);