Formatting cleanup.
[frameshot.git] / output.c
blob6abd0c880a4619e519dfbdaaf507b6ebbc219812
2 #include <stdint.h>
3 #include <malloc.h>
4 #include <libavutil/avutil.h>
5 #include <libswscale/swscale.h>
6 #include <png.h>
8 #include "common.h"
9 #include "output.h"
11 typedef struct {
12 FILE *fh;
13 png_structp p_png;
14 png_infop p_info;
15 } png_output_t;
17 int open_file_png(char *psz_filename, hnd_t *p_handle, int i_compression )
19 png_output_t *h = NULL;
20 if( (h = calloc(1, sizeof(*h))) == NULL )
21 return -1;
23 if( !strcmp(psz_filename, "-") )
24 h->fh = stdout;
25 else if( (h->fh = fopen(psz_filename, "wb")) == NULL )
27 goto error;
30 if( (h->p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL )) == NULL )
32 goto error;
35 if( (h->p_info = png_create_info_struct(h->p_png)) == NULL )
37 png_destroy_write_struct(&(h->p_png), (png_infopp)NULL);
38 goto error;
41 png_init_io( h->p_png, h->fh );
43 png_set_compression_level( h->p_png, i_compression );
45 *p_handle = h;
47 return 0;
49 error:
50 if( h->fh != NULL && h->fh != stdout )
51 fclose( h->fh );
52 free(h);
54 return -1;
57 int close_file_png( hnd_t handle )
59 int ret = 0;
60 png_output_t *h = handle;
62 png_destroy_write_struct( &(h->p_png), &(h->p_info) );
64 if ((h->fh == NULL) || (h->fh == stdout))
65 return ret;
67 ret = fclose(h->fh);
69 free(h);
71 return ret;
74 int write_image_png( hnd_t handle, picture_t *p_pic, config_t *p_config )
76 png_output_t *h = handle;
77 uint8_t *out_data = malloc(p_config->i_width * p_config->i_height * 4);
78 uint8_t **pp_rows = calloc(p_config->i_height, sizeof(*pp_rows));
79 picture_t pic_out;
80 struct SwsContext *p_sws;
81 int i;
83 pic_out.img.plane[0] = out_data;
84 pic_out.img.plane[1] = pic_out.img.plane[2] = pic_out.img.plane[3] = NULL;
85 pic_out.img.i_stride[0] = 3*p_config->i_width;
86 pic_out.img.i_stride[1] = pic_out.img.i_stride[2] = pic_out.img.i_stride[3] = 0;
88 p_sws = sws_getContext( p_config->i_width, p_config->i_height, PIX_FMT_YUV420P,
89 p_config->i_width, p_config->i_height, PIX_FMT_RGB24,
90 SWS_FAST_BILINEAR | SWS_ACCURATE_RND,
91 NULL, NULL, NULL );
93 sws_scale(p_sws, p_pic->img.plane, p_pic->img.i_stride, 0, p_config->i_height, pic_out.img.plane, pic_out.img.i_stride );
95 __asm__ volatile ("emms\n\t");
98 png_set_IHDR( h->p_png, h->p_info, p_config->i_width, p_config->i_height,
99 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
100 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
102 for(i=0; i < p_config->i_height; i++)
103 pp_rows[i] = pic_out.img.plane[0] + i * pic_out.img.i_stride[0];
105 png_set_rows( h->p_png, h->p_info, pp_rows );
107 png_write_png( h->p_png, h->p_info, 0, NULL );
109 free(pp_rows);
110 free(out_data);
112 return 0;