wmiv: align version with wm
[wmaker-crm.git] / wrlib / save_xpm.c
blob243f9506d2aaa4e7c3d6fe854e13131b6cee3cf7
1 /* save_xpm.c - save "normalized" XPM image
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
31 #include "wraster.h"
32 #include "imgformat.h"
33 #include "wr_i18n.h"
37 * Restricted support for XPM images.
39 * The images must be in the following "normalized" format:
42 * line content
43 * 1 signature comment
44 * 2 ignored ( normally "static char *xpm[] = {" )
45 * 3 "width height color_count chars" where chars is 1 or 2
46 * 4 color definitions. Only c values with #rrggbb or #rrrrggggbbb
47 * format OR None
48 * n data
50 * - no comments or blank lines are allowed, except for the signature
51 * - all lines must have at most 256 characters
52 * - no white spaces allowed at left of each line
55 typedef struct XPMColor {
56 unsigned char red;
57 unsigned char green;
58 unsigned char blue;
59 int index;
60 struct XPMColor *next;
61 } XPMColor;
63 #define I2CHAR(i) ((i)<12 ? (i)+'0' : ((i)<38 ? (i)+'A'-12 : (i)+'a'-38))
64 #define CINDEX(xpmc) (((unsigned)(xpmc)->red)<<16|((unsigned)(xpmc)->green)<<8|((unsigned)(xpmc)->blue))
66 static XPMColor *lookfor(XPMColor * list, int index)
68 if (!list)
69 return NULL;
71 for (; list != NULL; list = list->next) {
72 if (CINDEX(list) == index)
73 return list;
75 return NULL;
79 * Looks for the color in the colormap and inserts if it is not found.
81 * list is a binary search list. The unbalancing problem is just ignored.
83 * Returns False on error
85 static Bool addcolor(XPMColor ** list, unsigned r, unsigned g, unsigned b, int *colors)
87 XPMColor *tmpc;
88 XPMColor *newc;
89 int index;
91 index = r << 16 | g << 8 | b;
93 tmpc = lookfor(*list, index);
95 if (tmpc)
96 return True;
98 newc = malloc(sizeof(XPMColor));
100 if (!newc) {
102 RErrorCode = RERR_NOMEMORY;
104 return False;
107 newc->red = r;
108 newc->green = g;
109 newc->blue = b;
110 newc->next = *list;
111 *list = newc;
113 (*colors)++;
115 return True;
118 static char *index2str(char *buffer, int index, int charsPerPixel)
120 int i;
122 for (i = 0; i < charsPerPixel; i++) {
123 buffer[i] = I2CHAR(index & 63);
124 index >>= 6;
126 buffer[i] = 0;
128 return buffer;
131 static void outputcolormap(FILE * file, XPMColor * colormap, int charsPerPixel)
133 int index;
134 char buf[128];
136 if (!colormap)
137 return;
139 for (index = 0; colormap != NULL; colormap = colormap->next, index++) {
140 colormap->index = index;
141 fprintf(file, "\"%s c #%02x%02x%02x\",\n",
142 index2str(buf, index, charsPerPixel), colormap->red, colormap->green, colormap->blue);
146 static void freecolormap(XPMColor * colormap)
148 XPMColor *tmp;
150 while (colormap) {
151 tmp = colormap->next;
152 free(colormap);
153 colormap = tmp;
157 /* save routine is common to internal support and library support */
158 Bool RSaveXPM(RImage * image, const char *filename)
160 FILE *file;
161 int x, y;
162 int colorCount = 0;
163 int charsPerPixel;
164 XPMColor *colormap = NULL;
165 XPMColor *tmpc;
166 int i;
167 int ok = 0;
168 unsigned char *r, *g, *b, *a;
169 char transp[16];
170 char buf[128];
172 file = fopen(filename, "wb+");
173 if (!file) {
174 RErrorCode = RERR_OPEN;
175 return False;
178 fprintf(file, "/* XPM */\n");
180 fprintf(file, "static char *image[] = {\n");
182 r = image->data;
183 g = image->data + 1;
184 b = image->data + 2;
185 if (image->format == RRGBAFormat)
186 a = image->data + 3;
187 else
188 a = NULL;
190 /* first pass: make colormap for the image */
191 if (a)
192 colorCount = 1;
193 for (y = 0; y < image->height; y++) {
194 for (x = 0; x < image->width; x++) {
195 if (!a || *a > 127) {
196 if (!addcolor(&colormap, *r, *g, *b, &colorCount)) {
197 goto uhoh;
200 if (a) {
201 r += 4;
202 g += 4;
203 b += 4;
204 a += 4;
205 } else {
206 r += 3;
207 g += 3;
208 b += 3;
213 charsPerPixel = 1;
214 while ((1 << charsPerPixel * 6) < colorCount)
215 charsPerPixel++;
217 /* write header info */
218 fprintf(file, "\"%i %i %i %i\",\n", image->width, image->height, colorCount, charsPerPixel);
220 /* write colormap data */
221 if (a) {
222 for (i = 0; i < charsPerPixel; i++)
223 transp[i] = ' ';
224 transp[i] = 0;
226 fprintf(file, "\"%s c None\",\n", transp);
229 outputcolormap(file, colormap, charsPerPixel);
231 r = image->data;
232 g = image->data + 1;
233 b = image->data + 2;
234 if (image->format == RRGBAFormat)
235 a = image->data + 3;
236 else
237 a = NULL;
239 /* write data */
240 for (y = 0; y < image->height; y++) {
242 fprintf(file, "\"");
244 for (x = 0; x < image->width; x++) {
246 if (!a || *a > 127) {
247 tmpc = lookfor(colormap, (unsigned)*r << 16 | (unsigned)*g << 8 | (unsigned)*b);
249 fprintf(file, "%s", index2str(buf, tmpc->index, charsPerPixel));
250 } else {
251 fprintf(file, "%s", transp);
254 if (a) {
255 r += 4;
256 g += 4;
257 b += 4;
258 a += 4;
259 } else {
260 r += 3;
261 g += 3;
262 b += 3;
266 if (y < image->height - 1)
267 fprintf(file, "\",\n");
268 else
269 fprintf(file, "\"};\n");
272 ok = 1;
273 uhoh:
274 errno = 0;
275 fclose(file);
276 if (ok && errno == ENOSPC) {
277 RErrorCode = RERR_WRITE;
280 freecolormap(colormap);
282 return ok ? True : False;