summaryrefslogtreecommitdiff
path: root/examples/jpeg2avifex.c
blob: a8afb3b20492f9fc59d146ea25b853430ecd59f1 (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
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
/**
 * A short program which converts a .jpg file into a .avif file -
 * just to get a little practice with the basic functionality.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gd.h"

static void usage() {
	fprintf(stderr, "Usage: jpeg2avifex [-q quality] [-s speed] infile.jpg outfile.avif\n");
	exit(1);
}

int main(int argc, char **argv)
{
	gdImagePtr im;
	FILE *in, *out;
	int c;
	int speed = -1, quality = -1; // use default values if unspecified
	char *infile, *outfile;
	int failed = 0;

	if (argc < 3) {
		usage();
	}

	while ((c = getopt(argc, argv, "q:s:")) != -1) {
		switch (c) {
			case 'q':
				quality = atoi(optarg);
				break;

			case 's':
				speed = atoi(optarg);
				break;

			default:
				usage();
		}
	}

	if (optind > argc - 2)
		usage();

	infile = strdup(argv[optind++]);
	outfile = strdup(argv[optind]);

	printf("Reading infile %s\n", infile);

	in = fopen(infile, "rb");
	if (!in) {
		fprintf(stderr, "\nError: input file %s does not exist.\n", infile);
		failed = 1;
		goto cleanup;
	}

	im = gdImageCreateFromJpeg(in);
	fclose(in);
	if (!im) {
		fprintf(stderr, "\nError: input file %s is not in JPEG format.\n", infile);
		failed = 1;
		goto cleanup;
	}

	out = fopen(outfile, "wb");
	if (!out) {
		fprintf(stderr, "\nError: can't write to output file %s\n", outfile);
		failed = 1;
		goto cleanup;
	}

	fprintf(stderr, "Encoding...\n");

	gdImageAvifEx(im, out, quality, speed);

	printf("Wrote outfile %s.\n", outfile);

	fclose(out);

cleanup:
	if (im)
		gdImageDestroy(im);

	gdFree(infile);
	gdFree(outfile);

	exit(failed);
}