avif: lossless support

This commit is contained in:
Daniel Novomeský 2022-04-05 22:47:55 +02:00
parent 384f78a13c
commit 84d56d00cf
2 changed files with 47 additions and 36 deletions

View File

@ -86,8 +86,7 @@ if (TARGET avif)
kimageformats_read_tests(
avif
)
# because the plug-ins use RGB->YUV conversion which sometimes results in 1 value difference.
kimageformats_write_tests(FUZZ 1
kimageformats_write_tests(
avif-nodatacheck-lossless
)
endif()

View File

@ -452,8 +452,13 @@ bool QAVIFHandler::write(const QImage &image)
return false;
}
if (m_quality >= 100 && !avifCodecName(AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_FLAG_CAN_ENCODE)) {
qWarning("You are using %s encoder. It is recommended to enable libAOM encoder in libavif for better near-lossless compression.", encoder_name);
bool lossless = false;
if (m_quality >= 100) {
if (avifCodecName(AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_FLAG_CAN_ENCODE)) {
lossless = true;
} else {
qWarning("You are using %s encoder. It is recommended to enable libAOM encoder in libavif to use lossless compression.", encoder_name);
}
}
int maxQuantizer = AVIF_QUANTIZER_WORST_QUALITY * (100 - qBound(0, m_quality, 100)) / 100;
@ -648,6 +653,9 @@ bool QAVIFHandler::write(const QImage &image)
// in case primaries or trc were not identified
if ((primaries_to_save == 2) || (transfer_to_save == 2)) {
if (lossless) {
iccprofile = tmpcolorimage.colorSpace().iccProfile();
} else {
// upgrade image to higher bit depth
if (save_depth == 8) {
save_depth = 10;
@ -687,6 +695,7 @@ bool QAVIFHandler::write(const QImage &image)
tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::SRgb));
}
}
}
} else { // profile is unsupported by Qt
iccprofile = tmpcolorimage.colorSpace().iccProfile();
if (iccprofile.size() > 0) {
@ -694,6 +703,9 @@ bool QAVIFHandler::write(const QImage &image)
}
}
if (lossless && pixel_format == AVIF_PIXEL_FORMAT_YUV444) {
matrix_to_save = (avifMatrixCoefficients)0;
}
avif = avifImageCreate(tmpcolorimage.width(), tmpcolorimage.height(), save_depth, pixel_format);
avif->matrixCoefficients = matrix_to_save;