Readpixel problem with 16-32 bits display mode

BlitzPlus Forums/BlitzPlus Programming/Readpixel problem with 16-32 bits display mode

hub(Posted 2004) [#1]
Hi !
i've some problems with this code. It's work fine if the desktop display current depth setting is 32 bits. But not with 16 bits mode. (i've not tested with 24 bits). Could you help me ? Thanks !

Graphics 800,600,32,2
Mask =CreateImage(100,100)
SetBuffer ImageBuffer(Mask)
Color 255,0,255
Rect 0,0,100,100
SetBuffer BackBuffer()
DrawImage mask,0,0
col = ReadPixel (50 , 50, ImageBuffer(Mask)) And $FFFFFF
If col = $ff00ff Then Notify "YES ;-)" Else Notify "no ;-("



skn3(Posted 2004) [#2]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1082


Rob Farley(Posted 2004) [#3]
The problem you've got is 16 bit modes don't have 8 bits per colour channel (0-255 on RG and B). So the 16 bit might be made up of 5,6,5 bits on the R,G,B. The problem with this is that the value you want might not be the value you get...

This bit of code writes a pixel to an image of the mask colour your want (255,0,255 in this case) and reads it back into the mask_r,g,b varibles to give you the true mask colour.

This only needs to be done in 16 bit modes.
Global gotr=0
Global gotg=0
Global gotb=0

Global mask_r=0
Global mask_g=0
Global mask_b=0

Function get_mask()
	tmp=CreateImage(1,1)
	LockBuffer ImageBuffer(tmp)
	WriteRGB(tmp,1,1,255,0,255)
	GetRGB(tmp,1,1)
	mask_r=gotr
	mask_g=gotg
	mask_b=gotb
	UnlockBuffer ImageBuffer(tmp)
	FreeImage tmp
End Function

Function GetRGB(image_name,x,y)
	argb=ReadPixelFast(x,y,ImageBuffer(image_name))
	gotr=(ARGB Shr 16) And $ff 
	gotg=(ARGB Shr 8) And $ff 
	gotb=ARGB And $ff
End Function

Function WriteRGB(image_name,x,y,red,green,blue)
	argb=(blue Or (green Shl 8) Or (red Shl 16) Or ($ff000000))
	WritePixelFast x,y,argb,ImageBuffer(image_name)
End Function



hub(Posted 2004) [#4]
Thanks !
Nice idea Rob ! It works fine now !


Rob Farley(Posted 2004) [#5]
You could shortern that code loads but the the GetRGB and WriteRGB functions are handy to have seperate, I use in pretty much everything at some point.