OverviewFilesCommitsIssuesDocsReleasesPeers

ifsrenderer-linux / AvaloniaShell / ImageWriter.cs

27 lines · csharp ref:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace IFSRenderer.AvaloniaShell;

internal static class ImageWriter
{
    /// <summary>
    /// Writes a PNG from the engine's BGRA8 output.
    /// OpenGL reads bottom-up, so rows are flipped (the WPF app did RotateNoneFlipY).
    /// </summary>
    public static void SavePng(string path, byte[] bgra, int width, int height)
    {
        using var image = new Image<Rgba32>(width, height);
        for (int y = 0; y < height; y++)
        {
            int srcRow = (height - 1 - y) * width * 4;
            for (int x = 0; x < width; x++)
            {
                int i = srcRow + x * 4;
                image[x, y] = new Rgba32(bgra[i + 2], bgra[i + 1], bgra[i + 0], bgra[i + 3]);
            }
        }
        image.SaveAsPng(path);
    }
}