Skip to main content
gamedev.net
🔒 Locked

Compare Pixels

Started by Zool 1 reply 250+ views
Original Post
Zool
Zool
Lets say I have a Rectangle (lets call it rect1). I need to find out if there is a black pixel inside rect1. I used a nested for loop and GetPixel but for some reason it didnt work. So does anyone know how to compare pixels from the HDC or HBITMAP and a COLORREF?
knealeaj
knealeaj
Here is a function that will do the trick if your using directX

x1 y1 is top left of rect ,x2 y2 right bottom,scan start is the
start colour for the scan and scan_end is the end colour,scan buffer is the memory to scan i.e your primary or secondry surface buffer and scan_lpitch is the linear memory pitch

e.g To search for a colour in the range 124 - 126 that is 40 * 40
on the back surface

Color_Scan(10,10,50,50,124,126,back_buffer,back_lpitch);

int Color_Scan(int x1, int y1, int x2, int y2,unsigned char scan_start, unsigned char scan_end,unsigned char *scan_buffer, int scan_lpitch)
{


// x coords first
if (x1 >= screen_width)
x1=screen_width-1;
else
if (x1 < 0)
x1=0;

if (x2 >= screen_width)
x2=screen_width-1;
else
if (x2 < 0)
x2=0;

// now y-coords
if (y1 >= screen_height)
y1=screen_height-1;
else
if (y1 < 0)
y1=0;

if (y2 >= screen_height)
y2=screen_height-1;
else
if (y2 < 0)
y2=0;

// scan the region
scan_buffer +=y1*scan_lpitch;

for (int scan_y=y1; scan_y<=y2; scan_y++)
{
for (int scan_x=x1; scan_x<=x2; scan_x++)
{
if (scan_buffer[scan_x] >= scan_start && scan_buffer[scan_x] <= scan_end )
return(1);
} // end for x

// move down a line
scan_buffer+=scan_lpitch;

} // end for y

// return failure
return(0);

} // end Color_Scan



Can I ask why you are doing this are you using it for collision detection of the two rects??Hope the function helped.Let me Know how you get on.
Zool
Zool
i have seen that code in one of my books, i figured it out right after I posted it. I''m trying to do a Col dectect of a rectangle with any sort of object. If the rectangle overlaps any other object no matter what shape it will detect collision. primitive but works for the project.

Topic Locked

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

Sign in to reply to this topic.