WPrefs: trivial fixes in text strings
[wmaker-crm.git] / wrlib / load_xpm.c
blob8f3592d1b43785734c3aaff7c4c9b345b073fef8
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 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"
35 static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
37 Display *dpy = context->dpy;
38 Colormap cmap = context->cmap;
39 RImage *image;
40 unsigned char *color_table[4];
41 unsigned char *data;
42 int i;
43 int *p;
45 if (xpm.height < 1 || xpm.width < 1) {
46 RErrorCode = RERR_BADIMAGEFILE;
47 return NULL;
50 if (xpm.colorTable == NULL) {
51 RErrorCode = RERR_BADIMAGEFILE;
52 return NULL;
54 image = RCreateImage(xpm.width, xpm.height, True);
55 if (!image)
56 return NULL;
58 /* make color table */
59 for (i = 0; i < 4; i++) {
60 color_table[i] = malloc(xpm.ncolors * sizeof(unsigned char));
61 if (!color_table[i]) {
62 for (i = i - 1; i >= 0; i--) {
63 if (color_table[i])
64 free(color_table[i]);
66 RReleaseImage(image);
67 RErrorCode = RERR_NOMEMORY;
68 return NULL;
72 for (i = 0; i < xpm.ncolors; i++) {
73 XColor xcolor;
74 char *color = NULL;
76 if (xpm.colorTable[i].c_color)
77 color = xpm.colorTable[i].c_color;
78 else if (xpm.colorTable[i].g_color)
79 color = xpm.colorTable[i].g_color;
80 else if (xpm.colorTable[i].g4_color)
81 color = xpm.colorTable[i].g4_color;
82 else if (xpm.colorTable[i].m_color)
83 color = xpm.colorTable[i].m_color;
84 else if (xpm.colorTable[i].symbolic)
85 color = xpm.colorTable[i].symbolic;
87 if (!color) {
88 color_table[0][i] = 0xbe;
89 color_table[1][i] = 0xbe;
90 color_table[2][i] = 0xbe;
91 color_table[3][i] = 0xff;
92 continue;
95 if (strncmp(color, "None", 4) == 0) {
96 color_table[0][i] = 0;
97 color_table[1][i] = 0;
98 color_table[2][i] = 0;
99 color_table[3][i] = 0;
100 continue;
102 if (XParseColor(dpy, cmap, color, &xcolor)) {
103 color_table[0][i] = xcolor.red >> 8;
104 color_table[1][i] = xcolor.green >> 8;
105 color_table[2][i] = xcolor.blue >> 8;
106 color_table[3][i] = 0xff;
107 } else {
108 color_table[0][i] = 0xbe;
109 color_table[1][i] = 0xbe;
110 color_table[2][i] = 0xbe;
111 color_table[3][i] = 0xff;
114 /* convert pixmap to RImage */
115 p = (int *)xpm.data;
116 data = image->data;
117 for (i = 0; i < xpm.width * xpm.height; i++, p++) {
118 *(data++) = color_table[0][*p];
119 *(data++) = color_table[1][*p];
120 *(data++) = color_table[2][*p];
121 *(data++) = color_table[3][*p];
123 for (i = 0; i < 4; i++)
124 free(color_table[i]);
125 return image;
128 static int is_xpm_error(int status)
130 if (status == XpmSuccess)
131 return 0;
133 switch (status) {
134 case XpmOpenFailed:
135 RErrorCode = RERR_OPEN;
136 break;
137 case XpmFileInvalid:
138 RErrorCode = RERR_BADIMAGEFILE;
139 break;
140 case XpmNoMemory:
141 RErrorCode = RERR_NOMEMORY;
142 break;
143 default:
144 RErrorCode = RERR_BADIMAGEFILE;
145 break;
147 return 1;
150 RImage *RGetImageFromXPMData(RContext *context, char **xpmData)
152 RImage *image;
153 XpmImage xpm;
154 int status;
156 status = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
157 if (is_xpm_error(status))
158 return NULL;
160 image = create_rimage_from_xpm(context, xpm);
161 XpmFreeXpmImage(&xpm);
162 return image;
165 RImage *RLoadXPM(RContext *context, const char *file)
167 RImage *image;
168 XpmImage xpm;
169 int status;
171 status = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
172 if (is_xpm_error(status))
173 return NULL;
175 image = create_rimage_from_xpm(context, xpm);
176 XpmFreeXpmImage(&xpm);
177 return image;