Grayscale conversion - what's wrong?

Just starting out? Need help? Post your questions and find answers here.
abarkley
User
User
Posts: 10
Joined: Sun Jun 07, 2009 7:44 pm
Location: London, UK

Grayscale conversion - what's wrong?

Post 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
User avatar
Michael Vogel
Addict
Addict
Posts: 2806
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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 :lol:

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
abarkley
User
User
Posts: 10
Joined: Sun Jun 07, 2009 7:44 pm
Location: London, UK

Post 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.
rrpl
Enthusiast
Enthusiast
Posts: 121
Joined: Fri Apr 18, 2008 7:22 am
Location: Australia

Post 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. :)
"What you are is what you have been. What you’ll be is what you do now.” -Buddha
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Post Reply