Skip to content

Check libavif API return values, if available #16434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion ext/gd/libgd/gd_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,13 @@ gdImagePtr gdImageCreateFromAvifCtx (gdIOCtx *ctx)
// (While AVIF image pixel depth can be 8, 10, or 12 bits, GD truecolor images are 8-bit.)
avifRGBImageSetDefaults(&rgb, decoder->image);
rgb.depth = 8;
#if AVIF_VERSION >= 1000000
result = avifRGBImageAllocatePixels(&rgb);
if (isAvifError(result, "Allocating RGB pixels failed"))
goto cleanup;
#else
avifRGBImageAllocatePixels(&rgb);
#endif

result = avifImageYUVToRGB(decoder->image, &rgb);
if (isAvifError(result, "Conversion from YUV to RGB failed"))
Expand Down Expand Up @@ -522,14 +528,25 @@ void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed)
// Note that MATRIX_COEFFICIENTS_IDENTITY enables lossless conversion from RGB to YUV.

avifImage *avifIm = avifImageCreate(gdImageSX(im), gdImageSY(im), 8, subsampling);

#if AVIF_VERSION >= 1000000
if (avifIm == NULL) {
gd_error("avif error - Creating image failed\n");
goto cleanup;
}
#endif
avifIm->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709;
avifIm->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB;
avifIm->matrixCoefficients = lossless ? AVIF_MATRIX_COEFFICIENTS_IDENTITY : AVIF_MATRIX_COEFFICIENTS_BT709;

avifRGBImageSetDefaults(&rgb, avifIm);
// this allocates memory, and sets rgb.rowBytes and rgb.pixels.
#if AVIF_VERSION >= 1000000
result = avifRGBImageAllocatePixels(&rgb);
if (isAvifError(result, "Allocating RGB pixels failed"))
goto cleanup;
#else
avifRGBImageAllocatePixels(&rgb);
#endif

// Parse RGB data from the GD image, and copy it into the AVIF RGB image.
// Convert 7-bit GD alpha channel values to 8-bit AVIF values.
Expand All @@ -555,6 +572,12 @@ void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed)
// Encode the image in AVIF format.

encoder = avifEncoderCreate();
#if AVIF_VERSION >= 1000000
if (encoder == NULL) {
gd_error("avif error - Creating encoder failed\n");
goto cleanup;
}
#endif
int quantizerQuality = quality == QUALITY_DEFAULT ?
QUANTIZER_DEFAULT : quality2Quantizer(quality);

Expand Down