wmaker: Removed variable for X Context that is not used
[wmaker-crm.git] / wrlib / xpm.c
blob12aadc60ff03301b26f6caaad041ec06414a9927
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., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #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"
34 #include "imgformat.h"
36 RImage *RGetImageFromXPMData(RContext * context, char **xpmData)
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 *data;
44 int *p;
45 int i;
47 i = XpmCreateXpmImageFromData(xpmData, &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 RReleaseImage(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 /* convert pixmap to RImage */
140 p = (int *)xpm.data;
141 data = image->data;
142 for (i = 0; i < xpm.width * xpm.height; i++) {
143 *(data++) = color_table[0][*p];
144 *(data++) = color_table[1][*p];
145 *(data++) = color_table[2][*p];
146 *(data++) = color_table[3][*p];
147 p++;
149 for (i = 0; i < 4; i++) {
150 free(color_table[i]);
152 XpmFreeXpmImage(&xpm);
153 return image;
156 RImage *RLoadXPM(RContext * context, const char *file)
158 Display *dpy = context->dpy;
159 Colormap cmap = context->cmap;
160 RImage *image;
161 XpmImage xpm;
162 unsigned char *color_table[4];
163 unsigned char *data;
164 int *p;
165 int i;
167 i = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
168 if (i != XpmSuccess) {
169 switch (i) {
170 case XpmOpenFailed:
171 RErrorCode = RERR_OPEN;
172 break;
173 case XpmFileInvalid:
174 RErrorCode = RERR_BADIMAGEFILE;
175 break;
176 case XpmNoMemory:
177 RErrorCode = RERR_NOMEMORY;
178 break;
179 default:
180 RErrorCode = RERR_BADIMAGEFILE;
181 break;
183 return NULL;
185 if (xpm.height < 1 || xpm.width < 1) {
186 RErrorCode = RERR_BADIMAGEFILE;
187 XpmFreeXpmImage(&xpm);
188 return NULL;
191 if (xpm.colorTable == NULL) {
192 RErrorCode = RERR_BADIMAGEFILE;
193 XpmFreeXpmImage(&xpm);
194 return NULL;
196 image = RCreateImage(xpm.width, xpm.height, True);
197 if (!image) {
198 XpmFreeXpmImage(&xpm);
199 return NULL;
202 /* make color table */
203 for (i = 0; i < 4; i++) {
204 color_table[i] = malloc(xpm.ncolors * sizeof(char));
205 if (!color_table[i]) {
206 for (i = i - 1; i >= 0; i--) {
207 if (color_table[i])
208 free(color_table[i]);
210 RReleaseImage(image);
211 RErrorCode = RERR_NOMEMORY;
212 XpmFreeXpmImage(&xpm);
213 return NULL;
217 for (i = 0; i < xpm.ncolors; i++) {
218 XColor xcolor;
219 char *color = NULL;
221 if (xpm.colorTable[i].c_color)
222 color = xpm.colorTable[i].c_color;
223 else if (xpm.colorTable[i].g_color)
224 color = xpm.colorTable[i].g_color;
225 else if (xpm.colorTable[i].g4_color)
226 color = xpm.colorTable[i].g4_color;
227 else if (xpm.colorTable[i].m_color)
228 color = xpm.colorTable[i].m_color;
229 else if (xpm.colorTable[i].symbolic)
230 color = xpm.colorTable[i].symbolic;
232 if (!color) {
233 color_table[0][i] = 0xbe;
234 color_table[1][i] = 0xbe;
235 color_table[2][i] = 0xbe;
236 color_table[3][i] = 0xff;
237 continue;
240 if (strncmp(color, "None", 4) == 0) {
241 color_table[0][i] = 0;
242 color_table[1][i] = 0;
243 color_table[2][i] = 0;
244 color_table[3][i] = 0;
245 continue;
247 if (XParseColor(dpy, cmap, color, &xcolor)) {
248 color_table[0][i] = xcolor.red >> 8;
249 color_table[1][i] = xcolor.green >> 8;
250 color_table[2][i] = xcolor.blue >> 8;
251 color_table[3][i] = 0xff;
252 } else {
253 color_table[0][i] = 0xbe;
254 color_table[1][i] = 0xbe;
255 color_table[2][i] = 0xbe;
256 color_table[3][i] = 0xff;
259 /* convert pixmap to RImage */
260 p = (int *)xpm.data;
261 data = image->data;
262 for (i = 0; i < xpm.width * xpm.height; i++, p++) {
263 *(data++) = color_table[0][*p];
264 *(data++) = color_table[1][*p];
265 *(data++) = color_table[2][*p];
266 *(data++) = color_table[3][*p];
268 for (i = 0; i < 4; i++) {
269 free(color_table[i]);
271 XpmFreeXpmImage(&xpm);
272 return image;
275 #endif /* USE_XPM */