4

When I attempt to load a png file, the error "unsupported image format" is returned. People have previously posted about this topic on StackOverflow, but none of the solutions have seemed to work for me.

I am working on a Linux machine, compiling with g++

Here are my libraries...

#include <SDL2/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <string>
#include <zlib.h>
#include <png.h>

And when I compile I include the -lSDL2 and -lSDL_image flags. I do not use SDL2/SDL_image.h because it is not installed on the machines I am working on. Additionally, my png file is definitely in the same directory as my code that tries to load it and I do call IMG_Init(IMG_INIT_PNG) and IMG_INIT_PNG.

Here is my code, the error occurs in the loadSurface function(I believe).

//Using SDL, SDL_image, standard IO, and strings
#include <SDL2/SDL.h>
//#include <SDL/SDL_version.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <string>
#include <zlib.h>
#include <png.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//Loads individual image
SDL_Surface* loadSurface( std::string path );

//The window we'll be rendering to
SDL_Window* gWindow = NULL;

//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//Current displayed PNG image
SDL_Surface* gPNGSurface = NULL;

bool init()
{
        //Initialization flag
 bool success = true;

        //Initialize SDL
        if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
                printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
                success = false;
        }
        else
        {
                //Create window
                gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
                if( gWindow == NULL )
                {
                        printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
                        success = false;
                }
                else
                {
                        //Initialize PNG loading
                        int imgFlags = IMG_INIT_PNG;
                      if( !( IMG_Init( imgFlags ) & imgFlags ) )
                      {
                              printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
                              success = false;
                      }
                      else
                        {
                                //Get window surface
                                gScreenSurface = SDL_GetWindowSurface( gWindow );
                        }
                }
        }

        return success;
}

bool loadMedia()
{
        //Loading success flag
        bool success = true;

        //Load PNG surface
        gPNGSurface = loadSurface( "loaded.png" );
        if( gPNGSurface == NULL )
        {
                printf( "Failed to load PNG image!\n" );
                success = false;
        }

        return success;
}

void close()
{
        //Free loaded image
        SDL_FreeSurface( gPNGSurface );
        gPNGSurface = NULL;

        //Destroy window
        SDL_DestroyWindow( gWindow );
        gWindow = NULL;

        //Quit SDL subsystems
        IMG_Quit();
        SDL_Quit();
}

SDL_Surface* loadSurface( std::string path )
{
        //The final optimized image
        SDL_Surface* optimizedSurface = NULL;
 //Load image at specified path
        SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
        if( loadedSurface == NULL )
        {
                printf( "1Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
        }
        else
        {
                //Convert surface to screen format
                optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
                if( optimizedSurface == NULL )
                {
                        printf( "2Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
                }

                //Get rid of old loaded surface
                SDL_FreeSurface( loadedSurface );
        }

        return optimizedSurface;
}

int main( int argc, char* args[] )
{
        //Start up SDL and create window
        if( !init() )
        {
                printf( "Failed to initialize!\n" );
        }
        else
        {
                //Load media
                if( !loadMedia() )
                {
                        printf( "Failed to load media!\n" );
                }
                else
                {
                       //Main loop flag
                        bool quit = false;

                        //Event handler
                        SDL_Event e;

                        //While application is running
                        while( !quit )
                        {
                                //Handle events on queue
                                while( SDL_PollEvent( &e ) != 0 )
                                {
                                        //User requests quit
                                        if( e.type == SDL_QUIT )
                                        {
                                                quit = true;
                                        }
                                }

                                //Apply the PNG image
                                SDL_BlitSurface( gPNGSurface, NULL, gScreenSurface, NULL );

                                //Update the surface
                                SDL_UpdateWindowSurface( gWindow );
                        }
                }
        }

        //Free resources and close SDL
        close();

        return 0;
}

When I compile, I use g++, compiler flags -w, and linker flags -lSDL2 -lSDL_image

When I run, the output is as shows...

1Unable to load image loaded.png! SDL_image Error: Unsupported image format

Failed to load PNG image!

Failed to load media!

5
  • Have you got a minimal example that compiles and exhibits the error?
    – Peter R
    Commented Mar 18, 2015 at 0:58
  • //Load image at specified path\n SDL_Surface* loadedSurface = IMG_Load( path.c_str() ); \n if( loadedSurface == NULL ) { \n printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() ); \n } then the output displays... "Unable to load image loaded.png! SDL_image Error: Unsupported image format"
    – Patrick
    Commented Mar 18, 2015 at 1:54
  • 1
    Does this help? stackoverflow.com/questions/23323450/…
    – Peter R
    Commented Mar 18, 2015 at 5:28
  • No, their solution was to link SDL2_image instead of SDL_image. However, I do not have access to SDL2_image.
    – Patrick
    Commented Mar 18, 2015 at 14:50
  • I had the same problem in Ubuntu 16.04 with packages installed from APT: libsdl2-image-2.0-0, libsdl2-image-dev, libsdl2-image-2.0-0, libsdl2-dev, but on my other OS it just works with default packages installed from the package manager.
    – engineerX
    Commented May 20, 2019 at 16:58

2 Answers 2

6

The compiler flag -lSDL_image should be -lSDL2_image

1
  • 1
    I was encountering a similar issue, and this helped. Thanks!
    – archgoon
    Commented May 20, 2015 at 6:28
0

First of all, there are few steps when you're compiling your code. Compilation, linking, and execution. If your program gets past first two steps, it's ready to work - to be executed. If it passed first two steps, you know that there are no syntax errors or linking errors(e.g. you passed proper flags to linker). However, if you receive error on runtime(which is exactly what happened), something is wrong with environment or program logic.

This said, you didn't provide any code at all, so that's hard to help you. I'm not sure if you know how to load image at all - you seem to include lots of unnecessary libs for this operations. I'd recommend you reading this tutorial to learn how to load images with SDL. Probably something is wrong with code(flags, maybe?), but you might as well compiled the code by yourself and be missing some dependencies.

2
  • I went ahead and edited the original post to give you more detail. I have actually been using the Lazy Foo tutorials to learn SDL, and got stuck at that very tutorial page. Any help would be greatly appreciated!
    – Patrick
    Commented Mar 18, 2015 at 14:41
  • I compiled your code, and opened file successfully. Code is ok. Make sure you have valid file, check its name. I have it in project directory, not bin directory. You may want to add full path. I'm using Code::Blocks, by the way. Edit: actually, if I were you, if it can't load png file, I'd try other png file. After that, look for dependancies(maybe reinstall libs?). Commented Mar 18, 2015 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.