Debian Codegraph @

examples/jpeg2avifex.c

raw
1 /**
2 * A short program which converts a .jpg file into a .avif file -
3 * just to get a little practice with the basic functionality.
4 */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16
17 #include "gd.h"
18
19 static void usage() {
20 fprintf(stderr, "Usage: jpeg2avifex [-q quality] [-s speed] infile.jpg outfile.avif\n");
21 exit(1);
22 }
23
24 int main(int argc, char **argv)
25 {
26 gdImagePtr im;
27 FILE *in, *out;
28 int c;
29 int speed = -1, quality = -1; // use default values if unspecified
30 char *infile, *outfile;
31 int failed = 0;
32
33 if (argc < 3) {
34 usage();
35 }
36
37 while ((c = getopt(argc, argv, "q:s:")) != -1) {
38 switch (c) {
39 case 'q':
40 quality = atoi(optarg);
41 break;
42
43 case 's':
44 speed = atoi(optarg);
45 break;
46
47 default:
48 usage();
49 }
50 }
51
52 if (optind > argc - 2)
53 usage();
54
55 infile = strdup(argv[optind++]);
56 outfile = strdup(argv[optind]);
57
58 printf("Reading infile %s\n", infile);
59
60 in = fopen(infile, "rb");
61 if (!in) {
62 fprintf(stderr, "\nError: input file %s does not exist.\n", infile);
63 failed = 1;
64 goto cleanup;
65 }
66
67 im = gdImageCreateFromJpeg(in);
68 fclose(in);
69 if (!im) {
70 fprintf(stderr, "\nError: input file %s is not in JPEG format.\n", infile);
71 failed = 1;
72 goto cleanup;
73 }
74
75 out = fopen(outfile, "wb");
76 if (!out) {
77 fprintf(stderr, "\nError: can't write to output file %s\n", outfile);
78 failed = 1;
79 goto cleanup;
80 }
81
82 fprintf(stderr, "Encoding...\n");
83
84 gdImageAvifEx(im, out, quality, speed);
85
86 printf("Wrote outfile %s.\n", outfile);
87
88 fclose(out);
89
90 cleanup:
91 if (im)
92 gdImageDestroy(im);
93
94 gdFree(infile);
95 gdFree(outfile);
96
97 exit(failed);
98 }