From 8b610c874fc8985e740b383d85817778c0927c58 Mon Sep 17 00:00:00 2001 From: Sunghyun kim Date: Wed, 10 Jul 2019 12:41:46 +0900 Subject: [PATCH] Multiply the Alpha of mask to the premultiplied image After move MultiplyColorByAlpha() from main thread to resource thread, MultiplyColorByAlpha() is called first than ApplyMask(). In this case, the alpha value of mask is not multiplied properly to other channels in the image. Therefore, if image is premultiplied, the other channels of the image need to multiply by alpha of mask. Change-Id: I60c056460537db604566f080302be09a6f88d047 --- dali/internal/imaging/common/alpha-mask.cpp | 53 ++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/dali/internal/imaging/common/alpha-mask.cpp b/dali/internal/imaging/common/alpha-mask.cpp index c6cfe31..e2e0b25 100644 --- a/dali/internal/imaging/common/alpha-mask.cpp +++ b/dali/internal/imaging/common/alpha-mask.cpp @@ -44,7 +44,8 @@ void ApplyMaskToAlphaChannel( PixelBuffer& buffer, const PixelBuffer& mask ) int destAlphaByteOffset=0; int destAlphaMask=0; - Dali::Pixel::GetAlphaOffsetAndMask( buffer.GetPixelFormat(), destAlphaByteOffset, destAlphaMask ); + Dali::Pixel::Format destPixelFormat = buffer.GetPixelFormat(); + Dali::Pixel::GetAlphaOffsetAndMask( destPixelFormat, destAlphaByteOffset, destAlphaMask ); unsigned int srcBytesPerPixel = Dali::Pixel::GetBytesPerPixel( srcPixelFormat ); unsigned char* srcBuffer = mask.GetBuffer(); @@ -57,21 +58,49 @@ void ApplyMaskToAlphaChannel( PixelBuffer& buffer, const PixelBuffer& mask ) float srcAlphaValue = 1.0f; - for( unsigned int row = 0; row < buffer.GetHeight(); ++row ) + // if image is premultiplied, the other channels of the image need to multiply by alpha. + if( buffer.IsAlphaPreMultiplied() ) { - for( unsigned int col = 0; col < buffer.GetWidth(); ++col ) + for( unsigned int row = 0; row < buffer.GetHeight(); ++row ) { - unsigned char alpha = srcBuffer[srcOffset + srcAlphaByteOffset] & srcAlphaMask; - srcAlphaValue = float(alpha)/255.0f; + for( unsigned int col = 0; col < buffer.GetWidth(); ++col ) + { + auto srcAlpha = ReadChannel( srcBuffer + srcOffset, srcPixelFormat, Adaptor::ALPHA); + auto destRed = ReadChannel( destBuffer + destOffset, destPixelFormat, Adaptor::RED); + auto destGreen = ReadChannel( destBuffer + destOffset, destPixelFormat, Adaptor::GREEN); + auto destBlue = ReadChannel( destBuffer + destOffset, destPixelFormat, Adaptor::BLUE); + auto destLuminance = ReadChannel( destBuffer + destOffset, destPixelFormat, Adaptor::LUMINANCE); + auto destAlpha = ReadChannel( destBuffer + destOffset, destPixelFormat, Adaptor::ALPHA); + + WriteChannel( destBuffer + destOffset, destPixelFormat, Adaptor::RED, destRed*srcAlpha / 255 ); + WriteChannel( destBuffer + destOffset, destPixelFormat, Adaptor::GREEN, destGreen*srcAlpha/255 ); + WriteChannel( destBuffer + destOffset, destPixelFormat, Adaptor::BLUE, destBlue*srcAlpha/255 ); + WriteChannel( destBuffer + destOffset, destPixelFormat, Adaptor::LUMINANCE, destLuminance*srcAlpha/255 ); + WriteChannel( destBuffer + destOffset, destPixelFormat, Adaptor::ALPHA, destAlpha*srcAlpha/255 ); + + srcOffset += srcBytesPerPixel; + destOffset += destBytesPerPixel; + } + } + } + else + { + for( unsigned int row = 0; row < buffer.GetHeight(); ++row ) + { + for( unsigned int col = 0; col < buffer.GetWidth(); ++col ) + { + unsigned char alpha = srcBuffer[srcOffset + srcAlphaByteOffset] & srcAlphaMask; + srcAlphaValue = float(alpha)/255.0f; - unsigned char destAlpha = destBuffer[destOffset + destAlphaByteOffset] & destAlphaMask; - float destAlphaValue = Clamp(float(destAlpha) * srcAlphaValue, 0.0f, 255.0f); - destAlpha = destAlphaValue; - destBuffer[destOffset + destAlphaByteOffset] &= ~destAlphaMask; - destBuffer[destOffset + destAlphaByteOffset] |= ( destAlpha & destAlphaMask ); + unsigned char destAlpha = destBuffer[destOffset + destAlphaByteOffset] & destAlphaMask; + float destAlphaValue = Clamp(float(destAlpha) * srcAlphaValue, 0.0f, 255.0f); + destAlpha = destAlphaValue; + destBuffer[destOffset + destAlphaByteOffset] &= ~destAlphaMask; + destBuffer[destOffset + destAlphaByteOffset] |= ( destAlpha & destAlphaMask ); - srcOffset += srcBytesPerPixel; - destOffset += destBytesPerPixel; + srcOffset += srcBytesPerPixel; + destOffset += destBytesPerPixel; + } } } } -- 2.7.4