SDL_SavePNG? SDL_SaveJPG? png save, or jpg save functions?

Hello,

has anyone written a png, or jpeg saving function?

Rene Dudfield wrote:

has anyone written a png, or jpeg saving function?

As for PNG, I think several people have. Search the list archives.

One thread I remember because I tried to contact the author about it
some months ago (without success) was
http://thread.gmane.org/gmane.comp.lib.sdl/14673. It’s about a patch
that adds PNG saving to SDL_image, and even though most people who
responded seemed to agree that such an extension would be useful
(especially considering that it could be enabled or disabled at compile
time, and that libpng is already linked in anyway), no further action
seems to have been taken, and the author never posted his code.

-Christian

Yeah, I’ve contacted that person about their save png function too.

They probably get a few emails a month or so asking for it. hehe.

I had a quick look at the example code from libpng. It’s not too
complex. So I think I’ll make a 32bit RGBA surface to png save
function if I can’t find one. Then just use SDL to convert everything
to that format before saving to png.

I haven’t looked at the libjpeg code yet.On 5/20/06, Christian Walther wrote:

Rene Dudfield wrote:

has anyone written a png, or jpeg saving function?

As for PNG, I think several people have. Search the list archives.

One thread I remember because I tried to contact the author about it
some months ago (without success) was
http://thread.gmane.org/gmane.comp.lib.sdl/14673. It’s about a patch
that adds PNG saving to SDL_image, and even though most people who
responded seemed to agree that such an extension would be useful
(especially considering that it could be enabled or disabled at compile
time, and that libpng is already linked in anyway), no further action
seems to have been taken, and the author never posted his code.

-Christian


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

i hope i don’t misunderstand, you need some code to save PNG?

I just implemented a screenshot function in a game that is
based on some code that i found on this list some years ago:

/* screenshot handling /
static int ss_do;
static unsigned char
* ss_rows;
static int ss_size;
#define SS_LEN 1024
static char ss_name[SS_LEN];
static int ss_num;
static int ss_w, ss_h;
static unsigned char* ss_data;
static SDL_Surface* ss_surface;
static SDL_Rect ss_rect;

int write_png(char *file_name, png_bytep *rows, int w, int h, int colortype,
int bitdepth) {
png_structp png_ptr;
png_infop info_ptr;
FILE *fp = fopen(file_name, “wb”);
char *doing = “open for writing”;

if (!(fp = fopen(file_name, “wb”))) goto fail;
doing = “create png write struct”;
if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL))) goto fail;
doing = “create png info struct”;
if (!(info_ptr = png_create_info_struct(png_ptr))) goto fail;
if (setjmp(png_jmpbuf(png_ptr))) goto fail;
doing = “init IO”;
png_init_io(png_ptr, fp);
doing = “write header”;
png_set_IHDR(png_ptr, info_ptr, w, h, bitdepth, colortype,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
doing = “write info”;
png_write_info(png_ptr, info_ptr);
doing = “write image”;
png_write_image(png_ptr, rows);
doing = “write end”;
png_write_end(png_ptr, NULL);
return 0;

fail:
printf(“Write_png: could not %s\n”, doing);
return -1;
}

initialisation:
ss_rows = 0;
ss_size = 0;
ss_surface = NULL;

actually make a screenshot:
/* make a screenshot */
if(ss_do && (cstate == CSTATE_GAME)) {
ss_do = 0;
w = skin_inf->w;
ss_w = cfw * w;
ss_h = cfh * w;

xs = (xwidth - ss_w) / 2;
ys = (yheight - ss_h) / 2;

snprintf(ss_name, SS_LEN, "screenshot_%i.png", ss_num);

if(ss_size != 0 || ss_size != ss_h) {
  ss_size = 0;
  free(ss_rows);
}

if(ss_size == 0) {
  ss_size = ss_h;
  ss_rows = (unsigned char**)malloc(sizeof(unsigned char*) * ss_size);
  if(ss_rows == NULL) {
common_die("could not get memory for screenshot\n");
  }
}

if(ss_surface != NULL) {
  if(ss_surface->w != ss_w || ss_surface->h != ss_h) {
SDL_FreeSurface(ss_surface);
ss_surface = NULL;
  }
}

if(ss_surface == NULL) {
  ss_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, ss_w, ss_h, 24,

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff0000, 0xff00, 0xff, 0
#else
0xff, 0xff00, 0xff0000, 0
#endif
);

  if(ss_surface == NULL) {
common_die("could not get screenshot memory");
  }
}
ss_rect.x = xs;
ss_rect.y = ys;
ss_rect.w = ss_w;
ss_rect.h = ss_h;
SDL_BlitSurface(screen, &ss_rect, ss_surface, NULL);

for(i = 0; i < ss_h; i++) {
  ss_rows[i] = ((unsigned char*)ss_surface->pixels) + i * 

ss_surface->pitch;
}

//    printf("ss_w %i   ss_h %i   ss_size %i   ss_num %i\n", ss_w, ss_h, 

ss_size, ss_num);

write_png(ss_name, ss_rows, ss_w, ss_h, PNG_COLOR_TYPE_RGB, 8);
ss_num++;

}

There may be some small parts missing.

Best regards,
Torsten.> Yeah, I’ve contacted that person about their save png function too.

They probably get a few emails a month or so asking for it. hehe.

I had a quick look at the example code from libpng. It’s not too
complex. So I think I’ll make a 32bit RGBA surface to png save
function if I can’t find one. Then just use SDL to convert everything
to that format before saving to png.

I haven’t looked at the libjpeg code yet.

On 5/20/06, Christian Walther wrote:

Rene Dudfield wrote:

has anyone written a png, or jpeg saving function?

As for PNG, I think several people have. Search the list archives.

One thread I remember because I tried to contact the author about it
some months ago (without success) was
http://thread.gmane.org/gmane.comp.lib.sdl/14673. It’s about a patch
that adds PNG saving to SDL_image, and even though most people who
responded seemed to agree that such an extension would be useful
(especially considering that it could be enabled or disabled at compile
time, and that libpng is already linked in anyway), no further action
seems to have been taken, and the author never posted his code.

-Christian


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thanks. That looks good.On 5/20/06, Torsten Mohr wrote:

Hi,

i hope i don’t misunderstand, you need some code to save PNG?

I just implemented a screenshot function in a game that is
based on some code that i found on this list some years ago:

/* screenshot handling /
static int ss_do;
static unsigned char
* ss_rows;
static int ss_size;
#define SS_LEN 1024
static char ss_name[SS_LEN];
static int ss_num;
static int ss_w, ss_h;
static unsigned char* ss_data;
static SDL_Surface* ss_surface;
static SDL_Rect ss_rect;

int write_png(char *file_name, png_bytep *rows, int w, int h, int colortype,
int bitdepth) {
png_structp png_ptr;
png_infop info_ptr;
FILE *fp = fopen(file_name, “wb”);
char *doing = “open for writing”;

if (!(fp = fopen(file_name, “wb”))) goto fail;
doing = “create png write struct”;
if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL))) goto fail;
doing = “create png info struct”;
if (!(info_ptr = png_create_info_struct(png_ptr))) goto fail;
if (setjmp(png_jmpbuf(png_ptr))) goto fail;
doing = “init IO”;
png_init_io(png_ptr, fp);
doing = “write header”;
png_set_IHDR(png_ptr, info_ptr, w, h, bitdepth, colortype,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
doing = “write info”;
png_write_info(png_ptr, info_ptr);
doing = “write image”;
png_write_image(png_ptr, rows);
doing = “write end”;
png_write_end(png_ptr, NULL);
return 0;

fail:
printf(“Write_png: could not %s\n”, doing);
return -1;
}

initialisation:
ss_rows = 0;
ss_size = 0;
ss_surface = NULL;

actually make a screenshot:
/* make a screenshot */
if(ss_do && (cstate == CSTATE_GAME)) {
ss_do = 0;
w = skin_inf->w;
ss_w = cfw * w;
ss_h = cfh * w;

xs = (xwidth - ss_w) / 2;
ys = (yheight - ss_h) / 2;

snprintf(ss_name, SS_LEN, "screenshot_%i.png", ss_num);

if(ss_size != 0 || ss_size != ss_h) {
  ss_size = 0;
  free(ss_rows);
}

if(ss_size == 0) {
  ss_size = ss_h;
  ss_rows = (unsigned char**)malloc(sizeof(unsigned char*) * ss_size);
  if(ss_rows == NULL) {
    common_die("could not get memory for screenshot\n");
  }
}

if(ss_surface != NULL) {
  if(ss_surface->w != ss_w || ss_surface->h != ss_h) {
    SDL_FreeSurface(ss_surface);
    ss_surface = NULL;
  }
}

if(ss_surface == NULL) {
  ss_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, ss_w, ss_h, 24,

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff0000, 0xff00, 0xff, 0
#else
0xff, 0xff00, 0xff0000, 0
#endif
);

  if(ss_surface == NULL) {
    common_die("could not get screenshot memory");
  }
}
ss_rect.x = xs;
ss_rect.y = ys;
ss_rect.w = ss_w;
ss_rect.h = ss_h;
SDL_BlitSurface(screen, &ss_rect, ss_surface, NULL);

for(i = 0; i < ss_h; i++) {
  ss_rows[i] = ((unsigned char*)ss_surface->pixels) + i *

ss_surface->pitch;
}

//    printf("ss_w %i   ss_h %i   ss_size %i   ss_num %i\n", ss_w, ss_h,

ss_size, ss_num);

write_png(ss_name, ss_rows, ss_w, ss_h, PNG_COLOR_TYPE_RGB, 8);
ss_num++;

}

There may be some small parts missing.

Best regards,
Torsten.

Yeah, I’ve contacted that person about their save png function too.

They probably get a few emails a month or so asking for it. hehe.

I had a quick look at the example code from libpng. It’s not too
complex. So I think I’ll make a 32bit RGBA surface to png save
function if I can’t find one. Then just use SDL to convert everything
to that format before saving to png.

I haven’t looked at the libjpeg code yet.

On 5/20/06, Christian Walther wrote:

Rene Dudfield wrote:

has anyone written a png, or jpeg saving function?

As for PNG, I think several people have. Search the list archives.

One thread I remember because I tried to contact the author about it
some months ago (without success) was
http://thread.gmane.org/gmane.comp.lib.sdl/14673. It’s about a patch
that adds PNG saving to SDL_image, and even though most people who
responded seemed to agree that such an extension would be useful
(especially considering that it could be enabled or disabled at compile
time, and that libpng is already linked in anyway), no further action
seems to have been taken, and the author never posted his code.

-Christian


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hello,

I’ve made a Save_PNG function for pygame.

I haven’t done a RWOPS version yet. Also I have only made it save
images as RGB 24 or RGBA 32 depending on the surface.

RWOPS should be possible after looking at the png manual some more.

Latest version will be in pygame version control in the imageext.c file.
The two functions write_png and SavePNG.

It should be able to be copied out of that file and reused pretty easily.

Perhaps this could go into sdl 1.3 at some point?

Cheers.On 5/22/06, Rene Dudfield <@Rene_Dudfield> wrote:

Thanks. That looks good.

On 5/20/06, Torsten Mohr wrote:

Hi,

i hope i don’t misunderstand, you need some code to save PNG?

I just implemented a screenshot function in a game that is
based on some code that i found on this list some years ago:

/* screenshot handling /
static int ss_do;
static unsigned char
* ss_rows;
static int ss_size;
#define SS_LEN 1024
static char ss_name[SS_LEN];
static int ss_num;
static int ss_w, ss_h;
static unsigned char* ss_data;
static SDL_Surface* ss_surface;
static SDL_Rect ss_rect;

int write_png(char *file_name, png_bytep *rows, int w, int h, int colortype,
int bitdepth) {
png_structp png_ptr;
png_infop info_ptr;
FILE *fp = fopen(file_name, “wb”);
char *doing = “open for writing”;

if (!(fp = fopen(file_name, “wb”))) goto fail;
doing = “create png write struct”;
if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL))) goto fail;
doing = “create png info struct”;
if (!(info_ptr = png_create_info_struct(png_ptr))) goto fail;
if (setjmp(png_jmpbuf(png_ptr))) goto fail;
doing = “init IO”;
png_init_io(png_ptr, fp);
doing = “write header”;
png_set_IHDR(png_ptr, info_ptr, w, h, bitdepth, colortype,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
doing = “write info”;
png_write_info(png_ptr, info_ptr);
doing = “write image”;
png_write_image(png_ptr, rows);
doing = “write end”;
png_write_end(png_ptr, NULL);
return 0;

fail:
printf(“Write_png: could not %s\n”, doing);
return -1;
}

initialisation:
ss_rows = 0;
ss_size = 0;
ss_surface = NULL;

actually make a screenshot:
/* make a screenshot */
if(ss_do && (cstate == CSTATE_GAME)) {
ss_do = 0;
w = skin_inf->w;
ss_w = cfw * w;
ss_h = cfh * w;

xs = (xwidth - ss_w) / 2;
ys = (yheight - ss_h) / 2;

snprintf(ss_name, SS_LEN, "screenshot_%i.png", ss_num);

if(ss_size != 0 || ss_size != ss_h) {
  ss_size = 0;
  free(ss_rows);
}

if(ss_size == 0) {
  ss_size = ss_h;
  ss_rows = (unsigned char**)malloc(sizeof(unsigned char*) * ss_size);
  if(ss_rows == NULL) {
    common_die("could not get memory for screenshot\n");
  }
}

if(ss_surface != NULL) {
  if(ss_surface->w != ss_w || ss_surface->h != ss_h) {
    SDL_FreeSurface(ss_surface);
    ss_surface = NULL;
  }
}

if(ss_surface == NULL) {
  ss_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, ss_w, ss_h, 24,

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff0000, 0xff00, 0xff, 0
#else
0xff, 0xff00, 0xff0000, 0
#endif
);

  if(ss_surface == NULL) {
    common_die("could not get screenshot memory");
  }
}
ss_rect.x = xs;
ss_rect.y = ys;
ss_rect.w = ss_w;
ss_rect.h = ss_h;
SDL_BlitSurface(screen, &ss_rect, ss_surface, NULL);

for(i = 0; i < ss_h; i++) {
  ss_rows[i] = ((unsigned char*)ss_surface->pixels) + i *

ss_surface->pitch;
}

//    printf("ss_w %i   ss_h %i   ss_size %i   ss_num %i\n", ss_w, ss_h,

ss_size, ss_num);

write_png(ss_name, ss_rows, ss_w, ss_h, PNG_COLOR_TYPE_RGB, 8);
ss_num++;

}

There may be some small parts missing.

Best regards,
Torsten.

Yeah, I’ve contacted that person about their save png function too.

They probably get a few emails a month or so asking for it. hehe.

I had a quick look at the example code from libpng. It’s not too
complex. So I think I’ll make a 32bit RGBA surface to png save
function if I can’t find one. Then just use SDL to convert everything
to that format before saving to png.

I haven’t looked at the libjpeg code yet.

On 5/20/06, Christian Walther wrote:

Rene Dudfield wrote:

has anyone written a png, or jpeg saving function?

As for PNG, I think several people have. Search the list archives.

One thread I remember because I tried to contact the author about it
some months ago (without success) was
http://thread.gmane.org/gmane.comp.lib.sdl/14673. It’s about a patch
that adds PNG saving to SDL_image, and even though most people who
responded seemed to agree that such an extension would be useful
(especially considering that it could be enabled or disabled at compile
time, and that libpng is already linked in anyway), no further action
seems to have been taken, and the author never posted his code.

-Christian


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl