mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-19 04:20:27 +01:00
wrlib: remove duplicate code from xpm support
This patch is removing the duplicate code by using functions create_rimage_xpm and is_xpm_error previously created.
This commit is contained in:
committed by
Carlos R. Mafra
parent
4005557bb3
commit
1e06e1e29b
226
wrlib/load_xpm.c
226
wrlib/load_xpm.c
@@ -146,241 +146,33 @@ static int is_xpm_error(int status)
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
RImage *RGetImageFromXPMData(RContext *context, char **xpmData)
|
RImage *RGetImageFromXPMData(RContext *context, char **xpmData)
|
||||||
{
|
{
|
||||||
Display *dpy = context->dpy;
|
|
||||||
Colormap cmap = context->cmap;
|
|
||||||
RImage *image;
|
RImage *image;
|
||||||
XpmImage xpm;
|
XpmImage xpm;
|
||||||
unsigned char *color_table[4];
|
int status;
|
||||||
unsigned char *data;
|
|
||||||
int *p;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
|
status = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *) NULL);
|
||||||
if (i != XpmSuccess) {
|
if (is_xpm_error(status))
|
||||||
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;
|
return NULL;
|
||||||
}
|
|
||||||
if (xpm.height < 1 || xpm.width < 1) {
|
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xpm.colorTable == NULL) {
|
image = create_rimage_from_xpm(context, xpm);
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
image = RCreateImage(xpm.width, xpm.height, True);
|
|
||||||
if (!image) {
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* make color table */
|
|
||||||
for (i = 0; i < 4; i++) {
|
|
||||||
color_table[i] = malloc(xpm.ncolors * sizeof(unsigned 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < xpm.ncolors; i++) {
|
|
||||||
XColor xcolor;
|
|
||||||
char *color = NULL;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (!color) {
|
|
||||||
color_table[0][i] = 0xbe;
|
|
||||||
color_table[1][i] = 0xbe;
|
|
||||||
color_table[2][i] = 0xbe;
|
|
||||||
color_table[3][i] = 0xff;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
XpmFreeXpmImage(&xpm);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
RImage *RLoadXPM(RContext *context, const char *file)
|
RImage *RLoadXPM(RContext *context, const char *file)
|
||||||
{
|
{
|
||||||
Display *dpy = context->dpy;
|
|
||||||
Colormap cmap = context->cmap;
|
|
||||||
RImage *image;
|
RImage *image;
|
||||||
XpmImage xpm;
|
XpmImage xpm;
|
||||||
unsigned char *color_table[4];
|
int status;
|
||||||
unsigned char *data;
|
|
||||||
int *p;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
|
status = XpmReadFileToXpmImage((char *)file, &xpm, (XpmInfo *) NULL);
|
||||||
if (i != XpmSuccess) {
|
if (is_xpm_error(status))
|
||||||
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;
|
return NULL;
|
||||||
}
|
|
||||||
if (xpm.height < 1 || xpm.width < 1) {
|
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xpm.colorTable == NULL) {
|
image = create_rimage_from_xpm(context, xpm);
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
image = RCreateImage(xpm.width, xpm.height, True);
|
|
||||||
if (!image) {
|
|
||||||
XpmFreeXpmImage(&xpm);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* make color table */
|
|
||||||
for (i = 0; i < 4; i++) {
|
|
||||||
color_table[i] = malloc(xpm.ncolors * sizeof(unsigned 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < xpm.ncolors; i++) {
|
|
||||||
XColor xcolor;
|
|
||||||
char *color = NULL;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (!color) {
|
|
||||||
color_table[0][i] = 0xbe;
|
|
||||||
color_table[1][i] = 0xbe;
|
|
||||||
color_table[2][i] = 0xbe;
|
|
||||||
color_table[3][i] = 0xff;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
XpmFreeXpmImage(&xpm);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user