updated translations and image files for WINGs, bug fixes in WINGs
[wmaker-crm.git] / wrlib / xpm.c
blobd5f9c633fc794a1c3ac44aa3ec31f80bff420dad
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;
100 if (strncmp(xpm.colorTable[i].c_color,"None",4)==0) {
101 color_table[0][i]=0;
102 color_table[1][i]=0;
103 color_table[2][i]=0;
104 color_table[3][i]=0;
105 continue;
107 if (XParseColor(dpy, cmap, xpm.colorTable[i].c_color, &xcolor)) {
108 color_table[0][i] = xcolor.red>>8;
109 color_table[1][i] = xcolor.green>>8;
110 color_table[2][i] = xcolor.blue>>8;
111 color_table[3][i] = 0xff;
112 } else {
113 color_table[0][i] = 0xbe;
114 color_table[1][i] = 0xbe;
115 color_table[2][i] = 0xbe;
116 color_table[3][i] = 0xff;
119 memset(image->data[3], 255, xpm.width*xpm.height);
120 /* convert pixmap to RImage */
121 p = (int*)xpm.data;
122 r = image->data[0];
123 g = image->data[1];
124 b = image->data[2];
125 a = image->data[3];
126 for (i=0; i<xpm.width*xpm.height; i++) {
127 *(r++)=color_table[0][*p];
128 *(g++)=color_table[1][*p];
129 *(b++)=color_table[2][*p];
130 *(a++)=color_table[3][*p];
131 p++;
133 for(i=0; i<4; i++) {
134 free(color_table[i]);
136 XpmFreeXpmImage(&xpm);
137 return image;
142 RImage*
143 RLoadXPM(RContext *context, char *file, int index)
145 Display *dpy = context->dpy;
146 Colormap cmap = context->cmap;
147 RImage *image;
148 XpmImage xpm;
149 unsigned char *color_table[4];
150 unsigned char *r, *g, *b, *a;
151 int *p;
152 int i;
154 i = XpmReadFileToXpmImage(file, &xpm, (XpmInfo *)NULL);
155 if (i!=XpmSuccess) {
156 switch (i) {
157 case XpmOpenFailed:
158 RErrorCode = RERR_OPEN;
159 break;
160 case XpmFileInvalid:
161 RErrorCode = RERR_BADIMAGEFILE;
162 break;
163 case XpmNoMemory:
164 RErrorCode = RERR_NOMEMORY;
165 break;
166 default:
167 RErrorCode = RERR_BADIMAGEFILE;
168 break;
170 return NULL;
172 if (xpm.height<1 || xpm.width < 1) {
173 RErrorCode = RERR_BADIMAGEFILE;
174 XpmFreeXpmImage(&xpm);
175 return NULL;
178 if (xpm.colorTable==NULL) {
179 RErrorCode = RERR_BADIMAGEFILE;
180 XpmFreeXpmImage(&xpm);
181 return NULL;
183 image = RCreateImage(xpm.width, xpm.height, True);
184 if (!image) {
185 XpmFreeXpmImage(&xpm);
186 return NULL;
189 /* make color table */
190 for (i=0; i<4; i++) {
191 color_table[i] = malloc(xpm.ncolors*sizeof(char));
192 if (!color_table[i]) {
193 for (i=i-1;i>=0; i--) {
194 if (color_table[i])
195 free(color_table[i]);
197 RDestroyImage(image);
198 RErrorCode = RERR_NOMEMORY;
199 XpmFreeXpmImage(&xpm);
200 return NULL;
204 for (i=0; i<xpm.ncolors; i++) {
205 XColor xcolor;
207 if (strncmp(xpm.colorTable[i].c_color,"None",4)==0) {
208 color_table[0][i]=0;
209 color_table[1][i]=0;
210 color_table[2][i]=0;
211 color_table[3][i]=0;
212 continue;
214 if (XParseColor(dpy, cmap, xpm.colorTable[i].c_color, &xcolor)) {
215 color_table[0][i] = xcolor.red>>8;
216 color_table[1][i] = xcolor.green>>8;
217 color_table[2][i] = xcolor.blue>>8;
218 color_table[3][i] = 0xff;
219 } else {
220 color_table[0][i] = 0xbe;
221 color_table[1][i] = 0xbe;
222 color_table[2][i] = 0xbe;
223 color_table[3][i] = 0xff;
226 /* convert pixmap to RImage */
227 p = (int*)xpm.data;
228 r = image->data[0];
229 g = image->data[1];
230 b = image->data[2];
231 a = image->data[3];
232 for (i=0; i<xpm.width*xpm.height; i++) {
233 *(r++)=color_table[0][*p];
234 *(g++)=color_table[1][*p];
235 *(b++)=color_table[2][*p];
236 *(a++)=color_table[3][*p];
237 p++;
239 for(i=0; i<4; i++) {
240 free(color_table[i]);
242 XpmFreeXpmImage(&xpm);
243 return image;
246 #endif /* USE_XPM */