Skip to main content
gamedev.net
🔒 Locked

Comparing pixels

Started by Zeke Dec 3, 2001 at 5:46 AM 1 replies 300+ views
Original Post
Zeke
Zeke
I need to read in an image from file and compare the first pixel to another specified pixel (RGB(255,0,255)) to see if they are the same color. Can anyone help me to do this. I can load in the images fine (i store them in char*) but i dont know how to get a particular pixel colour unless i display them somewhere and then use GetPixel(), I dont want to have to display them though. Can anyone give me any help with this? Thanks very much
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Abstraction
Abstraction
I''ll assume you throw the whole image uncompressed into a array of 8 bit words. When I load images from files (bmps, tgas whatever) I throw them in something like this:

  
struct IMAGE
{
UINT width; // Width of image

UINT height; // Height of image

UINT bytespp; // Bytes per pixel (not bits)

UCHAR *data; // Image data (all the pixels)

};


Now to get the value of a pixel at (x,y) do this:

  
IMAGE Image; // Loaded image from file

DWORD Pixel; // Value of pixel at (x,y)


Pixel = Image.data + (x * Image.bytespp)
+ (y * Image.bytespp * Image.width);


If you have two values you want to compare then just do this (Pixel1 and Pixel2):

  
if(Pixel1 == Pixel2)
{ // Do whatever }



That''s it. A few things to remember:

Bmp files are padded on 4byte boundaries and I am sure other formats do similiar things as well. So pitch becomes a factor. A better image struct would be this:
  
struct IMAGE
{
UINT pitch; // Pitch or stride of image

UINT height; // Height of image

UINT bytespp; // Bytes per pixel

UCHAR *data; // Image data (raw pixels)

};

Just use pitch instead of width.

This will work for 8 - 32 bit images.


Abstraction
Abstraction
Whoa! wait just a minute. The code I gave you will only work for 8 bit images. The second chunck of code should look like this:
  
IMAGE Image; // Image loaded from file

DWORD Pixel; // Value of pixel

UCHAR *source; // 8 bit pointer for memcpy


source = Image.data + (x * Image.bytespp)
+ (y * Image.bytespp * Image.Width);

memcpy(source, &Pixel, Image.bytespp);


Okay, that will work for 8-32 bit images. Sorry about that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.