diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/avif2jpeg.c | 55 | ||||
-rw-r--r-- | examples/jpeg2avif.c | 57 | ||||
-rw-r--r-- | examples/jpeg2avifex.c | 98 | ||||
-rw-r--r-- | examples/png2avif.c | 58 |
4 files changed, 268 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> + +#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 <stdio.h> +#include <stdlib.h> + +#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 <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); +} 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 <stdio.h> +#include <stdlib.h> + +#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; +} |