Repository: mxgmn/MarkovJunior
File: source/Graphics.cs (C#)

Code before:
            for (var j = 0; j < height; j += 1)
            {
                for (var i = 0; i < width; i += 1)
                {
                    result[j * width + i] = unchecked((int) LeReverseBytes(image[i, j].Argb));
                }
            }

            image.Dispose();
            return (result, width, height, 1);
        }
        catch (Exception)
        {
            return (null, -1, -1, -1);
        }
    }
    
    static UInt32 ReverseBytes(UInt32 value)
    {
        return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
               (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
    }

    static UInt32 LeReverseBytes(UInt32 value)
    {
        return BitConverter.IsLittleEndian ? ReverseBytes(value) : value;
    }


    public static void SaveBitmap(int[] data, int width, int height, string filename)
    {
        Argb32[] formattedData = new Argb32[data.Length];
        for (int i = 0; i < data.Length; i += 1)
        {
            formattedData[i] = new Argb32(LeReverseBytes(unchecked((uint) data[i])));
        }

        var image = Image.WrapMemory(Configuration.Default, new Memory<Argb32>(formattedData), width, height);
        image.SaveAsPng(filename);
        image.Dispose();
    }

    public static (int[], int, int) Render(byte[] state, int MX, int MY, int MZ, int[] colors, int pixelsize, int MARGIN) => MZ == 1 ? BitmapRender(state, MX, MY, colors, pixelsize, MARGIN) : IsometricRender(state, MX, MY, MZ, colors, pixelsize, MARGIN);

    public static (int[], int, int) BitmapRender(byte[] state, int MX, int MY, int[] colors, int pixelsize, int MARGIN)
    {
        int WIDTH = MARGIN + MX * pixelsize, HEIGHT = MY * pixelsize;
        int TOTALWIDTH = WIDTH, TOTALHEIGHT = HEIGHT;
        //int TOTALWIDTH = 189 + MARGIN, TOTALHEIGHT = 189;
        int[] bitmap = new int[TOTALWIDTH * TOTALHEIGHT];
        for (int i = 0; i < bitmap.Length; i++) bitmap[i] = GUI.BACKGROUND;

Review comment (suggestion):
You can use [`BinaryPrimitives.ReverseEndianness(uint value)`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives.reverseendianness?view=net-6.0#system-buffers-binary-binaryprimitives-reverseendianness(system-uint32)) which is a JIT intrinsic and emits `bswap` directly.

Code after:
            var result = new int[width * height];
            for (var j = 0; j < height; j += 1)
            {
                for (var i = 0; i < width; i += 1)
                {
                    result[j * width + i] = unchecked((int) image[i, j].Bgra);
                }
            }

            image.Dispose();
            return (result, width, height, 1);
        }
        catch (Exception)
        {
            return (null, -1, -1, -1);
        }
    }

    public static void SaveBitmap(int[] data, int width, int height, string filename)
    {
        var formattedData = new Bgra32[data.Length];
        for (var i = 0; i < data.Length; i += 1)
        {
            formattedData[i] = new Bgra32
            {
                Bgra = unchecked((uint) data[i])
            };
        }

        var image = Image.WrapMemory(Configuration.Default, new Memory<Bgra32>(formattedData), width, height);
        image.SaveAsPng(filename);
        image.Dispose();
    }

    public static (int[], int, int) Render(byte[] state, int MX, int MY, int MZ, int[] colors, int pixelsize, int MARGIN) => MZ == 1 ? BitmapRender(state, MX, MY, colors, pixelsize, MARGIN) : IsometricRender(state, MX, MY, MZ, colors, pixelsize, MARGIN);

    public static (int[], int, int) BitmapRender(byte[] state, int MX, int MY, int[] colors, int pixelsize, int MARGIN)
    {
        int WIDTH = MARGIN + MX * pixelsize, HEIGHT = MY * pixelsize;
        int TOTALWIDTH = WIDTH, TOTALHEIGHT = HEIGHT;
        //int TOTALWIDTH = 189 + MARGIN, TOTALHEIGHT = 189;
        int[] bitmap = new int[TOTALWIDTH * TOTALHEIGHT];
        for (int i = 0; i < bitmap.Length; i++) bitmap[i] = GUI.BACKGROUND;
        //for (int i = 0; i < bitmap.Length; i++) bitmap[i] = 255 << 24;

        int DX = (TOTALWIDTH - WIDTH) / 2;
        int DY = (TOTALHEIGHT - HEIGHT) / 2;

        for (int y = 0; y < MY; y++) for (int x = 0; x < MX; x++)
            {
                int c = colors[state[x + y * MX]];