March 05, 2021, 07:02:51 AM

Author Topic: [bb] Read and Write pixel functions by Rob Farley [ 1+ years ago ]  (Read 411 times)

Offline BlitzBot

Title : Read and Write pixel functions
Author : Rob Farley
Posted : 1+ years ago

Description : OK, 2 functions here...

GetRGB and WriteRGB, these have to be used on locked image buffers.

Set up the globals so when you've read the RGB of an XY of an image you only need to do one readpixelfast.


This reads the RGB from X=50, Y=20 on image GFX and places the RGB vlaues into GotR, GotG and GotB globals.


This writes the RGB value of 100,160,200 to x=50, y=20 on image GFX.

I hope this helps!

Code :
Code: BlitzBasic
  1. ;ARGB Functions by Rob Farley 2003
  2. ;
  3. ;
  5. ; RGB Functions
  6. Global GotR=0
  7. Global GotG=0
  8. Global GotB=0
  11. Function GetRGB(image_name,x,y)
  12. ; Gets the RGB components from an image.
  13. ; The imagebuffer needs to be locked as it does a read pixel fast.
  14. ; The components are put into the global varibles gotr, gotg and gotb
  15.         argb=ReadPixelFast(x,y,ImageBuffer(image_name))
  16.         gotr=(ARGB Shr 16) And $ff
  17.         gotg=(ARGB Shr 8) And $ff
  18.         gotb=ARGB And $ff
  19. End Function
  21. Function WriteRGB(image_name,x,y,red,green,blue)
  22. ; Writes a pixel to an image.
  23. ; The imagebuffer needs to be locked as it does a write pixel fast.
  24. argb=(blue Or (green Shl 8) Or (red Shl 16) Or ($ff000000))
  25. WritePixelFast x,y,argb,ImageBuffer(image_name)
  26. End Function

Comments :

Rob Farley(Posted 1+ years ago)

 Updated due the the old ones being... um... old.

aab(Posted 1+ years ago)

 Uses slightly less code than my ol method: thanksThis is something that all Mid-Newbies should read.i spent hours figuring this out on my own when i was a Mid-Newbie, and i didnt even know i was using Hex at the time (!)

DheDarkhCustard(Posted 1+ years ago)

 that's cool ;)


SimplePortal 2.3.6 © 2008-2014, SimplePortal