+void Typesetter::SetMaskForImageBuffer(Devel::PixelBuffer& __restrict__ topPixelBuffer, Devel::PixelBuffer& __restrict__ bottomPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, float originAlpha)
+{
+ // Assume that we always combine two RGBA images
+ // Jump with 4bytes for optimize runtime.
+ uint32_t* topBuffer = reinterpret_cast<uint32_t*>(topPixelBuffer.GetBuffer());
+ uint32_t* bottomBuffer = reinterpret_cast<uint32_t*>(bottomPixelBuffer.GetBuffer());
+
+ if(topBuffer == NULL || bottomBuffer == NULL)
+ {
+ // Nothing to do if one of both buffers are empty.
+ return;
+ }
+
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+
+ for(uint32_t pixelIndex = 0; pixelIndex < bufferSizeInt; ++pixelIndex)
+ {
+ uint32_t topBufferColor = *(topBuffer);
+ uint32_t bottomBufferColor = *(bottomBuffer);
+ uint8_t* __restrict__ topBufferColorBuffer = reinterpret_cast<uint8_t*>(&topBufferColor);
+ uint8_t* __restrict__ bottomBufferColorBuffer = reinterpret_cast<uint8_t*>(&bottomBufferColor);
+
+ uint8_t topAlpha = topBufferColorBuffer[3];
+ uint8_t bottomAlpha = 255 - topAlpha;
+
+ float tempTop[4], tempBottom[4];
+
+ // Return the transparency of the text to original.
+ tempTop[0] = static_cast<float>(topBufferColorBuffer[0]) * originAlpha;
+ tempTop[1] = static_cast<float>(topBufferColorBuffer[1]) * originAlpha;
+ tempTop[2] = static_cast<float>(topBufferColorBuffer[2]) * originAlpha;
+ tempTop[3] = static_cast<float>(topBufferColorBuffer[3]) * originAlpha;
+
+ // Manual blending.
+ tempBottom[0] = static_cast<float>(bottomBufferColorBuffer[0]) * static_cast<float>(bottomAlpha) / 255.f;
+ tempBottom[1] = static_cast<float>(bottomBufferColorBuffer[1]) * static_cast<float>(bottomAlpha) / 255.f;
+ tempBottom[2] = static_cast<float>(bottomBufferColorBuffer[2]) * static_cast<float>(bottomAlpha) / 255.f;
+ tempBottom[3] = static_cast<float>(bottomBufferColorBuffer[3]) * static_cast<float>(bottomAlpha) / 255.f;
+
+ bottomBufferColorBuffer[0] = static_cast<uint8_t>(std::min(255u, static_cast<uint32_t>(tempBottom[0] + tempTop[0])));
+ bottomBufferColorBuffer[1] = static_cast<uint8_t>(std::min(255u, static_cast<uint32_t>(tempBottom[1] + tempTop[1])));
+ bottomBufferColorBuffer[2] = static_cast<uint8_t>(std::min(255u, static_cast<uint32_t>(tempBottom[2] + tempTop[2])));
+ bottomBufferColorBuffer[3] = static_cast<uint8_t>(std::min(255u, static_cast<uint32_t>(tempBottom[3] + tempTop[3])));
+
+ *(bottomBuffer) = bottomBufferColor;
+
+ // Increase each buffer's pointer.
+ ++topBuffer;
+ ++bottomBuffer;
+ }
+}
+