WINGs: Remove proplist-compat.h
[wmaker-crm.git] / wrlib / xpm.c
blob92376895f7d9261ce1522df2d1d8fb580748fed3
1 /* xpm.c - load XPM image from file
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
24 #ifdef USE_XPM
26 #include <X11/Xlib.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <X11/xpm.h>
32 #include "wraster.h"
34 RImage *RGetImageFromXPMData(RContext * context, char **xpmData)
36 Display *dpy = context->dpy;
37 Colormap cmap = context->cmap;
38 RImage *image;
39 XpmImage xpm;
40 unsigned char *color_table[4];
41 unsigned char *data;
42 int *p;
43 int i;
45 i = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
46 if (i != XpmSuccess) {
47 switch (i) {
48 case XpmOpenFailed:
49 RErrorCode = RERR_OPEN;
50 break;
51 case XpmFileInvalid:
52 RErrorCode = RERR_BADIMAGEFILE;
53 break;
54 case XpmNoMemory:
55 RErrorCode = RERR_NOMEMORY;
56 break;
57 default:
58 RErrorCode = RERR_BADIMAGEFILE;
59 break;
61 return NULL;
63 if (xpm.height < 1 || xpm.width < 1) {
64 RErrorCode = RERR_BADIMAGEFILE;
65 XpmFreeXpmImage(&xpm);
66 return NULL;
69 if (xpm.colorTable == NULL) {
70 RErrorCode = RERR_BADIMAGEFILE;
71 XpmFreeXpmImage(&xpm);
72 return NULL;
74 image = RCreateImage(xpm.width, xpm.height, True);
75 if (!image) {
76 XpmFreeXpmImage(&xpm);
77 return NULL;
80 /* make color table */
81 for (i = 0; i < 4; i++) {
82 color_table[i] = malloc(xpm.ncolors * sizeof(char));
83 if (!color_table[i]) {
84 for (i = i - 1; i >= 0; i--) {
85 if (color_table[i])
86 free(color_table[i]);
88 RReleaseImage(image);
89 RErrorCode = RERR_NOMEMORY;
90 XpmFreeXpmImage(&xpm);
91 return NULL;
95 for (i = 0; i < xpm.ncolors; i++) {
96 XColor xcolor;
97 char *color = NULL;
99 if (xpm.colorTable[i].c_color)
100 color = xpm.colorTable[i].c_color;
101 else if (xpm.colorTable[i].g_color)
102 color = xpm.colorTable[i].g_color;
103 else if (xpm.colorTable[i].g4_color)
104 color = xpm.colorTable[i].g4_color;
105 else if (xpm.colorTable[i].m_color)
106 color = xpm.colorTable[i].m_color;
107 else if (xpm.colorTable[i].symbolic)
108 color = xpm.colorTable[i].symbolic;
110 if (!color) {
111 color_table[0][i] = 0xbe;
112 color_table[1][i] = 0xbe;
113 color_table[2][i] = 0xbe;
114 color_table[3][i] = 0xff;
115 continue;
118 if (strncmp(color, "None", 4) == 0) {
119 color_table[0][i] = 0;
120 color_table[1][i] = 0;
121 color_table[2][i] = 0;
122 color_table[3][i] = 0;
123 continue;
125 if (XParseColor(dpy, cmap, color, &xcolor)) {
126 color_table[0][i] = xcolor.red >> 8;
127 color_table[1][i] = xcolor.green >> 8;
128 color_table[2][i] = xcolor.blue >> 8;
129 color_table[3][i] = 0xff;
130 } else {
131 color_table[0][i] = 0xbe;
132 color_table[1][i] = 0xbe;
133 color_table[2][i] = 0xbe;
134 color_table[3][i] = 0xff;
137 /* convert pixmap to RImage */
138 p = (int *)xpm.data;
139 data = image->data;
140 for (i = 0; i < xpm.width * xpm.height; i++) {
141 *(data++) = color_table[0][*p];
142 *(data++) = color_table[1][*p];
143 *(data++) = color_table[2][*p];
144 *(data++) = color_table[3][*p];
145 p++;
147 for (i = 0; i < 4; i++) {
148 free(color_table[i]);
150 XpmFreeXpmImage(&xpm);
151 return image;
154 RImage *RLoadXPM(RContext * context, char *file)
156 Display *dpy = context->dpy;
157 Colormap cmap = context->cmap;
158 RImage *image;
159 XpmImage xpm;
160 unsigned char *color_table[4];
161 unsigned char *data;
162 int *p;
163 int i;
165 i = XpmReadFileToXpmImage(file, &xpm, (XpmInfo *) NULL);
166 if (i != XpmSuccess) {
167 switch (i) {
168 case XpmOpenFailed:
169 RErrorCode = RERR_OPEN;
170 break;
171 case XpmFileInvalid:
172 RErrorCode = RERR_BADIMAGEFILE;
173 break;
174 case XpmNoMemory:
175 RErrorCode = RERR_NOMEMORY;
176 break;
177 default:
178 RErrorCode = RERR_BADIMAGEFILE;
179 break;
181 return NULL;
183 if (xpm.height < 1 || xpm.width < 1) {
184 RErrorCode = RERR_BADIMAGEFILE;
185 XpmFreeXpmImage(&xpm);
186 return NULL;
189 if (xpm.colorTable == NULL) {
190 RErrorCode = RERR_BADIMAGEFILE;
191 XpmFreeXpmImage(&xpm);
192 return NULL;
194 image = RCreateImage(xpm.width, xpm.height, True);
195 if (!image) {
196 XpmFreeXpmImage(&xpm);
197 return NULL;
200 /* make color table */
201 for (i = 0; i < 4; i++) {
202 color_table[i] = malloc(xpm.ncolors * sizeof(char));
203 if (!color_table[i]) {
204 for (i = i - 1; i >= 0; i--) {
205 if (color_table[i])
206 free(color_table[i]);
208 RReleaseImage(image);
209 RErrorCode = RERR_NOMEMORY;
210 XpmFreeXpmImage(&xpm);
211 return NULL;
215 for (i = 0; i < xpm.ncolors; i++) {
216 XColor xcolor;
217 char *color = NULL;
219 if (xpm.colorTable[i].c_color)
220 color = xpm.colorTable[i].c_color;
221 else if (xpm.colorTable[i].g_color)
222 color = xpm.colorTable[i].g_color;
223 else if (xpm.colorTable[i].g4_color)
224 color = xpm.colorTable[i].g4_color;
225 else if (xpm.colorTable[i].m_color)
226 color = xpm.colorTable[i].m_color;
227 else if (xpm.colorTable[i].symbolic)
228 color = xpm.colorTable[i].symbolic;
230 if (!color) {
231 color_table[0][i] = 0xbe;
232 color_table[1][i] = 0xbe;
233 color_table[2][i] = 0xbe;
234 color_table[3][i] = 0xff;
235 continue;
238 if (strncmp(color, "None", 4) == 0) {
239 color_table[0][i] = 0;
240 color_table[1][i] = 0;
241 color_table[2][i] = 0;
242 color_table[3][i] = 0;
243 continue;
245 if (XParseColor(dpy, cmap, color, &xcolor)) {
246 color_table[0][i] = xcolor.red >> 8;
247 color_table[1][i] = xcolor.green >> 8;
248 color_table[2][i] = xcolor.blue >> 8;
249 color_table[3][i] = 0xff;
250 } else {
251 color_table[0][i] = 0xbe;
252 color_table[1][i] = 0xbe;
253 color_table[2][i] = 0xbe;
254 color_table[3][i] = 0xff;
257 /* convert pixmap to RImage */
258 p = (int *)xpm.data;
259 data = image->data;
260 for (i = 0; i < xpm.width * xpm.height; i++, p++) {
261 *(data++) = color_table[0][*p];
262 *(data++) = color_table[1][*p];
263 *(data++) = color_table[2][*p];
264 *(data++) = color_table[3][*p];
266 for (i = 0; i < 4; i++) {
267 free(color_table[i]);
269 XpmFreeXpmImage(&xpm);
270 return image;
273 #endif /* USE_XPM */