From 68bc6a5dd68006ab86e7ef26f9e5d241cb118879 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sat, 13 Mar 2021 12:00:28 +0900 Subject: add PixelFormat enum that replaces bit count in Bitmap/BitmapEx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bit count for the image is a numeric value (sal_uInt16) but only a handful of values make sense - namely 1,4,8,24 and 32. This replaces the numeric value with an enum, which only accepts those values and checks the correct values are used at compile time. Change-Id: I0fc137c62bce3b0d021f05019a1648da628521bc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112408 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- include/vcl/BitmapTools.hxx | 4 +-- include/vcl/bitmap.hxx | 8 +++--- include/vcl/bitmap/BitmapTypes.hxx | 55 ++++++++++++++++++++++++++++++++++++++ include/vcl/bitmapex.hxx | 13 +++++++-- 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 include/vcl/bitmap/BitmapTypes.hxx (limited to 'include/vcl') diff --git a/include/vcl/BitmapTools.hxx b/include/vcl/BitmapTools.hxx index 12cdb1947b2c..d7ce4be9e90f 100644 --- a/include/vcl/BitmapTools.hxx +++ b/include/vcl/BitmapTools.hxx @@ -44,9 +44,9 @@ void loadFromSvg(SvStream& rStream, const OUString& sPath, BitmapEx& rBitmapEx, @param pData The block of data to copy @param nStride - The number of bytes in a scanline, must be >= (width * nBitCount / 8) + The number of bytes in a scanline, must be >= (width * bitcount / 8) */ -BitmapEx VCL_DLLPUBLIC CreateFromData( sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_uInt16 nBitCount ); +BitmapEx VCL_DLLPUBLIC CreateFromData( sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, vcl::PixelFormat ePixelFormat); BitmapEx VCL_DLLPUBLIC CreateFromData( RawBitmap && data ); diff --git a/include/vcl/bitmap.hxx b/include/vcl/bitmap.hxx index d9d6c81d7661..3a2baf9e24c7 100644 --- a/include/vcl/bitmap.hxx +++ b/include/vcl/bitmap.hxx @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -114,7 +115,7 @@ public: Bitmap(); Bitmap( const Bitmap& rBitmap ); - Bitmap( const Size& rSizePixel, sal_uInt16 nBitCount, const BitmapPalette* pPal = nullptr ); + Bitmap( const Size& rSizePixel, vcl::PixelFormat ePixelFormat, const BitmapPalette* pPal = nullptr ); explicit Bitmap( std::shared_ptr const & xSalBitmap ); virtual ~Bitmap(); @@ -135,6 +136,7 @@ public: Size GetSizePixel() const; + vcl::PixelFormat getPixelFormat() const; sal_uInt16 GetBitCount() const; inline sal_Int64 GetColorCount() const; inline sal_uLong GetSizeBytes() const; @@ -538,8 +540,8 @@ public: typedef vcl::ScopedBitmapAccess ScopedInfoAccess; private: - SAL_DLLPRIVATE bool ImplConvertUp(sal_uInt16 nBitCount, Color const* pExtColor = nullptr); - SAL_DLLPRIVATE bool ImplConvertDown(sal_uInt16 nBitCount, Color const* pExtColor = nullptr); + SAL_DLLPRIVATE bool ImplConvertUp(vcl::PixelFormat ePixelFormat, Color const* pExtColor = nullptr); + SAL_DLLPRIVATE bool ImplConvertDown(vcl::PixelFormat ePixelFormat, Color const* pExtColor = nullptr); private: std::shared_ptr mxSalBmp; diff --git a/include/vcl/bitmap/BitmapTypes.hxx b/include/vcl/bitmap/BitmapTypes.hxx new file mode 100644 index 000000000000..866cf0b2950e --- /dev/null +++ b/include/vcl/bitmap/BitmapTypes.hxx @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#pragma once + +namespace vcl +{ +/** Pixel format of the bitmap in bits per pixel */ +enum class PixelFormat +{ + INVALID = 0, + N1_BPP = 1, + N4_BPP = 4, + N8_BPP = 8, + N24_BPP = 24, + N32_BPP = 32 +}; + +/** Is it a pixel format that forces creation of a palette */ +constexpr bool isPalettePixelFormat(PixelFormat ePixelFormat) +{ + assert(ePixelFormat != PixelFormat::INVALID); + return sal_uInt16(ePixelFormat) <= 8; +} + +constexpr PixelFormat bitDepthToPixelFormat(sal_uInt16 nBitDepth) +{ + switch (nBitDepth) + { + case 1: + return PixelFormat::N1_BPP; + case 4: + return PixelFormat::N4_BPP; + case 8: + return PixelFormat::N8_BPP; + case 24: + return PixelFormat::N24_BPP; + case 32: + return PixelFormat::N32_BPP; + default: + break; + } + return PixelFormat::INVALID; +} + +} // end namespace vcl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/vcl/bitmapex.hxx b/include/vcl/bitmapex.hxx index 506901134719..5736e4d38b6a 100644 --- a/include/vcl/bitmapex.hxx +++ b/include/vcl/bitmapex.hxx @@ -49,7 +49,7 @@ public: explicit BitmapEx( const OUString& rIconName ); BitmapEx( const BitmapEx& rBitmapEx ); BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize ); - BitmapEx( Size aSize, sal_uInt16 nBitCount ); + BitmapEx(Size aSize, vcl::PixelFormat ePixelFormat); explicit BitmapEx( const Bitmap& rBmp ); BitmapEx( const Bitmap& rBmp, const Bitmap& rMask ); BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask ); @@ -90,7 +90,16 @@ public: const MapMode& GetPrefMapMode() const { return maBitmap.GetPrefMapMode(); } void SetPrefMapMode( const MapMode& rPrefMapMode ) { maBitmap.SetPrefMapMode( rPrefMapMode ); } - sal_uInt16 GetBitCount() const { return maBitmap.GetBitCount(); } + sal_uInt16 GetBitCount() const + { + return maBitmap.GetBitCount(); + } + + vcl::PixelFormat getPixelFormat() const + { + return maBitmap.getPixelFormat(); + } + sal_uLong GetSizeBytes() const; BitmapChecksum GetChecksum() const; -- cgit