From 6498776b525bb4c8640d19d4683061f132b2db17 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Mon, 4 Jan 2016 18:30:39 +0100 Subject: [PATCH] Initialize internal bitmap when creating D2D graphics context from wxImage. Internal D2D WIC bitmap should be initialized with source wxImage contents in wxD2DRenderer::CreateContextFromImage. Closes #17314. --- src/msw/graphicsd2d.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index 1f244f38fe..f3ad1cdf5d 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2273,6 +2273,56 @@ protected: &m_wicBitmap); wxCHECK_HRESULT_RET(hr); + // Copy contents of source image to the WIC bitmap. + const int width = m_resultImage->GetWidth(); + const int height = m_resultImage->GetHeight(); + WICRect rcLock = { 0, 0, width, height }; + IWICBitmapLock *pLock = NULL; + hr = m_wicBitmap->Lock(&rcLock, WICBitmapLockWrite, &pLock); + wxCHECK_HRESULT_RET(hr); + + UINT rowStride = 0; + hr = pLock->GetStride(&rowStride); + if ( FAILED(hr) ) + { + pLock->Release(); + wxFAILED_HRESULT_MSG(hr); + return; + } + + UINT bufferSize = 0; + BYTE *pBmpBuffer = NULL; + hr = pLock->GetDataPointer(&bufferSize, &pBmpBuffer); + if ( FAILED(hr) ) + { + pLock->Release(); + wxFAILED_HRESULT_MSG(hr); + return; + } + + const unsigned char *imgRGB = m_resultImage->GetData(); // source RGB buffer + const unsigned char *imgAlpha = m_resultImage->GetAlpha(); // source alpha buffer + for( int y = 0; y < height; y++ ) + { + BYTE *pPixByte = pBmpBuffer; + for ( int x = 0; x < width; x++ ) + { + unsigned char r = *imgRGB++; + unsigned char g = *imgRGB++; + unsigned char b = *imgRGB++; + unsigned char a = imgAlpha ? *imgAlpha++ : 255; + // Premultiply RGB values + *pPixByte++ = (b * a + 127) / 255; + *pPixByte++ = (g * a + 127) / 255; + *pPixByte++ = (r * a + 127) / 255; + *pPixByte++ = a; + } + + pBmpBuffer += rowStride; + } + + pLock->Release(); + // Create the render target hr = m_factory->CreateWicBitmapRenderTarget( m_wicBitmap,