NUlliiON / QoiSharp

QoiSharp is an implementation of the QOI https://github.com/phoboslab/qoi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System.IndexOutOfRangeException 

neod opened this issue · comments

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=QoiSharp
  Arborescence des appels de procédure :
   à QoiSharp.QoiEncoder.Encode(QoiImage image)
   à Program.<<Main>$>d__0.MoveNext() dans D:\Users\neod\source\repos\ConsoleAppQOI\ConsoleAppQOI\Program.cs :ligne 28
string pngFileName = @"d:\Users\neod\Pictures\file_example_PNG_500kB.png"; // your png file
string qoiFileName = pngFileName.Replace(".png", ".qoi");
ImageInfo? pngInfo;

using (FileStream fspng = File.Open(pngFileName, FileMode.Open))
{
    pngInfo = ImageInfo.FromStream(fspng);
}

if (pngInfo != null)
{
    byte channels = 3;

    byte[] data = ImageResult.FromMemory(await File.ReadAllBytesAsync(pngFileName),
        pngInfo.Value.ColorComponents).Data;

    QoiImage qoiImage = new QoiImage(data, pngInfo.Value.Width, pngInfo.Value.Height, channels);
    byte[] qoiData = QoiEncoder.Encode(qoiImage); // <<< line 28

    FileStream fs = File.Create(qoiFileName);
    fs.Write(qoiData, 0, qoiData.Length);
}
commented

The library is currently in beta.
Please try something like this:

var image = ImageResult.FromMemory(await File.ReadAllBytesAsync(pngFileName), ColorComponents.RedGreenBlueAlpha);
byte[] qoiData = QoiEncoder.Encode(new QoiImage(image.Data, image.Width, image.Height, (byte)image.Comp));

I confirm that by forcing ColorComponents.RedGreenBlueAlpha, we bypass the error