CustomImageCodec Class

The base class of the codecs of the formats the library does not read or write itself. Register a codec in ImageConfiguration.Codecs to detect, identify, load and save its format through the entry points of the built-in formats.

public abstract class CustomImageCodec

Remarks

A codec recognizes its files from their first bytes (CustomImageCodec.IsMatch), never from their names. The library reads the whole file, within ImageResourceLimits.MaxEncodedBytes, before it calls CustomImageCodec.Decode or CustomImageCodec.Identify with the bytes: a codec never performs I/O, and the same code serves the span, stream and path overloads, synchronous and asynchronous.

The decoder creates its image with CustomImageDecodeContext.CreateImage, which applies the resource limits of the configuration and charges the pixels to the load, fills its pixels (for example with ImageFrame<TPixel>.ProcessPixelRows) and returns it. The library then applies the frame limit and the pixel type of a typed load (Image.Load), so a codec can always produce the pixel format that holds its samples best.

Throw InvalidImageContentException for malformed data, UnsupportedImageFeatureException for valid data the codec does not support, with ImageFormat.Custom as their format: the library propagates the exceptions of a codec unchanged.

A codec is shared by every operation of its configurations: it must be immutable and thread-safe.

Constructors

NameDescription
CustomImageCodec(ImageFormatInfo)Initializes a codec of a format.

CustomImageCodec(ImageFormatInfo)

CustomImageCodec.cs

Initializes a codec of a format.

protected CustomImageCodec(ImageFormatInfo formatInfo)

Parameters

formatInfo ImageFormatInfo
The description of the format, created with the public constructor of ImageFormatInfo.

Exceptions

ArgumentNullException
formatInfo is null.
ArgumentException
formatInfo describes a built-in format.

Properties

NameDescription
FormatInfoGets the description of the format: its name, its media types and its file extensions.

Gets the description of the format: its name, its media types and its file extensions.

public ImageFormatInfo FormatInfo { get; }

Property value

ImageFormatInfo

Methods

NameDescription
CreateEncoder()Creates the encoder of the format with its default settings, which Image.Save uses for the file extensions of CustomImageCodec.FormatInfo, or null (the default) when the codec only reads the format.
CreateImageInfo(Image)Creates the description of a decoded image of this format.
CreateImageInfo(int, int, PixelFormat, int?, ImageMetadata?)Creates the description of a file of this format from its header.
Decode(ReadOnlySpan<byte>, CustomImageDecodeContext)Decodes a file.
Identify(ReadOnlySpan<byte>, CustomImageDecodeContext)Describes a file without returning its pixels.
IsMatch(ReadOnlySpan<byte>)Determines whether data starts with the signature of the format.

CreateEncoder()

CustomImageCodec.cs

Creates the encoder of the format with its default settings, which Image.Save uses for the file extensions of CustomImageCodec.FormatInfo, or null (the default) when the codec only reads the format.

public virtual CustomImageEncoder? CreateEncoder()

Returns

CustomImageEncoder? A new encoder, or null.

CreateImageInfo overloads

CreateImageInfo(Image)

CustomImageCodec.cs

Creates the description of a decoded image of this format.

protected ImageInfo CreateImageInfo(Image image)
Parameters
image Image
The image.
Returns

ImageInfo The description of its size, pixel format, frames, animation settings and metadata.

Exceptions
ArgumentNullException
image is null.
ObjectDisposedException
image is disposed.

CreateImageInfo(int, int, PixelFormat, int?, ImageMetadata?)

CustomImageCodec.cs

Creates the description of a file of this format from its header.

protected ImageInfo CreateImageInfo(int width, int height, PixelFormat pixelFormat, int? frameCount = 1, ImageMetadata? metadata = null)
Parameters
width int
The width of the image.
height int
The height of the image.
pixelFormat PixelFormat
The pixel format CustomImageCodec.Decode produces.
frameCount int?
The number of displayed frames, or null when the header does not tell it.
metadata ImageMetadata?
The metadata of the file, or null for none. The description stores a copy.
Returns

ImageInfo The description, whose ImageInfo.FormatInfo is CustomImageCodec.FormatInfo.

Exceptions
ArgumentOutOfRangeException
A dimension or the frame count is not positive, or pixelFormat is not defined.

Decode(ReadOnlySpan<byte>, CustomImageDecodeContext)

Decodes a file.

public abstract Image Decode(ReadOnlySpan<byte> data, CustomImageDecodeContext context)

Parameters

data ReadOnlySpan<byte>
The whole file.
context CustomImageDecodeContext
The context of the load: create the image with it.

Returns

Image An image created by CustomImageDecodeContext.CreateImage.

Identify(ReadOnlySpan<byte>, CustomImageDecodeContext)

CustomImageCodec.cs

Describes a file without returning its pixels.

public virtual ImageInfo Identify(ReadOnlySpan<byte> data, CustomImageDecodeContext context)

Parameters

data ReadOnlySpan<byte>
The whole file.
context CustomImageDecodeContext
The context of the identification; its CustomImageDecodeContext.IdentifyMode is set.

Returns

ImageInfo The description, created by CustomImageCodec.CreateImageInfo or CustomImageCodec.CreateImageInfo.

Remarks

The default implementation decodes the file and describes the image (CustomImageCodec.CreateImageInfo). Override it to read the header only.

IsMatch(ReadOnlySpan<byte>)

Determines whether data starts with the signature of the format.

public abstract bool IsMatch(ReadOnlySpan<byte> prefix)

Parameters

prefix ReadOnlySpan<byte>
The first bytes of the data: Image.FormatDetectionPrefixLength bytes, or the whole data when it is shorter (at least 8 bytes: a shorter input is never recognized).

Returns

bool true if the codec reads the data.