Files
wmaker/wrlib/load_xpm.c
T

272 lines
6.4 KiB
C
Raw Normal View History

/* xpm.c - load XPM image from file using libXpm
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Raster graphics library
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
1998-09-29 22:36:29 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
2013-01-07 21:51:05 +01:00
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
1998-09-29 22:36:29 +00:00
*/
#include <config.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/xpm.h>
#include "wraster.h"
#include "imgformat.h"
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
RImage *RGetImageFromXPMData(RContext * context, char **xpmData)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
Display *dpy = context->dpy;
Colormap cmap = context->cmap;
RImage *image;
XpmImage xpm;
unsigned char *color_table[4];
unsigned char *data;
int *p;
int i;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
i = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
if (i != XpmSuccess) {
switch (i) {
case XpmOpenFailed:
RErrorCode = RERR_OPEN;
break;
case XpmFileInvalid:
RErrorCode = RERR_BADIMAGEFILE;
break;
case XpmNoMemory:
RErrorCode = RERR_NOMEMORY;
break;
default:
RErrorCode = RERR_BADIMAGEFILE;
break;
}
return NULL;
}
if (xpm.height < 1 || xpm.width < 1) {
RErrorCode = RERR_BADIMAGEFILE;
XpmFreeXpmImage(&xpm);
return NULL;
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (xpm.colorTable == NULL) {
RErrorCode = RERR_BADIMAGEFILE;
XpmFreeXpmImage(&xpm);
return NULL;
}
image = RCreateImage(xpm.width, xpm.height, True);
if (!image) {
XpmFreeXpmImage(&xpm);
return NULL;
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
/* make color table */
for (i = 0; i < 4; i++) {
color_table[i] = malloc(xpm.ncolors * sizeof(char));
if (!color_table[i]) {
for (i = i - 1; i >= 0; i--) {
if (color_table[i])
free(color_table[i]);
}
RReleaseImage(image);
RErrorCode = RERR_NOMEMORY;
XpmFreeXpmImage(&xpm);
return NULL;
}
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
for (i = 0; i < xpm.ncolors; i++) {
XColor xcolor;
char *color = NULL;
2009-08-20 00:59:40 +02:00
if (xpm.colorTable[i].c_color)
color = xpm.colorTable[i].c_color;
else if (xpm.colorTable[i].g_color)
color = xpm.colorTable[i].g_color;
else if (xpm.colorTable[i].g4_color)
color = xpm.colorTable[i].g4_color;
else if (xpm.colorTable[i].m_color)
color = xpm.colorTable[i].m_color;
else if (xpm.colorTable[i].symbolic)
color = xpm.colorTable[i].symbolic;
1999-07-04 20:43:53 +00:00
2009-08-20 00:59:40 +02:00
if (!color) {
color_table[0][i] = 0xbe;
color_table[1][i] = 0xbe;
color_table[2][i] = 0xbe;
color_table[3][i] = 0xff;
continue;
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (strncmp(color, "None", 4) == 0) {
color_table[0][i] = 0;
color_table[1][i] = 0;
color_table[2][i] = 0;
color_table[3][i] = 0;
continue;
}
if (XParseColor(dpy, cmap, color, &xcolor)) {
color_table[0][i] = xcolor.red >> 8;
color_table[1][i] = xcolor.green >> 8;
color_table[2][i] = xcolor.blue >> 8;
color_table[3][i] = 0xff;
} else {
color_table[0][i] = 0xbe;
color_table[1][i] = 0xbe;
color_table[2][i] = 0xbe;
color_table[3][i] = 0xff;
}
}
/* convert pixmap to RImage */
p = (int *)xpm.data;
data = image->data;
for (i = 0; i < xpm.width * xpm.height; i++) {
*(data++) = color_table[0][*p];
*(data++) = color_table[1][*p];
*(data++) = color_table[2][*p];
*(data++) = color_table[3][*p];
p++;
}
for (i = 0; i < 4; i++) {
free(color_table[i]);
}
XpmFreeXpmImage(&xpm);
return image;
1998-09-29 22:36:29 +00:00
}
RImage *RLoadXPM(RContext * context, const char *file)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
Display *dpy = context->dpy;
Colormap cmap = context->cmap;
RImage *image;
XpmImage xpm;
unsigned char *color_table[4];
unsigned char *data;
int *p;
int i;
2004-10-12 21:28:27 +00:00
i = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
2009-08-20 00:59:40 +02:00
if (i != XpmSuccess) {
switch (i) {
case XpmOpenFailed:
RErrorCode = RERR_OPEN;
break;
case XpmFileInvalid:
RErrorCode = RERR_BADIMAGEFILE;
break;
case XpmNoMemory:
RErrorCode = RERR_NOMEMORY;
break;
default:
RErrorCode = RERR_BADIMAGEFILE;
break;
}
return NULL;
}
if (xpm.height < 1 || xpm.width < 1) {
RErrorCode = RERR_BADIMAGEFILE;
XpmFreeXpmImage(&xpm);
return NULL;
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (xpm.colorTable == NULL) {
RErrorCode = RERR_BADIMAGEFILE;
XpmFreeXpmImage(&xpm);
return NULL;
}
image = RCreateImage(xpm.width, xpm.height, True);
if (!image) {
XpmFreeXpmImage(&xpm);
return NULL;
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
/* make color table */
for (i = 0; i < 4; i++) {
color_table[i] = malloc(xpm.ncolors * sizeof(char));
if (!color_table[i]) {
for (i = i - 1; i >= 0; i--) {
if (color_table[i])
free(color_table[i]);
}
RReleaseImage(image);
RErrorCode = RERR_NOMEMORY;
XpmFreeXpmImage(&xpm);
return NULL;
}
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
for (i = 0; i < xpm.ncolors; i++) {
XColor xcolor;
char *color = NULL;
1999-07-04 20:43:53 +00:00
2009-08-20 00:59:40 +02:00
if (xpm.colorTable[i].c_color)
color = xpm.colorTable[i].c_color;
else if (xpm.colorTable[i].g_color)
color = xpm.colorTable[i].g_color;
else if (xpm.colorTable[i].g4_color)
color = xpm.colorTable[i].g4_color;
else if (xpm.colorTable[i].m_color)
color = xpm.colorTable[i].m_color;
else if (xpm.colorTable[i].symbolic)
color = xpm.colorTable[i].symbolic;
1999-07-04 20:43:53 +00:00
2009-08-20 00:59:40 +02:00
if (!color) {
color_table[0][i] = 0xbe;
color_table[1][i] = 0xbe;
color_table[2][i] = 0xbe;
color_table[3][i] = 0xff;
continue;
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (strncmp(color, "None", 4) == 0) {
color_table[0][i] = 0;
color_table[1][i] = 0;
color_table[2][i] = 0;
color_table[3][i] = 0;
continue;
}
if (XParseColor(dpy, cmap, color, &xcolor)) {
color_table[0][i] = xcolor.red >> 8;
color_table[1][i] = xcolor.green >> 8;
color_table[2][i] = xcolor.blue >> 8;
color_table[3][i] = 0xff;
} else {
color_table[0][i] = 0xbe;
color_table[1][i] = 0xbe;
color_table[2][i] = 0xbe;
color_table[3][i] = 0xff;
}
}
/* convert pixmap to RImage */
p = (int *)xpm.data;
data = image->data;
for (i = 0; i < xpm.width * xpm.height; i++, p++) {
*(data++) = color_table[0][*p];
*(data++) = color_table[1][*p];
*(data++) = color_table[2][*p];
*(data++) = color_table[3][*p];
}
for (i = 0; i < 4; i++) {
free(color_table[i]);
}
XpmFreeXpmImage(&xpm);
return image;
1998-09-29 22:36:29 +00:00
}