Directory
Entry
Decoding images
Encoded images
WIC Image Factory
Using a Stream
Using WIC through WPF
What's the next step?
Microsoft®windows®imaging Component (WIC) is an extensible framework for encoding, decoding, and manipulating images. WIC was originally designed for Windows vista® and Windows Presentation Foundation (WPF), but now, not only Windows Vista and Microsoft. NET Framework 3.0 and The later version comes with this framework, and it is a download for Windows XP and Windows server®2003 that can be used by native applications.
As one of several powerful native frameworks that support WPF, the WIC described in this article is the framework for implementing the System.Windows.Media.Imaging namespace. However, it is also ideal for native applications written in C + + because it provides a simple yet powerful API that is rendered through a set of COM interfaces.
WIC supports multiple image formats using a scalable set of image codecs. Each codec supports a different image format and usually provides both encoders and decoders. WIC includes a set of built-in codecs for all major image formats, including PNG, JPEG, GIF, TIFF, HD Photo (HDP), ICO, and of course, Windows BMP.
HDP may be the only format you've never heard of. Originally called Windows Media Photo and developed with Windows Vista, it is designed to overcome some of the limitations of existing formats and provide better performance and higher image quality. For more information about HDP, check the specifications on the microsoft.com/whdc/xps/wmphoto.mspx. Fortunately, WIC can support this new image format well, so applications can use them without knowing the specifics of the format.
This month, I'll show you how to use WIC to encode and decode different image formats and multiple issues. Next time, I'll talk about some of the more advanced features and show you how to use your own image codec to extend WIC.
Entry
The WIC API contains COM interfaces, functions, structures, and error codes, as well as GUIDs that identify various codecs, containers, and formats. All required declarations are included in the Wincodec.h and Wincodecsdk.h header files, which are part of the Windows SDK (included with these files in Visual studio®2008). You must also link to the WindowsCodecs.lib Library, which provides the various definitions you might need. The following code can be added to the project's precompiled header file to make it fully available:
#include <wincodec.h>
#include <wincodecsdk.h>
#pragma comment(lib, "WindowsCodecs.lib")
Since the WIC API primarily contains COM interfaces, I use the Active Template Library (ATL) CComPtr class to handle the creation and management of interface pointers. If you want to do the same, include the atlbase.h header file that defines the CComPtr template class:
#include <atlbase.h>
The WIC API also uses COM libraries, so any thread that uses this API must call the CoInitializeEx function.
Finally, the WIC API uses HRESULT to describe errors. The example in this article uses an HR macro to clearly identify where the method returns the HRESULT that needs to be checked. You can replace it with your own error-handling policy-it throws an exception or you return the HRESULT yourself.
Decoding images
The decoder is represented by the IWICBitmapDecoder interface. WIC provides a variety of methods for creating decoder objects, but you can create an instance using only the CLSID of a specific decoder. The following example creates a decoder for the TIFF image:
CComPtr<IWICBitmapDecoder> decoder;
HR(decoder.CoCreateInstance(CLSID_WICTiffDecoder));
Figure 1 lists the codecs included with WIC and the CLSID that can be used to create different decoders. After you have created the decoder, you need to initialize it with a stream that contains pixels and optional metadata that they use in a decoder-understandable format:
Figure1 the CLSID of the built-in WIC codec
Format |
Decoder |
Encoder device |
Bmp |
Clsid_wicbmpdecoder |
Clsid_wicbmpencoder |
Png |
Clsid_wicpngdecoder |
Clsid_wicpngencoder |
ICO |
Clsid_wicicodecoder |
Not available |
Jpeg |
Clsid_wicjpegdecoder |
Clsid_wicjpegencoder |
Gif |
Clsid_wicgifdecoder |
Clsid_wicgifencoder |
Tiff |
Clsid_wictiffdecoder |
Clsid_wictiffencoder |
HDP |
Clsid_wicwmpdecoder |
Clsid_wicwmpencoder |
CComPtr<IStream> stream;
// Create stream object here...
HR(decoder->Initialize(
stream,
WICDecodeMetadataCacheOnDemand));
I'll discuss the stream later in this article, but IStream is just a traditional COM stream interface used by many APIs, including the MSDN® magazine, which I published in April 2007 (Msdn.microsoft.com/msdnmag/issues/07/04/xml) The XmlLite Analyzer described in.