include/vcl/BitmapPalette.hxx | 11 +++++++ vcl/source/bitmap/bitmap.cxx | 51 ++++++++++++------------------------ vcl/source/bitmap/bitmappalette.cxx | 9 ++++++ 3 files changed, 37 insertions(+), 34 deletions(-)
New commits: commit 1987aa7b597650930e32c274240fdec68617d903 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Fri Jul 22 17:11:44 2022 +0300 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Sat Jul 23 07:40:30 2022 +0200 Simplify greyscale palette initialization further Change-Id: Iaab6a493e0a0c329f6b6e229f0ce0cdabb188112 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137361 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/include/vcl/BitmapPalette.hxx b/include/vcl/BitmapPalette.hxx index d470dfbb5521..4f20970e15ec 100644 --- a/include/vcl/BitmapPalette.hxx +++ b/include/vcl/BitmapPalette.hxx @@ -24,6 +24,8 @@ #include <vcl/checksum.hxx> #include <o3tl/cow_wrapper.hxx> +#include <array> + class ImplBitmapPalette; class VCL_DLLPUBLIC BitmapPalette @@ -43,6 +45,7 @@ public: BitmapPalette( const BitmapPalette& ); BitmapPalette( BitmapPalette&& ) noexcept; BitmapPalette(std::initializer_list<BitmapColor> aBitmapColor); + template <size_t N> BitmapPalette(const std::array<BitmapColor, N>& colors); explicit BitmapPalette(sal_uInt16 nCount); ~BitmapPalette(); @@ -72,7 +75,15 @@ public: typedef o3tl::cow_wrapper< ImplBitmapPalette > ImplType; private: + BitmapPalette(const BitmapColor* first, const BitmapColor* last); + ImplType mpImpl; }; +template <size_t N> +BitmapPalette::BitmapPalette(const std::array<BitmapColor, N>& colors) + : BitmapPalette(colors.data(), colors.data() + N) +{ +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/bitmap/bitmap.cxx b/vcl/source/bitmap/bitmap.cxx index f96bad8cb0f9..2a9f949ac1fe 100644 --- a/vcl/source/bitmap/bitmap.cxx +++ b/vcl/source/bitmap/bitmap.cxx @@ -169,6 +169,19 @@ Bitmap::~Bitmap() #endif } +namespace +{ +template <size_t N> +constexpr std::enable_if_t<255 % (N - 1) == 0, std::array<BitmapColor, N>> getGreyscalePalette() +{ + const int step = 255 / (N - 1); + std::array<BitmapColor, N> a; + for (size_t i = 0; i < N; ++i) + a[i] = BitmapColor(i * step, i * step, i * step); + return a; +} +} + const BitmapPalette& Bitmap::GetGreyPalette( int nEntries ) { // Create greyscale palette with 2, 4, 16 or 256 entries @@ -176,52 +189,22 @@ const BitmapPalette& Bitmap::GetGreyPalette( int nEntries ) { case 2: { - static const BitmapPalette aGreyPalette2 = { - BitmapColor(0, 0, 0), - BitmapColor(255, 255, 255), - }; - + static const BitmapPalette aGreyPalette2 = getGreyscalePalette<2>(); return aGreyPalette2; } case 4: { - static const BitmapPalette aGreyPalette4 = { - BitmapColor(0, 0, 0), - BitmapColor(85, 85, 85), - BitmapColor(170, 170, 170), - BitmapColor(255, 255, 255), - }; - + static const BitmapPalette aGreyPalette4 = getGreyscalePalette<4>(); return aGreyPalette4; } case 16: { - static const BitmapPalette aGreyPalette16 = [] { - sal_uInt8 cGrey = 0; - sal_uInt8 const cGreyInc = 17; - - BitmapPalette aPalette(16); - - for (sal_uInt16 i = 0; i < 16; ++i, cGrey += cGreyInc) - aPalette[i] = BitmapColor(cGrey, cGrey, cGrey); - - return aPalette; - }(); - + static const BitmapPalette aGreyPalette16 = getGreyscalePalette<16>(); return aGreyPalette16; } case 256: { - static const BitmapPalette aGreyPalette256 = [] { - BitmapPalette aPalette(256); - - for (sal_uInt16 i = 0; i < 256; ++i) - aPalette[i] = BitmapColor(static_cast<sal_uInt8>(i), static_cast<sal_uInt8>(i), - static_cast<sal_uInt8>(i)); - - return aPalette; - }(); - + static const BitmapPalette aGreyPalette256 = getGreyscalePalette<256>(); return aGreyPalette256; } } diff --git a/vcl/source/bitmap/bitmappalette.cxx b/vcl/source/bitmap/bitmappalette.cxx index 61a8a8252794..e0bf53db033e 100644 --- a/vcl/source/bitmap/bitmappalette.cxx +++ b/vcl/source/bitmap/bitmappalette.cxx @@ -37,6 +37,10 @@ public: : maBitmapColor(aBitmapColor) { } + ImplBitmapPalette(const BitmapColor* first, const BitmapColor* last) + : maBitmapColor(first, last) + { + } ImplBitmapPalette() {} ImplBitmapPalette(sal_uInt16 nCount) : maBitmapColor(nCount) @@ -82,6 +86,11 @@ BitmapPalette::BitmapPalette(std::initializer_list<BitmapColor> aBitmapColor) { } +BitmapPalette::BitmapPalette(const BitmapColor* first, const BitmapColor* last) + : mpImpl({ first, last }) +{ +} + BitmapPalette::BitmapPalette(sal_uInt16 nCount) : mpImpl(nCount) {