Jump to content
  • Advertisement


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


Compare Pixels

This topic is 5577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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


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)
if (x1 < 0)

if (x2 >= screen_width)
if (x2 < 0)

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

if (y2 >= screen_height)
if (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 )
} // end for x

// move down a line

} // end for y

// return failure

} // 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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!