WritePixel argb format

Blitz3D Forums/Blitz3D Programming/WritePixel argb format

JoeGr(Posted 2004) [#1]
I wonder if someone could just take a moment to explain the argb format used by the WritePixel command to me. I've looked at other people's code and it appears that (for example) the value -16777216 corresponds to black and the value -65536 is red, but I'm a little baffled about how one arrives at these values from the more familiar r(0-255),g(0-255),b(0-255).


Yan(Posted 2004) [#2]
The 32 bit integer used by write/read pixel uses:
The first 8bits to represent the RED component of a colour, the next 8bits to represent the GREEN component of a colour, the next 8 bits to represent the Blue componet and the top 8 bits to represent the alpha component (this is ignored in normal 2D operations and is always set to 255)

To make it clearer:
 [   A  ] [  R   ] [  G   ] [  B   ]
%00000000 00000000 00000000 00000000 - Binary

 A  R  G  B
$00 00 00 00 - Hex
Therefore, if I had a pixel who's RED, BLUE and GREEN components were each set to 128 (mid grey) the integer would look like this:

%11111111100000001000000010000000 - Binary
$FF808080 - Hex
4286611584 - Decimal (-8355712 in Blitz cos it uses signed integers)

In your example above, you can now see that -65536 is $FFFF000 (use Print Hex$(-65536)).
This is indeed pure red with full alpha (Blitz will return always full alpha from a readpixel command [edit]for 2D stuff[/edit]).


Probably as clear as mud...But I tried ;o)

[edit]
The above assumes a 32bit screen mode. Things get really 'interesting' for 16bit modes...
http://www.blitzbasic.com/Community/posts.php?topic=31316
[/edit]


YAN


electronin(Posted 2004) [#3]
I'm pretty sure it has to do with the "Shr" command. I'll go look that up...


AntonyWells(Posted 2004) [#4]
IT's basically,

rgb = blue or (green shl 8) or (red shl 16)

and to reverse the process(I.e decode the rgb)

red = (rgb shr 16) and 255
green = (rgb shr 8) and 255
blue = (rgb and 255)
-


Yan(Posted 2004) [#5]
LOL...I got so carried away with explaining the format, I forgot about showing you how to extract the RGB components. :o)

Just for the sake of completeness.

You can get the RGB value from the ReadPixel command with...
RGB = ReadPixel(x, y, buffer) And $FFFFFF
(then red would just be red = RGB Shr 16)


YAN


JoeGr(Posted 2004) [#6]
Thankyou all. I understand *most* of that. Just a couple of questions:

-Forgive my ignorance Yan, but what are signed integers? I was with you right up until that point :)

-I can work with the hexadecimal colour format, no problem. But how do I convert from Hex to 32-bit integer using Blitz? In other words I kind of need the opposite of 'Hex$(integer)'.

Thanks for your patience. I struggle a bit with this low-level stuff.

EDIT: Okay, got it - thankyou again. :)


GW(Posted 2004) [#7]
So If you read a pixel from the buffer of a texture that has alpha in it (.TGA), Blitz will not return the alpha portion?


N(Posted 2004) [#8]
GW: As far as the tests I've done go, that's incorrect. It will return the alpha of the texture. Try this, it modifies the alpha of a texture.

graphics3d 800,600,32,2

;include "h_vein.bb"


C = CreateCamera()
T = CreateTexture(64,64,1+2+16+32+256)
T2 = CreateTexture(128,128,1+2+16+32+256)
T3 = CreateTexture(256,256,1+2+16+32+256)

For X = 0 To 63
For Y = 0 To 63
WritePixel x,y,intcolor(rand(255),rand(255),rand(255),255),TextureBuffer(T)
next
next

CameraClsColor C,64,64,64


PositionEntity C,0,0,-8

S = CreateSprite()
s1 = createsprite()
moveentity s1,-2,0,0
s2=createsprite()
moveentity s2,2,0,0
EntityTexture S,T,0,0
EntityTexture S1,T2,0,0
EntityTexture S2,T3,0,0

global fps,timenext,frames
time = millisecs()
entityblend s,1
Repeat

a# = a# + .25
if a > 360 then a = a - 360
     if frames mod 2 = 0 then
     lockbuffer texturebuffer(t)
     For Y = 0 To 63
          colorb = readpixelfast(X,Y,TextureBuffer(T))
          Colora = IntColor(Rand(255),Rand(255),Rand(255),Min(Max(Abs Cos(A)*255,Max(Y*4,255)),0))
          For X = 0 To 63
               WritePixelFast X,Y,Colora,TextureBuffer(T)
          Next
     Next
     unlockbuffer texturebuffer(t)

    lockbuffer texturebuffer(t2)
     For Y = 0 To 127
          Colora = IntColor(Rand(255),Rand(255),Rand(255),Min(Max(Abs Cos(A)*255,Max(Y*4,255)),0))
          For X = 0 To 127
               WritePixelfast X,Y,Colora,TextureBuffer(T2)
          Next
     Next
     unlockbuffer texturebuffer(t2)
     endif
     ;positiontexture t,-.5,.5
     ;rotatetexture t,a


     UpdateWorld
     RenderWorld
     Text 2,2,GetFPS(False)
     Flip False
Until Keyhit(1)

clearworld

Function GetFPS(JustChecking = False)
	If Not JustChecking Then frames = frames+1

	If MilliSecs() > timenext Then
		timenext = MilliSecs()+1000
		fps = frames
		frames = 0
	EndIf

	Return fps
End Function

; IntColor returns an integer color useable by WritePixel() and WritePixelFast()
; R = Red
; G = Green
; B = Blue
; A = Alpha.
; return = An integer color useable by functions such as WritePixel() and WritePixelFast()
Function IntColor(R,G,B,A=255)
	Return A Shl 24 Or R Shl 16 Or G Shl 8 Or B Shl 0
End Function

; RColor returns the value of Red, Green, Blue, or Alpha in C.
; c = An integer color returned by, for example, ReadPixelFast()
; c = The amount of bits C should be shifted right to.  Valid constants are: RRED, RGREEN, RBLUE, RALPHA
; return = An integer ranging from 0 to 255.
Function RColor%(c%,d%)
	Return c Shr d And 255 Shl 0
End Function

Const RALPHA = 24	;Return Alpha when using RColor
Const RRED = 16	;Return Red when using RColor
Const RGREEN = 8	;Return Green when using RColor
Const RBLUE = 0	;Return Blue when using RColor

; Calculates the minimum of two variables.
; V = The variable that you are modifying.
; M = The lowest the variable you are modifying can go.
; return = V if it is larger than M, otherwise M.
Function Min#(V#,M#)
	If V# < M# Then
		Return M#
	Else
		Return V#
	EndIf
End Function

; Calculates the maximum of two variables.
; V = The variable that you are modifying.
; M = The greatest the variable you are modifying can go.
; return = V if it is smaller than M, otherwise M.
Function Max#(V#,M#)
	If V# > M# Then
		Return M#
	Else
		Return V#
	EndIf
End Function



Yan(Posted 2004) [#9]
I did say...
this is ignored in normal 2D operations and is always set to 255
I meant things like this...
Graphics3D 800, 600, 0, 2

Text 0, 0, Hex$(ReadPixel(400,300))

WritePixel(400, 300, $00FFFFFF)

Text 0, 12, Hex$(ReadPixel(400, 300))

Flip

WaitKey()

End
Accessing the texture buffer is obviously 3D and will return a valid alpha value.


Admittedly the last bit was misleading and I've now edited to make it read as intended.


[Feeble Excuse No 25678]
I came in via Active Topics and didn't realise this was the 3D forum. As the write/readpixel commands are generally 2D questions, it didn't occur to me to make the 2D / 3D distinction clear.
[/Feeble Excuse No 25678]

YAN


GW(Posted 2004) [#10]
thanks Noel. ;-)


Yan(Posted 2004) [#11]
Hmm...I knew there was a reason I don't post much!


YAN


N(Posted 2004) [#12]
[Feeble Excuse No 25678]


You're off by about 40,000 ;)


Yan(Posted 2004) [#13]
-14322 ??

;o)


YAN


N(Posted 2004) [#14]
Yep, you're overdue ^_^


Prym(Posted 2011) [#15]
How to detect the max available argb on the used system ?


Adam Novagen(Posted 2011) [#16]
This thread is - err, WAS - over SEVEN YEARS OLD. Please make a new topic >.>


Warner(Posted 2011) [#17]
^ that, and GraphicsDepth()