Ooops
October 16, 2021, 16:51:02

Author Topic: [bb] write/read color, alpha, of each texel of a texture, with different commands by RemiD [ 8 months ago ]  (Read 2031 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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 :
Code: BlitzBasic
  1. ;write/read color, alpha, of each texel of a texture, with different commands
  2. ;these are the functions that i use to write/read texels properties
  3. ;
  4. ;variables, lists, functions
  5.  
  6. Global PixAlpha% = 0
  7. Global PixRed% = 0
  8. Global PixGreen% = 0
  9. Global PixBlue% = 0
  10.  
  11. Function SetPixColor(PX%,PY%,R%,G%,B%)
  12.  Color(R,G,B)
  13.  Plot(PX,PY)
  14. End Function
  15.  
  16. Function GetPixColor(PX%,PY%)
  17.  GetColor(PX,PY)
  18.  PixRed = ColorRed()
  19.  PixGreen = ColorGreen()
  20.  PixBlue = ColorBlue()
  21. End Function
  22.  
  23. Function WritePix(PX%,PY%,R%,G%,B%,A%=255)
  24.  HexARGB = RGBAToHexARGB(R,G,B,A)
  25.  WritePixel(PX,PY,HexARGB)
  26. End Function
  27.  
  28. Function ReadPix(PX%,PY%)
  29.  HexARGB% = ReadPixel(PX,PY)
  30.  HexARGBToRGBA(HexARGB%)
  31. End Function
  32.  
  33. Function WritePixFast(PX%,PY%,R%,G%,B%,A%=255)
  34.  HexARGB = RGBAToHexARGB(R,G,B,A)
  35.  WritePixelFast(PX,PY,HexARGB)
  36. End Function
  37.  
  38. Function ReadPixFast(PX%,PY%)
  39.  HexARGB% = ReadPixelFast(PX,PY)
  40.  HexARGBToRGBA(HexARGB%)
  41. End Function
  42.  
  43. Function RGBAToHexARGB%(R%,G%,B%,A%)
  44.  HexARGB% = A Shl(24) + R Shl(16) + G Shl(8) + B Shl(0)
  45.  Return HexARGB
  46. End Function
  47.  
  48. Function HexARGBToRGBA(HexARGB%)
  49.  PixAlpha = HexARGB Shr(24) And 255
  50.  PixRed = HexARGB Shr(16) And 255
  51.  PixGreen = HexARGB Shr(8) And 255
  52.  PixBlue = HexARGB Shl(0) And 255
  53. End Function
  54.  
  55.  
  56.  
  57. ;demo
  58. GPWidth% = 640
  59. GPHeight% = 480
  60. Graphics3D(GPWidth,GPHeight,32,2)
  61.  
  62. SeedRnd(MilliSecs())
  63.  
  64. Global Camera = CreateCamera()
  65. CameraRange(Camera,0.001,100)
  66. CameraClsColor(Camera,000,000,000)
  67.  
  68. WH% = GPWidth
  69.  
  70. ;create a quad
  71. Test_Mesh = CreateMesh()
  72. Surface = CreateSurface(Test_Mesh)
  73. AddVertex(Surface,-(8.0)/WH,(8.0)/WH,0.0,Float(0)/8,Float(0)/8)
  74. AddVertex(Surface,(8.0)/WH,(8.0)/WH,0.0,Float(8)/8,Float(0)/8)
  75. AddVertex(Surface,-(8.0)/WH,-(8.0)/WH,0.0,Float(0)/8,Float(8)/8)
  76. AddVertex(Surface,(8.0)/WH,-(8.0)/WH,0.0,Float(8)/8,Float(8)/8)
  77. AddTriangle(Surface,0,1,2)
  78. AddTriangle(Surface,2,1,3)
  79. UpdateNormals(Test_Mesh)
  80. EntityColor(Test_Mesh,255,255,255)
  81. EntityFX(Test_Mesh,1)
  82. EntityBlend(Test_Mesh,1)
  83. ;MoveEntity(Test_Mesh,0,0,1.0)
  84. MoveEntity(Test_Mesh,0,0,0.1)
  85.  
  86. ;create a texture
  87. Test_Texture = CreateTexture(8,8,1+2+256)
  88. TextureBlend(Test_Texture,1)
  89. SetBuffer(TextureBuffer(Test_Texture))
  90. ClsColor(128,128,128)
  91. Cls()
  92. Color(255,000,000) : Plot(0,0)
  93. Color(000,255,000) : Plot(8-1,0)
  94. Color(000,000,255) : Plot(0,8-1)
  95. Color(255,255,000) : Plot(8-1,8-1)
  96.  
  97. EntityTexture(Test_Mesh,Test_Texture)
  98.  
  99. SetBuffer(BackBuffer())
  100. ClsColor(000,000,000)
  101. Cls()
  102. CameraClsColor(Camera,255,000,255)
  103. RenderWorld()
  104. Flip()
  105. WaitKey()
  106.  
  107.  
  108.  
  109. ;write/read the colors/alpha of the texels on the texture :
  110.  
  111.  
  112.  
  113. ;using setpixcolor / getpixcolor ( color+plot / getcolor+colorred+colorgreen+colorblue )
  114. DebugLog("")
  115. SetBuffer(TextureBuffer(Test_Texture))
  116. For PX% = 0 To 8-1 Step 1
  117.  For PY% = 0 To 8-1 Step 1
  118.   R% = Rand(025,255)
  119.   G% = 000
  120.   B% = 000
  121.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
  122.   SetPixColor(PX,PY,R,G,B)
  123.  Next
  124. Next
  125.  
  126. DebugLog("")
  127. SetBuffer(TextureBuffer(Test_Texture))
  128. For PX% = 0 To 8-1 Step 1
  129.  For PY% = 0 To 8-1 Step 1
  130.   GetPixColor(PX,PY)
  131.   R% = PixRed
  132.   G% = PixGreen
  133.   B% = PixBlue
  134.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B)
  135.  Next
  136. Next
  137.  
  138. SetBuffer(BackBuffer())
  139. ClsColor(000,000,000)
  140. Cls()
  141. CameraClsColor(Camera,255,000,255)
  142. RenderWorld()
  143. Flip()
  144. WaitKey()
  145.  
  146.  
  147.  
  148. ;using writepix / readpix ( writepixel / readpixel )
  149. DebugLog("")
  150. SetBuffer(TextureBuffer(Test_Texture))
  151. For PX% = 0 To 8-1 Step 1
  152.  For PY% = 0 To 8-1 Step 1
  153.   R% = 000
  154.   G% = Rand(025,255)
  155.   B% = 000
  156.   A% = Rand(000,255)
  157.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
  158.   WritePix(PX,PY,R,G,B,A)
  159.  Next
  160. Next
  161.  
  162. DebugLog("")
  163. SetBuffer(TextureBuffer(Test_Texture))
  164. For PX% = 0 To 8-1 Step 1
  165.  For PY% = 0 To 8-1 Step 1
  166.   ReadPix(PX,PY)
  167.   R% = PixRed
  168.   G% = PixGreen
  169.   B% = PixBlue
  170.   A% = PixAlpha
  171.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
  172.  Next
  173. Next
  174.  
  175. SetBuffer(BackBuffer())
  176. ClsColor(000,000,000)
  177. Cls()
  178. CameraClsColor(Camera,255,000,255)
  179. RenderWorld()
  180. Flip()
  181. WaitKey()
  182.  
  183.  
  184.  
  185. ;using writepixfast / readpixfast ( writepixelfast / readpixelfast )
  186. DebugLog("")
  187. SetBuffer(TextureBuffer(Test_Texture))
  188. LockBuffer(TextureBuffer(Test_Texture))
  189. For PX% = 0 To 8-1 Step 1
  190.  For PY% = 0 To 8-1 Step 1
  191.   R% = 000
  192.   G% = 000
  193.   B% = Rand(025,255)
  194.   A% = Rand(000,255)
  195.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
  196.   WritePix(PX,PY,R,G,B,A)
  197.  Next
  198. Next
  199. UnlockBuffer(TextureBuffer(Test_Texture))
  200.  
  201. DebugLog("")
  202. SetBuffer(TextureBuffer(Test_Texture))
  203. LockBuffer(TextureBuffer(Test_Texture))
  204. For PX% = 0 To 8-1 Step 1
  205.  For PY% = 0 To 8-1 Step 1
  206.   ReadPixFast(PX,PY)
  207.   R% = PixRed
  208.   G% = PixGreen
  209.   B% = PixBlue
  210.   A% = PixAlpha
  211.   ;DebugLog("Texel at "+PX+","+PY+" is colored in "+R+","+G+","+B+" and has an alpha of "+A)
  212.  Next
  213. Next
  214. UnlockBuffer(TextureBuffer(Test_Texture))
  215.  
  216. SetBuffer(BackBuffer())
  217. ClsColor(000,000,000)
  218. Cls()
  219. CameraClsColor(Camera,255,000,255)
  220. RenderWorld()
  221. Flip()
  222. WaitKey()
  223.  
  224.  
  225.  
  226. 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 :
Code: [Select]
;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]

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1290
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 :
Code: [Select]
;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
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal