Title : write/read color, alpha, of each texel of a texture, with different commands
Author : RemiD
Posted : 8 months ago
Description : also an example to write/read color, maskcolor, of each pixel of an image, with different commands
Code :
;write/read color, alpha, of each texel of a texture, with different commands
;these are the functions that i use to write/read texels properties
;
;variables, lists, functions
Global PixAlpha% = 0
Global PixRed% = 0
Global PixGreen% = 0
Global PixBlue% = 0
Function SetPixColor(PX%,PY%,R%,G%,B%)
Color(R,G,B)
Plot(PX,PY)
End Function
Function GetPixColor(PX%,PY%)
GetColor(PX,PY)
PixRed = ColorRed()
PixGreen = ColorGreen()
PixBlue = ColorBlue()
End Function
Function WritePix(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixel(PX,PY,HexARGB)
End Function
Function ReadPix(PX%,PY%)
HexARGB% = ReadPixel(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function WritePixFast(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixelFast(PX,PY,HexARGB)
End Function
Function ReadPixFast(PX%,PY%)
HexARGB% = ReadPixelFast(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function RGBAToHexARGB%(R%,G%,B%,A%)
HexARGB% = A Shl(24) + R Shl(16) + G Shl(8) + B Shl(0)
Return HexARGB
End Function
Function HexARGBToRGBA(HexARGB%)
PixAlpha = HexARGB Shr(24) And 255
PixRed = HexARGB Shr(16) And 255
PixGreen = HexARGB Shr(8) And 255
PixBlue = HexARGB Shl(0) And 255
End Function
;demo
GPWidth% = 640
GPHeight% = 480
Graphics3D(GPWidth,GPHeight,32,2)
SeedRnd(MilliSecs())
Global Camera = CreateCamera()
CameraRange(Camera,0.001,100)
CameraClsColor(Camera,000,000,000)
WH% = GPWidth
;create a quad
Test_Mesh = CreateMesh()
Surface = CreateSurface(Test_Mesh)
AddVertex(Surface,-(8.0)/WH,(8.0)/WH,0.0,Float(0)/8,Float(0)/8)
AddVertex(Surface,(8.0)/WH,(8.0)/WH,0.0,Float(8)/8,Float(0)/8)
AddVertex(Surface,-(8.0)/WH,-(8.0)/WH,0.0,Float(0)/8,Float(8)/8)
AddVertex(Surface,(8.0)/WH,-(8.0)/WH,0.0,Float(8)/8,Float(8)/8)
AddTriangle(Surface,0,1,2)
AddTriangle(Surface,2,1,3)
UpdateNormals(Test_Mesh)
EntityColor(Test_Mesh,255,255,255)
EntityFX(Test_Mesh,1)
EntityBlend(Test_Mesh,1)
;MoveEntity(Test_Mesh,0,0,1.0)
MoveEntity(Test_Mesh,0,0,0.1)
;create a texture
Test_Texture = CreateTexture(8,8,1+2+256)
TextureBlend(Test_Texture,1)
SetBuffer(TextureBuffer(Test_Texture))
ClsColor(128,128,128)
Cls()
Color(255,000,000) : Plot(0,0)
Color(000,255,000) : Plot(8-1,0)
Color(000,000,255) : Plot(0,8-1)
Color(255,255,000) : Plot(8-1,8-1)
EntityTexture(Test_Mesh,Test_Texture)
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
;write/read the colors/alpha of the texels on the texture :
;using setpixcolor / getpixcolor ( color+plot / getcolor+colorred+colorgreen+colorblue )
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = Rand(025,255)
G% = 000
B% = 000
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
SetPixColor(PX,PY,R,G,B)
Next
Next
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
GetPixColor(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
Next
Next
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
;using writepix / readpix ( writepixel / readpixel )
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = 000
G% = Rand(025,255)
B% = 000
A% = Rand(000,255)
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
WritePix(PX,PY,R,G,B,A)
Next
Next
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPix(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
A% = PixAlpha
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
Next
Next
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
;using writepixfast / readpixfast ( writepixelfast / readpixelfast )
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
LockBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = 000
G% = 000
B% = Rand(025,255)
A% = Rand(000,255)
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
WritePix(PX,PY,R,G,B,A)
Next
Next
UnlockBuffer(TextureBuffer(Test_Texture))
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
LockBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPixFast(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
A% = PixAlpha
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
Next
Next
UnlockBuffer(TextureBuffer(Test_Texture))
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
End()
Comments :
RemiD(Posted 8 months ago)
another example to write/read the color, maskcolor (black 000,000,000), of each pixel of an image :;write/read color, maskcolor (black 000,000,000), of each pixel of an image, with different commands
;these are the functions that i use to write/read pixels properties
;
;variables, lists, functions
Global PixAlpha% = 0
Global PixRed% = 0
Global PixGreen% = 0
Global PixBlue% = 0
Function SetPixColor(PX%,PY%,R%,G%,B%)
Color(R,G,B)
Plot(PX,PY)
End Function
Function GetPixColor(PX%,PY%)
GetColor(PX,PY)
PixRed = ColorRed()
PixGreen = ColorGreen()
PixBlue = ColorBlue()
End Function
Function WritePix(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixel(PX,PY,HexARGB)
End Function
Function ReadPix(PX%,PY%)
HexARGB% = ReadPixel(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function WritePixFast(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixelFast(PX,PY,HexARGB)
End Function
Function ReadPixFast(PX%,PY%)
HexARGB% = ReadPixelFast(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function RGBAToHexARGB%(R%,G%,B%,A%)
HexARGB% = A Shl(24) + R Shl(16) + G Shl(8) + B Shl(0)
Return HexARGB
End Function
Function HexARGBToRGBA(HexARGB%)
PixAlpha = HexARGB Shr(24) And 255
PixRed = HexARGB Shr(16) And 255
PixGreen = HexARGB Shr(8) And 255
PixBlue = HexARGB Shl(0) And 255
End Function
;demo
Graphics3D(640,480,32,2)
SeedRnd(MilliSecs())
;create an image
Test_Image = CreateImage(8,8)
SetBuffer(ImageBuffer(Test_Image))
ClsColor(128,128,128)
Cls()
Color(255,000,000) : Plot(0,0)
Color(000,255,000) : Plot(8-1,0)
Color(000,000,255) : Plot(0,8-1)
Color(255,255,000) : Plot(8-1,8-1)
SetBuffer(BackBuffer())
ClsColor(255,000,255)
Cls()
;ScaleImage(Test_Image,10.0,10.0)
DrawImage(Test_Image,GraphicsWidth()/2-ImageWidth(Test_Image)/2,GraphicsHeight()/2-ImageHeight(Test_Image)/2)
Flip()
WaitKey()
;write/read the colors/maskcolor of the pixels on the image :
;using setpixcolor / getpixcolor ( color+plot / getcolor+colorred+colorgreen+colorblue )
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = Rand(025,255)
G% = 000
B% = 000
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
SetPixColor(PX,PY,R,G,B)
Next
Next
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
GetPixColor(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
Next
Next
SetBuffer(BackBuffer())
ClsColor(255,000,255)
Cls()
;ScaleImage(Test_Image,10.0,10.0)
DrawImage(Test_Image,GraphicsWidth()/2-ImageWidth(Test_Image)/2,GraphicsHeight()/2-ImageHeight(Test_Image)/2)
Flip()
WaitKey()
;using writepix / readpix ( writepixel / readpixel )
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
Choice% = Rand(1,2)
If( Choice = 1 )
;write a colored pixel
R% = 000
G% = Rand(025,255)
B% = 000
Else If( Choice = 2 )
;write a black pixel (so that it will not be drawn when using drawimage)
R% = 000
G% = 000
B% = 000
EndIf
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
WritePix(PX,PY,R,G,B)
Next
Next
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPix(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
Next
Next
SetBuffer(BackBuffer())
ClsColor(255,000,255)
Cls()
;ScaleImage(Test_Image,10.0,10.0)
DrawImage(Test_Image,GraphicsWidth()/2-ImageWidth(Test_Image)/2,GraphicsHeight()/2-ImageHeight(Test_Image)/2)
Flip()
WaitKey()
;using writepixfast / readpixfast ( writepixelfast / readpixelfast )
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
LockBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
Choice% = Rand(1,2)
If( Choice = 1 )
;write a colored pixel
R% = 000
G% = Rand(025,255)
B% = 000
Else If( Choice = 2 )
;write a black pixel (so that it will not be drawn when using drawimage)
R% = 000
G% = 000
B% = 000
EndIf
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
WritePixFast(PX,PY,R,G,B)
Next
Next
UnlockBuffer(ImageBuffer(Test_Image))
DebugLog("")
SetBuffer(ImageBuffer(Test_Image))
LockBuffer(ImageBuffer(Test_Image))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPixFast(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
;DebugLog("Pixel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
Next
Next
UnlockBuffer(ImageBuffer(Test_Image))
SetBuffer(BackBuffer())
ClsColor(255,000,255)
Cls()
;ScaleImage(Test_Image,10.0,10.0)
DrawImage(Test_Image,GraphicsWidth()/2-ImageWidth(Test_Image)/2,GraphicsHeight()/2-ImageHeight(Test_Image)/2)
Flip()
WaitKey()
End()
jfk EO-11110(Posted 7 months ago)
Thanks for sharing. [/i]
i have just noticed an error in the example (the functions seem correct), so new code to read write the color / alpha of each texel of a texture :
;write read the color alpha of each texel of a texture
;to read / write texels properties
Global PixAlpha% = 0
Global PixRed% = 0
Global PixGreen% = 0
Global PixBlue% = 0
;demo
GPWidth% = 640
GPHeight% = 480
Graphics3D(GPWidth,GPHeight,32,2)
SeedRnd(MilliSecs())
Global Camera = CreateCamera()
CameraRange(Camera,0.001,100)
CameraClsColor(Camera,000,000,000)
WH% = GPWidth
;create a quad
Test_Mesh = CreateMesh()
Surface = CreateSurface(Test_Mesh)
AddVertex(Surface,-(8.0)/WH,(8.0)/WH,0.0,Float(0)/8,Float(0)/8)
AddVertex(Surface,(8.0)/WH,(8.0)/WH,0.0,Float(8)/8,Float(0)/8)
AddVertex(Surface,-(8.0)/WH,-(8.0)/WH,0.0,Float(0)/8,Float(8)/8)
AddVertex(Surface,(8.0)/WH,-(8.0)/WH,0.0,Float(8)/8,Float(8)/8)
AddTriangle(Surface,0,1,2)
AddTriangle(Surface,2,1,3)
UpdateNormals(Test_Mesh)
EntityColor(Test_Mesh,255,255,255)
EntityFX(Test_Mesh,1)
EntityBlend(Test_Mesh,1)
;MoveEntity(Test_Mesh,0,0,1.0)
MoveEntity(Test_Mesh,0,0,0.1)
;create a texture
Test_Texture = CreateTexture(8,8,1+2+256)
TextureBlend(Test_Texture,1)
SetBuffer(TextureBuffer(Test_Texture))
ClsColor(128,128,128)
Cls()
Color(255,000,000) : Plot(0,0)
Color(000,255,000) : Plot(8-1,0)
Color(000,000,255) : Plot(0,8-1)
Color(255,255,000) : Plot(8-1,8-1)
EntityTexture(Test_Mesh,Test_Texture)
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
;write/read the colors/alpha of the texels on the texture :
;using writepix / readpix ( writepixel / readpixel )
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = 000
G% = Rand(025,255)
B% = 000
A% = Rand(000,255)
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
WritePix(PX,PY,R,G,B,A)
Next
Next
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPix(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
A% = PixAlpha
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
Next
Next
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
;using writepixfast / readpixfast ( writepixelfast / readpixelfast )
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
LockBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
R% = 000
G% = 000
B% = Rand(025,255)
A% = Rand(000,255)
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
WritePixFast(PX,PY,R,G,B,A)
Next
Next
UnlockBuffer(TextureBuffer(Test_Texture))
DebugLog("")
SetBuffer(TextureBuffer(Test_Texture))
LockBuffer(TextureBuffer(Test_Texture))
For PX% = 0 To 8-1 Step 1
For PY% = 0 To 8-1 Step 1
ReadPixFast(PX,PY)
R% = PixRed
G% = PixGreen
B% = PixBlue
A% = PixAlpha
;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
Next
Next
UnlockBuffer(TextureBuffer(Test_Texture))
SetBuffer(BackBuffer())
ClsColor(000,000,000)
Cls()
CameraClsColor(Camera,255,000,255)
RenderWorld()
Flip()
WaitKey()
End()
Function WritePix(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixel(PX,PY,HexARGB)
End Function
Function ReadPix(PX%,PY%)
HexARGB% = ReadPixel(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function WritePixFast(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixelFast(PX,PY,HexARGB)
End Function
Function ReadPixFast(PX%,PY%)
HexARGB% = ReadPixelFast(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function
Function RGBAToHexARGB%(R%,G%,B%,A%)
HexARGB% = A Shl(24) + R Shl(16) + G Shl(8) + B Shl(0)
Return HexARGB
End Function
Function HexARGBToRGBA(HexARGB%)
PixAlpha = HexARGB Shr(24) And 255
PixRed = HexARGB Shr(16) And 255
PixGreen = HexARGB Shr(8) And 255
PixBlue = HexARGB Shl(0) And 255
End Function