Set an image to GreyScale

The example invert the images colors changing what I feel is a CLUT. But I was not able to set the CLUT to Grey.

In fact, Transform allows to write, not read :wink:

I converted it from Color to Greyscale using GIMP and tried to extract a map from it, but read is not possible.

Must I use Pixel in an If …/… End IF block to change the colors ?
Small images only (the test image is 374?×?512).

grey is something like
grey = (r+g+b) / 3
=Color.RGB(grey ,grey ,grey )

For a small image, I’d use the RGBSurface.Pixel method and loop through all pixels in the RGB image.

A quick, approximate conversion from RGB to gray can be done with a weighted sum (rather than Markus’s average): grey = 0.299 * R + 0.587 * G + 0.114 * B This better approximates the response of the human visual system. For example, human vision is more sensitive to green, so the weighting factor for green is greater.

Check out:

A more rigorous approach would involve using the color management system of your display if you have a calibrated display. If you cannot do this, you might be safe assuming your user has a “standard” display that is calibrated to “sRGB”. Be aware that XOJO uses the “generic RBG” model, so you may have to invoke transforms between “generic RGB” and “sRGB” to get it right.

It would be nice if XOJO could build in easy access to the system’s color management system and be more clear about these issues in the ‘documentation’.

RGBSurface.Transform indeed applies a LUT, but it’s a 1D-LUT (independently mapping red to red, green to green, and blue) while turning a color image to grayscale requires a more complex 3D-LUT (mapping RGB to RGB). Looping through the pixels in the RGBSurface, calculating a weighted sum of the RGB values, and creating a new picture from the resulting grayscale pixels would be your best bet.

IF you need speed, maybe try MBS Xojo Picture Plugin with GrayscaleMBS method.

It is for small, very small images, speed is not an issue.

@Craig: works fine, I forgot I’ve already followed that road longtime ago, so I had to search a bit.

Now, I have to implement the solution from the test project to the real project.