]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/wrffri.c
2 * Copyright (C) 1989-94 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 \*****************************************************************************/
42 LFUNC(WriteColors
, void, (FILE *file
, XpmColor
*colors
, unsigned int ncolors
));
44 LFUNC(WritePixels
, int, (FILE *file
, unsigned int width
, unsigned int height
,
45 unsigned int cpp
, unsigned int *pixels
,
48 LFUNC(WriteExtensions
, void, (FILE *file
, XpmExtension
*ext
,
52 XpmWriteFileFromImage(Display
*display
, char *filename
, XImage
*image
, XImage
*shapeimage
, XpmAttributes
*attributes
)
58 /* create an XpmImage from the image */
59 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
60 &xpmimage
, attributes
);
61 if (ErrorStatus
!= XpmSuccess
)
64 /* write the file from the XpmImage */
66 xpmSetInfo(&info
, attributes
);
67 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, &info
);
69 ErrorStatus
= XpmWriteFileFromXpmImage(filename
, &xpmimage
, NULL
);
71 /* free the XpmImage */
72 XpmFreeXpmImage(&xpmimage
);
78 XpmWriteFileFromXpmImage(char *filename
, XpmImage
*image
, XpmInfo
*info
)
81 char *name
, *dot
, *s
, new_name
[BUFSIZ
];
85 /* open file to write */
86 if ((ErrorStatus
= xpmWriteFile(filename
, &mdata
)) != XpmSuccess
)
89 /* figure out a name */
95 if (!(name
= strchr(filename
, '\\')))
97 if (!(name
= strchr(filename
, '/')))
103 if (dot
= strchr(name
, '.')) {
104 strcpy(new_name
, name
);
106 // Convert to lower case
107 len
= strlen(new_name
);
108 for (i
= 0; i
< len
; i
++)
109 new_name
[i
] = tolower(new_name
[i
]);
111 /* change '.' to '_' to get a valid C syntax name */
113 while (dot
= strchr(s
, '.')) {
121 /* write the XpmData from the XpmImage */
122 if (ErrorStatus
== XpmSuccess
)
123 ErrorStatus
= xpmWriteData(&mdata
, image
, name
, info
);
125 xpmDataClose(&mdata
);
127 return (ErrorStatus
);
131 xpmWriteData(xpmData
*mdata
, XpmImage
*image
, char *name
, XpmInfo
*info
)
133 /* calculation variables */
134 unsigned int cmts
, extensions
;
138 /* store this to speed up */
139 file
= mdata
->stream
.file
;
141 cmts
= info
&& (info
->valuemask
& XpmComments
);
142 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
143 && info
->nextensions
;
145 /* print the header line */
146 fprintf(file
, "/* XPM */\nstatic char * %s[] = {\n", name
);
148 /* print the hints line */
149 if (cmts
&& info
->hints_cmt
)
150 fprintf(file
, "/*%s*/\n", info
->hints_cmt
);
152 fprintf(file
, "\"%d %d %d %d", image
->width
, image
->height
,
153 image
->ncolors
, image
->cpp
);
155 if (info
&& (info
->valuemask
& XpmHotspot
))
156 fprintf(file
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
159 fprintf(file
, " XPMEXT");
161 fprintf(file
, "\",\n");
164 if (cmts
&& info
->colors_cmt
)
165 fprintf(file
, "/*%s*/\n", info
->colors_cmt
);
167 WriteColors(file
, image
->colorTable
, image
->ncolors
);
170 if (cmts
&& info
->pixels_cmt
)
171 fprintf(file
, "/*%s*/\n", info
->pixels_cmt
);
173 ErrorStatus
= WritePixels(file
, image
->width
, image
->height
, image
->cpp
,
174 image
->data
, image
->colorTable
);
175 if (ErrorStatus
!= XpmSuccess
)
176 return (ErrorStatus
);
178 /* print extensions */
180 WriteExtensions(file
, info
->extensions
, info
->nextensions
);
182 /* close the array */
183 fprintf(file
, "};\n");
189 WriteColors(FILE *file
, XpmColor
*colors
, unsigned int ncolors
)
195 for (a
= 0; a
< ncolors
; a
++, colors
++) {
197 defaults
= (char **) colors
;
198 fprintf(file
, "\"%s", *defaults
++);
200 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
202 fprintf(file
, "\t%s %s", xpmColorKeys
[key
- 1], s
);
204 fprintf(file
, "\",\n");
210 WritePixels(FILE *file
, unsigned int width
, unsigned int height
, unsigned int cpp
, unsigned int *pixels
, XpmColor
*colors
)
213 unsigned int x
, y
, h
;
216 p
= buf
= (char *) XpmMalloc(width
* cpp
+ 3);
218 return (XpmNoMemory
);
221 for (y
= 0; y
< h
; y
++) {
223 for (x
= 0; x
< width
; x
++, pixels
++) {
224 strncpy(s
, colors
[*pixels
].string
, cpp
);
229 fprintf(file
, "%s,\n", buf
);
231 /* duplicate some code to avoid a test in the loop */
233 for (x
= 0; x
< width
; x
++, pixels
++) {
234 strncpy(s
, colors
[*pixels
].string
, cpp
);
239 fprintf(file
, "%s", buf
);
246 WriteExtensions(FILE *file
, XpmExtension
*ext
, unsigned int num
)
248 unsigned int x
, y
, n
;
251 for (x
= 0; x
< num
; x
++, ext
++) {
252 fprintf(file
, ",\n\"XPMEXT %s\"", ext
->name
);
254 for (y
= 0, line
= ext
->lines
; y
< n
; y
++, line
++)
255 fprintf(file
, ",\n\"%s\"", *line
);
257 fprintf(file
, ",\n\"XPMENDEXT\"");