From 794911bc29c5ddf7a1629b4872a4bd8c663c0005 Mon Sep 17 00:00:00 2001 From: Joogab Yun Date: Tue, 29 Jun 2021 15:55:23 +0900 Subject: [PATCH] Add GetBrightness() Get the brightness value of the buffer. Change-Id: I3b95ff0d786cfb3a60a10aeca994079504edfea8 --- dali/devel-api/adaptor-framework/pixel-buffer.cpp | 7 ++++- dali/devel-api/adaptor-framework/pixel-buffer.h | 9 ++++++- dali/internal/imaging/common/pixel-buffer-impl.cpp | 31 ++++++++++++++++++++++ dali/internal/imaging/common/pixel-buffer-impl.h | 5 ++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/dali/devel-api/adaptor-framework/pixel-buffer.cpp b/dali/devel-api/adaptor-framework/pixel-buffer.cpp index 06d61d2..e5856c3 100644 --- a/dali/devel-api/adaptor-framework/pixel-buffer.cpp +++ b/dali/devel-api/adaptor-framework/pixel-buffer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -139,6 +139,11 @@ bool PixelBuffer::IsAlphaPreMultiplied() const return GetImplementation(*this).IsAlphaPreMultiplied(); } +uint32_t PixelBuffer::GetBrightness() const +{ + return GetImplementation(*this).GetBrightness(); +} + } // namespace Devel } // namespace Dali diff --git a/dali/devel-api/adaptor-framework/pixel-buffer.h b/dali/devel-api/adaptor-framework/pixel-buffer.h index 18b5a5f..9e6f58a 100644 --- a/dali/devel-api/adaptor-framework/pixel-buffer.h +++ b/dali/devel-api/adaptor-framework/pixel-buffer.h @@ -2,7 +2,7 @@ #define DALI_PIXEL_BUFFER_H /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -244,6 +244,13 @@ public: */ bool IsAlphaPreMultiplied() const; + /** + * @brief Get the brightness of the pixel buffer. + * @note The range is 255 to 0. The closer to 255, the brighter. 0 is not bright. + * @return brightness of the pixel buffer. + */ + uint32_t GetBrightness() const; + public: /** * @brief The constructor. diff --git a/dali/internal/imaging/common/pixel-buffer-impl.cpp b/dali/internal/imaging/common/pixel-buffer-impl.cpp index 1b74988..e69f2c8 100644 --- a/dali/internal/imaging/common/pixel-buffer-impl.cpp +++ b/dali/internal/imaging/common/pixel-buffer-impl.cpp @@ -37,6 +37,10 @@ namespace Adaptor namespace { const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians +// based on W3C Recommendations (https://www.w3.org/TR/AERT/#color-contrast) +constexpr uint32_t BRIGHTNESS_CONSTANT_R = 299; +constexpr uint32_t BRIGHTNESS_CONSTANT_G = 587; +constexpr uint32_t BRIGHTNESS_CONSTANT_B = 114; } // namespace PixelBuffer::PixelBuffer(unsigned char* buffer, @@ -466,6 +470,33 @@ bool PixelBuffer::IsAlphaPreMultiplied() const return mPreMultiplied; } +uint32_t PixelBuffer::GetBrightness() const +{ + uint32_t brightness = 0; + + uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat); + if(bytesPerPixel) + { + unsigned char* pixel = mBuffer; + const uint32_t bufferSize = mWidth * mHeight; + uint64_t red = 0; + uint64_t green = 0; + uint64_t blue = 0; + + for(uint32_t i = 0; i < bufferSize; ++i) + { + red += ReadChannel(pixel, mPixelFormat, Adaptor::RED); + green += ReadChannel(pixel, mPixelFormat, Adaptor::GREEN); + blue += ReadChannel(pixel, mPixelFormat, Adaptor::BLUE); + pixel += bytesPerPixel; + } + // http://www.w3.org/TR/AERT#color-contrast + brightness = (red * BRIGHTNESS_CONSTANT_R + green * BRIGHTNESS_CONSTANT_G + blue * BRIGHTNESS_CONSTANT_B) / (1000uLL * bufferSize); + } + + return brightness; +} + } // namespace Adaptor } // namespace Internal } // namespace Dali diff --git a/dali/internal/imaging/common/pixel-buffer-impl.h b/dali/internal/imaging/common/pixel-buffer-impl.h index 47c478c..da25097 100644 --- a/dali/internal/imaging/common/pixel-buffer-impl.h +++ b/dali/internal/imaging/common/pixel-buffer-impl.h @@ -221,6 +221,11 @@ public: */ bool IsAlphaPreMultiplied() const; + /** + * @copydoc Devel::PixelBuffer::GetBrightness() + */ + uint32_t GetBrightness() const; + private: /* * Undefined copy constructor. -- 2.7.4