Blitz not reading in bitmaps properly? Colour prob

Blitz3D Forums/Blitz3D Programming/Blitz not reading in bitmaps properly? Colour prob

Fry Crayola(Posted 2004) [#1]
My system adds tiles to a texture by reading in a bitmap image, and then to ensure the proper alpha channels it goes through it pixel by pixel, checking the colours using ReadPixelFast.

If the pixel is all black (i.e. $000000) then the game will make it fully transparent ($00), otherwise it will make it fully opaque ($FF).

However, one of my graphics uses $010101 to represent black. Ideally my system will make this fully opaque, yet it comes through transparent.

The code that adds the image to the texture is as follows:

Function E2_editTexture(image, texture, x, y)
	LockBuffer TextureBuffer(texture)
	LockBuffer ImageBuffer(image)
	width = ImageWidth(image)
	height = ImageHeight(image)
	
	For a = 0 To height-1
		For b = 0 To width-1
			colour = ReadPixelFast (b, a, ImageBuffer(image))
			If (colour And $00FFFFFF) = $00000000 Then
				WritePixelFast x+b, y+a, $00000000, TextureBuffer(texture)
			Else
				colour = (colour Or $FF000000)
				WritePixelFast x+b, y+a, colour, TextureBuffer(texture)
			End If
		Next
	Next

	UnlockBuffer TextureBuffer(texture)
	UnlockBuffer ImageBuffer(image)
End Function


I've also noticed in the past that $FFFFFF, which is white, has been coming out something like $F8FAF8 or thereabouts. What's going on that I'm losing values? This means I cannot use black in any of my images, which isn't acceptable.

Any help appreciated.


Rob Farley(Posted 2004) [#2]
This sounds like a 16 bit graphics issue... whoop it to 32 bit and it'll sort the problem, this question needs to go in the faq as it keeps recurring over and over.

This will return 16bit values based on your graphics card, there's a lot of functions and globals in there as these functions are pretty handy to be seperate.

This can be optimised big time if all you wanted to do was return a true 16 bit mask colour. But these are a bunch of functions that I always use and I couldn't be bothered!

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



Fry Crayola(Posted 2004) [#3]
You're right, in 32-bit colour mode it works perfectly.


JazzieB(Posted 2004) [#4]
You could just use a value of $040404 to represent black instead. When read in 16 bit mode it would come back as $000400 on a card using a565 mode. Or, to be even safer use $080808 for those rare cards that use a555 (15 bit) format.