[Solved] Problem loading .png "not really a png"

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Apickx
Posts: 3
Joined: Mon May 28, 2018 7:40 pm

[Solved] Problem loading .png "not really a png"

Post by Apickx »

Hi, I'm having some trouble loading a png. When I call loadImage(), irrlicht prints "LOAD PNG: not really a png". I dug into the source a bit, and found that a call to png_sig_cmp() failed.

I checked my image with

Code: Select all

od -tx1 client\data\ui\background2.png
, which outputs

Code: Select all

 
0000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
0000020 00 00 00 40 00 00 00 40 08 02 00 00 00 25 0b e6
0000040 89 00 00 00 09 70 48 59 73 00 00 0b 13 00 00 0b
0000060 13 01 00 9a 9c 18 00 00 00 07 74 49 4d 45 07 e2
0000100 08 04 10 21 1a 45 91 83 56 00 00 00 1d 69 54 58
0000120 74 43 6f 6d 6d 65 6e 74 00 00 00 00 00 43 72 65
0000140 61 74 65 64 20 77 69 74 68 20 47 49 4d 50 64 2e
0000160 65 07 00 00 00 5c 49 44 41 54 68 de ed d9 b1 0d
0000200 00 30 08 03 41 93 41 19 8e 49 33 43 0a 8a 48 f7
0000220 0b a0 2b 5c 51 dd 9d b5 66 26 c9 ea 89 93 cf 03
0000240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0000260 00 00 00 00 90 9e 2b 0f 0e 23 06 00 00 00 00 00
0000300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 78
0000320 ed 02 4f 26 08 af 93 c6 1a 72 00 00 00 00 49 45
0000340 4e 44 ae 42 60 82
0000346
 
the first 8 bytes seem to line up with https://en.wikipedia.org/wiki/List_of_file_signatures as well as irrlicht/source/Irrlicht/libpng/png.c:54

My code to load the file looks like this:

Code: Select all

 
int newiimagefromfile(...){
    printf("Creating new iimage from file");
    const char* strpath = ...;
    int numloaders = driver->getImageLoaderCount();
    bool hasloaded = false;
    IImage* img = NULL;
    printf("About to create and open file\n");
    io::IReadFile* f = device->getFileSystem()->createAndOpenFile(strpath);
    if(!f){
        ...
    }
    printf("Opened file\n");
    for(int j = 0; j < numloaders; j++){
        IImageLoader* loader = driver->getImageLoader(j);
        if(!loader->isALoadableFileExtension(strpath))
            continue;
        if(!loader->isALoadableFileFormat(f))
            continue;
        hasloaded = true;
        printf("Found a loader, about to load\n");
        img = loader->loadImage(f);
        printf("Done loading\n");
        f->drop();
    }
    if(!hasloaded){
        ...
    }
    if(!img){
        ...
    }
    ...
    return 1;
}
 
My prints get to:

Code: Select all

 
Creating new iimage from fileAbout to create and open file
Opened file
Found a loader, about to load
LOAD PNG: not really a png
: c:/Users/Alex/Desktop/broken_texturemaker/client/data/ui/background2.png
Done loading
 
I've been scratching my head for a day now, any help is appreciated.
Last edited by Apickx on Fri Oct 26, 2018 8:26 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Problem loading .png "not really a png"

Post by CuteAlien »

Yeah, the 8 bytes look correct. Pretty sure that's not the reason for failing. But can't tell without looking at the png itself what the reason is. Or enough code so I can compile and test. You are correct that you usually get the message when png_sig_cmp fails - so either that is not the file that is loaded or it's messed up by that point (for example you overwrite the content of the file pointer at some point).

For example you try on loading in your loop after you drop()'ed the file. Maybe you managed to have the png loader twice in your loaders somehow? In that case it would try to load a second time - but this time with a file where the memory was released. So - quit that loop as soon as you got it loaded once.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
MartinVee
Posts: 139
Joined: Tue Aug 02, 2016 3:38 pm
Location: Québec, Canada

Re: Problem loading .png "not really a png"

Post by MartinVee »

Have you tried loading it with one of the tutorial solution?
Apickx
Posts: 3
Joined: Mon May 28, 2018 7:40 pm

Re: Problem loading .png "not really a png"

Post by Apickx »

Figured it out, CImageLoaderPng::isALoadableFileFormat() reads the first 8 bytes of the file passed, but does not reset the cursor to the beginning of the file. So my check for isALoadableFileFormat() reads the first 8 bytes, and then CImageLoaderPng::loadImage() takes the next 8 bytes, assuming they are the first 8 bytes, and sees that it is not a png file from there. (Maybe isALoadableFileformat() should reset the file cursor? I don't usually expect methods starting with "is..." to mutate data; on the other hand, the documentation for isALoadableFileFormat() does say "Check might look into the file."). In any case, my solution was to use IFileRead::seek(0,false) to manually reset the file cursor after after isALodableFileFormat(). My loop now looks like this:

Code: Select all

 
    for(int j = 0; j < numloaders; j++){
        IImageLoader* loader = driver->getImageLoader(j);
        if(!loader->isALoadableFileExtension(strpath))
            continue;
        if(!loader->isALoadableFileFormat(f))
            continue;
        f->seek(0,false);
        hasloaded = true;
        img = loader->loadImage(f);
        f->drop();
        break;
    }
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [Solved] Problem loading .png "not really a png"

Post by CuteAlien »

OK, unexpected problem indeed
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply