Archived

This topic is now archived and is closed to further replies.

Zool

Compare Pixels

Recommended Posts

Zool    122
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?

Share this post


Link to post
Share on other sites
knealeaj    122
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.

Share this post


Link to post
Share on other sites
Zool    122
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.

Share this post


Link to post
Share on other sites