]>
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 XpmWriteFileFromImage(display
, filename
, image
, shapeimage
, attributes
)
68 XpmAttributes
*attributes
;
74 /* create an XpmImage from the image */
75 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
76 &xpmimage
, attributes
);
77 if (ErrorStatus
!= XpmSuccess
)
80 /* write the file from the XpmImage */
82 xpmSetInfo(&info
, attributes
);
83 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, &info
);
85 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, NULL
);
87 /* free the XpmImage */
88 XpmFreeXpmImage(&xpmimage
);
94 XpmWriteFileFromXpmImage(filename
, image
, info
)
100 char *name
, *dot
, *s
, new_name
[BUFSIZ
];
103 /* open file to write */
104 if ((ErrorStatus
= OpenWriteFile(filename
, &mdata
)) != XpmSuccess
)
105 return (ErrorStatus
);
107 /* figure out a name */
112 if (!(name
= rindex(filename
, '/'))
114 && !(name
= rindex(filename
, ':'))
121 /* let's try to make a valid C syntax name */
122 if (dot
= index(name
, '.')) {
123 strcpy(new_name
, name
);
124 /* change '.' to '_' */
126 while (dot
= index(s
, '.')) {
131 if (dot
= index(name
, '-')) {
132 if (name
!= new_name
) {
133 strcpy(new_name
, name
);
136 /* change '-' to '_' */
138 while (dot
= index(s
, '-')) {
146 /* write the XpmData from the XpmImage */
147 if (ErrorStatus
== XpmSuccess
)
148 ErrorStatus
= xpmWriteFile(mdata
.stream
.file
, image
, name
, info
);
150 xpmDataClose(&mdata
);
152 return (ErrorStatus
);
156 xpmWriteFile(file
, image
, name
, info
)
162 /* calculation variables */
163 unsigned int cmts
, extensions
;
166 cmts
= info
&& (info
->valuemask
& XpmComments
);
167 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
168 && info
->nextensions
;
170 /* print the header line */
171 fprintf(file
, "/* XPM */\nstatic char * %s[] = {\n", name
);
173 /* print the hints line */
174 if (cmts
&& info
->hints_cmt
)
175 fprintf(file
, "/*%s*/\n", info
->hints_cmt
);
177 fprintf(file
, "\"%d %d %d %d", image
->width
, image
->height
,
178 image
->ncolors
, image
->cpp
);
180 if (info
&& (info
->valuemask
& XpmHotspot
))
181 fprintf(file
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
184 fprintf(file
, " XPMEXT");
186 fprintf(file
, "\",\n");
189 if (cmts
&& info
->colors_cmt
)
190 fprintf(file
, "/*%s*/\n", info
->colors_cmt
);
192 WriteColors(file
, image
->colorTable
, image
->ncolors
);
195 if (cmts
&& info
->pixels_cmt
)
196 fprintf(file
, "/*%s*/\n", info
->pixels_cmt
);
198 ErrorStatus
= WritePixels(file
, image
->width
, image
->height
, image
->cpp
,
199 image
->data
, image
->colorTable
);
200 if (ErrorStatus
!= XpmSuccess
)
201 return (ErrorStatus
);
203 /* print extensions */
205 WriteExtensions(file
, info
->extensions
, info
->nextensions
);
207 /* close the array */
208 fprintf(file
, "};\n");
214 WriteColors(file
, colors
, ncolors
)
217 unsigned int ncolors
;
223 for (a
= 0; a
< ncolors
; a
++, colors
++) {
225 defaults
= (char **) colors
;
226 fprintf(file
, "\"%s", *defaults
++);
228 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
230 fprintf(file
, "\t%s %s", xpmColorKeys
[key
- 1], s
);
232 fprintf(file
, "\",\n");
238 WritePixels(file
, width
, height
, cpp
, pixels
, colors
)
243 unsigned int *pixels
;
247 unsigned int x
, y
, h
;
250 p
= buf
= (char *) XpmMalloc(width
* cpp
+ 3);
252 return (XpmNoMemory
);
255 for (y
= 0; y
< h
; y
++) {
257 for (x
= 0; x
< width
; x
++, pixels
++) {
258 strncpy(s
, colors
[*pixels
].string
, cpp
);
263 fprintf(file
, "%s,\n", buf
);
265 /* duplicate some code to avoid a test in the loop */
267 for (x
= 0; x
< width
; x
++, pixels
++) {
268 strncpy(s
, colors
[*pixels
].string
, cpp
);
273 fprintf(file
, "%s", buf
);
280 WriteExtensions(file
, ext
, num
)
285 unsigned int x
, y
, n
;
288 for (x
= 0; x
< num
; x
++, ext
++) {
289 fprintf(file
, ",\n\"XPMEXT %s\"", ext
->name
);
291 for (y
= 0, line
= ext
->lines
; y
< n
; y
++, line
++)
292 fprintf(file
, ",\n\"%s\"", *line
);
294 fprintf(file
, ",\n\"XPMENDEXT\"");
298 * open the given file to be written as an xpmData which is returned
301 OpenWriteFile(filename
, mdata
)
311 mdata
->stream
.file
= (stdout
);
312 mdata
->type
= XPMFILE
;
315 int len
= strlen(filename
);
316 if (len
> 2 && !strcmp(".Z", filename
+ (len
- 2))) {
317 sprintf(buf
, "compress > \"%s\"", filename
);
318 if (!(mdata
->stream
.file
= popen(buf
, "w")))
319 return (XpmOpenFailed
);
321 mdata
->type
= XPMPIPE
;
322 } else if (len
> 3 && !strcmp(".gz", filename
+ (len
- 3))) {
323 sprintf(buf
, "gzip -q > \"%s\"", filename
);
324 if (!(mdata
->stream
.file
= popen(buf
, "w")))
325 return (XpmOpenFailed
);
327 mdata
->type
= XPMPIPE
;
330 if (!(mdata
->stream
.file
= fopen(filename
, "w")))
331 return (XpmOpenFailed
);
333 mdata
->type
= XPMFILE
;
342 * close the file related to the xpmData if any
348 switch (mdata
->type
) {
350 if (mdata
->stream
.file
!= (stdout
))
351 fclose(mdata
->stream
.file
);
355 pclose(mdata
->stream
.file
);