1using System.Runtime.InteropServices;
2using SixLabors.ImageSharp;
3using SixLabors.ImageSharp.Formats;
4using SixLabors.ImageSharp.Formats.Bmp;
5using SixLabors.ImageSharp.Formats.Gif;
6using SixLabors.ImageSharp.Formats.Jpeg;
7using SixLabors.ImageSharp.Formats.Pbm;
8using SixLabors.ImageSharp.Formats.Png;
9using SixLabors.ImageSharp.Formats.Qoi;
10using SixLabors.ImageSharp.Formats.Tga;
11using SixLabors.ImageSharp.Formats.Tiff;
12using SixLabors.ImageSharp.Formats.Webp;
13using SixLabors.ImageSharp.PixelFormats;
18 new BmpConfigurationModule(),
19 new GifConfigurationModule(),
20 new JpegConfigurationModule(),
21 new PbmConfigurationModule(),
22 new PngConfigurationModule(),
23 new QoiConfigurationModule(),
24 new TgaConfigurationModule(),
25 new TiffConfigurationModule(),
26 new WebpConfigurationModule()
31 public static readonly JpegEncoder
DefaultJpegEncoder =
new() { Quality = 95, ColorType = JpegEncodingColor.YCbCrRatio420 };
32 public static readonly GifEncoder
DefaultGifEncoder =
new() { ColorTableMode = GifColorTableMode.Local };
47 Span<Color> pixelsSpan =
m_pixels.AsSpan();
48 for (
int y = 0; y < accessor.Height; y++) {
49 MemoryMarshal.Cast<Rgba32,
Color>(accessor.GetRowSpan(y)).CopyTo(pixelsSpan.Slice(y *
Width,
Width));
66 ArgumentNullException.ThrowIfNull(image);
71 ArgumentNullException.ThrowIfNull(image);
76 ArgumentNullException.ThrowIfNull(image);
79 Span<Color> pixels = image.
Pixels.AsSpan();
80 for (
int y = 0; y < accessor.Height; y++) {
81 MemoryMarshal.Cast<
Color, Rgba32>(pixels.Slice(y * image.
Width, image.
Height)).CopyTo(accessor.GetRowSpan(y));
87 public Image(
int width,
int height) {
89 throw new ArgumentOutOfRangeException(nameof(width));
92 throw new ArgumentOutOfRangeException(nameof(height));
99 public Color GetPixel(
int x,
int y) => x < 0 || x >=
Width ?
throw new ArgumentOutOfRangeException(nameof(x)) :
100 y < 0 || y >=
Height ? throw new ArgumentOutOfRangeException(nameof(y)) : new
Color(
m_trueImage[x, y].PackedValue);
107 throw new ArgumentOutOfRangeException(nameof(x));
111 throw new ArgumentOutOfRangeException(nameof(y));
122 :
throw new InvalidOperationException(
"Unsupported image file format.");
127 :
throw new InvalidOperationException(
"Unsupported image file format.");
131 && IdentifiedFormat == format
133 : throw new FormatException($
"Image format({IdentifiedFormat}) is not ${format}");
137 return Load(stream, format);
152 BmpEncoder encoder =
new() { BitsPerPixel = saveAlpha ? BmpBitsPerPixel.Pixel32 : BmpBitsPerPixel.Pixel24 };
162 PngEncoder encoder =
new() {
163 ColorType = saveAlpha ? PngColorType.RgbWithAlpha : PngColorType.Rgb, TransparentColorMode = PngTransparentColorMode.Clear
199 QoiEncoder encoder =
new() {
200 ColorSpace = QoiColorSpace.SrgbWithLinearAlpha, Channels = saveAlpha ? QoiChannels.Rgba : QoiChannels.Rgb
211 TiffEncoder encoder =
new() { BitsPerPixel = saveAlpha ? TiffBitsPerPixel.Bit32 : TiffBitsPerPixel.Bit24 };
216 image.
m_trueImage.SaveAsTiffAsync(stream, encoder);
221 TgaEncoder encoder =
new() {
222 BitsPerPixel = saveAlpha ? TgaBitsPerPixel.Pixel32 : TgaBitsPerPixel.Pixel24, Compression = TgaCompression.RunLength
233 WebpEncoder encoder =
new() {
234 TransparentColorMode = saveAlpha ? WebpTransparentColorMode.Preserve : WebpTransparentColorMode.Clear,
235 FileFormat = WebpFileFormatType.Lossless
241 image.
m_trueImage.SaveAsWebpAsync(stream, encoder);
245 default:
throw new InvalidOperationException(
"Unsupported image file format.");
251 Save(image, stream, format, saveAlpha);
255 public void ProcessPixelRows(PixelAccessorAction<Rgba32> accessorAction,
bool shouldUpdatePixelsCache =
true) {
257 if (shouldUpdatePixelsCache) {
262 public void ProcessPixels(Func<Rgba32, Rgba32> pixelFunc,
bool shouldUpdatePixelsCache =
true) {
265 for (
int y = 0; y < accessor.Height; y++) {
266 foreach (ref Rgba32 pixel
in accessor.GetRowSpan(y)) {
267 pixel = pixelFunc(pixel);
271 shouldUpdatePixelsCache
static Stream OpenFile(string path, OpenFileMode openFileMode)