wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / wrlib / save_xpm.c
blob08cb52ecfd6c6818edcf4031e55569de6efed6cc
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;
91 tmpc = lookfor(*list, index);
93 if (tmpc)
94 return True;
96 newc = malloc(sizeof(XPMColor));
98 if (!newc) {
100 RErrorCode = RERR_NOMEMORY;
102 return False;
105 newc->red = r;
106 newc->green = g;
107 newc->blue = b;
108 newc->next = *list;
109 *list = newc;
111 (*colors)++;
113 return True;
116 static char *index2str(char *buffer, int index, int charsPerPixel)
118 int i;
120 for (i = 0; i < charsPerPixel; i++) {
121 buffer[i] = I2CHAR(index & 63);
122 index >>= 6;
124 buffer[i] = 0;
126 return buffer;
129 static void outputcolormap(FILE * file, XPMColor * colormap, int charsPerPixel)
131 int index;
132 char buf[128];
134 if (!colormap)
135 return;
137 for (index = 0; colormap != NULL; colormap = colormap->next, index++) {
138 colormap->index = index;
139 fprintf(file, "\"%s c #%02x%02x%02x\",\n",
140 index2str(buf, index, charsPerPixel), colormap->red, colormap->green, colormap->blue);
144 static void freecolormap(XPMColor * colormap)
146 XPMColor *tmp;
148 while (colormap) {
149 tmp = colormap->next;
150 free(colormap);
151 colormap = tmp;
155 /* save routine is common to internal support and library support */
156 Bool RSaveXPM(RImage * image, const char *filename)
158 FILE *file;
159 int x, y;
160 int colorCount = 0;
161 int charsPerPixel;
162 XPMColor *colormap = NULL;
163 XPMColor *tmpc;
164 int i;
165 int ok = 0;
166 unsigned char *r, *g, *b, *a;
167 char transp[16];
168 char buf[128];
170 file = fopen(filename, "wb+");
171 if (!file) {
172 RErrorCode = RERR_OPEN;
173 return False;
176 fprintf(file, "/* XPM */\n");
178 fprintf(file, "static char *image[] = {\n");
180 r = image->data;
181 g = image->data + 1;
182 b = image->data + 2;
183 if (image->format == RRGBAFormat)
184 a = image->data + 3;
185 else
186 a = NULL;
188 /* first pass: make colormap for the image */
189 if (a)
190 colorCount = 1;
191 for (y = 0; y < image->height; y++) {
192 for (x = 0; x < image->width; x++) {
193 if (!a || *a > 127) {
194 if (!addcolor(&colormap, *r, *g, *b, &colorCount)) {
195 goto uhoh;
198 if (a) {
199 r += 4;
200 g += 4;
201 b += 4;
202 a += 4;
203 } else {
204 r += 3;
205 g += 3;
206 b += 3;
211 charsPerPixel = 1;
212 while ((1 << charsPerPixel * 6) < colorCount)
213 charsPerPixel++;
215 /* write header info */
216 fprintf(file, "\"%i %i %i %i\",\n", image->width, image->height, colorCount, charsPerPixel);
218 /* write colormap data */
219 if (a) {
220 for (i = 0; i < charsPerPixel; i++)
221 transp[i] = ' ';
222 transp[i] = 0;
224 fprintf(file, "\"%s c None\",\n", transp);
227 outputcolormap(file, colormap, charsPerPixel);
229 r = image->data;
230 g = image->data + 1;
231 b = image->data + 2;
232 if (image->format == RRGBAFormat)
233 a = image->data + 3;
234 else
235 a = NULL;
237 /* write data */
238 for (y = 0; y < image->height; y++) {
240 fprintf(file, "\"");
242 for (x = 0; x < image->width; x++) {
244 if (!a || *a > 127) {
245 tmpc = lookfor(colormap, (unsigned)*r << 16 | (unsigned)*g << 8 | (unsigned)*b);
247 fprintf(file, "%s", index2str(buf, tmpc->index, charsPerPixel));
248 } else {
249 fprintf(file, "%s", transp);
252 if (a) {
253 r += 4;
254 g += 4;
255 b += 4;
256 a += 4;
257 } else {
258 r += 3;
259 g += 3;
260 b += 3;
264 if (y < image->height - 1)
265 fprintf(file, "\",\n");
266 else
267 fprintf(file, "\"};\n");
270 ok = 1;
271 uhoh:
272 errno = 0;
273 fclose(file);
274 if (ok && errno == ENOSPC) {
275 RErrorCode = RERR_WRITE;
278 freecolormap(colormap);
280 return ok ? True : False;