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
;
48 i
= XpmCreateXpmImageFromData(data
, &xpm
, (XpmInfo
*)NULL
);
52 RErrorCode
= RERR_OPEN
;
55 RErrorCode
= RERR_BADIMAGEFILE
;
58 RErrorCode
= RERR_NOMEMORY
;
61 RErrorCode
= RERR_BADIMAGEFILE
;
66 if (xpm
.height
<1 || xpm
.width
< 1) {
67 RErrorCode
= RERR_BADIMAGEFILE
;
68 XpmFreeXpmImage(&xpm
);
72 if (xpm
.colorTable
==NULL
) {
73 RErrorCode
= RERR_BADIMAGEFILE
;
74 XpmFreeXpmImage(&xpm
);
77 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
79 XpmFreeXpmImage(&xpm
);
83 /* make color table */
85 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
86 if (!color_table
[i
]) {
87 for (i
=i
-1;i
>=0; i
--) {
92 RErrorCode
= RERR_NOMEMORY
;
93 XpmFreeXpmImage(&xpm
);
98 for (i
=0; i
<xpm
.ncolors
; i
++) {
101 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
108 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
109 color_table
[0][i
] = xcolor
.red
>>8;
110 color_table
[1][i
] = xcolor
.green
>>8;
111 color_table
[2][i
] = xcolor
.blue
>>8;
113 color_table
[0][i
]=0xbe;
114 color_table
[1][i
]=0xbe;
115 color_table
[2][i
]=0xbe;
118 memset(image
->data
[3], 255, xpm
.width
*xpm
.height
);
119 /* convert pixmap to RImage */
125 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
129 *(r
++)=color_table
[0][*p
];
130 *(g
++)=color_table
[1][*p
];
131 *(b
++)=color_table
[2][*p
];
135 free(color_table
[i
]);
137 XpmFreeXpmImage(&xpm
);
144 RLoadXPM(RContext
*context
, char *file
, int index
)
146 Display
*dpy
= context
->dpy
;
147 Colormap cmap
= context
->cmap
;
150 unsigned char *color_table
[3];
151 unsigned char *r
, *g
, *b
, *a
;
156 i
= XpmReadFileToXpmImage(file
, &xpm
, (XpmInfo
*)NULL
);
160 RErrorCode
= RERR_OPEN
;
163 RErrorCode
= RERR_BADIMAGEFILE
;
166 RErrorCode
= RERR_NOMEMORY
;
169 RErrorCode
= RERR_BADIMAGEFILE
;
174 if (xpm
.height
<1 || xpm
.width
< 1) {
175 RErrorCode
= RERR_BADIMAGEFILE
;
176 XpmFreeXpmImage(&xpm
);
180 if (xpm
.colorTable
==NULL
) {
181 RErrorCode
= RERR_BADIMAGEFILE
;
182 XpmFreeXpmImage(&xpm
);
185 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
187 XpmFreeXpmImage(&xpm
);
191 /* make color table */
192 for (i
=0; i
<3; i
++) {
193 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
194 if (!color_table
[i
]) {
195 for (i
=i
-1;i
>=0; i
--) {
197 free(color_table
[i
]);
199 RDestroyImage(image
);
200 RErrorCode
= RERR_NOMEMORY
;
201 XpmFreeXpmImage(&xpm
);
206 for (i
=0; i
<xpm
.ncolors
; i
++) {
209 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
216 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
217 color_table
[0][i
] = xcolor
.red
>>8;
218 color_table
[1][i
] = xcolor
.green
>>8;
219 color_table
[2][i
] = xcolor
.blue
>>8;
221 color_table
[0][i
]=0xbe;
222 color_table
[1][i
]=0xbe;
223 color_table
[2][i
]=0xbe;
226 memset(image
->data
[3], 255, xpm
.width
*xpm
.height
);
227 /* convert pixmap to RImage */
233 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
237 *(r
++)=color_table
[0][*p
];
238 *(g
++)=color_table
[1][*p
];
239 *(b
++)=color_table
[2][*p
];
243 free(color_table
[i
]);
245 XpmFreeXpmImage(&xpm
);