View Full Version : Check for a red pixel in a bitmaptexture
07-12-2011, 10:41 AM
I need to check some generated wirecolor-elements for red pixels. If there are any, they need to be processed some more. I have made something which checks for red pixels, but it uses try/catch. I'm wondering if there is a more elegant solution out there.
function fn_pasteCheckForRed colorRedWire p1 c2 p2 =
<DOC> Checks an image for any red pixels. Returns an undefined value if this happens. This will create an error which can be caught.
<color> colorRedWire: the red wirecolor channel
<color> the resulting color.
if (colorRedWire as point4).x > 0.0 do colorRedWire=undefined
--theBmp is the bitmaptexture I'm checking for red pixels
redPresent = try(pastebitmap theBmp theBmp[0,0] [0,0] type:#function function:fn_pasteCheckForRed)catch(true)
If there's any pixel with a hint of red, the pastebitmap function will create an error. This is caught by the try/catch.
What version of max, I think in 2010 pasteBitmap function is broken.
07-12-2011, 02:53 PM
i think that simple getpixels method will work faster:
fn doesColorExist bmp color:red =
found = off
for y=0 to bmp.height-1 while not found do
for c in (getpixels bmp [y,0] bmp.width) while not found do found = (c == color)
07-12-2011, 08:27 PM
@JHN: you might want to delete the comma after my function. I copied it out of a struct, sorry. I made that method in max 2010 and it works for me.
@DenisT: I've tested both. Your method checks for pure red color, but can be adjusted for any shade of red, which is what I actually needed.
I've tested with two photo's, both the same, but the second had it's red channel totally blacked out. For the image with red, both methods had the same speed. When checking the image without red, the getpixel-method was much faster, or more precisely the pastebitmap method took about 100 times as long.
So I'll be using getpixel for this in the future!
Thanks for the help,
07-14-2011, 08:41 AM
Actually, I've tested again and must revoke the initial results. The method from denisT contains a slight mistake. The line (getpixels bmp [y,0] bmp.width) should be (getpixels bmp [0,y] bmp.width)It was only checking the last column of pixels thus was a lot faster and produced the wrong results, since it was basically ignoring most of the image.
Turns out the pastebitmap function is faster, for images with and without red pixels. In both cases about a third faster.
07-14-2011, 08:41 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.