Created
March 26, 2026 16:13
-
-
Save antonfirsov/4db2af5b5ac6ed5f9b485cc750c95991 to your computer and use it in GitHub Desktop.
LoadPreservedPixelType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static Image LoadPreservePixelType(string path) | |
| { | |
| var info = Image.Identify(path); | |
| var format = info.Metadata.DecodedImageFormat; | |
| // PNG: PngColorType fully describes the pixel layout including alpha | |
| if (format is PngFormat) return LoadPng(path, info); | |
| // BMP, TGA: native channel order is BGR/BGRA | |
| if (format is BmpFormat) return LoadBmp(path, info); | |
| if (format is TgaFormat) return LoadTga(path, info); | |
| // TIFF, JPEG, WebP, GIF, QOI, PBM, etc. | |
| // AlphaRepresentation is not populated by Image.Identify in 3.1.x, so we | |
| // rely on BitsPerPixel only. TIFF limitation: La16/La32 are indistinguishable | |
| // from L16/Rgba32 without photometric interpretation metadata. | |
| return info.PixelType.BitsPerPixel switch | |
| { | |
| 8 => Image.Load<L8>(path), | |
| 16 => Image.Load<L16>(path), | |
| 24 => Image.Load<Rgb24>(path), | |
| 32 => Image.Load<Rgba32>(path), | |
| 48 => Image.Load<Rgb48>(path), | |
| 64 => Image.Load<Rgba64>(path), | |
| _ => Image.Load(path) | |
| }; | |
| static Image LoadPng(string path, ImageInfo info) | |
| { | |
| var meta = info.Metadata.GetPngMetadata(); | |
| return (meta.ColorType, info.PixelType.BitsPerPixel) switch | |
| { | |
| (PngColorType.Grayscale, 8) => Image.Load<L8>(path), | |
| (PngColorType.Grayscale, 16) => Image.Load<L16>(path), | |
| (PngColorType.GrayscaleWithAlpha, 16) => Image.Load<La16>(path), | |
| (PngColorType.GrayscaleWithAlpha, 32) => Image.Load<La32>(path), | |
| (PngColorType.Rgb, 24) => Image.Load<Rgb24>(path), | |
| (PngColorType.Rgb, 48) => Image.Load<Rgb48>(path), | |
| (PngColorType.RgbWithAlpha, 32) => Image.Load<Rgba32>(path), | |
| (PngColorType.RgbWithAlpha, 64) => Image.Load<Rgba64>(path), | |
| _ => Image.Load(path) // Palette, exotic | |
| }; | |
| } | |
| static Image LoadBmp(string path, ImageInfo info) => | |
| info.Metadata.GetBmpMetadata().BitsPerPixel switch | |
| { | |
| BmpBitsPerPixel.Pixel16 => Image.Load<Bgr565>(path), | |
| BmpBitsPerPixel.Pixel24 => Image.Load<Bgr24>(path), | |
| BmpBitsPerPixel.Pixel32 => Image.Load<Bgra32>(path), | |
| _ => Image.Load(path) // Pixel1/2/4/8: palette/indexed | |
| }; | |
| static Image LoadTga(string path, ImageInfo info) => | |
| info.Metadata.GetTgaMetadata().BitsPerPixel switch | |
| { | |
| TgaBitsPerPixel.Pixel8 => Image.Load<L8>(path), | |
| TgaBitsPerPixel.Pixel16 => Image.Load<Bgra5551>(path), // 5+5+5+1 | |
| TgaBitsPerPixel.Pixel24 => Image.Load<Bgr24>(path), | |
| TgaBitsPerPixel.Pixel32 => Image.Load<Bgra32>(path), | |
| _ => Image.Load(path) | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment