toor1245 / MatrixDotNet

Powerful .NET library for calculating matrix
https://toor1245.github.io/MatrixDotNet/
MIT License
10 stars 3 forks source link

Investigate how will work Matrix with byte data type for handling image #84

Open toor1245 opened 3 years ago

toor1245 commented 3 years ago

need check how will be work matrix with System.Drawing.Image and other image library on other OS and define the best library for cross-platform.

Dilorfin commented 2 years ago

MatrixDotNet can be used with imaged. As proof of concept (work with one line bmp only...):

using System.Drawing;
using System.Runtime.InteropServices;
using MatrixDotNet;
using MatrixDotNet.Extensions.Conversion;

static class ImageExt
{
    public static byte[] toByteArray(this Image imageIn)
    {
        using var ms = new MemoryStream();
        imageIn.Save(ms, imageIn.RawFormat);
        return ms.ToArray();
    }

    public static int[] toRGBAIntArray(this Image imageIn)
    {
        var bytes = imageIn.toByteArray();
        var list = bytes.Skip(54).ToList();

        for (int i = 3; i < list.Count-3; i+=4)
        {
            list.Insert(i, 0);
        }

        byte[] arr = list.ToArray();

        return MemoryMarshal.Cast<byte, int>(arr).ToArray();
    }
}

class Program
{
    static int Main()
    {
        var image = Image.FromFile(@"D:\Projects\MatrixDotNet_ImagesTest\test.bmp");

        Matrix<int> a = new Matrix<int>(1, image.Width)
        {
            [0, State.Row] = image.toRGBAIntArray()
        };

        MatrixConverter.Reverse(a);

        byte[] rgba = MemoryMarshal.Cast<int, byte>(a[0, State.Row]).ToArray();

        Console.WriteLine(a);

        foreach (var b in rgba.Where((t, i) => t != 0).ToArray())
        {
            Console.WriteLine(b);
        }

        var file = File.Create(@"D:\Projects\MatrixDotNet_ImagesTest\test1.bmp");

        file.Write(image.toByteArray().Take(54).ToArray());
        file.Write(rgba.Where((t, i) => t != 0).ToArray());
        file.WriteByte(0);
        file.Close();

        return 0;
    }
}
Dilorfin commented 2 years ago

In proof of concept I used int for storing RGB data, however it's obviously has limitations, i.e. we can't work colors, just shuffle pixels. Idea of using bar bytes instead of ints as for me has even more problems.

So I think should be created possibility to work with logical structs or different abstraction. And I'm not sure if it is effective way of working with images.