DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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

Denzel D. user avatar by
Denzel D.
·
Jan. 04, 13 · Interview
Like (0)
Save
Tweet
Share
4.69K Views

Join the DZone community and get the full member experience.

Join For Free

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.

application Brush Opacity (optics) Snippet (programming) Memory (storage engine) Pass (software) Bitmap

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Promises, Thenables, and Lazy-Evaluation: What, Why, How
  • Memory Debugging: A Deep Level of Insight
  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • DevOps Roadmap for 2022

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: