Modifying the MSDN WIC Sample for Windows 8 To Add FillOpacityMask Support
Join the DZone community and get the full member experience.
Join For Freeas 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.
Opinions expressed by DZone contributors are their own.
Trending
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
Logging Best Practices Revisited [Video]
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
Comments