Testing if a picture has no content part II

Thankyou Brad Hutchings for giving me method of seeing if a graphics object has no contents. This post is not a question but is just here for the benefit of others. For those who do not know, to see if a picture has no graphical contents. Brad Hutchings gave me code that loops through all of the pixels in a picture and it sees if that pixel has any contents but I have found a better way of doing this which is mostly likely much more efficient and faster on the CPU and RAM).

What I have found out is instead of using Brad Hutchings’ code, you can just write in a if statement:

dim p as new picture
if (p.graphics is nil) then
p = new picture(16,16,32)
end if

Thankyou for your help Brad and sorry to maybe sound rude but I am just trying to help. :wink:

Hope this helps

Just fyi, while that is what I recommended in your other post, it’s not what you originally asked.

I don’t understand the example. You can’t do dim p as new Picture as that Constructor is protected, and any existing picture will return a Graphics object even if it doesn’t have “content” (which, I take it, means that the picture is a single, uniform color). What am I missing?

Oops i meant to put Dim p as picture.

Thanks

That code would crash. If p is nil, you’ll get a NilObjectException if you try to test p.Graphics. Did you mean if p is nil then …?

Would have been nice if you had used a more descriptive title than ‘Okay’. ‘Testing if a picture is nil’ would really have benefited others, don’t you think ?

I think he changed it accidentally. The original subject was quite descriptive.

You’re welcome? As Tim pointed out, the solution ended up being something a little different than you asked for. More information for you… If you want to check pixel by pixel for some sense of “emptiness”, write a plugin. Some not-so-clever pointer caching in your loops will save gobs and gobs of pixel address computation, and that means time. And one place you might use something similar to the solution I offered is to strip empty borders off a picture.

If “empty” is defined as “nothing has been drawn into it yet”, I’d use GetData and StrComp to compare it to a new Picture of the same dimensions.

If your code has to test for whether anything has been drawn yet, wouldn’t a global flag (or leaving the picture object nil) be easier?

You’re right unless the picture is coming from an outside source, I suppose.