Initial revision
[wmaker-crm.git] / wrlib / xpm.c
blob53bcbdb870d249ac336e4d00191ed98a8ba7e3ea
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[3];
43 unsigned char *r, *g, *b, *a;
44 int *p;
45 int i;
46 int transp=-1;
48 RErrorString[0] = 0;
49 if (XpmCreateXpmImageFromData(data, &xpm, (XpmInfo *)NULL)!=XpmSuccess) {
50 sprintf(RErrorString, "error converting XPM data");
51 return NULL;
53 if (xpm.height<1 || xpm.width < 1) {
54 sprintf(RErrorString, "can't convert XPM data. Size too weird");
55 XpmFreeXpmImage(&xpm);
56 return NULL;
59 if (xpm.colorTable==NULL) {
60 sprintf(RErrorString, "error converting XPM data");
61 XpmFreeXpmImage(&xpm);
62 return NULL;
64 image = RCreateImage(xpm.width, xpm.height, True);
65 if (!image) {
66 sprintf(RErrorString, ":could not create RImage");
67 XpmFreeXpmImage(&xpm);
68 return NULL;
71 /* make color table */
72 for (i=0; i<3; i++) {
73 color_table[i] = malloc(xpm.ncolors*sizeof(char));
74 if (!color_table[i]) {
75 for (i=i-1;i>=0; i--) {
76 if (color_table[i])
77 free(color_table[i]);
79 RDestroyImage(image);
80 sprintf(RErrorString, "out of memory while converting XPM data");
81 XpmFreeXpmImage(&xpm);
82 return NULL;
86 for (i=0; i<xpm.ncolors; i++) {
87 XColor xcolor;
89 if (strncmp(xpm.colorTable[i].c_color,"None",4)==0) {
90 color_table[0][i]=0;
91 color_table[1][i]=0;
92 color_table[2][i]=0;
93 transp = i;
94 continue;
96 if (XParseColor(dpy, cmap, xpm.colorTable[i].c_color, &xcolor)) {
97 color_table[0][i] = xcolor.red>>8;
98 color_table[1][i] = xcolor.green>>8;
99 color_table[2][i] = xcolor.blue>>8;
100 } else {
101 color_table[0][i]=0xbe;
102 color_table[1][i]=0xbe;
103 color_table[2][i]=0xbe;
106 memset(image->data[3], 255, xpm.width*xpm.height);
107 /* convert pixmap to RImage */
108 p = (int*)xpm.data;
109 r = image->data[0];
110 g = image->data[1];
111 b = image->data[2];
112 a = image->data[3];
113 for (i=0; i<xpm.width*xpm.height; i++) {
114 if (*p==transp)
115 *a=0;
116 a++;
117 *(r++)=color_table[0][*p];
118 *(g++)=color_table[1][*p];
119 *(b++)=color_table[2][*p];
120 p++;
122 for(i=0; i<3; i++) {
123 free(color_table[i]);
125 XpmFreeXpmImage(&xpm);
126 return image;
131 RImage*
132 RLoadXPM(RContext *context, char *file, int index)
134 Display *dpy = context->dpy;
135 Colormap cmap = context->cmap;
136 RImage *image;
137 XpmImage xpm;
138 unsigned char *color_table[3];
139 unsigned char *r, *g, *b, *a;
140 int *p;
141 int i;
142 int transp=-1;
144 RErrorString[0] = 0;
145 if (XpmReadFileToXpmImage(file, &xpm, (XpmInfo *)NULL)!=XpmSuccess) {
146 sprintf(RErrorString, "error loading XPM file \"%s\"", file);
147 return NULL;
149 if (xpm.height<1 || xpm.width < 1) {
150 sprintf(RErrorString, "can't load XPM file \"%s\". Size too weird",
151 file);
152 XpmFreeXpmImage(&xpm);
153 return NULL;
156 if (xpm.colorTable==NULL) {
157 sprintf(RErrorString, "error loading XPM file \"%s\"", file);
158 XpmFreeXpmImage(&xpm);
159 return NULL;
161 image = RCreateImage(xpm.width, xpm.height, True);
162 if (!image) {
163 strcat(RErrorString, ":could not create RImage");
164 XpmFreeXpmImage(&xpm);
165 return NULL;
168 /* make color table */
169 for (i=0; i<3; i++) {
170 color_table[i] = malloc(xpm.ncolors*sizeof(char));
171 if (!color_table[i]) {
172 for (i=i-1;i>=0; i--) {
173 if (color_table[i])
174 free(color_table[i]);
176 RDestroyImage(image);
177 sprintf(RErrorString, "out of memory while loading file \"%s\"",
178 file);
179 XpmFreeXpmImage(&xpm);
180 return NULL;
184 for (i=0; i<xpm.ncolors; i++) {
185 XColor xcolor;
187 if (strncmp(xpm.colorTable[i].c_color,"None",4)==0) {
188 color_table[0][i]=0;
189 color_table[1][i]=0;
190 color_table[2][i]=0;
191 transp = i;
192 continue;
194 if (XParseColor(dpy, cmap, xpm.colorTable[i].c_color, &xcolor)) {
195 color_table[0][i] = xcolor.red>>8;
196 color_table[1][i] = xcolor.green>>8;
197 color_table[2][i] = xcolor.blue>>8;
198 } else {
199 color_table[0][i]=0xbe;
200 color_table[1][i]=0xbe;
201 color_table[2][i]=0xbe;
204 memset(image->data[3], 255, xpm.width*xpm.height);
205 /* convert pixmap to RImage */
206 p = (int*)xpm.data;
207 r = image->data[0];
208 g = image->data[1];
209 b = image->data[2];
210 a = image->data[3];
211 for (i=0; i<xpm.width*xpm.height; i++) {
212 if (*p==transp)
213 *a=0;
214 a++;
215 *(r++)=color_table[0][*p];
216 *(g++)=color_table[1][*p];
217 *(b++)=color_table[2][*p];
218 p++;
220 for(i=0; i<3; i++) {
221 free(color_table[i]);
223 XpmFreeXpmImage(&xpm);
224 return image;
228 #endif /* USE_XPM */