- int x, y;
- unsigned char *result, *o;
- unsigned char *grays;
- XWindowAttributes xgwa;
-
- *w = xpm_image.width;
- *h = xpm_image.height;
- result = (unsigned char *) malloc ((*w) * (*h));
- if (!result)
- {
- fprintf(stderr, "%s: out of memory loading %s\n",
- progname, bitmap_name);
- exit (1);
- }
-
- XGetWindowAttributes (display, window, &xgwa);
-
- grays = (unsigned char *) calloc (xpm_image.ncolors+1, 1);
- for (x = 0; x < xpm_image.ncolors; x++)
- {
- XColor xc;
- XpmColor *xpmc = &xpm_image.colorTable[x];
- char *cstring = 0;
- if (xpmc->g_color && *xpmc->g_color)
- cstring = xpmc->g_color;
- else if (xpmc->g4_color && *xpmc->g4_color)
- cstring = xpmc->g4_color;
- else if (xpmc->c_color && *xpmc->c_color)
- cstring = xpmc->c_color;
- else
- cstring = xpmc->m_color;
-
- memset (&xc, 0, sizeof(xc));
- if (!cstring ||
- !*cstring ||
- !XParseColor (display, xgwa.colormap, cstring, &xc))
- grays[x] = 0;
- else
- grays[x] = (int) (((xc.red * 0.299) +
- (xc.green * 0.587) +
- (xc.blue * 0.114))
- / 255);
- }
-
- o = result;
- for (y = 0; y < *h; y++)
- for (x = 0; x < *w; x++)
- {
- int color = xpm_image.data[(y * (*w)) + x];
- if (color < 0 || color > xpm_image.ncolors) abort();
- *o++ = grays[color];
- }
- return result;