.NET Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Modifying the MSDN WIC Sample for Windows 8 To Add FillOpacityMask Support

01.04.2013
| 2925 views |
  • submit to reddit

As a Windows 8 developer, you probably already know that it does not support OpacityMask in XAML applications. This leaves you with the option of using Direct2D. A while ago, Microsoft released a sample that demonstrated the opacity mask capabilities, available here.

This sample compiles and works well on Windows 8 (with Visual Studio 2012) as a Win32 application.

The problem, however, lies in the fact that some of the memory manipulations that are being done in a Win32 application, cannot be done in a Windows Store application, including but not limited to SafeRelease. The Direct2D core, however, remains practically untouched. To demonstrate this, I downloaded the stock WIC sample, available in the MSDN Code Gallery.

I began by refactoring the code in WicImage.cpp. Specifically, I separated the converter (IWICFormatConverter) initialization code in a separate function, also separating the converter reference itself:

void WicImage::InitializeConverter()
{
	DX::ThrowIfFailed(wicFactory_->CreateFormatConverter(converter_.ReleaseAndGetAddressOf()));

	DX::ThrowIfFailed(converter_->Initialize(
			bitmap_.Get(),					 // Input bitmap to convert
			GUID_WICPixelFormat32bppPBGRA,   // Destination pixel format
			WICBitmapDitherTypeNone,         // Specified dither pattern
			NULL,                            // Specify a particular palette 
			0.f,                             // Alpha threshold
			WICBitmapPaletteTypeCustom       // Palette translation type
			));
}
In the header file for WicImage, I added:
ComPtr<IWICFormatConverter> converter_;
Now, in the GetD2DCompatibleBitmap function, I can include a call to InitializeConverter instead of having the snippet directly integrated in that function. The reason for this is simple - I will need to ultimately export the reference to the converter, that will be ready-to-go instead of relying on repeated reinitialization.

I added a new internal function that will return the converter:

Microsoft::WRL::ComPtr<IWICFormatConverter> WicImage::GetFormatConverter()
{
	return converter_;
}
Once this is complete, I can proceed to operate on my opacity mask. In the ImageEditor class, I create a new function called ApplyTestMask:
void ImageEditor::ApplyTestMask()
{
	D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 155, 155);

	renderTarget_->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);

	ComPtr<ID2D1Bitmap> bitmap;
	ID2D1BitmapBrush *m_pFernBitmapBrush;

	DX::ThrowIfFailed(renderTarget_->CreateBitmapFromWicBitmap(image_->GetFormatConverter().Get(),
		bitmap.ReleaseAndGetAddressOf()));

	D2D1_BITMAP_BRUSH_PROPERTIES propertiesXClampYClamp = 
                    D2D1::BitmapBrushProperties(
                    D2D1_EXTEND_MODE_CLAMP,
                    D2D1_EXTEND_MODE_CLAMP,
                    D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
                    );

	DX::ThrowIfFailed(renderTarget_->CreateBitmapBrush(
		bitmap.Get(),
        propertiesXClampYClamp,
        &m_pFernBitmapBrush
        ));

	
	renderTarget_->FillOpacityMask(
		bitmap.Get(),
            m_pFernBitmapBrush,
            D2D1_OPACITY_MASK_CONTENT_GRAPHICS,
            &rcBrushRect
            );

	DX::ThrowIfFailed(renderTarget_->CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::Black),
                &m_pBlackBrush
                ));

    renderTarget_->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
	renderTarget_->DrawRectangle(&rcBrushRect, m_pBlackBrush, 1.f);
}
Pay attention to the CreateBitmapFromWicBitmap call. A mistake here would be attempting to directly retrieve the IWICBitmap from the WicImage class. This, however, will result in an exception notifying you that an existing read or write lock already exists. Instead, pass the converter, that will return a valid image that is already pre-formatted, after which you will be able to create a bitmap brush and call FillOpacityMask.

The approach is very similar for gradient brushes, which do not require explicit access to the WicImage content.