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"
35 static RImage
*create_rimage_from_xpm(RContext
*context
, XpmImage xpm
)
37 Display
*dpy
= context
->dpy
;
38 Colormap cmap
= context
->cmap
;
40 unsigned char *color_table
[4];
45 if (xpm
.height
< 1 || xpm
.width
< 1) {
46 RErrorCode
= RERR_BADIMAGEFILE
;
50 if (xpm
.colorTable
== NULL
) {
51 RErrorCode
= RERR_BADIMAGEFILE
;
54 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
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
--) {
67 RErrorCode
= RERR_NOMEMORY
;
72 for (i
= 0; i
< xpm
.ncolors
; i
++) {
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
;
88 color_table
[0][i
] = 0xbe;
89 color_table
[1][i
] = 0xbe;
90 color_table
[2][i
] = 0xbe;
91 color_table
[3][i
] = 0xff;
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;
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;
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 */
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
]);
128 static int is_xpm_error(int status
)
130 if (status
== XpmSuccess
)
135 RErrorCode
= RERR_OPEN
;
138 RErrorCode
= RERR_BADIMAGEFILE
;
141 RErrorCode
= RERR_NOMEMORY
;
144 RErrorCode
= RERR_BADIMAGEFILE
;
150 RImage
*RGetImageFromXPMData(RContext
*context
, char **xpmData
)
156 status
= XpmCreateXpmImageFromData(xpmData
, &xpm
, (XpmInfo
*) NULL
);
157 if (is_xpm_error(status
))
160 image
= create_rimage_from_xpm(context
, xpm
);
161 XpmFreeXpmImage(&xpm
);
165 RImage
*RLoadXPM(RContext
*context
, const char *file
)
171 status
= XpmReadFileToXpmImage((char *)file
, &xpm
, (XpmInfo
*) NULL
);
172 if (is_xpm_error(status
))
175 image
= create_rimage_from_xpm(context
, xpm
);
176 XpmFreeXpmImage(&xpm
);