Skip to content

ImageFormats

Namespace Nfty.Core.Imaging in assembly Nfty.Core.

public static class ImageFormats

THE one way nfty decodes a picture. Every Image.Load, Image.Identify and DetectFormat in the product goes through here, so exactly two decoders (PNG, JPEG) are reachable and no other.

Remarks

Why: ImageSharp is pinned at 3.1.11 (see Directory.Packages.props) and that version carries six advisories whose fixes exist only in 3.2.0+ (which needs a license key). Five are TIFF decoders/encoders or HistogramEqualization, none of which nfty ever calls; the sixth is ICC CLUT parsing, reached only through IccProfile.Entries/CheckIntegrity, which nothing here touches. Registering only PNG + JPEG makes the TIFF half unreachable from any file a user hands us, whatever its extension says, and SkipMetadata keeps embedded ICC profiles from being read.

A file in any other format is refused with UnknownImageFormatException (the type every existing caller already handles) carrying a message in the product's words. The keepMetadata overloads exist for ONE reason: the sprite sheet's nfty-grid text chunk lives in PNG metadata. There a PNG's ICC chunk is stored unparsed and written back untouched.

Encoders are untouched, so no output byte changes.

Fields

Refusal

public const string Refusal = "nfty opens only PNG and JPEG pictures; this file is in another format (or is not a picture).";

What a refused file is told.

Methods

DetectFormat(string)

public static IImageFormat DetectFormat(string path);

Names a file's format, among the two nfty reads.

Parameters

  • path (string) - The file.

Returns. The format.

Exceptions

  • UnknownImageFormatException - The file is neither PNG nor JPEG.

Identify(byte[])

public static ImageInfo Identify(byte[] bytes);

Reads a picture's size without decoding pixels.

Parameters

  • bytes (byte[]) - The encoded PNG or JPEG.

Returns. What the header says.

Exceptions

  • UnknownImageFormatException - The bytes are neither PNG nor JPEG.

Identify(string, bool)

public static ImageInfo Identify(string path, bool keepMetadata = false);

Reads a picture's size (and, when asked, its metadata) without decoding pixels.

Parameters

  • path (string) - The PNG or JPEG file.
  • keepMetadata (bool) - Whether PNG text chunks are read.

Returns. What the header says.

Exceptions

  • UnknownImageFormatException - The file is neither PNG nor JPEG.

Load(byte[])

public static Image<Rgba32> Load(byte[] bytes);

Decodes picture bytes.

Parameters

  • bytes (byte[]) - The encoded PNG or JPEG.

Returns. The decoded image; the caller owns it.

Exceptions

  • UnknownImageFormatException - The bytes are neither PNG nor JPEG.

Load(string)

public static Image<Rgba32> Load(string path);

Decodes a picture file.

Parameters

  • path (string) - The PNG or JPEG file.

Returns. The decoded image; the caller owns it.

Exceptions

  • UnknownImageFormatException - The file is neither PNG nor JPEG.

LoadAny(string, bool)

public static Image LoadAny(string path, bool keepMetadata = false);

Decodes a picture file without fixing the pixel type.

Parameters

  • path (string) - The PNG or JPEG file.
  • keepMetadata (bool) - Whether PNG text chunks are read (the sheet grid lives in one).

Returns. The decoded image; the caller owns it.

Exceptions

  • UnknownImageFormatException - The file is neither PNG nor JPEG.

LoadAsync(string, CancellationToken)

public static Task<Image<Rgba32>> LoadAsync(string path, CancellationToken ct = default);

Decodes a picture file asynchronously.

Parameters

  • path (string) - The PNG or JPEG file.
  • ct (CancellationToken) - Cancels the decode.

Returns. The decoded image; the caller owns it.

Exceptions

  • UnknownImageFormatException - The file is neither PNG nor JPEG.