summaryrefslogtreecommitdiff
path: root/src/frame_to_pgm.c
blob: b8789682a28aa2fe99f500915edba6db7a66bde0 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "video_decode.h"

static void
pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
{
	FILE *f;
	int i;

	f = fopen(filename, "w");
	fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
	for (i=0; i < ysize; i++)
		fwrite(buf + i * wrap, 1, xsize, f);
	fclose(f);
}

int
main(int argc, char **argv)
{
	struct video_decode *vd;
	struct video_frame *frame = NULL;
	int ret;
	int64_t timestamp = 0;

	if (argc < 3)
		return 1;

	if (argc == 4 && strtol(argv[3], NULL, 10) > 0)
		timestamp = strtol(argv[3], NULL, 10);

	ret = video_decode_init(&vd, argv[1], timestamp);
	if (ret < 0)
		return -ret;

	ret = video_decode_get_frame(vd, &frame);
	if (ret < 1)
		return -ret;

	pgm_save(frame->data, frame->stride, frame->width, frame->height,
		 argv[2]);

	video_decode_free_frame(&frame);
	video_decode_uninit(&vd);
}