Page 1 of 1
Grayscale conversion - what's wrong?
Posted: Sat Sep 05, 2009 10:57 pm
by abarkley
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
Posted: Sat Sep 05, 2009 11:08 pm
by Michael Vogel
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...
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
Posted: Sat Sep 05, 2009 11:16 pm
by abarkley
Yes, Rule 1 of forums..search around first. Apologies.
That looks very helpful but I'm trying to avoid using API calls. I don't really understand them anyway.
Posted: Sat Sep 05, 2009 11:41 pm
by rrpl
That looks very helpful but I'm trying to avoid using API calls. I don't really understand them anyway.
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.

Posted: Sun Sep 06, 2009 9:08 am
by Trond
Try to create a 24-bit image and plot the colour RGB(c, c, c) where c is the 8-bit result of your formula.
Posted: Sun Sep 06, 2009 9:53 am
by srod
JFI: the code posted above will not work with PB 4.4. I do have a version which does run under PB 4.4 but have not posted it. Netmaestro has posted a version of his code which will run under PB 4.4.