- added standard colormap support
[wmaker-crm.git] / wrlib / xpm.c
bloba0058639dbeade0d3887c86b201959af16d1c4fa
1 /* xpm.c - load XPM image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
25 #ifdef USE_XPM
27 #include <X11/Xlib.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <X11/xpm.h>
33 #include "wraster.h"
35 RImage*
36 RGetImageFromXPMData(RContext *context, char **data)
38 Display *dpy = context->dpy;
39 Colormap cmap = context->cmap;
40 RImage *image;
41 XpmImage xpm;
42 unsigned char *color_table[4];
43 unsigned char *r, *g, *b, *a;
44 int *p;
45 int i;
47 i = XpmCreateXpmImageFromData(data, &xpm, (XpmInfo *)NULL);
48 if (i!=XpmSuccess) {
49 switch (i) {
50 case XpmOpenFailed:
51 RErrorCode = RERR_OPEN;
52 break;
53 case XpmFileInvalid:
54 RErrorCode = RERR_BADIMAGEFILE;
55 break;
56 case XpmNoMemory:
57 RErrorCode = RERR_NOMEMORY;
58 break;
59 default:
60 RErrorCode = RERR_BADIMAGEFILE;
61 break;
63 return NULL;
65 if (xpm.height<1 || xpm.width < 1) {
66 RErrorCode = RERR_BADIMAGEFILE;
67 XpmFreeXpmImage(&xpm);
68 return NULL;
71 if (xpm.colorTable==NULL) {
72 RErrorCode = RERR_BADIMAGEFILE;
73 XpmFreeXpmImage(&xpm);
74 return NULL;
76 image = RCreateImage(xpm.width, xpm.height, True);
77 if (!image) {
78 XpmFreeXpmImage(&xpm);
79 return NULL;
82 /* make color table */
83 for (i=0; i<4; i++) {
84 color_table[i] = malloc(xpm.ncolors*sizeof(char));
85 if (!color_table[i]) {
86 for (i=i-1;i>=0; i--) {
87 if (color_table[i])
88 free(color_table[i]);
90 RDestroyImage(image);
91 RErrorCode = RERR_NOMEMORY;
92 XpmFreeXpmImage(&xpm);
93 return NULL;
97 for (i=0; i<xpm.ncolors; i++) {
98 XColor xcolor;
99 char * color = NULL;
101 if (xpm.colorTable[i].c_color)
102 color = xpm.colorTable[i].c_color;
103 else if (xpm.colorTable[i].g_color)
104 color = xpm.colorTable[i].g_color;
105 else if (xpm.colorTable[i].g4_color)
106 color = xpm.colorTable[i].g4_color;
107 else if (xpm.colorTable[i].m_color)
108 color = xpm.colorTable[i].m_color;
109 else if (xpm.colorTable[i].symbolic)
110 color = xpm.colorTable[i].symbolic;
112 if (!color) {
113 color_table[0][i] = 0xbe;
114 color_table[1][i] = 0xbe;
115 color_table[2][i] = 0xbe;
116 color_table[3][i] = 0xff;
117 continue;
120 if (strncmp(color,"None",4)==0) {
121 color_table[0][i]=0;
122 color_table[1][i]=0;
123 color_table[2][i]=0;
124 color_table[3][i]=0;
125 continue;
127 if (XParseColor(dpy, cmap, color, &xcolor)) {
128 color_table[0][i] = xcolor.red>>8;
129 color_table[1][i] = xcolor.green>>8;
130 color_table[2][i] = xcolor.blue>>8;
131 color_table[3][i] = 0xff;
132 } else {
133 color_table[0][i] = 0xbe;
134 color_table[1][i] = 0xbe;
135 color_table[2][i] = 0xbe;
136 color_table[3][i] = 0xff;
139 memset(image->data[3], 255, xpm.width*xpm.height);
140 /* convert pixmap to RImage */
141 p = (int*)xpm.data;
142 r = image->data[0];
143 g = image->data[1];
144 b = image->data[2];
145 a = image->data[3];
146 for (i=0; i<xpm.width*xpm.height; i++) {
147 *(r++)=color_table[0][*p];
148 *(g++)=color_table[1][*p];
149 *(b++)=color_table[2][*p];
150 *(a++)=color_table[3][*p];
151 p++;
153 for(i=0; i<4; i++) {
154 free(color_table[i]);
156 XpmFreeXpmImage(&xpm);
157 return image;
162 RImage*
163 RLoadXPM(RContext *context, char *file, int index)
165 Display *dpy = context->dpy;
166 Colormap cmap = context->cmap;
167 RImage *image;
168 XpmImage xpm;
169 unsigned char *color_table[4];
170 unsigned char *r, *g, *b, *a;
171 int *p;
172 int i;
174 i = XpmReadFileToXpmImage(file, &xpm, (XpmInfo *)NULL);
175 if (i!=XpmSuccess) {
176 switch (i) {
177 case XpmOpenFailed:
178 RErrorCode = RERR_OPEN;
179 break;
180 case XpmFileInvalid:
181 RErrorCode = RERR_BADIMAGEFILE;
182 break;
183 case XpmNoMemory:
184 RErrorCode = RERR_NOMEMORY;
185 break;
186 default:
187 RErrorCode = RERR_BADIMAGEFILE;
188 break;
190 return NULL;
192 if (xpm.height<1 || xpm.width < 1) {
193 RErrorCode = RERR_BADIMAGEFILE;
194 XpmFreeXpmImage(&xpm);
195 return NULL;
198 if (xpm.colorTable==NULL) {
199 RErrorCode = RERR_BADIMAGEFILE;
200 XpmFreeXpmImage(&xpm);
201 return NULL;
203 image = RCreateImage(xpm.width, xpm.height, True);
204 if (!image) {
205 XpmFreeXpmImage(&xpm);
206 return NULL;
209 /* make color table */
210 for (i=0; i<4; i++) {
211 color_table[i] = malloc(xpm.ncolors*sizeof(char));
212 if (!color_table[i]) {
213 for (i=i-1;i>=0; i--) {
214 if (color_table[i])
215 free(color_table[i]);
217 RDestroyImage(image);
218 RErrorCode = RERR_NOMEMORY;
219 XpmFreeXpmImage(&xpm);
220 return NULL;
224 for (i=0; i<xpm.ncolors; i++) {
225 XColor xcolor;
226 char * color = NULL;
228 if (xpm.colorTable[i].c_color)
229 color = xpm.colorTable[i].c_color;
230 else if (xpm.colorTable[i].g_color)
231 color = xpm.colorTable[i].g_color;
232 else if (xpm.colorTable[i].g4_color)
233 color = xpm.colorTable[i].g4_color;
234 else if (xpm.colorTable[i].m_color)
235 color = xpm.colorTable[i].m_color;
236 else if (xpm.colorTable[i].symbolic)
237 color = xpm.colorTable[i].symbolic;
239 if (!color) {
240 color_table[0][i] = 0xbe;
241 color_table[1][i] = 0xbe;
242 color_table[2][i] = 0xbe;
243 color_table[3][i] = 0xff;
244 continue;
247 if (strncmp(color,"None",4)==0) {
248 color_table[0][i]=0;
249 color_table[1][i]=0;
250 color_table[2][i]=0;
251 color_table[3][i]=0;
252 continue;
254 if (XParseColor(dpy, cmap, color, &xcolor)) {
255 color_table[0][i] = xcolor.red>>8;
256 color_table[1][i] = xcolor.green>>8;
257 color_table[2][i] = xcolor.blue>>8;
258 color_table[3][i] = 0xff;
259 } else {
260 color_table[0][i] = 0xbe;
261 color_table[1][i] = 0xbe;
262 color_table[2][i] = 0xbe;
263 color_table[3][i] = 0xff;
266 /* convert pixmap to RImage */
267 p = (int*)xpm.data;
268 r = image->data[0];
269 g = image->data[1];
270 b = image->data[2];
271 a = image->data[3];
272 for (i=0; i<xpm.width*xpm.height; i++) {
273 *(r++)=color_table[0][*p];
274 *(g++)=color_table[1][*p];
275 *(b++)=color_table[2][*p];
276 *(a++)=color_table[3][*p];
277 p++;
279 for(i=0; i<4; i++) {
280 free(color_table[i]);
282 XpmFreeXpmImage(&xpm);
283 return image;
286 #endif /* USE_XPM */