#include <dali/internal/imaging/common/image-operations.h>
#include <dali/internal/imaging/common/pixel-buffer-impl.h>
#include <dali/internal/imaging/common/pixel-manipulation.h>
-#include <dali/public-api/images/image-operations.h> // For ImageDimensions
+#include <dali/public-api/images/image-operations.h> // For ImageDimensions and MultiplyAndNormalizeColor
namespace Dali
{
for(const Channel& channel : validChannelList)
{
auto color = ReadChannel(destBuffer + destOffset, destPixelFormat, channel);
- WriteChannel(destBuffer + destOffset, destPixelFormat, channel, color * srcAlpha / 255);
+ WriteChannel(destBuffer + destOffset, destPixelFormat, channel, Platform::MultiplyAndNormalizeColor(color, srcAlpha));
}
}
else
for(unsigned int col = 0; col < buffer.GetWidth(); ++col)
{
- unsigned char srcAlpha = srcBuffer[srcOffset + srcAlphaByteOffset] & srcAlphaMask;
- unsigned char destAlpha = destBuffer[destOffset + destAlphaByteOffset] & destAlphaMask;
+ uint8_t srcAlpha = srcBuffer[srcOffset + srcAlphaByteOffset] & srcAlphaMask;
+ uint8_t destAlpha = destBuffer[destOffset + destAlphaByteOffset] & destAlphaMask;
- destAlpha = (static_cast<std::uint16_t>(destAlpha) * static_cast<std::uint16_t>(srcAlpha)) / 255;
+ destAlpha = Platform::MultiplyAndNormalizeColor(srcAlpha, destAlpha);
destBuffer[destOffset + destAlphaByteOffset] &= ~destAlphaMask;
destBuffer[destOffset + destAlphaByteOffset] |= (destAlpha & destAlphaMask);
if(hasAlpha)
{
destAlpha = ConvertAlphaChannelToA8(oldBuffer, srcColorOffset, srcColorPixelFormat);
- destAlpha = (static_cast<std::uint16_t>(destAlpha) * static_cast<std::uint16_t>(srcAlpha)) / 255;
+ destAlpha = Platform::MultiplyAndNormalizeColor(srcAlpha, destAlpha);
}
else
{
return rounded;
}
+/**
+ * @brief Fast multiply & divide by 255. It wiil be useful when we applying alpha value in color
+ *
+ * @param x The value between [0..255]
+ * @param y The value between [0..255]
+ * @return (x*y)/255
+ */
+inline uint8_t MultiplyAndNormalizeColor(const uint8_t& x, const uint8_t& y) noexcept
+{
+ const uint32_t xy = static_cast<const uint32_t>(x) * y;
+ return ((xy << 15) + (xy << 7) + xy) >> 23;
+}
+
/**@}*/
} /* namespace Platform */
for(const Channel& channel : validChannelList)
{
auto color = ReadChannel(&pixel[x], mPixelFormat, channel);
- WriteChannel(&pixel[x], mPixelFormat, channel, color * alpha / 255);
+ WriteChannel(&pixel[x], mPixelFormat, channel, Platform::MultiplyAndNormalizeColor(color, alpha));
}
}
else