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