The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Introducing SDL_stbimage.h - creating SDL_Surfaces from imag
Daniel Gibson
Guest

Hi,

I created a small header-only library that uses the awesome stb_image.h
(see https://github.com/nothings/stb ) to decode image files and creates
SDL_Surfaces from that (in RGB or RGBA, depending on whether the image
file had an alpha channel or not):
https://github.com/DanielGibson/Snippets/blob/master/SDL_stbimage.h

It supports all file formats supported by stb_image.h, which are
currently JPEG, PNG, BMP, PSD, TGA, GIF, PIC and PNM (.ppm, .pgm).

Using it is super-easy: Just drop SDL_stbimage.h and stb_image.h in your
project, and in one of your .c/.cpp files, do

#define SDL_STBIMAGE_IMPLEMENTATION
#include "SDL_stbimage.h"

which will create the implementation of the libraries (both SDL_stbimage
and stb_image) there. Isn't that a lot easier than integrating libpng,
libjpeg etc in your buildsystem? :-)

Now you're ready to use it, like:

SDL_Surface* surf = STBIMG_Load("test.png");
if(surf == NULL) {
printf("ERROR: Couldn't load test.png: %s\n", SDL_GetError());
exit(1);
}

// ... do something with surf ...

SDL_FreeSurface(surf);

(In your other source files just do #include "SDL_stbimage.h" without
the #define SDL_STBIMAGE_IMPLEMENTATION to use the STBIMG_* functions)

It also has functions to load from a constant memory buffer and from
SDL_RWops and a function to create stb_image specific IO-callbacks from
SDL_RWops, in case you wanna use SDL_RWops and stb_image functions, but
not SDL_Surface.
See the homepage and the header itself for more details:
https://github.com/DanielGibson/Snippets
https://github.com/DanielGibson/Snippets/blob/master/SDL_stbimage.h


I hope you like it!

Cheers,
Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Introducing SDL_stbimage.h - creating SDL_Surfaces from imag
Jonny D


Joined: 12 Sep 2009
Posts: 932
This looks pretty nice!

I'll have to look it over and compare it to my implementation in SDL_gpu, which uses stb-image and stb-image-write.


SDL_gpu has GPU_LoadSurface, GPU_LoadSurface_RW, and GPU_SaveSurface functions (hmm... looks like it needs SaveSurface_RW).


Jonny D






On Thu, Nov 19, 2015 at 11:06 AM, Daniel Gibson wrote:
Quote:
Hi,

I created a small header-only library that uses the awesome stb_image.h (see https://github.com/nothings/stb ) to decode image files and creates SDL_Surfaces from that (in RGB or RGBA, depending on whether the image file had an alpha channel or not):
  https://github.com/DanielGibson/Snippets/blob/master/SDL_stbimage.h

It supports all file formats supported by stb_image.h, which are currently JPEG, PNG, BMP, PSD, TGA, GIF, PIC and PNM (.ppm, .pgm).

Using it is super-easy: Just drop SDL_stbimage.h and stb_image.h in your project, and in one of your .c/.cpp files, do

  #define SDL_STBIMAGE_IMPLEMENTATION
  #include "SDL_stbimage.h"

which will create the implementation of the libraries (both SDL_stbimage and stb_image) there. Isn't that a lot easier than integrating libpng, libjpeg etc in your buildsystem? :-)

Now you're ready to use it, like:

  SDL_Surface* surf = STBIMG_Load("test.png");
  if(surf == NULL) {
    printf("ERROR: Couldn't load test.png: %s\n", SDL_GetError());
    exit(1);
  }

  // ... do something with surf ...

  SDL_FreeSurface(surf);

(In your other source files just do #include "SDL_stbimage.h" without the #define SDL_STBIMAGE_IMPLEMENTATION to use the STBIMG_* functions)

It also has functions to load from a constant memory buffer and from SDL_RWops and a function to create stb_image specific IO-callbacks from SDL_RWops, in case you wanna use SDL_RWops and stb_image functions, but not SDL_Surface.
See the homepage and the header itself for more details:
  https://github.com/DanielGibson/Snippets
  https://github.com/DanielGibson/Snippets/blob/master/SDL_stbimage.h


I hope you like it!

Cheers,
Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Introducing SDL_stbimage.h - creating SDL_Surfaces from imag
Daniel Gibson
Guest

On 11/19/2015 07:49 PM, Jonathan Dearborn wrote:
Quote:
This looks pretty nice!

I'll have to look it over and compare it to my implementation in
SDL_gpu, which uses stb-image and stb-image-write.

SDL_gpu has GPU_LoadSurface, GPU_LoadSurface_RW, and GPU_SaveSurface
functions (hmm... looks like it needs SaveSurface_RW).

I only support loading surfaces, no writing.

I guess SaveSurface_RW() is pretty easy with stb_image_write, you only
need to write a wrapper around SDL_RWwrite() for stbi_write_func.

One disadvantage of stb_image_write is that the png compression is kinda
poor, the resulting files are often 40-50% bigger than what you'd get
with libpng and 40% bigger than the results from LodePNG.
But it's still a lot smaller than TGA (even with RLE), so I guess it's
bearable.


I dunno if I should add support for writing - I guess if there is demand
for it I'd put it in a separate lib and also use stb_image_write.

But currently I'm curious if anyone actually ends up using
SDL_stbimage.h Wink

Cheers,
Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Introducing SDL_stbimage.h - creating SDL_Surfaces from imag
Jonny D


Joined: 12 Sep 2009
Posts: 932
Yeah, stb_image_write's PNG compression is also very slow (~800 ms for a screen-sized image).  I have to use TGA when saving images as state in apps.  For me it'd be great if only that were fixed.

Jonny D






On Thu, Nov 19, 2015 at 4:32 PM, Daniel Gibson wrote:
Quote:
On 11/19/2015 07:49 PM, Jonathan Dearborn wrote:
Quote:
This looks pretty nice!

I'll have to look it over and compare it to my implementation in
SDL_gpu, which uses stb-image and stb-image-write.

SDL_gpu has GPU_LoadSurface, GPU_LoadSurface_RW, and GPU_SaveSurface
functions (hmm... looks like it needs SaveSurface_RW).

I only support loading surfaces, no writing.

I guess SaveSurface_RW() is pretty easy with stb_image_write, you only need to write a wrapper around SDL_RWwrite() for stbi_write_func.

One disadvantage of stb_image_write is that the png compression is kinda poor, the resulting files are often 40-50% bigger than what you'd get with libpng and 40% bigger than the results from LodePNG.
But it's still a lot smaller than TGA (even with RLE), so I guess it's bearable.


I dunno if I should add support for writing - I guess if there is demand for it I'd put it in a separate lib and also use stb_image_write.

But currently I'm curious if anyone actually ends up using SDL_stbimage.h Wink

Cheers,
Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org