From f221ac04c5ab6ca24648321fdb1c58107262758d Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 19 Dec 2023 12:44:06 +0100 Subject: [PATCH] QPixelLayout: remove explicit array sizes For qPixelLayouts, there's a static_assert right below it that checks that the array has a specific size, as a guard for extending QImage::NImageFormats without also extending the array. Giving an explicit size to qPixelLayouts defeats the entire purpose. For the other arrays of function pointers, drop the sizing and add the missing static_asserts. Use std::size as a drive-by. This work has been kindly sponsored by the QGIS project (https://qgis.org/). Change-Id: Ic791a706a8ae964e3aee482f23b7eeeedf97bdc6 Reviewed-by: Qt CI Bot Reviewed-by: Allan Sandfeld Jensen --- src/gui/painting/qpixellayout.cpp | 21 +++++++++++++++------ src/gui/painting/qpixellayout_p.h | 8 ++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/gui/painting/qpixellayout.cpp b/src/gui/painting/qpixellayout.cpp index 9e08bbd15aa..e6dc774b2dc 100644 --- a/src/gui/painting/qpixellayout.cpp +++ b/src/gui/painting/qpixellayout.cpp @@ -1661,7 +1661,7 @@ static const QRgba64 *QT_FASTCALL fetchRGBA32FPMToRGBA64PM(QRgba64 *buffer, cons // convertToArgb32() assumes that no color channel is less than 4 bits. // storeRGBFromARGB32PM() assumes that no color channel is more than 8 bits. // QImage::rgbSwapped() assumes that the red and blue color channels have the same number of bits. -QPixelLayout qPixelLayouts[QImage::NImageFormats] = { +QPixelLayout qPixelLayouts[] = { { false, false, QPixelLayout::BPPNone, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }, // Format_Invalid { false, false, QPixelLayout::BPP1MSB, nullptr, convertIndexedToARGB32PM, convertIndexedTo, @@ -1781,7 +1781,7 @@ QPixelLayout qPixelLayouts[QImage::NImageFormats] = { storeRGB32FFromRGB32, storeRGB32FFromRGB32 }, // Format_RGBA32FPx4_Premultiplied }; -static_assert(sizeof(qPixelLayouts) / sizeof(*qPixelLayouts) == QImage::NImageFormats); +static_assert(std::size(qPixelLayouts) == QImage::NImageFormats); static void QT_FASTCALL convertFromRgb64(uint *dest, const QRgba64 *src, int length) { @@ -1916,7 +1916,7 @@ static void QT_FASTCALL storeRGBA32FPMFromRGBA64PM(uchar *dest, const QRgba64 *s d[i] = qConvertRgb64ToRgbaF32(src[i]); } -ConvertAndStorePixelsFunc64 qStoreFromRGBA64PM[QImage::NImageFormats] = { +ConvertAndStorePixelsFunc64 qStoreFromRGBA64PM[] = { nullptr, nullptr, nullptr, @@ -1955,6 +1955,8 @@ ConvertAndStorePixelsFunc64 qStoreFromRGBA64PM[QImage::NImageFormats] = { storeRGBA32FPMFromRGBA64PM, }; +static_assert(std::size(qStoreFromRGBA64PM) == QImage::NImageFormats); + #if QT_CONFIG(raster_fp) static void QT_FASTCALL convertToRgbaF32(QRgbaFloat32 *dest, const uint *src, int length) { @@ -2000,7 +2002,7 @@ static const QRgbaFloat32 * QT_FASTCALL convertRGB30ToRGBA32F(QRgbaFloat32 *buff return buffer; } -ConvertToFPFunc qConvertToRGBA32F[QImage::NImageFormats] = { +ConvertToFPFunc qConvertToRGBA32F[] = { nullptr, convertIndexedTo, convertIndexedTo, @@ -2039,6 +2041,8 @@ ConvertToFPFunc qConvertToRGBA32F[QImage::NImageFormats] = { nullptr, }; +static_assert(std::size(qConvertToRGBA32F) == QImage::NImageFormats); + static const QRgbaFloat32 *QT_FASTCALL fetchRGBX64ToRGBA32F(QRgbaFloat32 *buffer, const uchar *src, int index, int count, const QList *, QDitherInfo *) { @@ -2103,7 +2107,7 @@ static const QRgbaFloat32 *QT_FASTCALL fetchRGBA32F(QRgbaFloat32 *, const uchar return s; } -FetchAndConvertPixelsFuncFP qFetchToRGBA32F[QImage::NImageFormats] = { +FetchAndConvertPixelsFuncFP qFetchToRGBA32F[] = { nullptr, fetchIndexedToRGBA32F, fetchIndexedToRGBA32F, @@ -2142,6 +2146,8 @@ FetchAndConvertPixelsFuncFP qFetchToRGBA32F[QImage::NImageFormats] = { fetchRGBA32F, }; +static_assert(std::size(qFetchToRGBA32F) == QImage::NImageFormats); + static void QT_FASTCALL convertFromRgba32f(uint *dest, const QRgbaFloat32 *src, int length) { for (int i = 0; i < length; ++i) @@ -2278,7 +2284,7 @@ static void QT_FASTCALL storeRGBA32FPMFromRGBA32F(uchar *dest, const QRgbaFloat3 } } -ConvertAndStorePixelsFuncFP qStoreFromRGBA32F[QImage::NImageFormats] = { +ConvertAndStorePixelsFuncFP qStoreFromRGBA32F[] = { nullptr, nullptr, nullptr, @@ -2316,6 +2322,9 @@ ConvertAndStorePixelsFuncFP qStoreFromRGBA32F[QImage::NImageFormats] = { storeRGBA32FFromRGBA32F, storeRGBA32FPMFromRGBA32F, }; + +static_assert(std::size(qStoreFromRGBA32F) == QImage::NImageFormats); + #endif // QT_CONFIG(raster_fp) QT_END_NAMESPACE diff --git a/src/gui/painting/qpixellayout_p.h b/src/gui/painting/qpixellayout_p.h index 45f6c15365a..14f19f4e742 100644 --- a/src/gui/painting/qpixellayout_p.h +++ b/src/gui/painting/qpixellayout_p.h @@ -321,12 +321,12 @@ struct QPixelLayout extern ConvertAndStorePixelsFunc64 qStoreFromRGBA64PM[QImage::NImageFormats]; #if QT_CONFIG(raster_fp) -extern ConvertToFPFunc qConvertToRGBA32F[QImage::NImageFormats]; -extern FetchAndConvertPixelsFuncFP qFetchToRGBA32F[QImage::NImageFormats]; -extern ConvertAndStorePixelsFuncFP qStoreFromRGBA32F[QImage::NImageFormats]; +extern ConvertToFPFunc qConvertToRGBA32F[]; +extern FetchAndConvertPixelsFuncFP qFetchToRGBA32F[]; +extern ConvertAndStorePixelsFuncFP qStoreFromRGBA32F[]; #endif -extern QPixelLayout qPixelLayouts[QImage::NImageFormats]; +extern QPixelLayout qPixelLayouts[]; extern MemRotateFunc qMemRotateFunctions[QPixelLayout::BPPCount][3];