NEWS: Hot corners feature description
[wmaker-crm.git] / wrlib / load_xpm.c
blob54697acfb0eea641ce9d3c540afb902105ad4df9
1 /* xpm.c - load XPM image from file using libXpm
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 2014-2021 Window Maker Team
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
24 #include <config.h>
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"
33 #include "imgformat.h"
34 #include "wr_i18n.h"
37 static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
39 Display *dpy = context->dpy;
40 Colormap cmap = context->cmap;
41 RImage *image;
42 unsigned char *color_table[4];
43 unsigned char *data;
44 int i;
45 int *p;
47 if (xpm.height < 1 || xpm.width < 1) {
48 RErrorCode = RERR_BADIMAGEFILE;
49 return NULL;
52 if (xpm.colorTable == NULL) {
53 RErrorCode = RERR_BADIMAGEFILE;
54 return NULL;
56 image = RCreateImage(xpm.width, xpm.height, True);
57 if (!image)
58 return NULL;
60 /* make color table */
61 for (i = 0; i < 4; i++) {
62 color_table[i] = malloc(xpm.ncolors * sizeof(unsigned char));
63 if (!color_table[i]) {
64 for (i = i - 1; i >= 0; i--) {
65 if (color_table[i])
66 free(color_table[i]);
68 RReleaseImage(image);
69 RErrorCode = RERR_NOMEMORY;
70 return NULL;
74 for (i = 0; i < xpm.ncolors; i++) {
75 XColor xcolor;
76 char *color = NULL;
78 if (xpm.colorTable[i].c_color)
79 color = xpm.colorTable[i].c_color;
80 else if (xpm.colorTable[i].g_color)
81 color = xpm.colorTable[i].g_color;
82 else if (xpm.colorTable[i].g4_color)
83 color = xpm.colorTable[i].g4_color;
84 else if (xpm.colorTable[i].m_color)
85 color = xpm.colorTable[i].m_color;
86 else if (xpm.colorTable[i].symbolic)
87 color = xpm.colorTable[i].symbolic;
89 if (!color) {
90 color_table[0][i] = 0xbe;
91 color_table[1][i] = 0xbe;
92 color_table[2][i] = 0xbe;
93 color_table[3][i] = 0xff;
94 continue;
97 if (strncmp(color, "None", 4) == 0) {
98 color_table[0][i] = 0;
99 color_table[1][i] = 0;
100 color_table[2][i] = 0;
101 color_table[3][i] = 0;
102 continue;
104 if (XParseColor(dpy, cmap, color, &xcolor)) {
105 color_table[0][i] = xcolor.red >> 8;
106 color_table[1][i] = xcolor.green >> 8;
107 color_table[2][i] = xcolor.blue >> 8;
108 color_table[3][i] = 0xff;
109 } else {
110 color_table[0][i] = 0xbe;
111 color_table[1][i] = 0xbe;
112 color_table[2][i] = 0xbe;
113 color_table[3][i] = 0xff;
116 /* convert pixmap to RImage */
117 p = (int *)xpm.data;
118 data = image->data;
119 for (i = 0; i < xpm.width * xpm.height; i++, p++) {
120 *(data++) = color_table[0][*p];
121 *(data++) = color_table[1][*p];
122 *(data++) = color_table[2][*p];
123 *(data++) = color_table[3][*p];
125 for (i = 0; i < 4; i++)
126 free(color_table[i]);
127 return image;
130 static int is_xpm_error(int status)
132 if (status == XpmSuccess)
133 return 0;
135 switch (status) {
136 case XpmOpenFailed:
137 RErrorCode = RERR_OPEN;
138 break;
139 case XpmFileInvalid:
140 RErrorCode = RERR_BADIMAGEFILE;
141 break;
142 case XpmNoMemory:
143 RErrorCode = RERR_NOMEMORY;
144 break;
145 default:
146 RErrorCode = RERR_BADIMAGEFILE;
147 break;
149 return 1;
152 RImage *RGetImageFromXPMData(RContext *context, char **xpmData)
154 RImage *image;
155 XpmImage xpm;
156 int status;
158 status = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
159 if (is_xpm_error(status))
160 return NULL;
162 image = create_rimage_from_xpm(context, xpm);
163 XpmFreeXpmImage(&xpm);
164 return image;
167 RImage *RLoadXPM(RContext *context, const char *file)
169 RImage *image;
170 XpmImage xpm;
171 int status;
173 status = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
174 if (is_xpm_error(status))
175 return NULL;
177 image = create_rimage_from_xpm(context, xpm);
178 XpmFreeXpmImage(&xpm);
179 return image;