avif: fix memory corruption when saving Grayscale images

Some Grayscale images with certain color profiles triggered
problems during saving with lossy compression,
16-bit depth pixel values were written into buffer allocated
for 8-bit depth.
This commit is contained in:
Daniel Novomeský
2026-11-12 19:06:14 +01:00
parent 53366e32ec
commit 2e36a53947
+40 -27
View File
@@ -732,61 +732,53 @@ bool QAVIFHandler::write(const QImage &image)
} }
QImage tmpgrayimage = image.convertToFormat(tmpformat); QImage tmpgrayimage = image.convertToFormat(tmpformat);
avif = avifImageCreate(tmpgrayimage.width(), tmpgrayimage.height(), save_depth, AVIF_PIXEL_FORMAT_YUV400); avifMatrixCoefficients gray_matrix_to_save = (avifMatrixCoefficients)2; // unspecified;
res = avifImageAllocatePlanes(avif, AVIF_PLANES_YUV); avifColorPrimaries gray_primaries_to_save = (avifColorPrimaries)2;
if (res != AVIF_RESULT_OK) { avifTransferCharacteristics gray_transfer_to_save = (avifTransferCharacteristics)2;
qCWarning(LOG_AVIFPLUGIN, "ERROR in avifImageAllocatePlanes: %s", avifResultToString(res)); QByteArray iccprofile_gray;
return false;
}
// set EXIF and XMP metadata
setMetadata(avif, tmpgrayimage);
if (tmpgrayimage.colorSpace().isValid()) { if (tmpgrayimage.colorSpace().isValid()) {
avif->colorPrimaries = (avifColorPrimaries)1; gray_primaries_to_save = (avifColorPrimaries)1;
avif->matrixCoefficients = (avifMatrixCoefficients)1; gray_matrix_to_save = (avifMatrixCoefficients)1;
switch (tmpgrayimage.colorSpace().transferFunction()) { switch (tmpgrayimage.colorSpace().transferFunction()) {
case QColorSpace::TransferFunction::Linear: case QColorSpace::TransferFunction::Linear:
/* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */ /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
avif->transferCharacteristics = (avifTransferCharacteristics)8; gray_transfer_to_save = (avifTransferCharacteristics)8;
break; break;
case QColorSpace::TransferFunction::Gamma: case QColorSpace::TransferFunction::Gamma:
if (qAbs(tmpgrayimage.colorSpace().gamma() - 2.2f) < 0.1f) { if (qAbs(tmpgrayimage.colorSpace().gamma() - 2.2f) < 0.1f) {
/* AVIF_TRANSFER_CHARACTERISTICS_BT470M */ /* AVIF_TRANSFER_CHARACTERISTICS_BT470M */
avif->transferCharacteristics = (avifTransferCharacteristics)4; gray_transfer_to_save = (avifTransferCharacteristics)4;
} else if (qAbs(tmpgrayimage.colorSpace().gamma() - 2.8f) < 0.1f) { } else if (qAbs(tmpgrayimage.colorSpace().gamma() - 2.8f) < 0.1f) {
/* AVIF_TRANSFER_CHARACTERISTICS_BT470BG */ /* AVIF_TRANSFER_CHARACTERISTICS_BT470BG */
avif->transferCharacteristics = (avifTransferCharacteristics)5; gray_transfer_to_save = (avifTransferCharacteristics)5;
} }
break; break;
case QColorSpace::TransferFunction::SRgb: case QColorSpace::TransferFunction::SRgb:
/* AVIF_TRANSFER_CHARACTERISTICS_SRGB */ /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
avif->transferCharacteristics = (avifTransferCharacteristics)13; gray_transfer_to_save = (avifTransferCharacteristics)13;
break; break;
case QColorSpace::TransferFunction::St2084: case QColorSpace::TransferFunction::St2084:
avif->transferCharacteristics = (avifTransferCharacteristics)16; gray_transfer_to_save = (avifTransferCharacteristics)16;
break; break;
case QColorSpace::TransferFunction::Hlg: case QColorSpace::TransferFunction::Hlg:
avif->transferCharacteristics = (avifTransferCharacteristics)18; gray_transfer_to_save = (avifTransferCharacteristics)18;
break; break;
default: default:
/* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */ /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
break; break;
} }
if (avif->transferCharacteristics == 2) { // in case TransferFunction was not identified yet if (gray_transfer_to_save == 2) { // in case TransferFunction was not identified yet
if (tmpgrayimage.colorSpace().colorModel() == QColorSpace::ColorModel::Gray && lossless) { if (tmpgrayimage.colorSpace().colorModel() == QColorSpace::ColorModel::Gray && lossless) {
avif->colorPrimaries = (avifColorPrimaries)2; gray_primaries_to_save = (avifColorPrimaries)2;
avif->matrixCoefficients = (avifMatrixCoefficients)6; gray_matrix_to_save = (avifMatrixCoefficients)6;
QByteArray iccprofile_gray = tmpgrayimage.colorSpace().iccProfile(); iccprofile_gray = tmpgrayimage.colorSpace().iccProfile();
if (iccprofile_gray.size() > 0) { if (iccprofile_gray.isEmpty()) {
res = avifImageSetProfileICC(avif, reinterpret_cast<const uint8_t *>(iccprofile_gray.constData()), iccprofile_gray.size()); qCWarning(LOG_AVIFPLUGIN, "Unable to save Gray ICC profile");
if (res != AVIF_RESULT_OK) {
qCWarning(LOG_AVIFPLUGIN, "ERROR in avifImageSetProfileICC: %s", avifResultToString(res));
return false;
}
} }
} else { // convert to grayscale with SRgb } else { // convert to grayscale with SRgb
tmpgrayimage.convertToColorSpace(QColorSpace(QPointF(0.3127f, 0.329f), QColorSpace::TransferFunction::SRgb), QImage::Format_Grayscale16); tmpgrayimage.convertToColorSpace(QColorSpace(QPointF(0.3127f, 0.329f), QColorSpace::TransferFunction::SRgb), QImage::Format_Grayscale16);
@@ -796,7 +788,7 @@ bool QAVIFHandler::write(const QImage &image)
break; break;
case QImage::Format_Grayscale16: case QImage::Format_Grayscale16:
save_depth = 10; save_depth = 10;
avif->transferCharacteristics = (avifTransferCharacteristics)13; gray_transfer_to_save = (avifTransferCharacteristics)13;
break; break;
default: default:
qCWarning(LOG_AVIFPLUGIN, "Error saving Gray image"); qCWarning(LOG_AVIFPLUGIN, "Error saving Gray image");
@@ -807,6 +799,27 @@ bool QAVIFHandler::write(const QImage &image)
} }
} }
avif = avifImageCreate(tmpgrayimage.width(), tmpgrayimage.height(), save_depth, AVIF_PIXEL_FORMAT_YUV400);
avif->matrixCoefficients = gray_matrix_to_save;
avif->colorPrimaries = gray_primaries_to_save;
avif->transferCharacteristics = gray_transfer_to_save;
res = avifImageAllocatePlanes(avif, AVIF_PLANES_YUV);
if (res != AVIF_RESULT_OK) {
qCWarning(LOG_AVIFPLUGIN, "ERROR in avifImageAllocatePlanes: %s", avifResultToString(res));
return false;
}
// set EXIF and XMP metadata
setMetadata(avif, tmpgrayimage);
if (iccprofile_gray.size() > 0) {
res = avifImageSetProfileICC(avif, reinterpret_cast<const uint8_t *>(iccprofile_gray.constData()), iccprofile_gray.size());
if (res != AVIF_RESULT_OK) {
qCWarning(LOG_AVIFPLUGIN, "ERROR in avifImageSetProfileICC: %s", avifResultToString(res));
return false;
}
}
if (save_depth > 8) { // QImage::Format_Grayscale16 if (save_depth > 8) { // QImage::Format_Grayscale16
for (int y = 0; y < tmpgrayimage.height(); y++) { for (int y = 0; y < tmpgrayimage.height(); y++) {
const uint16_t *src16bit = reinterpret_cast<const uint16_t *>(tmpgrayimage.constScanLine(y)); const uint16_t *src16bit = reinterpret_cast<const uint16_t *>(tmpgrayimage.constScanLine(y));