Search code examples
c++directximguidirectxtk

How to access individual frames of an animated GIF loaded into a ID3D11ShaderResourceView?


I used CreateWICTextureFromFile() from DirectXTK to load an animated GIF texture.

ID3D11Resource* Resource;
ID3D11ShaderResourceView* View;
hr = CreateWICTextureFromFile(d3dDevice, L"sample.gif",
    &Resource, &View);

Then I displayed it on an ImageButton in dear IMGUI library:

ImGui::ImageButton((void*)View, ImVec2(width, height));

But it only displays a still image (the first frame of the GIF file).

I think I have to give it the texture of each frame separately. But I don't know how. Can you give me a hint?


Solution

  • The CreateWICTextureFromFile function in DirectX Tool Kit (a.k.a. the 'light-weight' version in the WICTextureLoader module) only loads a single 2D texture, not multi-frame images like animated GIF or TIFF.

    The DirectXTex function LoadFromWICFile can load multiframe images if you give it the WIC_FLAGS_ALL_FRAMES flag. Because the library is focused on DirectX resources, it will resize them all to match the first image size.

    That said, what WIC is going to return to you is a bunch of raw frames. You have query the metadata from WIC to actually get the animation information, and it's a little complicated to reconstruct. I have a simple implementation in the DirectXTex texassemble tool you can reference here. I focused on converting the animated GIF into a 'flip-book' style 2D texture array which is quite a bit larger.

    The sample I referenced can be found on GitHub