I recently tried out Sean Barrett’s stb_image.h
and was blown away by how fucking easy it is to use.
Integrating it into your project is trivial: Just add the header and somewhere do:
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
That’s all. (If you wanna use it in multiple files you just #include "stb_image.h"
there without the #define
.)
And the API is trivial too:
int width, height, bytesPerPixel;
unsigned char *pixeldata, *pixeldata2;
pixeldata = stbi_load("bla.jpg", &width, &height, &bytesPerPixel, 0);
// if you have already read the image file data into a buffer:
pixeldata2 = stbi_load_from_memory(bufferWithImageData, bufferLength,
&width, &height, &bytesPerPixel, 0);
if(pixeldata2 == NULL)
printf("Some error happened: %s\n", stbi_failure_reason());
There’s also a simple callback-API which allows you to define some callbacks that stb_image will call to get the data, handy if you’re using some kind of virtual filesystem or want to load the data from .zip files or something.
And it supports lots of common image file types including JPEG, PNG, TGA, BMP, GIF and PSD.
So I wondered if there are any downsides regarding speed.
Read More…