I want to convert a colour bitmap to a grayscale image. There's a Luminance formula for converting 24 bit to 8 bit (R * 0.3 + G * 0.5 + B * 0.2).
Easy enough to extract with POINT. Do the maths and I've then got a grid array of 8 bit entries. So, CreateImage an 8 bit depth image and plot the individual pixels onto it.
Works fine, except that the 'grayscale' isn't gray. The new image is a sort of magenta/purple colour. All the detail is there but I'd really like it to be proper white/gray/black.
Suggestions gratefully received.
Al
Grayscale conversion - what's wrong?
- Michael Vogel
- Addict
- Posts: 2806
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Would start the "easy" way, use an RGB picture and set red, green and blue to the calculated value - if this result is not presented in clear gray, there's maybe something wrong with your screen settings
Here's a snippet from the forum (maybe you should browse a little bit around) which may help...

Here's a snippet from the forum (maybe you should browse a little bit around) which may help...
Code: Select all
Procedure.i ImageToGrayscale(imageIn)
;The following utility function converts the given PB image to 'gray-scale' via a temporary 8-bit image.
;Returns #True if successful.
Protected result, imageOut, color, width, height, hdc
Protected Dim colors.l(255)
width = ImageWidth(imageIn) : height = ImageHeight(imageIn)
If width And height
;Set up a 'grey' colortable.
For color = 0 To 255
colors(color) = color + color<<8 + color<<16
Next
;Create an 8-bit image.
imageOut = CreateImage(#PB_Any, width, height, 8)
If imageOut
;Convert the original image to gray-scale by simply setting the new image's color table and then copying the first image.
;Windows will match the original colors to thos in the color table by making some 'shortest path' calculations.
hdc = StartDrawing(ImageOutput(imageOut))
If hdc
SetDIBColorTable_(hdc, 0, 256, @colors())
DrawImage(ImageID(imageIN),0,0)
StopDrawing()
;Now copy to the original image.
If StartDrawing(ImageOutput(imageIn))
DrawImage(ImageID(imageOut), 0, 0)
StopDrawing()
result = #True
EndIf
EndIf
FreeImage(imageOut)
EndIf
EndIf
ProcedureReturn result
EndProcedure
If your using PB for windows avoiding using API calls seriously restricts what you can do. Try downloading the Microsoft® Win32® Programmer's Reference (help file) and put it in your purebasic help directory. Then when you highlight an API command (in an example) and hit the F1 key the relevant portion of the Programmers Reference will open. I forget where to download it from, just doing a search these pages should locate it for you.That looks very helpful but I'm trying to avoid using API calls. I don't really understand them anyway.

"What you are is what you have been. What you’ll be is what you do now.” -Buddha