Reading the RGB values of a pixel

Started by
7 comments, last by benryves 17 years, 1 month ago
Can anyone help me with how i can read the RGB values of a pixel of a picture? What i want to do is... 1) Load a picture, the formats not specific as i don't know whats best. 2) Read a pixels RGB values, i've know idea how this would be read, line by line or co-ordinate specific.
Advertisement
This is in 32 bit...

r = pixel shr 16 g = pixel shr 8 and 255 b = pixel and 255 


or

r = pixel >> 16 & 255g = pixel >> 8 & 255 b = pixel & 255 
Hi.
You would need to tell us which API/language you were intending on using.

In C# it's all very easy (though this method is slow);

using (Bitmap B = new Bitmap("someimage.jpg")) {    Color C = B.GetPixel(10, 10); // Get the pixel colour at (10,10)    int Red = C.R;    int Green = C.G;    int Blue = C.B;    int Alpha = C.A;}


If you intend on reading lots of pixels you'll end up having to manually work with the raw data, which depends on the pixel format of the image. relsoft's method works for images using 32-bit RGB/ARGB values; if the bits were packed differently (eg 15/16-bit graphics, or worse palettised images) you'd need to work around that. Depending on which graphics API you decide to use, you might be able to just convert the image to 32-bit RGB/ARGB which is the "typical" (and easiest to manipulate) format.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

C++ / OpenGL
glReadPixel and use the code I posted above.
Hi.
I'm not quite sure what your code is relsoft. I've not had much experience with this before and so your code is too sparse that it makes no sense to me, can you put it in context like benryves did?

I see it as a mixture of the two, like follows...?????

using (Bitmap B = new Bitmap("someimage.jpg")) {

Color C = B.glReadPixel(10, 10); // Get the pixel colour at (10,10)

int C.r = pixel >> 16 & 255
int C.g = pixel >> 8 & 255
int C.b = pixel & 255
}

And that makes no logical sense in the history of programming or sanity!
Okay, if your bmp is already loaded on a GL context ( A texture) you could just read each pixel in the GL buffer via glreadpixels.

glEnable(GL_TEXTURE_2D)glBindtexture(GL_TEXTURE_2D, texture_id)glReadPixels(0, 0, wid, hei, GL_RGBA, GL_UNSIGNED_BYTE, memoryaddress)


Sometimes you can even do it directly, w/o GL at all.

Say you have a memory chunk that you loaded you image. For clarity I'l use your code:

using (Bitmap B = new Bitmap("someimage.jpg")) {    Color C = B.GetPixel(10, 10); // Get the pixel colour at (10,10)    char Red = C >> 16 & 255;    char Green = C >> 8 & 255;    char Blue = C & 255 ;    char Alpha = C >> 24 & 255;}


Hi.
Thanks relsoft, thats perfect. You're a gent and a genius!
For clarity, the code I posted was the .NET way of doing things which therefore wraps up a lot of the mucky details for you.

If you have an image loaded into memory (let's say you have a pointer to its data) you need to know several things; the pixel format, the width, the height, and the stride of the image.

The width and height should be fairly self-explanatory. The pixel format indicates how each pixel is defined; for a 32-bit image that's one byte alpha, one byte red, one byte green blue, one byte green (sometimes alpha is ignored, so you have a dud byte there).

The stride of the image is the width of the image in bytes (the size of a scanline). This is not always the same as the width multiplied by the nuber of bytes per pixel; it is often rounded up to the nearest four or eight bytes, for example (and the extra data is ignored when drawing the image).

Therefore, to get the location of a pixel within a buffer in memory you'd calculate it like this:

PixelAddress = (y * Stride) + (x * BytesPerPixel)


This assumes that the image is stored with the first byte of data corresponding to the top-left pixel. Some formats store the bottom-right first (so the image grows "up" rather than "down"), for which you'd need to adjust the y coordinate first.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement