diff options
Diffstat (limited to 'examples/jpeg2avifex.c')
-rw-r--r-- | examples/jpeg2avifex.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/examples/jpeg2avifex.c b/examples/jpeg2avifex.c new file mode 100644 index 0000000..a8afb3b --- /dev/null +++ b/examples/jpeg2avifex.c @@ -0,0 +1,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); +} |