I'm working on a game engine and I'm too much of a wuss to write image loaders for multiple formats, so my question is this: Is there an abstracted image loading library to load image files? I just need to load files then splat them on to the screen using an array of pixels.
7 Answers
I'm always a fan of CImg. It's very easy to use. Another user liked the answer as well. I'll post the same example I posted in the answer so you can see how easy it is to access pixels and dimension info.
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel @ 10,10
unsigned char pixel = *ptr;
-
A shame they do not have a 64-bit port. At least they say so in their FAQ. – Jonas Schäfer Mar 16 '12 at 07:52
-
What do you mean ? This library is just a header file and it compiles in 64bit mode. – Maciej Szpakowski Jan 12 '17 at 01:12
-
Would this work for a RGB image as well? Presumably not since instead of unsigned char you need an array? – so860 Mar 01 '21 at 18:14
-
FreeImage is a good open source library
Here is an example code, data is accessible with "out.data()"
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeU(filename.c_str());
if (format == FIF_UNKNOWN) format = FreeImage_GetFIFFromFilenameU(filename.c_str());
if (format == FIF_UNKNOWN) throw(std::runtime_error("File format not supported"));
FIBITMAP* bitmap = FreeImage_LoadU(format, filename.c_str());
FIBITMAP* bitmap2 = FreeImage_ConvertTo32Bits(bitmap);
FreeImage_Unload(bitmap);
std::vector<char> out(FreeImage_GetWidth(bitmap2) * FreeImage_GetHeight(bitmap2) * 4);
FreeImage_ConvertToRawBits((BYTE*)out.data(), bitmap2, FreeImage_GetWidth(bitmap2) * 4, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);
FreeImage_Unload(bitmap2);

- 4,832
- 5
- 29
- 38
-
The c++ wrapper seems a bit buggy, at least with g++. Linking the library is problematic, though the c version works fine. – jiggunjer Jul 23 '15 at 10:08
-
For some reason, the library header tries to define "_WINDOWS_" which interferes with the Windows.h header. Windows.h header uses that as an include guard. Not sure what would be a proper way of solving this issue. – Spidey Jul 17 '18 at 12:27
There is Boost's GIL which was originally developed by Adobe. It might not be the most intuitive one, but it certainly is one of the most complete and powerful libs.

- 4,300
- 18
- 24
-
1`boost::gil` does not by itself load any image formats. What it does is provide a uniform interface to perform pixel-algorithms. The image loader helpers in `boost::gil` require you to have the corresponding libraries (e.g. `libtiff`, `libpng`, etc.) – kizzx2 Oct 29 '11 at 17:09
-
1that is true for CImg too, it supports a few formats natively, the rest need 3rd party libraries. – jiggunjer Jun 30 '15 at 07:23
You could have a look at SDL_Image, a sub-project of the Simple DirectMedia Layer. It serves as an easy abstraction for loading several different image formats. SDL is written in C but is easy to use with either C or C++ code.

- 34,290
- 15
- 75
- 125
-
1OP does not even imply that C and C++ are 'a language'. OP is looking for a solution that is either C or C++. – Gregor Brandt Jul 19 '10 at 20:14
-
@gbrandt, updated my answer to reflect the implementation language for SDL. – Michael Kristofik Jul 19 '10 at 20:33
If you're using OpenGL, then DevIL is a good choice since its style and conventions adhere to OpenGL more than any other library. It's relatively easy to set up and also has great support for multiple formats.
One thing about the wuss thing. While it's nice to set up working 3rd party code that has been well tested and saves you time, there's something to be also said about learning how image loading works and why it works the way it does. If you really want to get good at something, I believe that you need to actually learn how to do it from scratch. Even if you end up using 3rd party code in the end.

- 1,137
- 7
- 12
Yet another possibility (primarily, if not exclusively for Windows) is CXImage
. Its obvious advantage over many others is supporting many camera raw formats, in case that matters to you.

- 476,176
- 80
- 629
- 1,111
OpenImageIO supports many different file formats among them TIFF, JPEG/JFIF, OpenEXR, PNG, HDR/RGBE, ICO, BMP, Targa, JPEG-2000, RMan Zfile, FITS, DDS, Softimage PIC, PNM, DPX, Cineon, IFF, Field3D, Ptex, Photoshop PSD, Wavefront RLA, SGI, WebP, and GIF

- 7,709
- 6
- 64
- 90