Detecting invalid image in display.newImageRect

Hi all-

When my code attempts to open an invalid image with newImageRect, an error like this is displayed on the console:

WARNING: /(path)/main-selectui.lua:394: file ‘x2017-08-04-23-07-57-000-00300-00225.jpg’ does not contain a valid image

I need to detect this in my code, but the table returned by display.newImageRect looks the same as a valid image file.  

If I wrap the display.newImageRect in pcall() it also returns success (true).

Any ideas?  

Are you downloading images and displaying them?  If not, then you should detect this at dev time.
 
 
Are you SURE the returned object looks like a normal display object?  Does it have a removeSelf function?

 

local badBob = display.newImageRect( 'x2017-08-04-23-07-57-000-00300-00225.jpg', 100, 100 ) if( badBob.removeSelf == nil ) then print( "babBob is invalid" ) end

Thanks for the reply.

No, the images aren’t downloaded.   Short answer - The reason they are invalid is out of the scope of the issue I’m trying to solve.

Yes, the invalid image does have a removeSelf method.   newImageRect actually seems to return a black rectangle of the specified size that is a valid display object.   I can position it, and it displays on screen.

Sorry I don’t have any insights then, but it puzzles me why you don’t just solve this by correcting the images.  

I can see this being important if you’re downloading images, but you have them already so you should be able to check and fix as needed.

I’m giving this ‘push back’ because I know that putting a check in the code to see if the image didn’t load will cost you a little slice of your performance and can’t help but complicate the code.  

If you are using Photoshop then make sure you are using “save for web” to ensure using only standard colour profiles and standard encoding.

Thanks for the suggestions all, but the problem is that the files in question are from an outside source that I do not control.  The requirement here is that my corona app needs to programmaticlly detect an invalid PNG file.

I’m still puzzled.  Even if you don’t control/create the images you can load them in photoshop or Gimp if you have no budget and save them in a valid format.  You’re still the one doing the app build right?  

Where are these images coming from that you don’t control them?  It seems like you could simply have your team follow some guideline on saving.

Are you simply NOT going to display invalid images?    It seems odd that you’d be OK paying the size penalty to include images in your app build that can’t be used.

Whatever the case, Corona isn’t going to make a change to support this in the timeframe you need.  You can submit a feature request, but the community will need to vote for it.  ( I’m not staff, but this isn’t a bug so its not something that would get a priority fix.  You’re asking for a feature. )

Sorry if this isn’t what you want to hear, but you must admit this is a very specialized case for an easily corrected issue. 

PS - I may also be missing the point.  Perhaps you have hundreds (or 1000’s ?) of images an you’re looking for and automated way to detect those that fail so you can correct them?

If you really want to do this then open up the files as binary and manually check the file headers - this will work for both JPGs and PNGs. You can then programmatically check for invalid ICC profiles (i.e. non-RGB encoded files).

This is a good starting point - https://www.w3.org/TR/PNG/#11iCCP

Are you downloading images and displaying them?  If not, then you should detect this at dev time.
 
 
Are you SURE the returned object looks like a normal display object?  Does it have a removeSelf function?

 

local badBob = display.newImageRect( 'x2017-08-04-23-07-57-000-00300-00225.jpg', 100, 100 ) if( badBob.removeSelf == nil ) then print( "babBob is invalid" ) end

Thanks for the reply.

No, the images aren’t downloaded.   Short answer - The reason they are invalid is out of the scope of the issue I’m trying to solve.

Yes, the invalid image does have a removeSelf method.   newImageRect actually seems to return a black rectangle of the specified size that is a valid display object.   I can position it, and it displays on screen.

Sorry I don’t have any insights then, but it puzzles me why you don’t just solve this by correcting the images.  

I can see this being important if you’re downloading images, but you have them already so you should be able to check and fix as needed.

I’m giving this ‘push back’ because I know that putting a check in the code to see if the image didn’t load will cost you a little slice of your performance and can’t help but complicate the code.  

If you are using Photoshop then make sure you are using “save for web” to ensure using only standard colour profiles and standard encoding.

Thanks for the suggestions all, but the problem is that the files in question are from an outside source that I do not control.  The requirement here is that my corona app needs to programmaticlly detect an invalid PNG file.

I’m still puzzled.  Even if you don’t control/create the images you can load them in photoshop or Gimp if you have no budget and save them in a valid format.  You’re still the one doing the app build right?  

Where are these images coming from that you don’t control them?  It seems like you could simply have your team follow some guideline on saving.

Are you simply NOT going to display invalid images?    It seems odd that you’d be OK paying the size penalty to include images in your app build that can’t be used.

Whatever the case, Corona isn’t going to make a change to support this in the timeframe you need.  You can submit a feature request, but the community will need to vote for it.  ( I’m not staff, but this isn’t a bug so its not something that would get a priority fix.  You’re asking for a feature. )

Sorry if this isn’t what you want to hear, but you must admit this is a very specialized case for an easily corrected issue. 

PS - I may also be missing the point.  Perhaps you have hundreds (or 1000’s ?) of images an you’re looking for and automated way to detect those that fail so you can correct them?

If you really want to do this then open up the files as binary and manually check the file headers - this will work for both JPGs and PNGs. You can then programmatically check for invalid ICC profiles (i.e. non-RGB encoded files).

This is a good starting point - https://www.w3.org/TR/PNG/#11iCCP