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,
33 #include "imgformat.h"
37 static RImage
*create_rimage_from_xpm(RContext
*context
, XpmImage xpm
)
39 Display
*dpy
= context
->dpy
;
40 Colormap cmap
= context
->cmap
;
42 unsigned char *color_table
[4];
47 if (xpm
.height
< 1 || xpm
.width
< 1) {
48 RErrorCode
= RERR_BADIMAGEFILE
;
52 if (xpm
.colorTable
== NULL
) {
53 RErrorCode
= RERR_BADIMAGEFILE
;
56 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
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
--) {
69 RErrorCode
= RERR_NOMEMORY
;
74 for (i
= 0; i
< xpm
.ncolors
; i
++) {
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
;
90 color_table
[0][i
] = 0xbe;
91 color_table
[1][i
] = 0xbe;
92 color_table
[2][i
] = 0xbe;
93 color_table
[3][i
] = 0xff;
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;
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;
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 */
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
]);
130 static int is_xpm_error(int status
)
132 if (status
== XpmSuccess
)
137 RErrorCode
= RERR_OPEN
;
140 RErrorCode
= RERR_BADIMAGEFILE
;
143 RErrorCode
= RERR_NOMEMORY
;
146 RErrorCode
= RERR_BADIMAGEFILE
;
152 RImage
*RGetImageFromXPMData(RContext
*context
, char **xpmData
)
158 status
= XpmCreateXpmImageFromData(xpmData
, &xpm
, (XpmInfo
*) NULL
);
159 if (is_xpm_error(status
))
162 image
= create_rimage_from_xpm(context
, xpm
);
163 XpmFreeXpmImage(&xpm
);
167 RImage
*RLoadXPM(RContext
*context
, const char *file
)
173 status
= XpmReadFileToXpmImage((char *)file
, &xpm
, (XpmInfo
*) NULL
);
174 if (is_xpm_error(status
))
177 image
= create_rimage_from_xpm(context
, xpm
);
178 XpmFreeXpmImage(&xpm
);