-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput.c
130 lines (100 loc) · 3.68 KB
/
output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*****************************************************************************
* output.c: output drivers.
*****************************************************************************
* Copyright (C) 2009
*
* Authors: Nathan Caldwell <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include <stdint.h>
#include <malloc.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <png.h>
#include "common.h"
#include "output.h"
typedef struct {
FILE *fp;
png_structp png;
png_infop info;
} png_output_t;
int open_file_png(char *filename, handle_t *handle, int compression)
{
png_output_t *h = NULL;
if ((h = calloc(1, sizeof(*h))) == NULL)
return -1;
if (!strcmp(filename, "-"))
h->fp = stdout;
else if ((h->fp = fopen(filename, "wb")) == NULL) {
goto error;
}
if ((h->png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) {
goto error;
}
if ((h->info = png_create_info_struct(h->png)) == NULL) {
png_destroy_write_struct(&(h->png), (png_infopp) NULL);
goto error;
}
png_init_io(h->png, h->fp);
png_set_compression_level(h->png, compression);
*handle = h;
return 0;
error:
if (h->fp != NULL && h->fp != stdout)
fclose(h->fp);
free(h);
return -1;
}
int close_file_png(handle_t handle)
{
int ret = 0;
png_output_t *h = handle;
png_destroy_write_struct(&(h->png), &(h->info));
if ((h->fp == NULL) || (h->fp == stdout))
return ret;
ret = fclose(h->fp);
free(h);
return ret;
}
int write_image_png(handle_t handle, picture_t *pic, config_t *config)
{
png_output_t *h = handle;
uint8_t *out_data = malloc(config->width * config->height * 4);
uint8_t **rows = calloc(config->height, sizeof(*rows));
picture_t pic_out;
struct SwsContext *sws_ctx;
int i;
pic_out.img.plane[0] = out_data;
pic_out.img.plane[1] = pic_out.img.plane[2] = pic_out.img.plane[3] = NULL;
pic_out.img.stride[0] = 3 * config->width;
pic_out.img.stride[1] = pic_out.img.stride[2] = pic_out.img.stride[3] = 0;
sws_ctx = sws_getContext(config->width, config->height, PIX_FMT_YUV420P,
config->width, config->height, PIX_FMT_RGB24,
SWS_FAST_BILINEAR | SWS_ACCURATE_RND,
NULL, NULL, NULL);
sws_scale(sws_ctx, pic->img.plane, pic->img.stride, 0, config->height, pic_out.img.plane, pic_out.img.stride);
__asm__ volatile ("emms\n\t");
png_set_IHDR(h->png, h->info, config->width, config->height,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
for (i = 0; i < config->height; i++)
rows[i] = pic_out.img.plane[0] + i * pic_out.img.stride[0];
png_set_rows(h->png, h->info, rows);
png_write_png(h->png, h->info, 0, NULL);
free(rows);
free(out_data);
return 0;
}