Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!asuvax!enuxha!rao From: rao@enuxha.eas.asu.edu (Arun Rao) Newsgroups: comp.windows.x Subject: Converting color images to monochrome - can I do it any faster ? Message-ID: <324@enuxha.eas.asu.edu> Date: 20 Oct 89 23:17:51 GMT Organization: Arizona State Univ, Tempe Lines: 65 My image processing application requires the user to select an arbitrary rectangular section from the screen to be processed further. Since the processing depends only upon brightness levels, I would like to display the selected portion as a gray-scale image regardless of what it looks like on the screen. Here's the code fragment I use to convert color pixel values to equivalent gray-scale values: width = image->width; height = image->height; /* Get default Colormap */ cmap = DefaultColormap(dpy, screen); for (i = 0; i < height; i++) for (j = 0; j < width; j++) { /* This is the most portable way of doing it, but slow as hell */ /* Get pixel values from the image and fill XColor's */ xcolor.pixel = XGetPixel(image, j, i); /* Get RGB values corresponding to pixel value */ XQueryColor(dpy, cmap, &xcolor); /* RGB to gray-scale formula (from Adrian Nye's book)*/ intensity = (int)((0.30 * xcolor.red) +(0.59 * xcolor.green) +(0.11 * xcolor.blue)); /* Put (equal) gray level values in RGB */ xcolor.red = (unsigned short)intensity; xcolor.green = (unsigned short)intensity; xcolor.blue = (unsigned short)intensity; /* Get pixel values corresponding to gray-level */ XAllocColor(dpy, cmap, &xcolor); /* Put pixel back in image */ XPutPixel(image, j, i, xcolor.pixel); } The above is very portable, but is horrendously slow, even for small images. Is there a better way of doing this without resorting to machine dependent bit-fiddling ? The machine is an Ardent Titan running SysV and X11R3 and I've tested the program in addition on an HP 9000 server. Thanks in advance. -Arun -- Arun Rao ARPANET: rao@enuxha.eas.asu.edu BITNET: agaxr@asuacvax, agazr@asuacad 950 S. Terrace Road, #B324, Tempe, AZ 85281 Phone: (602) 968-1852 (Home) (602) 965-3452 (Office)