Update .gitignore for CMake
[frameshot.git] / output.c
blob6f211d5d929a8bb7f9c4167c8eff43bf969d4d46
2 #include <stdint.h>
3 #include <malloc.h>
4 #include <png.h>
6 #include "common.h"
7 #include "output.h"
9 typedef struct {
10 FILE *fh;
11 png_structp p_png;
12 png_infop p_info;
13 } png_output_t;
15 int open_file_png(char *psz_filename, hnd_t *p_handle, int i_compression )
17 png_output_t *h = NULL;
18 if( (h = calloc(1, sizeof(*h))) == NULL )
19 return -1;
21 if( !strcmp(psz_filename, "-") )
22 h->fh = stdout;
23 else if( (h->fh = fopen(psz_filename, "wb")) == NULL )
25 goto error;
28 if( (h->p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL )) == NULL )
30 goto error;
33 if( (h->p_info = png_create_info_struct(h->p_png)) == NULL )
35 png_destroy_write_struct(&(h->p_png), (png_infopp)NULL);
36 goto error;
39 png_init_io( h->p_png, h->fh );
41 png_set_compression_level( h->p_png, i_compression );
43 *p_handle = h;
45 return 0;
47 error:
48 if( h->fh != NULL && h->fh != stdout )
49 fclose( h->fh );
50 free(h);
52 return -1;
55 int close_file_png( hnd_t handle )
57 int ret = 0;
58 png_output_t *h = handle;
60 png_destroy_write_struct( &(h->p_png), &(h->p_info) );
62 if ((h->fh == NULL) || (h->fh == stdout))
63 return ret;
65 ret = fclose(h->fh);
67 free(h);
69 return ret;