extraction using glReadPixels

The user creates a drawing using my program. I extract that drawing and convert it to a PNG. My code works great. I want to enhance it, and I have a question.

To extract the drawing, I use

glReadPixels( x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, dummy );

Now, I want to find the smallest rectangle in the image that contains any part of the user’s drawing. for instance, suppose the user only painted on the leftmost half of the drawing. I want the smallest image that still captures all of the drawing.

I thought I could scan the pixels and look for non-transparent pixels and figure this out?

Any suggestions?

Does the canvas start out black? If so, read the pixels back into an array using glReadPixels. Then check all non-black pixels in the array to find the min and max rows and columns.

If the drawing is done on top of another image, subtract the original image from the finished image (on a pixel by pixel basis). All painted pixels will be non-black (r,g, or b value will not be zero). Keep track of the min and max rows and columns that these non-black pixels occur in.

You may try to not do it graphically. If the user is using a brush to paint the picture, knowing the brush coordinates you may determinate min and max coordinates values thus the drawing dimensions.