Code refactoring: replaced macro 'HAVE_XRANDR' by 'USE_XRANDR' for consistency
[wmaker-crm.git] / wrlib / save_xpm.c
blob3e6c976a584279f1779b0420526f10637f59ec8f
1 /* nxpm.c - load "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"
35 * Restricted support for XPM images.
37 * The images must be in the following "normalized" format:
40 * line content
41 * 1 signature comment
42 * 2 ignored ( normally "static char *xpm[] = {" )
43 * 3 "width height color_count chars" where chars is 1 or 2
44 * 4 color definitions. Only c values with #rrggbb or #rrrrggggbbb
45 * format OR None
46 * n data
48 * - no comments or blank lines are allowed, except for the signature
49 * - all lines must have at most 256 characters
50 * - no white spaces allowed at left of each line
53 typedef struct XPMColor {
54 unsigned char red;
55 unsigned char green;
56 unsigned char blue;
57 int index;
58 struct XPMColor *next;
59 } XPMColor;
61 #define I2CHAR(i) ((i)<12 ? (i)+'0' : ((i)<38 ? (i)+'A'-12 : (i)+'a'-38))
62 #define CINDEX(xpmc) (((unsigned)(xpmc)->red)<<16|((unsigned)(xpmc)->green)<<8|((unsigned)(xpmc)->blue))
64 static XPMColor *lookfor(XPMColor * list, int index)
66 if (!list)
67 return NULL;
69 for (; list != NULL; list = list->next) {
70 if (CINDEX(list) == index)
71 return list;
73 return NULL;
77 * Looks for the color in the colormap and inserts if it is not found.
79 * list is a binary search list. The unbalancing problem is just ignored.
81 * Returns False on error
83 static Bool addcolor(XPMColor ** list, unsigned r, unsigned g, unsigned b, int *colors)
85 XPMColor *tmpc;
86 XPMColor *newc;
87 int index;
89 index = r << 16 | g << 8 | b;
90 tmpc = *list;
92 tmpc = lookfor(*list, index);
94 if (tmpc)
95 return True;
97 newc = malloc(sizeof(XPMColor));
99 if (!newc) {
101 RErrorCode = RERR_NOMEMORY;
103 return False;
106 newc->red = r;
107 newc->green = g;
108 newc->blue = b;
109 newc->next = *list;
110 *list = newc;
112 (*colors)++;
114 return True;
117 static char *index2str(char *buffer, int index, int charsPerPixel)
119 int i;
121 for (i = 0; i < charsPerPixel; i++) {
122 buffer[i] = I2CHAR(index & 63);
123 index >>= 6;
125 buffer[i] = 0;
127 return buffer;
130 static void outputcolormap(FILE * file, XPMColor * colormap, int charsPerPixel)
132 int index;
133 char buf[128];
135 if (!colormap)
136 return;
138 for (index = 0; colormap != NULL; colormap = colormap->next, index++) {
139 colormap->index = index;
140 fprintf(file, "\"%s c #%02x%02x%02x\",\n",
141 index2str(buf, index, charsPerPixel), colormap->red, colormap->green, colormap->blue);
145 static void freecolormap(XPMColor * colormap)
147 XPMColor *tmp;
149 while (colormap) {
150 tmp = colormap->next;
151 free(colormap);
152 colormap = tmp;
156 /* save routine is common to internal support and library support */
157 Bool RSaveXPM(RImage * image, const char *filename)
159 FILE *file;
160 int x, y;
161 int colorCount = 0;
162 int charsPerPixel;
163 XPMColor *colormap = NULL;
164 XPMColor *tmpc;
165 int i;
166 int ok = 0;
167 unsigned char *r, *g, *b, *a;
168 char transp[16];
169 char buf[128];
171 file = fopen(filename, "wb+");
172 if (!file) {
173 RErrorCode = RERR_OPEN;
174 return False;
177 fprintf(file, "/* XPM */\n");
179 fprintf(file, "static char *image[] = {\n");
181 r = image->data;
182 g = image->data + 1;
183 b = image->data + 2;
184 if (image->format == RRGBAFormat)
185 a = image->data + 3;
186 else
187 a = NULL;
189 /* first pass: make colormap for the image */
190 if (a)
191 colorCount = 1;
192 for (y = 0; y < image->height; y++) {
193 for (x = 0; x < image->width; x++) {
194 if (!a || *a > 127) {
195 if (!addcolor(&colormap, *r, *g, *b, &colorCount)) {
196 goto uhoh;
199 if (a) {
200 r += 4;
201 g += 4;
202 b += 4;
203 a += 4;
204 } else {
205 r += 3;
206 g += 3;
207 b += 3;
212 charsPerPixel = 1;
213 while ((1 << charsPerPixel * 6) < colorCount)
214 charsPerPixel++;
216 /* write header info */
217 fprintf(file, "\"%i %i %i %i\",\n", image->width, image->height, colorCount, charsPerPixel);
219 /* write colormap data */
220 if (a) {
221 for (i = 0; i < charsPerPixel; i++)
222 transp[i] = ' ';
223 transp[i] = 0;
225 fprintf(file, "\"%s c None\",\n", transp);
228 i = 0;
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;