+ if(Pixel::RGBA8888 == pixelFormat)
+ {
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+ const size_t bufferSizeChar = sizeof(uint32_t) * static_cast<std::size_t>(bufferSizeInt);
+ memset(imageBuffer.GetBuffer(), 0, bufferSizeChar);
+ }
+ else
+ {
+ memset(imageBuffer.GetBuffer(), 0, static_cast<std::size_t>(bufferWidth * bufferHeight));
+ }
+
+ return imageBuffer;
+}
+
+/**
+ * @brief Combine the two RGBA image buffers together.
+ *
+ * The top layer buffer will blend over the bottom layer buffer:
+ * - If the pixel is not fully opaque from either buffer, it will be blended with
+ * the pixel from the other buffer and copied to the combined buffer.
+ * - If the pixels from both buffers are fully opaque, the pixels from the top layer
+ * buffer will be copied to the combined buffer.
+ *
+ * Due to the performance issue, We need to re-use input'ed pixelBuffer memory.
+ * We can determine which pixelBuffer's memory is destination
+ *
+ * @param[in, out] topPixelBuffer The top layer buffer.
+ * @param[in, out] bottomPixelBuffer The bottom layer buffer.
+ * @param[in] bufferWidth The width of the image buffer.
+ * @param[in] bufferHeight The height of the image buffer.
+ * @param[in] storeResultIntoTop True if we store the combined image buffer result into topPixelBuffer.
+ * False if we store the combined image buffer result into bottomPixelBuffer.
+ *
+ */
+void CombineImageBuffer(Devel::PixelBuffer& __restrict__ topPixelBuffer, Devel::PixelBuffer& __restrict__ bottomPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, bool storeResultIntoTop)
+{
+ // 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 both buffers are empty.
+ return;
+ }
+
+ if(topBuffer == NULL)
+ {
+ // Nothing to do if topBuffer is empty.
+ // If we need to store the result into top, change topPixelBuffer as bottomPixelBuffer.
+ if(storeResultIntoTop)
+ {
+ topPixelBuffer = bottomPixelBuffer;
+ }
+ return;
+ }
+
+ if(bottomBuffer == NULL)
+ {
+ // Nothing to do if bottomBuffer is empty.
+ // If we need to store the result into bottom, change bottomPixelBuffer as topPixelBuffer.
+ if(!storeResultIntoTop)
+ {
+ bottomPixelBuffer = topPixelBuffer;
+ }
+ return;
+ }
+
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+
+ uint32_t* __restrict__ combinedBuffer = storeResultIntoTop ? topBuffer : bottomBuffer;
+ uint8_t* __restrict__ topAlphaBufferPointer = reinterpret_cast<uint8_t*>(topBuffer) + 3;
+
+ for(uint32_t pixelIndex = 0; pixelIndex < bufferSizeInt; ++pixelIndex)
+ {
+ // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
+ // Otherwise, copy pixel from topBuffer to combinedBuffer.
+ // Note : Be careful when we read & write into combinedBuffer. It can be write into same pointer.
+
+ uint8_t topAlpha = *topAlphaBufferPointer;
+
+ if(topAlpha == 0)
+ {
+ // Copy the pixel from bottomBuffer to combinedBuffer
+ if(storeResultIntoTop)
+ {
+ *(combinedBuffer) = *(bottomBuffer);
+ }
+ }
+ else if(topAlpha == 255)
+ {
+ // Copy the pixel from topBuffer to combinedBuffer
+ if(!storeResultIntoTop)
+ {
+ *(combinedBuffer) = *(topBuffer);
+ }
+ }
+ else
+ {
+ // At least one pixel is not fully opaque
+ // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
+ uint32_t blendedBottomBufferColor = *(bottomBuffer);
+ uint8_t* __restrict__ blendedBottomBufferColorBuffer = reinterpret_cast<uint8_t*>(&blendedBottomBufferColor);
+
+ blendedBottomBufferColorBuffer[0] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[0], 255 - topAlpha);
+ blendedBottomBufferColorBuffer[1] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[1], 255 - topAlpha);
+ blendedBottomBufferColorBuffer[2] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[2], 255 - topAlpha);
+ blendedBottomBufferColorBuffer[3] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[3], 255 - topAlpha);
+
+ *(combinedBuffer) = *(topBuffer) + blendedBottomBufferColor;