Reading and Writing Pixmap Pixel Values

BlitzMax Forums/BlitzMax Programming/Reading and Writing Pixmap Pixel Values

Twinprogrammer(Posted 2015) [#1]
Hello,

I've been trying to learn how to read and write pixels in an image. My problem, however, is that I don't understand the format of the pixels. I don't understand what the docs mean when they say this:

The returned 32 bit value contains the following components:
bits 24-31 pixel alpha
bits 16-23 pixel red
bits 8-15 pixel green
bits 0-7 pixel blue

I know this is probably a stupid question, but what does it mean? Also, how do I read and write these values? Thanks in advance!


Bobysait(Posted 2015) [#2]
It means that the color is an integer which contains the 4 components
the 4 first bits contains the alpha (the higher bits)
the 4 following are the red channel
the 4 next are the green channel
and finally the 4 last are the blue.

When you retreive an integer color, you can extract each channel using bitwise operators

Local alpha_channel:int = ARGB shr(24) & $FF ; (move 24 bits right and keep 4 bites -> mask with $FF)
Local red_channel:int = ARGB shr(16) & $FF
Local green_channel:int = ARGB shr(8) & $FF
Local blue_channel:int = ARGB & $FF



Endive(Posted 2015) [#3]
Code from the forum:
Function DoColorPicker()
  Local temp:TPixmap, rgba:Int
	
	temp = GrabPixmap(MouseX(), MouseY(), 1, 1)
	rgba = temp.ReadPixel(0, 0)
	temp = Null
	
	SetColor(255, 255, 255)
	DrawText("Color: " + RGBA_GetRed(rgba) + ", " + RGBA_GetGreen(rgba) + ", " + RGBA_GetBlue(rgba), 0, 0)
	
End Function

Function RGBA_GetRed:Int(rgba:Int)
	
	Return((rgba Shr 16) & $FF)
	
End Function
              
Function RGBA_GetGreen:Int(rgba:Int)
	
	Return((rgba Shr 8) & $FF)
	
End Function
	
Function RGBA_GetBlue:Int(rgba:Int)
	
	Return(rgba & $FF)
	
End Function
	
Function RGBA_GetAlpha:Int(rgba:Int)
	
	Return((rgba:Int Shr 24) & $FF)
	
End Function



I'm interested in how to writepixel to an image as well.


Twinprogrammer(Posted 2015) [#4]
Thanks for all your help everyone!


Mr. Goober(Posted 2015) [#5]
For images, you can edit the pixels directly by locking it using LockImage. This returns a Pixmap object which can be manipulated the same way Pixmaps usually do. An image MUST have the DYNAMICIMAGE flag on when it is created to be able to grab its pixels at runtime.

A pixmap can have different formats, but typically it is in ARGB format (8 bits for each color and alpha) this is an Int (32 bits). When reading and writing pixels to pixmaps, ReadPixel returns an Int, and WritePixel takes an Int as an argument.

In the code above, you can use Shl and Shr to shift the bits of a variable by a certain amount. 10 (binary 1010) shl 1 is actually 20 (10100). Shifting left is like multiplying it by 2 for the number of times you want to shift. Shifting right is like dividing by 2.

Also, Pixmaps are edited on the CPU's end. It is not recommended to use a Pixmap as a graphics context (treating it like a canvas) because writing and reading pixels is very slow. This can also be known as software rendering. You will only really get a decent frsmerate with lower resolution pixmaps if used this way.


Twinprogrammer(Posted 2015) [#6]
So how do you use the writepixel, though? I only understand how to read the binary, not write them.

EDIT: I figured it out, guys. Thanks!


EdzUp MkII(Posted 2015) [#7]
It's best to use this sparingly as using such techniques can turn your amazing game into a slideshow.