1 /* xpm.c - load XPM image from file using libXpm
3 * Raster graphics library
5 * Copyright (c) 1997-2003 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., 51 Franklin St, Fifth Floor, Boston,
32 #include "imgformat.h"
34 RImage
*RGetImageFromXPMData(RContext
* context
, char **xpmData
)
36 Display
*dpy
= context
->dpy
;
37 Colormap cmap
= context
->cmap
;
40 unsigned char *color_table
[4];
45 i
= XpmCreateXpmImageFromData(xpmData
, &xpm
, (XpmInfo
*) NULL
);
46 if (i
!= XpmSuccess
) {
49 RErrorCode
= RERR_OPEN
;
52 RErrorCode
= RERR_BADIMAGEFILE
;
55 RErrorCode
= RERR_NOMEMORY
;
58 RErrorCode
= RERR_BADIMAGEFILE
;
63 if (xpm
.height
< 1 || xpm
.width
< 1) {
64 RErrorCode
= RERR_BADIMAGEFILE
;
65 XpmFreeXpmImage(&xpm
);
69 if (xpm
.colorTable
== NULL
) {
70 RErrorCode
= RERR_BADIMAGEFILE
;
71 XpmFreeXpmImage(&xpm
);
74 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
76 XpmFreeXpmImage(&xpm
);
80 /* make color table */
81 for (i
= 0; i
< 4; i
++) {
82 color_table
[i
] = malloc(xpm
.ncolors
* sizeof(unsigned char));
83 if (!color_table
[i
]) {
84 for (i
= i
- 1; i
>= 0; i
--) {
89 RErrorCode
= RERR_NOMEMORY
;
90 XpmFreeXpmImage(&xpm
);
95 for (i
= 0; i
< xpm
.ncolors
; i
++) {
99 if (xpm
.colorTable
[i
].c_color
)
100 color
= xpm
.colorTable
[i
].c_color
;
101 else if (xpm
.colorTable
[i
].g_color
)
102 color
= xpm
.colorTable
[i
].g_color
;
103 else if (xpm
.colorTable
[i
].g4_color
)
104 color
= xpm
.colorTable
[i
].g4_color
;
105 else if (xpm
.colorTable
[i
].m_color
)
106 color
= xpm
.colorTable
[i
].m_color
;
107 else if (xpm
.colorTable
[i
].symbolic
)
108 color
= xpm
.colorTable
[i
].symbolic
;
111 color_table
[0][i
] = 0xbe;
112 color_table
[1][i
] = 0xbe;
113 color_table
[2][i
] = 0xbe;
114 color_table
[3][i
] = 0xff;
118 if (strncmp(color
, "None", 4) == 0) {
119 color_table
[0][i
] = 0;
120 color_table
[1][i
] = 0;
121 color_table
[2][i
] = 0;
122 color_table
[3][i
] = 0;
125 if (XParseColor(dpy
, cmap
, color
, &xcolor
)) {
126 color_table
[0][i
] = xcolor
.red
>> 8;
127 color_table
[1][i
] = xcolor
.green
>> 8;
128 color_table
[2][i
] = xcolor
.blue
>> 8;
129 color_table
[3][i
] = 0xff;
131 color_table
[0][i
] = 0xbe;
132 color_table
[1][i
] = 0xbe;
133 color_table
[2][i
] = 0xbe;
134 color_table
[3][i
] = 0xff;
137 /* convert pixmap to RImage */
140 for (i
= 0; i
< xpm
.width
* xpm
.height
; i
++) {
141 *(data
++) = color_table
[0][*p
];
142 *(data
++) = color_table
[1][*p
];
143 *(data
++) = color_table
[2][*p
];
144 *(data
++) = color_table
[3][*p
];
147 for (i
= 0; i
< 4; i
++) {
148 free(color_table
[i
]);
150 XpmFreeXpmImage(&xpm
);
154 RImage
*RLoadXPM(RContext
* context
, const char *file
)
156 Display
*dpy
= context
->dpy
;
157 Colormap cmap
= context
->cmap
;
160 unsigned char *color_table
[4];
165 i
= XpmReadFileToXpmImage((char *)file
, &xpm
, (XpmInfo
*) NULL
);
166 if (i
!= XpmSuccess
) {
169 RErrorCode
= RERR_OPEN
;
172 RErrorCode
= RERR_BADIMAGEFILE
;
175 RErrorCode
= RERR_NOMEMORY
;
178 RErrorCode
= RERR_BADIMAGEFILE
;
183 if (xpm
.height
< 1 || xpm
.width
< 1) {
184 RErrorCode
= RERR_BADIMAGEFILE
;
185 XpmFreeXpmImage(&xpm
);
189 if (xpm
.colorTable
== NULL
) {
190 RErrorCode
= RERR_BADIMAGEFILE
;
191 XpmFreeXpmImage(&xpm
);
194 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
196 XpmFreeXpmImage(&xpm
);
200 /* make color table */
201 for (i
= 0; i
< 4; i
++) {
202 color_table
[i
] = malloc(xpm
.ncolors
* sizeof(unsigned char));
203 if (!color_table
[i
]) {
204 for (i
= i
- 1; i
>= 0; i
--) {
206 free(color_table
[i
]);
208 RReleaseImage(image
);
209 RErrorCode
= RERR_NOMEMORY
;
210 XpmFreeXpmImage(&xpm
);
215 for (i
= 0; i
< xpm
.ncolors
; i
++) {
219 if (xpm
.colorTable
[i
].c_color
)
220 color
= xpm
.colorTable
[i
].c_color
;
221 else if (xpm
.colorTable
[i
].g_color
)
222 color
= xpm
.colorTable
[i
].g_color
;
223 else if (xpm
.colorTable
[i
].g4_color
)
224 color
= xpm
.colorTable
[i
].g4_color
;
225 else if (xpm
.colorTable
[i
].m_color
)
226 color
= xpm
.colorTable
[i
].m_color
;
227 else if (xpm
.colorTable
[i
].symbolic
)
228 color
= xpm
.colorTable
[i
].symbolic
;
231 color_table
[0][i
] = 0xbe;
232 color_table
[1][i
] = 0xbe;
233 color_table
[2][i
] = 0xbe;
234 color_table
[3][i
] = 0xff;
238 if (strncmp(color
, "None", 4) == 0) {
239 color_table
[0][i
] = 0;
240 color_table
[1][i
] = 0;
241 color_table
[2][i
] = 0;
242 color_table
[3][i
] = 0;
245 if (XParseColor(dpy
, cmap
, color
, &xcolor
)) {
246 color_table
[0][i
] = xcolor
.red
>> 8;
247 color_table
[1][i
] = xcolor
.green
>> 8;
248 color_table
[2][i
] = xcolor
.blue
>> 8;
249 color_table
[3][i
] = 0xff;
251 color_table
[0][i
] = 0xbe;
252 color_table
[1][i
] = 0xbe;
253 color_table
[2][i
] = 0xbe;
254 color_table
[3][i
] = 0xff;
257 /* convert pixmap to RImage */
260 for (i
= 0; i
< xpm
.width
* xpm
.height
; i
++, p
++) {
261 *(data
++) = color_table
[0][*p
];
262 *(data
++) = color_table
[1][*p
];
263 *(data
++) = color_table
[2][*p
];
264 *(data
++) = color_table
[3][*p
];
266 for (i
= 0; i
< 4; i
++) {
267 free(color_table
[i
]);
269 XpmFreeXpmImage(&xpm
);