]> git.saurik.com Git - wxWidgets.git/blob - src/xpm/wrffri.c
fixed compilation and added support for all 4 images (as a side effect) to the
[wxWidgets.git] / src / xpm / wrffri.c
1 /*
2 * Copyright (C) 1989-94 GROUPE BULL
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
20 *
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.
24 */
25
26 /*****************************************************************************\
27 * XpmWrFFrI.c: *
28 * *
29 * XPM library *
30 * Write an image and possibly its mask to an XPM file *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 #include "xpm34p.h"
36 #ifdef FOR_MSW
37 #include "ctype.h"
38 #endif
39
40 #include <string.h>
41
42 LFUNC(WriteColors, void, (FILE *file, XpmColor *colors, unsigned int ncolors));
43
44 LFUNC(WritePixels, int, (FILE *file, unsigned int width, unsigned int height,
45 unsigned int cpp, unsigned int *pixels,
46 XpmColor *colors));
47
48 LFUNC(WriteExtensions, void, (FILE *file, XpmExtension *ext,
49 unsigned int num));
50
51 int
52 XpmWriteFileFromImage(Display *display, char *filename, XImage *image, XImage *shapeimage, XpmAttributes *attributes)
53 {
54 XpmImage xpmimage;
55 XpmInfo info;
56 int ErrorStatus;
57
58 /* create an XpmImage from the image */
59 ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
60 &xpmimage, attributes);
61 if (ErrorStatus != XpmSuccess)
62 return (ErrorStatus);
63
64 /* write the file from the XpmImage */
65 if (attributes) {
66 xpmSetInfo(&info, attributes);
67 ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, &info);
68 } else
69 ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, NULL);
70
71 /* free the XpmImage */
72 XpmFreeXpmImage(&xpmimage);
73
74 return (ErrorStatus);
75 }
76
77 int
78 XpmWriteFileFromXpmImage(char *filename, XpmImage *image, XpmInfo *info)
79 {
80 xpmData mdata;
81 char *name, *dot, *s, new_name[BUFSIZ];
82 int ErrorStatus;
83 int len, i;
84
85 /* open file to write */
86 if ((ErrorStatus = xpmWriteFile(filename, &mdata)) != XpmSuccess)
87 return (ErrorStatus);
88
89 /* figure out a name */
90 if (filename) {
91 #ifdef VMS
92 name = filename;
93 #else
94 #ifdef FOR_MSW
95 if (!(name = strchr(filename, '\\')))
96 #else
97 if (!(name = strchr(filename, '/')))
98 #endif
99 name = filename;
100 else
101 name++;
102 #endif
103 if (dot = strchr(name, '.')) {
104 strcpy(new_name, name);
105 #ifdef FOR_MSW
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]);
110 #endif
111 /* change '.' to '_' to get a valid C syntax name */
112 name = s = new_name;
113 while (dot = strchr(s, '.')) {
114 *dot = '_';
115 s = dot;
116 }
117 }
118 } else
119 name = "image_name";
120
121 /* write the XpmData from the XpmImage */
122 if (ErrorStatus == XpmSuccess)
123 ErrorStatus = xpmWriteData(&mdata, image, name, info);
124
125 xpmDataClose(&mdata);
126
127 return (ErrorStatus);
128 }
129
130 int
131 xpmWriteData(xpmData *mdata, XpmImage *image, char *name, XpmInfo *info)
132 {
133 /* calculation variables */
134 unsigned int cmts, extensions;
135 FILE *file;
136 int ErrorStatus;
137
138 /* store this to speed up */
139 file = mdata->stream.file;
140
141 cmts = info && (info->valuemask & XpmComments);
142 extensions = info && (info->valuemask & XpmExtensions)
143 && info->nextensions;
144
145 /* print the header line */
146 fprintf(file, "/* XPM */\nstatic char * %s[] = {\n", name);
147
148 /* print the hints line */
149 if (cmts && info->hints_cmt)
150 fprintf(file, "/*%s*/\n", info->hints_cmt);
151
152 fprintf(file, "\"%d %d %d %d", image->width, image->height,
153 image->ncolors, image->cpp);
154
155 if (info && (info->valuemask & XpmHotspot))
156 fprintf(file, " %d %d", info->x_hotspot, info->y_hotspot);
157
158 if (extensions)
159 fprintf(file, " XPMEXT");
160
161 fprintf(file, "\",\n");
162
163 /* print colors */
164 if (cmts && info->colors_cmt)
165 fprintf(file, "/*%s*/\n", info->colors_cmt);
166
167 WriteColors(file, image->colorTable, image->ncolors);
168
169 /* print pixels */
170 if (cmts && info->pixels_cmt)
171 fprintf(file, "/*%s*/\n", info->pixels_cmt);
172
173 ErrorStatus = WritePixels(file, image->width, image->height, image->cpp,
174 image->data, image->colorTable);
175 if (ErrorStatus != XpmSuccess)
176 return (ErrorStatus);
177
178 /* print extensions */
179 if (extensions)
180 WriteExtensions(file, info->extensions, info->nextensions);
181
182 /* close the array */
183 fprintf(file, "};\n");
184
185 return (XpmSuccess);
186 }
187
188 static void
189 WriteColors(FILE *file, XpmColor *colors, unsigned int ncolors)
190 {
191 unsigned int a, key;
192 char *s;
193 char **defaults;
194
195 for (a = 0; a < ncolors; a++, colors++) {
196
197 defaults = (char **) colors;
198 fprintf(file, "\"%s", *defaults++);
199
200 for (key = 1; key <= NKEYS; key++, defaults++) {
201 if (s = *defaults)
202 fprintf(file, "\t%s %s", xpmColorKeys[key - 1], s);
203 }
204 fprintf(file, "\",\n");
205 }
206 }
207
208
209 static int
210 WritePixels(FILE *file, unsigned int width, unsigned int height, unsigned int cpp, unsigned int *pixels, XpmColor *colors)
211 {
212 char *s, *p, *buf;
213 unsigned int x, y, h;
214
215 h = height - 1;
216 p = buf = (char *) XpmMalloc(width * cpp + 3);
217 if (!buf)
218 return (XpmNoMemory);
219 *buf = '"';
220 p++;
221 for (y = 0; y < h; y++) {
222 s = p;
223 for (x = 0; x < width; x++, pixels++) {
224 strncpy(s, colors[*pixels].string, cpp);
225 s += cpp;
226 }
227 *s++ = '"';
228 *s = '\0';
229 fprintf(file, "%s,\n", buf);
230 }
231 /* duplicate some code to avoid a test in the loop */
232 s = p;
233 for (x = 0; x < width; x++, pixels++) {
234 strncpy(s, colors[*pixels].string, cpp);
235 s += cpp;
236 }
237 *s++ = '"';
238 *s = '\0';
239 fprintf(file, "%s", buf);
240
241 XpmFree(buf);
242 return (XpmSuccess);
243 }
244
245 static void
246 WriteExtensions(FILE *file, XpmExtension *ext, unsigned int num)
247 {
248 unsigned int x, y, n;
249 char **line;
250
251 for (x = 0; x < num; x++, ext++) {
252 fprintf(file, ",\n\"XPMEXT %s\"", ext->name);
253 n = ext->nlines;
254 for (y = 0, line = ext->lines; y < n; y++, line++)
255 fprintf(file, ",\n\"%s\"", *line);
256 }
257 fprintf(file, ",\n\"XPMENDEXT\"");
258 }