1 /* xpm.c - load XPM image from file
3 * Raster graphics library
5 * Copyright (c) 1997 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., 675 Mass Ave, Cambridge, MA 02139, USA.
36 RGetImageFromXPMData(RContext
*context
, char **data
)
38 Display
*dpy
= context
->dpy
;
39 Colormap cmap
= context
->cmap
;
42 unsigned char *color_table
[3];
43 unsigned char *r
, *g
, *b
, *a
;
49 if (XpmCreateXpmImageFromData(data
, &xpm
, (XpmInfo
*)NULL
)!=XpmSuccess
) {
50 sprintf(RErrorString
, "error converting XPM data");
53 if (xpm
.height
<1 || xpm
.width
< 1) {
54 sprintf(RErrorString
, "can't convert XPM data. Size too weird");
55 XpmFreeXpmImage(&xpm
);
59 if (xpm
.colorTable
==NULL
) {
60 sprintf(RErrorString
, "error converting XPM data");
61 XpmFreeXpmImage(&xpm
);
64 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
66 sprintf(RErrorString
, ":could not create RImage");
67 XpmFreeXpmImage(&xpm
);
71 /* make color table */
73 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
74 if (!color_table
[i
]) {
75 for (i
=i
-1;i
>=0; i
--) {
80 sprintf(RErrorString
, "out of memory while converting XPM data");
81 XpmFreeXpmImage(&xpm
);
86 for (i
=0; i
<xpm
.ncolors
; i
++) {
89 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
96 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
97 color_table
[0][i
] = xcolor
.red
>>8;
98 color_table
[1][i
] = xcolor
.green
>>8;
99 color_table
[2][i
] = xcolor
.blue
>>8;
101 color_table
[0][i
]=0xbe;
102 color_table
[1][i
]=0xbe;
103 color_table
[2][i
]=0xbe;
106 memset(image
->data
[3], 255, xpm
.width
*xpm
.height
);
107 /* convert pixmap to RImage */
113 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
117 *(r
++)=color_table
[0][*p
];
118 *(g
++)=color_table
[1][*p
];
119 *(b
++)=color_table
[2][*p
];
123 free(color_table
[i
]);
125 XpmFreeXpmImage(&xpm
);
132 RLoadXPM(RContext
*context
, char *file
, int index
)
134 Display
*dpy
= context
->dpy
;
135 Colormap cmap
= context
->cmap
;
138 unsigned char *color_table
[3];
139 unsigned char *r
, *g
, *b
, *a
;
145 if (XpmReadFileToXpmImage(file
, &xpm
, (XpmInfo
*)NULL
)!=XpmSuccess
) {
146 sprintf(RErrorString
, "error loading XPM file \"%s\"", file
);
149 if (xpm
.height
<1 || xpm
.width
< 1) {
150 sprintf(RErrorString
, "can't load XPM file \"%s\". Size too weird",
152 XpmFreeXpmImage(&xpm
);
156 if (xpm
.colorTable
==NULL
) {
157 sprintf(RErrorString
, "error loading XPM file \"%s\"", file
);
158 XpmFreeXpmImage(&xpm
);
161 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
163 strcat(RErrorString
, ":could not create RImage");
164 XpmFreeXpmImage(&xpm
);
168 /* make color table */
169 for (i
=0; i
<3; i
++) {
170 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
171 if (!color_table
[i
]) {
172 for (i
=i
-1;i
>=0; i
--) {
174 free(color_table
[i
]);
176 RDestroyImage(image
);
177 sprintf(RErrorString
, "out of memory while loading file \"%s\"",
179 XpmFreeXpmImage(&xpm
);
184 for (i
=0; i
<xpm
.ncolors
; i
++) {
187 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
194 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
195 color_table
[0][i
] = xcolor
.red
>>8;
196 color_table
[1][i
] = xcolor
.green
>>8;
197 color_table
[2][i
] = xcolor
.blue
>>8;
199 color_table
[0][i
]=0xbe;
200 color_table
[1][i
]=0xbe;
201 color_table
[2][i
]=0xbe;
204 memset(image
->data
[3], 255, xpm
.width
*xpm
.height
);
205 /* convert pixmap to RImage */
211 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
215 *(r
++)=color_table
[0][*p
];
216 *(g
++)=color_table
[1][*p
];
217 *(b
++)=color_table
[2][*p
];
221 free(color_table
[i
]);
223 XpmFreeXpmImage(&xpm
);