From f2aa2836ed910ca3510585a47a8a064b5140e148 Mon Sep 17 00:00:00 2001 From: Ben Morss Date: Wed, 3 Mar 2021 21:35:56 -0500 Subject: AVIF support (#671) Demand for AVIF support on the web is growing, as the word gets out about this new file format which allows higher-quality encoding at smaller sizes. Core contributors to major open-source CMSs are interested in auto-generating AVIF images! They've been simply waiting for support to appear in libgd. This PR aims to meet the growing demand, and to help bring smaller, more beautiful images to more of the web - to sites created by experienced developers and CMS users alike. This PR adds support by incorporating libavif in addition to the existing libheif support. It's generally felt that libavif has more complete support for the AVIF format. libavif is also used by the Chromium project and squoosh.app. In this PR, I've endeavored to incorporate the latest research into best practices for AVIF encoding - not just for default quantizer values, but also an algorithm for determining the number of horizontal tiles, vertical tiles, and threads. Fixes #557. --- examples/avif2jpeg.c | 55 ++++++++++++++++++++++++++++ examples/jpeg2avif.c | 57 +++++++++++++++++++++++++++++ examples/jpeg2avifex.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ examples/png2avif.c | 58 ++++++++++++++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 examples/avif2jpeg.c create mode 100644 examples/jpeg2avif.c create mode 100644 examples/jpeg2avifex.c create mode 100644 examples/png2avif.c (limited to 'examples') diff --git a/examples/avif2jpeg.c b/examples/avif2jpeg.c new file mode 100644 index 0000000..6ced15d --- /dev/null +++ b/examples/avif2jpeg.c @@ -0,0 +1,55 @@ +/** + * A short program which converts a .avif file into a .jpg file - + * just to get a little practice with the basic functionality. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include + +#include "gd.h" + +int main(int argc, char **argv) +{ + gdImagePtr im; + FILE *in, *out; + + if (argc != 3) { + fprintf(stderr, "Usage: avif2jpeg infile.avif outfile.jpg\n"); + exit(1); + } + + printf("Reading infile %s\n", argv[1]); + + in = fopen(argv[1], "rb"); + if (!in) { + fprintf(stderr, "\nError: input file %s does not exist.\n", argv[1]); + exit(1); + } + + im = gdImageCreateFromAvif(in); + fclose(in); + if (!im) { + fprintf(stderr, "\nError: input file %s is not in AVIF format.\n", argv[1]); + exit(1); + } + + out = fopen(argv[2], "wb"); + if (!out) { + fprintf(stderr, "\nError: can't write to output file %s\n", argv[2]); + gdImageDestroy(im); + exit(1); + } + + gdImageJpeg(im, out, 75); + + printf("Wrote outfile %s.\n", argv[2]); + + fclose(out); + gdImageDestroy(im); + + return 0; +} diff --git a/examples/jpeg2avif.c b/examples/jpeg2avif.c new file mode 100644 index 0000000..af55201 --- /dev/null +++ b/examples/jpeg2avif.c @@ -0,0 +1,57 @@ +/** + * 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 +#include + +#include "gd.h" + +int main(int argc, char **argv) +{ + gdImagePtr im; + FILE *in, *out; + + if (argc != 3) { + fprintf(stderr, "Usage: jpeg2avif filename.jpg filename.avif\n"); + exit(1); + } + + printf("Reading infile %s\n", argv[1]); + + in = fopen(argv[1], "rb"); + if (!in) { + fprintf(stderr, "Error: input file %s does not exist.\n", argv[1]); + exit(1); + } + + im = gdImageCreateFromJpeg(in); + fclose(in); + if (!im) { + fprintf(stderr, "Error: input file %s is not in JPEG format.\n", argv[1]); + exit(1); + } + + out = fopen(argv[2], "wb"); + if (!out) { + fprintf(stderr, "Error: can't write to output file %s\n", argv[2]); + gdImageDestroy(im); + exit(1); + } + + fprintf(stderr, "Encoding...\n"); + + gdImageAvif(im, out); + + printf("Wrote outfile %s.\n", argv[2]); + + fclose(out); + gdImageDestroy(im); + + return 0; +} 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 +#include +#include +#ifdef HAVE_UNISTD_H +#include +#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); +} diff --git a/examples/png2avif.c b/examples/png2avif.c new file mode 100644 index 0000000..18463e7 --- /dev/null +++ b/examples/png2avif.c @@ -0,0 +1,58 @@ +/** + * A short program which converts a .png file into a .avif file - + * just to get a little practice with the basic functionality. + * We convert losslessly. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include + +#include "gd.h" + +int main(int argc, char **argv) +{ + gdImagePtr im; + FILE *in, *out; + + if (argc != 3) { + fprintf(stderr, "Usage: png2avif infile.png outfile.avif\n"); + exit(1); + } + + printf("Reading infile %s\n", argv[1]); + + in = fopen(argv[1], "rb"); + if (!in) { + fprintf(stderr, "Error: input file %s does not exist.\n", argv[1]); + exit(1); + } + + im = gdImageCreateFromPng(in); + fclose(in); + if (!im) { + fprintf(stderr, "Error: input file %s is not in PNG format.\n", argv[1]); + exit(1); + } + + out = fopen(argv[2], "wb"); + if (!out) { + fprintf(stderr, "Error: can't write to output file %s\n", argv[2]); + gdImageDestroy(im); + exit(1); + } + + fprintf(stderr, "Encoding...\n"); + + gdImageAvifEx(im, out, 100, 0); + + printf("Wrote outfile %s.\n", argv[2]); + + fclose(out); + gdImageDestroy(im); + + return 0; +} -- cgit v1.2.1