> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/files-and-fonts/file-loading/custom-image-formats.md).

# Custom Image Formats

### Introduction

[Supported Files](https://docs.flatredball.com/gum/code/files-and-fonts/supported-files.md#image-formats) lists the image formats each runtime loads on its own. To load another format, add an image decoder. A decoder turns the file's bytes into pixels and Gum turns those pixels into a texture, so one decoder works on every runtime.

SkiaGum already loads WebP. The WebP example below is for MonoGame, KNI, FNA and raylib games, and it decodes with SkiaSharp, which those games add as a package.

{% hint style="info" %}
Available in November 2026, or now if building Gum from source.
{% endhint %}

### Writing a Decoder

A decoder implements `IImageDecoder`. Gum calls `TryDecode` with the file name and a stream positioned at the start of the file:

* Return `false` when the file is not your format. Gum then asks the next decoder, and then uses its own loading.
* Return `true` with a `DecodedImage` when you decoded the file.
* Throw when the file is your format but is broken. The load fails with your exception, and no other decoder runs.

`DecodedImage` holds RGBA bytes (4 bytes per pixel, row by row), the width, the height, and whether the colors are already multiplied by alpha. Most decoders return straight (not premultiplied) alpha, which is the default.

The following decoder loads WebP files through SkiaSharp. It checks the file's header, so it declines any file that is not WebP even when the extension says otherwise:

```csharp
public class WebPDecoder : RenderingLibrary.Content.IImageDecoder
{
    public bool TryDecode(string fileName, System.IO.Stream stream,
        out RenderingLibrary.Content.DecodedImage image)
    {
        image = null;

        // A WebP file starts with "RIFF", four size bytes, then "WEBP".
        byte[] header = new byte[12];
        if (stream.Read(header, 0, 12) < 12
            || System.Text.Encoding.ASCII.GetString(header, 0, 4) != "RIFF"
            || System.Text.Encoding.ASCII.GetString(header, 8, 4) != "WEBP")
        {
            return false;
        }
        stream.Position = 0;

        using SkiaSharp.SKCodec codec = SkiaSharp.SKCodec.Create(stream)
            ?? throw new System.IO.InvalidDataException($"Could not decode {fileName}");
        var info = new SkiaSharp.SKImageInfo(codec.Info.Width, codec.Info.Height,
            SkiaSharp.SKColorType.Rgba8888, SkiaSharp.SKAlphaType.Unpremul);
        SkiaSharp.SKCodecResult result = codec.GetPixels(info, out byte[] rgba);
        if (result != SkiaSharp.SKCodecResult.Success)
        {
            throw new System.IO.InvalidDataException($"Could not decode {fileName}: {result}");
        }

        image = new RenderingLibrary.Content.DecodedImage(rgba, info.Width, info.Height);
        return true;
    }
}
```

This decoder loads the first frame of an animated WebP file.

### Registering a Decoder

Add the decoder to `LoaderManager.Self.ImageDecoders` before Gum [loads your project](https://docs.flatredball.com/gum/code/getting-started/setup/loading-a-gum-project-.gumx.md). Textures that load before the decoder is registered go through Gum's own loading and fail for formats it does not know.

```csharp
// Before the GumUI.Initialize call that loads your project
RenderingLibrary.Content.LoaderManager.Self.ImageDecoders.Add(new WebPDecoder());
```

Gum asks decoders in the order they were added, and `Insert(0, decoder)` puts a decoder first.

A decoded texture goes into Gum's texture cache under its file name, so each file is decoded once. Files loaded through a MonoGame `ContentManager` (XNB files) never reach decoders.

Decoders also apply to the page images of a bitmap font (`.fnt`).

If no decoder handles a file whose format Gum does not load, the load fails with a message naming the extension and `LoaderManager.Self.ImageDecoders`.

### Premultiplied Alpha

Set `isPremultiplied` to `true` when the decoder returns colors already multiplied by alpha:

```csharp
// Initialize
image = new RenderingLibrary.Content.DecodedImage(rgba, width, height, isPremultiplied: true);
```

Gum then skips its own premultiply step. When the renderer draws straight alpha (`Renderer.UsePremultipliedAlpha` is `false`), Gum converts the pixels back to straight alpha and logs a warning, because the conversion loses precision for nearly transparent pixels. Return straight alpha when the library offers it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/gum/code/files-and-fonts/file-loading/custom-image-formats.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
