WPrefs: grouped items related to the texture selection in a single place
[wmaker-crm.git] / wrlib / load_xpm.c
blob35adbb1230f878511adc1eca91551ee087d16ada
1 /* xpm.c - load XPM image from file using libXpm
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 <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <X11/xpm.h>
31 #include "wraster.h"
32 #include "imgformat.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(unsigned 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, const 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((char *)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(unsigned 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;