Category Archives: WIC

Windows Imaging Component

Codexpert – 2016 Articles Summary

C++ Programming Language

Microsoft Libraries

Visual C++

See also

Read IFD and EXIF Metadata with WIC and MFC

A graphic file can store additional information abaout image (camera model, camera manufacturer, date and time, etc). Windows Imaging Component (WIC) provides interfaces which allow dealing with image metadata. Basically, for reading metadata we need an IWICMetadataQueryReader instance to call its GetMetadataByName method. The first argument of GetMetadataByName is based in Metadata Query Language and is composed by a path in metadata tree and an item name/identifier or a block name/identifier (a block contains itself items and/or other blocks). For more details, please see Metadata Query Language Overview in MSDN library. The second argument is a PROPVARIANT structure that gets the value. If returned value is of type VT_UNKNOWN then we’ve got a block, otherwise we’ve got an item value.

Among other metadata blocks defined in TIFF standard, which are are common to other graphic file formats (e.g. JPEG and raw image file formats like NEF) there are IFD (Image File Directory) and EXIF metadata blocks. I’ll not provide more details here (they can be found in MSDN or in other documents and articles). Just to note that EXIF is nested into IFD block and, while IFD block is placed in the metadata root for TIFF, it is nested/embedded into APP1 block for JPEG file format. For example, to get the ISOSpeed EXIF value, we can call IWICMetadataQueryReader::GetMetadataByName passing “/ifd/exif/{ushort=34867}” in case of TIFF or “/app1/ifd/exif/{ushort=34867}” for JPEG format.

Reading IFD and EXIF metadata from a JPEG file

Here is a brief example:

As can be seen, this example assumes that we have a JPEG file and uses the root metadata query reader by passing the item full path.
Let’s improve it in order to read metadata also from TIFF, NEF and other formats. Let’s also get and use the embedded metadata readers both for IFD and EXIF blocks.

Reading IFD and EXIF metadata from an image file

First, we need a functions which finds out if we are dealing with JPEG or other format.

Next, we can write a function that gets the IFD query reader.

Also we need a function to get the embedded EXIF query reader. Here we can write a generic one, which takes the parent query reader and the block name.

Let’a also write a function that gets the root query reader.

Now, the function which reads the image IFD and EXIF metadata may look like this:

It’s a little bit better than the previous example but still needs to be completed and a lot of code refactoring. I’ll do that in a future article to present a “full featured” IFD/EXIF metadata reader.

References and related articles

Getting Direct2D, DirectWrite and WIC Factories in MFC

MFC library offers a series of wrapper classes over Direct2D and DirectWrite interfaces (see CRenderTarget and CD2D classes). That’s pretty cool because allows to easily load and render images and draw texts, without care too much of direct dealing with COM interfaces.
However, let’s say we have to implemented something different so we need to start creating Direct2D or DirectWrite factories. Of course, we can call D2D1CreateFactory or DWriteCreateFactory functions, but a little bit handier is to use the factories instances created and kept by the MFC framework.

Getting Direct2D, DirectWrite and WIC factories in Visual Studio 2010

In MFC framework shipped with Visual Studio 2010, the instances of ID2D1Factory and IDWriteFactory are created and kept in a global AFX_GLOBAL_DATA structure named afxGlobalData. So, we have to do something like this:

Getting Direct2D, DirectWrite and WIC factories in Visual Studio 2012 – 2015

In Visual Studio 2012, these factory instances have been moved in a D2D-specific structure of type _AFX_D2D_STATE that can be accessed by calling AfxGetD2DState.

Now, we can note that MFC framework keeps also an instance of IWICImagingFactory which is internally used by CD2D classes. That’s also pretty cool; we’ll use it in the following example.

An example of getting and using IWICImagingFactory

Here is a brief example of getting IWICImagingFactory to further read metadata stored in JPEG, TIFF and other image formats.

References and related articles

Using Direct2D for Rendering WIC Bitmap Sources

Once having a WIC (Windows Imaging Component) source, we can render it by using GDI, GDI+ or Direct2D. There are many examples over the internet including MSDN Library that describes how to perform this.
This article describes how to use WIC Wrapper Library, which is designed for using WIC without worrying about directly deal with COM interfaces. The sample code as well as attached demo project uses MFC, but WIC Wrapper Library can be used in ATL or raw-Win32 projects, as well.

Let’s see the code, step by step!

  1. Add WIC and Direc2D factoy members to the aplication class.
  2. Create WIC and Direc2D factory instances after initializing OLE libraries.
  3. Release factories befor calling AfxOleTerm.

     
  4. Add WIC bitmap source member to document class then override CDocument::OnOpenDocument and CDocument::DeleteContents. Also add an implementation function that loads the WIC bitmap source from a file.


    Now we have a WIC bitmap source in the document class that will be used in the view class to create a Direct2D bitmap and render it in the view’s window.
  5. Add Direct2D render target and bitmap to the view class
  6. Create a Direct2D render target for the view window.

    A good place to call _CreateDirect2DRenderTarget is in the WM_CREATE message handler.
  7. Resize the Direct2D render target each time is necessary.

    If the render target must fit the view window, then the place to call _Resize2DRenderTarget is the WM_SIZE mesage handler.
  8. Create the Direct2D bitmap from WIC bitmap.

    In our case, the best place to call _CreateDirect2DBitmapFromWICBitmap is the overridden CView::OnUpdate function.
  9. Finally, render the bitmap.

    Of course, in a class derived from CView, the place for calling _RenderD2DBitmap is the overridden CView::OnDraw virtual function.
  10. Remains just few little things to ajust: handle WM_ERASEBKGND and override CScrollView::OnScrollBy. Anyway, you can find all the implementation details in the attached demo application.

Demo application

The demo application is a basic image file viewer that uses WIC Wrapper Library.
Download: WIC Wrapper Library v2_0 and Image Viewer Demo Project.zip (1192) (full WIC Wrapper Library source code is included).

Demo Image Viewer

Demo Image Viewer

Requirements

Resources, related articles, examples and projects

WIC File Open Dialog Using IFileOpenDialog Interface

A previoius article extends CFileDialog MFC class, in order to make a File Open or a File Save dialog that automatically filters files, according to available WIC (Windows Imaging Component) codecs. This article presents how to show a similar File Open dialog using IFileOpenDialog interface. This way, it can be used not only in an aplication that uses MFC, but also in an ATL one, even in a raw Win32 or console application that uses basic ATL stuff.
Let’s see an example.

Using IFileOpenDialog to show a WIC File Open Dialog

Enumerating the WIC decoders

Demo project

The demo project is a simple Win32 console application that uses the above functions.

Download: WIC_File_Open_Dialog_Win32_Demo.zip (594)

WIC File Open Dialog - Demo

WIC File Open Dialog – Demo

 

Notes

  • In a similar way, we can make a File Save dialog using IFileSaveDialog interface.
  • This article presents just proof of concepts. IFileOpenDialog offers much more methods to customize the dialog according to specific needs. And of course, a more elegant job can be done by writing some C++ wrapper class(es).
  • For the future, I intend to integrate WIC File Dialogs in WIC Wrapper Library also presented in this blog.
  • The built-in WIC decoders deal with the following image file formats: BMP, GIF, ICO, JPEG, PNG, TIFF and WMPhoto. However there are many other decoders that can befound over the internet. Just to mention two free ones: Microsoft Camera Codec Pack and Adobe DNG Codec for Windows.

Resources

Free WIC codecs download

See also

WIC Wrapper Library – Version 1.1

WIC Wrapper Library v1.1 has the following:

  • new and completed methods for existing wrapper classes in the previous version;
  • new classes: wic::Bitmap and wic::Palette;
  • a new header containg enums, WicWrapEnums.h.

Download: WIC Wrapper Library [1.1].zip (598)

Example 2: Using wic::ImagingFactory::CreateBitmapFromHICON and wic::Bitmap

The following example is close to the previous one, except that it uses wic::ImagingFactory::CreateBitmapFromHICON to display an icon resource.

Demo Application

The demo application displays an icon resource when no image file is loaded.

Download: WIC Wrapper Library Sample [1.1].zip (619)

WIC Wrapper Library – Sample Application #2

WIC Wrapper Library – Sample Application #2

 

See also

WIC Wrapper Library – Version 1.0

I’ve begun writing a series of wrapper classes over WIC (Windows Imaging Component) interfaces, that can easily be used in MFC, ATL, or Win32 applications without worry about directly dealing with COM interfaces.
This first version contains basic operation for loading, scaling and converting source image formats that are supported by WIC.
Download: WIC Wrapper Library.zip (488)
WIC Wrapper Library.zip archive contains the following:

    • Header files, located in \WicWrap\Include folder;
    • WicWrapDLL.dll, both Debug and Release builds, located in \WicWrap\Bin folder;
    • WicWrapDLL.lib, both Debug and Release builds, located in \WicWrap\Lib folder.

Adding WIC Wrapper Library to a project

As said before, WIC Wrapper Library can be used in MFC, ATL or Win32 projects. Here is an example shownig the steps for adding this library to an existing project:

        1. copy WicWrap folder, in the solution directory;
        2. in Solution Explorer window right-click on project name and choose the Properties menu item;
        3. choose All Configurations, go to Configuration Properties\C/C++ and add $(SolutionDir)WicWrap\Include in the Additional Include Directories field;
        4. go to Configuration Properties\Linker\Input and add WicWrapDLL.lib (also for all configurations);
        5. choose Debug configuration, go to Configuration Properties\Linker and add $(SolutionDir)WicWrap\Lib\Debug in the Additional Library Directories field;
        6. choose Release configuration, go to Configuration Properties\Linker and add $(SolutionDir)WicWrap\Lib\Release in the Additional Library Directories field
        7. copy the debug and release versions of WicWrapDLL.dll in solution Debug and Release output directories;
        8. in Solution Explorer window, right-click on project name, choose Add\Existing Item and add the WicWrap header files from \WicWrap\Include folder;
        9. include WicWrap.h in the project source code, e.g in stdafx.h .

Using WIC Wrapper Library

Next is a simple example, in an MFC program:

More details can be found in the demo application, attached below.

Demo Application

The demo application is a simple image viewer made in a simple SDI MFC project.
Download: WIC Wrapper Library Sample [1].zip (783)

WIC Wrapper Library - Sample Application

WIC Wrapper Library – Sample Application

Note: the image shown in the sample application is taken from SoftIcons.com site.

WIC-related articles and resources