Direct2D - emulation of color transparent bitmaps

I am currently updating a Windows GDI application to use Direct2D rendering, and I need to support "transparent" bitmaps using the color keyboard for backward compatibility.

I am currently working with the goal of rendering HWND and a converted WIC source file (in GUID_WICPixelFormat32bppPBGRA). My plan so far is to create an IWICBitmap from the converted bitmap, Lock (), and then process each pixel by setting its alpha value to 0 if it matches the color key.

It seems a little "brute force." Is this the best way to get close to this or is there a better way?

Edit: in the interest of completeness, here is an excerpt from what I’ve been with - it looks like it works fine, but I'm open to any improvements!

// pConvertedBmp contains a IWICFormatConverter* bitmap with the pixel // format set to GUID_WICPixelFormat32bppPBGRA IWICBitmap* pColorKeyedBmp = NULL; HRESULT hr = S_OK; UINT uBmpW = 0; UINT uBmpH = 0; pConvertedBmp->GetSize( &uBmpW, &uBmpH ); WICRect rcLock = { 0, 0, uBmpW, uBmpH }; // GetWIC() returns the WIC Factory instance in this app hr = GetWIC()->CreateBitmapFromSource( pConvertedBmp, WICBitmapCacheOnLoad, &pColorKeyedBmp ); if ( FAILED( hr ) ) { return hr; } IWICBitmapLock* pBitmapLock = NULL; hr = pColorKeyedBmp->Lock( &rcLock, WICBitmapLockRead, &pBitmapLock ); if ( FAILED( hr ) ) { SafeRelease( &pColorKeyedBmp ); return hr; } UINT uPixel = 0; UINT cbBuffer = 0; UINT cbStride = 0; BYTE* pPixelBuffer = NULL; hr = pBitmapLock->GetStride( &cbStride ); if ( SUCCEEDED( hr ) ) { hr = pBitmapLock->GetDataPointer( &cbBuffer, &pPixelBuffer ); if ( SUCCEEDED( hr ) ) { // If we haven't got a resolved color key then we need to // grab the pixel at the specified coordinates and get // it RGB if ( !clrColorKey.IsValidColor() ) { // This is an internal function to grab the color of a pixel ResolveColorKey( pPixelBuffer, cbBuffer, cbStride, uBmpW, uBmpH ); } // Convert the RGB to BGR UINT uColorKey = (UINT) RGB2BGR( clrColorKey.GetRGB() ); LPBYTE pPixel = pPixelBuffer; for ( UINT uRow = 0; uRow < uBmpH; uRow++ ) { pPixel = pPixelBuffer + ( uRow * cbStride ); for ( UINT uCol = 0; uCol < uBmpW; uCol++ ) { uPixel = *( (LPUINT) pPixel ); if ( ( uPixel & 0x00FFFFFF ) == uColorKey ) { *( (LPUINT) pPixel ) = 0; } pPixel += sizeof( uPixel ); } } } } pBitmapLock->Release(); if ( FAILED( hr ) ) { // We still use the original image SafeRelease( &pColorKeyedBmp ); } else { // We use the new image so we release the original SafeRelease( &pConvertedBmp ); } return hr; 
+4
source share
1 answer

If you only need to β€œprocess” a bitmap to render it, then the GPU will always be the fastest. Direct2D has effects (ID2D1Effect) for easy bitmap processing. You can write your own [it seems relatively complicated] or use one of the built-in effects [it's pretty simple]. There is one color key called (CLSID_D2D1ChromaKey) .

On the other hand, if you need further processing by the processor, it becomes more complex. You might be better off optimizing the code you have.

0
source

All Articles