Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!sharkey!umich!zip!martin From: martin@dip.eecs.umich.edu (Martin R. Friedmann) Newsgroups: comp.windows.x Subject: Re: Converting color images to monochrome - can I do it any faster ? Message-ID: Date: 21 Oct 89 21:56:03 GMT References: <324@enuxha.eas.asu.edu> Sender: news@zippy.eecs.umich.edu Organization: /n/dip/s/martin/.organization Lines: 58 In-reply-to: rao@enuxha.eas.asu.edu's message of 20 Oct 89 23:17:51 GMT In article <324@enuxha.eas.asu.edu> rao@enuxha.eas.asu.edu (Arun Rao) writes: Here's the code fragment I use to convert color pixel values to equivalent gray-scale values: 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 */ ->> Not the most portable way, just the most brain dead. I guess that ->> makes it portable huh? Braindead == portable? Nah... ->> ah!! Braindead == (portable & short & understandable) /* Get pixel values from the image and fill XColor's */ /* Get RGB values corresponding to pixel value */ /* RGB to gray-scale formula (from Adrian Nye's book)*/ /* Put (equal) gray level values in RGB */ /* Get pixel values corresponding to gray-level */ /* Put pixel back in image */ } 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. I know you run on a titan, that helps, but this code sucks! You want this code to run faster??? Then take as much out of the nested for loop as possible! Converting from colors to grey scale (not monochrome) will leave lots of duplicate pixels on the screen. Look at your code! for each pixel you allocate a color??? Wrong. First figure out which colors you need. Then allocate them... And By all means use XGetImage! You make soo many less calls to Xlib. Malloc an array width * height * sizeof(Pixel); then XGetImage into that array (set the image.data = malloc....) use the Nye's formula on each pixel in the array (use one for loop, not two) (remember which intensities you need to allocate for later) Figure out which values you need from 0-255, pack those in the colormap. Make a transfer function (This pixel(now intensity) value gets this colormap cell) allocate the colormap cells all at once (XAllocColorCells) use Fill the colorcells with the packed intensity values from your image. XStoreColors to store them all at once. Then use XPutImage to put the Image back. I'm afraid that all of this won't be of much help to you. Marty. -- Marty.