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
47 #include "sys/types.h"
53 /* MS Windows define a function called WriteFile @#%#&!!! */
54 LFUNC(xpmWriteFile, int, (FILE *file, XpmImage *image, const char *name,
57 LFUNC(WriteColors, void, (FILE *file, XpmColor *colors, unsigned int ncolors));
59 LFUNC(WritePixels, int, (FILE *file, unsigned int width, unsigned int height,
60 unsigned int cpp, unsigned int *pixels,
63 LFUNC(WriteExtensions, void, (FILE *file, XpmExtension *ext,
66 LFUNC(OpenWriteFile, int, (char *filename, xpmData *mdata));
67 LFUNC(xpmDataClose, void, (xpmData *mdata));
70 XpmWriteFileFromImage(
75 XpmAttributes *attributes)
81 /* create an XpmImage from the image */
82 ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
83 &xpmimage, attributes);
84 if (ErrorStatus != XpmSuccess)
87 /* write the file from the XpmImage */
89 xpmSetInfo(&info, attributes);
90 ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, &info);
92 ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, NULL);
94 /* free the XpmImage */
95 XpmFreeXpmImage(&xpmimage);
101 XpmWriteFileFromXpmImage(
108 char *dot, *s, new_name[BUFSIZ] = {0};
111 /* open file to write */
112 if ((ErrorStatus = OpenWriteFile(filename, &mdata)) != XpmSuccess)
113 return (ErrorStatus);
115 /* figure out a name */
120 if (!(name = strrchr(filename, '/'))
122 && !(name = strrchr(filename, ':'))
129 /* let's try to make a valid C syntax name */
130 if (strchr(name, '.')) {
131 strncpy(new_name, name, sizeof(new_name));
132 new_name[sizeof(new_name)-1] = '\0';
133 /* change '.' to '_' */
135 while ((dot = strchr(s, '.'))) {
140 if (strchr(name, '-')) {
141 if (name != new_name) {
142 strncpy(new_name, name, sizeof(new_name));
143 new_name[sizeof(new_name)-1] = '\0';
146 /* change '-' to '_' */
148 while ((dot = strchr(s, '-'))) {
156 /* write the XpmData from the XpmImage */
157 if (ErrorStatus == XpmSuccess)
158 ErrorStatus = xpmWriteFile(mdata.stream.file, image, name, info);
160 xpmDataClose(&mdata);
162 return (ErrorStatus);
172 /* calculation variables */
173 unsigned int cmts, extensions;
176 cmts = info && (info->valuemask & XpmComments);
177 extensions = info && (info->valuemask & XpmExtensions)
178 && info->nextensions;
180 /* print the header line */
181 fprintf(file, "/* XPM */\nstatic char * %s[] = {\n", name);
183 /* print the hints line */
184 if (cmts && info->hints_cmt)
185 fprintf(file, "/*%s*/\n", info->hints_cmt);
187 fprintf(file, "\"%d %d %d %d", image->width, image->height,
188 image->ncolors, image->cpp);
190 if (info && (info->valuemask & XpmHotspot))
191 fprintf(file, " %d %d", info->x_hotspot, info->y_hotspot);
194 fprintf(file, " XPMEXT");
196 fprintf(file, "\",\n");
199 if (cmts && info->colors_cmt)
200 fprintf(file, "/*%s*/\n", info->colors_cmt);
202 WriteColors(file, image->colorTable, image->ncolors);
205 if (cmts && info->pixels_cmt)
206 fprintf(file, "/*%s*/\n", info->pixels_cmt);
208 ErrorStatus = WritePixels(file, image->width, image->height, image->cpp,
209 image->data, image->colorTable);
210 if (ErrorStatus != XpmSuccess)
211 return (ErrorStatus);
213 /* print extensions */
215 WriteExtensions(file, info->extensions, info->nextensions);
217 /* close the array */
218 fprintf(file, "};\n");
227 unsigned int ncolors)
233 for (a = 0; a < ncolors; a++, colors++) {
235 defaults = (char **) colors;
236 fprintf(file, "\"%s", *defaults++);
238 for (key = 1; key <= NKEYS; key++, defaults++) {
240 fprintf(file, "\t%s %s", xpmColorKeys[key - 1], s);
242 fprintf(file, "\",\n");
253 unsigned int *pixels,
257 unsigned int x, y, h;
260 if (cpp != 0 && width >= (UINT_MAX - 3)/cpp)
262 p = buf = (char *) XpmMalloc(width * cpp + 3);
264 return (XpmNoMemory);
267 for (y = 0; y < h; y++) {
269 for (x = 0; x < width; x++, pixels++) {
270 strncpy(s, colors[*pixels].string, cpp);
275 fprintf(file, "%s,\n", buf);
277 /* duplicate some code to avoid a test in the loop */
279 for (x = 0; x < width; x++, pixels++) {
280 strncpy(s, colors[*pixels].string, cpp);
285 fprintf(file, "%s", buf);
297 unsigned int x, y, n;
300 for (x = 0; x < num; x++, ext++) {
301 fprintf(file, ",\n\"XPMEXT %s\"", ext->name);
303 for (y = 0, line = ext->lines; y < n; y++, line++)
304 fprintf(file, ",\n\"%s\"", *line);
306 fprintf(file, ",\n\"XPMENDEXT\"");
311 FUNC(xpmPipeThrough, FILE*, (int fd,
318 * open the given file to be written as an xpmData which is returned
326 mdata->stream.file = (stdout);
327 mdata->type = XPMFILE;
332 int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
334 return(XpmOpenFailed);
336 len = strlen(filename);
337 if (len > 2 && !strcmp(".Z", filename + (len - 2))) {
338 mdata->stream.file = xpmPipeThrough(fd, "compress", NULL, "w");
339 mdata->type = XPMPIPE;
340 } else if (len > 3 && !strcmp(".gz", filename + (len - 3))) {
341 mdata->stream.file = xpmPipeThrough(fd, "gzip", "-q", "w");
342 mdata->type = XPMPIPE;
346 mdata->stream.file = fdopen(fd, "w");
347 mdata->type = XPMFILE;
349 if (!mdata->stream.file)
350 return (XpmOpenFailed);
356 * close the file related to the xpmData if any
359 xpmDataClose(xpmData *mdata)
361 if (mdata->stream.file != (stdout))
362 fclose(mdata->stream.file);