opentk / LearnOpenTK

A port of learnopengl.com's tutorials to OpenTK and C#.
Creative Commons Attribution 4.0 International
466 stars 115 forks source link

("Textures" page) ImageSharp's GetPixelSpan() appears to have been removed/replaced #39

Open DoctorLogiq opened 4 years ago

DoctorLogiq commented 4 years ago

Hi all, Some of the information on the "Textures" page of the Learn OpenTK page may have fallen out of date, as unless my IDE (Visual Studio) is just being the usual bag of uselessness that it often is, one of the ImageSharp functions has changed.

It appears you can no longer create a byte array simply by using image.GetPixelSpan().ToArray().

Possibly related to: https://github.com/SixLabors/ImageSharp/issues/1164.

Sioma112233 commented 4 years ago

Hey, I'm also learning now using the documentation and noticed this. I've checked the current API of ImageSharp and updated the code in my project, so until they update it, you can use it:

Image<Rgba32> image = Image.Load<Rgba32>(imagePath);
image.Mutate(x => x.Flip(FlipMode.Vertical));

var pixels = new List<byte>(4 * image.Width * image.Height);

for (int y = 0; y < image.Height; y++) {
    var row = image.GetPixelRowSpan(y);

    for (int x = 0; x < image.Width; x++) {
        pixels.Add(row[x].R);
        pixels.Add(row[x].G);
        pixels.Add(row[x].B);
        pixels.Add(row[x].A);
    }
}

I also gave the list constructor a capacity value because I know how big the list will be