Saving images

Hello,

SDL_image is a very good library for loading images of different formats.
Unfortunately it doesn’t support saving in those formats.

What do you suggest to save standard image formats like jpg, png ?
The official libraries are not easy to handle.

Thanks,

Julien CLEMENT
@Julien_Clement1

I submitted a patch for saving to PNG over a year ago. It’s never been added to the codebase, though…________________________________
From: clementj2005@yahoo.fr (Julien Clement)
Subject: [SDL] Saving images

Hello,

SDL_image is a very good library for loading images of different formats.
Unfortunately it doesn’t support saving in those formats.

What do you suggest to save standard image formats like jpg, png ?
The official libraries are not easy to handle.

Thanks,

Julien CLEMENT
clementj2005 at yahoo.fr


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I submitted a patch for saving to PNG over a year ago. It’s never been added to the codebase, though…

Maybe you should send another email to the author ? (It’s Sam and Mattias Engdegard).
I know that Sam is currently busy.

It’d be very nice to have a PNG writer in SDL_image.
By the way I found a lightweight library to do it, LodePNG. Very nicely coded library.

See you,

Julien CLEMENT
@Julien_Clement1

Julien CLEMENT wrote:

I submitted a patch for saving to PNG over a year ago. It’s never been added to the codebase, though…

Maybe you should send another email to the author ? (It’s Sam and Mattias Engdegard).
I know that Sam is currently busy.

It’d be very nice to have a PNG writer in SDL_image.
By the way I found a lightweight library to do it, LodePNG. Very nicely coded library.

See you,

Julien CLEMENT
clementj2005 at yahoo.fr

I agree it would. But I’m content, having filched a png save function
off this mailing list. :wink: It’s in my “standard” library directory now.

If there’s a problem with this code, don’t blame me. I take no credit
and have made no modifications.

int png_save_surface(const char *filename, SDL_Surface *surf)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
int i, colortype;
png_bytep *row_pointers;

 /* Opening output file */
 fp = fopen(filename, "wb");
 if (fp == NULL) {
     wxLogError(wxT("fopen error"));
     return -1;
 }

 /* Initializing png structures and callbacks */
 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
     NULL, png_user_error, png_user_warn);
 if (png_ptr == NULL) {
     wxLogError(wxT("png_create_write_struct error!\n"));
     return -1;
 }

 info_ptr = png_create_info_struct(png_ptr);
 if (info_ptr == NULL) {
     png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
     wxLogError(wxT("png_create_info_struct error!\n"));
     exit(-1);
 }

 if (setjmp(png_jmpbuf(png_ptr))) {
     png_destroy_write_struct(&png_ptr, &info_ptr);
     fclose(fp);
     exit(-1);
 }

 png_init_io(png_ptr, fp);

 colortype = png_colortype_from_surface(surf);
 png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype,    

PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

 /* Writing the image */
 png_write_info(png_ptr, info_ptr);
 png_set_packing(png_ptr);

 row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surf->h);
 for (i = 0; i < surf->h; i++)
     row_pointers[i] = (png_bytep)(Uint8 *)surf->pixels + i*surf->pitch;
 png_write_image(png_ptr, row_pointers);
 png_write_end(png_ptr, info_ptr);

 /* Cleaning out... */
 free(row_pointers);
 png_destroy_write_struct(&png_ptr, &info_ptr);
 fclose(fp);

 return 0;

}