Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Image Loading [closed]

Tags:

c++

c

image

loading

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.

like image 648
Jookia Avatar asked Jul 19 '10 19:07

Jookia


2 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;
like image 189
NG. Avatar answered Nov 02 '22 20:11

NG.


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);
like image 11
Tomaka17 Avatar answered Nov 02 '22 18:11

Tomaka17