Jump to content
  • Advertisement
Sign in to follow this  
SteveDeFacto

What is the best library for loading images?

This topic is 2690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm mainly looking for the best library to load PNG files but if the library supports other formats that would be a plus. I'm using OpenGL so if the library works well with OpenGL that's also a plus. I have found a lot of libraries for this such as [color="#1C2837"]SDL_image, DevIL, libpng among others but I'm not sure which is best?

Share this post


Link to post
Share on other sites
Advertisement
I certainly don't know about best, but I've always used SDL_image (and use it in my current project to load PNG files). It is insanely easy to use and supports the following image formats: BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, XCF, XPM and XV.

Of course, SDL_image returns an SDL_Surface*, so if you are not using SDL, this may be an undesirable dependency.

Here is a code snippet for loading an image and using it as an OpenGL texture:


// initialize SDL_image for loading PNGs (you can tell it to initialize "everything" if you like). Real code should check the return value.
IMG_Init(IMG_INIT_PNG);

// OpenGL texture id
GLuint texture = 0;

SDL_Surface* tex = IMG_Load("image.png");
if(tex)
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// If BytesPerPixel is 3 then the image is 24bit RGB, if it is 4 then the image is 32bit RGBA. We won't bother supporting anything else.
const GLint gl_format_list[] = {0, 0, 0, GL_RGB8, GL_RGBA8};
const GLint img_format_list[] = {0, 0, 0, GL_RGB, GL_RGBA};


glTexImage2D(GL_TEXTURE_2D, 0, gl_format_list[tex->format->BytesPerPixel], tex->w, tex->h, 0, img_format_list[tex->format->BytesPerPixel], GL_UNSIGNED_BYTE, tex->pixels);


SDL_FreeSurface(tex);
}

Share this post


Link to post
Share on other sites
I use SOIL(simple open image lib), it loads basic format (png,bmp,dds,tga,jpg) and create an opengl texture. Could create cubemaps automatically. Could load HDR textures. But good for OGL user only I guess

Share this post


Link to post
Share on other sites

I certainly don't know about best, but I've always used SDL_image (and use it in my current project to load PNG files). It is insanely easy to use and supports the following image formats: BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, XCF, XPM and XV.

Of course, SDL_image returns an SDL_Surface*, so if you are not using SDL, this may be an undesirable dependency.

Here is a code snippet for loading an image and using it as an OpenGL texture:


// initialize SDL_image for loading PNGs (you can tell it to initialize "everything" if you like). Real code should check the return value.
IMG_Init(IMG_INIT_PNG);

// OpenGL texture id
GLuint texture = 0;

SDL_Surface* tex = IMG_Load("image.png");
if(tex)
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// If BytesPerPixel is 3 then the image is 24bit RGB, if it is 4 then the image is 32bit RGBA. We won't bother supporting anything else.
const GLint gl_format_list[] = {0, 0, 0, GL_RGB8, GL_RGBA8};
const GLint img_format_list[] = {0, 0, 0, GL_RGB, GL_RGBA};


glTexImage2D(GL_TEXTURE_2D, 0, gl_format_list[tex->format->BytesPerPixel], tex->w, tex->h, 0, img_format_list[tex->format->BytesPerPixel], GL_UNSIGNED_BYTE, tex->pixels);


SDL_FreeSurface(tex);
}




Hmmm... I've actually been thinking about using SDL for a long time but it didn't seem to offer enough utility to be worth my time. Now that I know it can be used to load image files I'm wondering if it can also be used to load audio files like ogg? Currently I'm using lib ogg but if I could combine all of these APIs into one then I see a lot more utility in using SDL.

Share this post


Link to post
Share on other sites
I used to prefer Corona above all else because of its very clean design, but it hasn't been updated in ages (I don't think Corona itself needs to an update, but the libraries it uses like libpng and openjpeg have jumped by several versions already, including some security issues discovered in libpng).

Another very good library is FreeImage. It supports a huge number of formats, including OpenEXR for HDR floating point images. Its been used in Ogre as the default image/texture loading plugin if I remember right.

Share this post


Link to post
Share on other sites
Another vote for FreeImage. It supports a lot of formats, including PSD, which was the reason for me to use this library.

Besides, it is very easy to use.

Carlos

Share this post


Link to post
Share on other sites

Hmmm... I've actually been thinking about using SDL for a long time but it didn't seem to offer enough utility to be worth my time. Now that I know it can be used to load image files I'm wondering if it can also be used to load audio files like ogg? Currently I'm using lib ogg but if I could combine all of these APIs into one then I see a lot more utility in using SDL.

Well, I've used the standard SDL addon libraries SDL_mixer and SDL_sound in the past and they were quite simple to use. I've not yet decided what I will use for audio in my current project, but it will either be an SDL_mixer/SDL-sound mix or OpenAL or FMOD.

Other common SDL extension libraries include SDL_net and SDL_ttf. If you do choose SDL, make sure to give the new SDL 1.3 a look. Its still under development, but is quite stable and has a good bit of additional functionality over SDL 1.2.

Share this post


Link to post
Share on other sites
I'd recommend staying away from DevIL. We used it in our project a few years ago and had a lot of problems with it. Namely, it didn't work the same across all systems despite being "cross-platform" and was more headaches than it was worth, so we switched libraries. We use SDL and OpenGL in our project, and we weren't keen on the idea of using SDL_image and having to convert all our SDL surfaces into OpenGL textures. Today we use libpng and libjpeg to load images and populate our textures. They have been absolutely flawless from day one and have never given us any problems of any sort.

Share this post


Link to post
Share on other sites
I ran into this problem some time ago and after looking at all the loaders out there and deciding they are all rubbish for various reasons (upto and including decompressing DDS textures onload... serious, wtf?) I wrote my own and released it under zlib.

Then, a bit later, I realised that was pretty pointless in itself; while it's handy to have a library which can load images in this is only something you should be doing within tools; in your game there is really only one texture/data format you need to care about; DDS aka DXTn compressed images.

Why?
Well, the compression is natively supported by the hardware which makes it useful for reducing bandwidth etc unlike png, tga and jpeg which all need to be decompressed on the CPU before upload. For data you don't want to compress DDS also supports uncompressed data, although you'll probably want to compress this file with something like zlib to reduce load time bandwidth requirements.

In short; find something which does what you need (be it one of the ones above or just rip the code out of mine, it handles pretty much all the common formats) and then make a tool which spits out DDS files, more than likely using conversion code from somewhere else such as from MS, NV or AMD.

Honestly, it's not worth worrying about anything else at runtime.

And yes, the DXTn format works just fine with OpenGL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!