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
[4];
43 unsigned char *r
, *g
, *b
, *a
;
47 i
= XpmCreateXpmImageFromData(data
, &xpm
, (XpmInfo
*)NULL
);
51 RErrorCode
= RERR_OPEN
;
54 RErrorCode
= RERR_BADIMAGEFILE
;
57 RErrorCode
= RERR_NOMEMORY
;
60 RErrorCode
= RERR_BADIMAGEFILE
;
65 if (xpm
.height
<1 || xpm
.width
< 1) {
66 RErrorCode
= RERR_BADIMAGEFILE
;
67 XpmFreeXpmImage(&xpm
);
71 if (xpm
.colorTable
==NULL
) {
72 RErrorCode
= RERR_BADIMAGEFILE
;
73 XpmFreeXpmImage(&xpm
);
76 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
78 XpmFreeXpmImage(&xpm
);
82 /* make color table */
84 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
85 if (!color_table
[i
]) {
86 for (i
=i
-1;i
>=0; i
--) {
91 RErrorCode
= RERR_NOMEMORY
;
92 XpmFreeXpmImage(&xpm
);
97 for (i
=0; i
<xpm
.ncolors
; i
++) {
100 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
107 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
108 color_table
[0][i
] = xcolor
.red
>>8;
109 color_table
[1][i
] = xcolor
.green
>>8;
110 color_table
[2][i
] = xcolor
.blue
>>8;
111 color_table
[3][i
] = 0xff;
113 color_table
[0][i
] = 0xbe;
114 color_table
[1][i
] = 0xbe;
115 color_table
[2][i
] = 0xbe;
116 color_table
[3][i
] = 0xff;
119 memset(image
->data
[3], 255, xpm
.width
*xpm
.height
);
120 /* convert pixmap to RImage */
126 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
127 *(r
++)=color_table
[0][*p
];
128 *(g
++)=color_table
[1][*p
];
129 *(b
++)=color_table
[2][*p
];
130 *(a
++)=color_table
[3][*p
];
134 free(color_table
[i
]);
136 XpmFreeXpmImage(&xpm
);
143 RLoadXPM(RContext
*context
, char *file
, int index
)
145 Display
*dpy
= context
->dpy
;
146 Colormap cmap
= context
->cmap
;
149 unsigned char *color_table
[4];
150 unsigned char *r
, *g
, *b
, *a
;
154 i
= XpmReadFileToXpmImage(file
, &xpm
, (XpmInfo
*)NULL
);
158 RErrorCode
= RERR_OPEN
;
161 RErrorCode
= RERR_BADIMAGEFILE
;
164 RErrorCode
= RERR_NOMEMORY
;
167 RErrorCode
= RERR_BADIMAGEFILE
;
172 if (xpm
.height
<1 || xpm
.width
< 1) {
173 RErrorCode
= RERR_BADIMAGEFILE
;
174 XpmFreeXpmImage(&xpm
);
178 if (xpm
.colorTable
==NULL
) {
179 RErrorCode
= RERR_BADIMAGEFILE
;
180 XpmFreeXpmImage(&xpm
);
183 image
= RCreateImage(xpm
.width
, xpm
.height
, True
);
185 XpmFreeXpmImage(&xpm
);
189 /* make color table */
190 for (i
=0; i
<4; i
++) {
191 color_table
[i
] = malloc(xpm
.ncolors
*sizeof(char));
192 if (!color_table
[i
]) {
193 for (i
=i
-1;i
>=0; i
--) {
195 free(color_table
[i
]);
197 RDestroyImage(image
);
198 RErrorCode
= RERR_NOMEMORY
;
199 XpmFreeXpmImage(&xpm
);
204 for (i
=0; i
<xpm
.ncolors
; i
++) {
207 if (strncmp(xpm
.colorTable
[i
].c_color
,"None",4)==0) {
214 if (XParseColor(dpy
, cmap
, xpm
.colorTable
[i
].c_color
, &xcolor
)) {
215 color_table
[0][i
] = xcolor
.red
>>8;
216 color_table
[1][i
] = xcolor
.green
>>8;
217 color_table
[2][i
] = xcolor
.blue
>>8;
218 color_table
[3][i
] = 0xff;
220 color_table
[0][i
] = 0xbe;
221 color_table
[1][i
] = 0xbe;
222 color_table
[2][i
] = 0xbe;
223 color_table
[3][i
] = 0xff;
226 /* convert pixmap to RImage */
232 for (i
=0; i
<xpm
.width
*xpm
.height
; i
++) {
233 *(r
++)=color_table
[0][*p
];
234 *(g
++)=color_table
[1][*p
];
235 *(b
++)=color_table
[2][*p
];
236 *(a
++)=color_table
[3][*p
];
240 free(color_table
[i
]);
242 XpmFreeXpmImage(&xpm
);