Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/PixiParser.Skia/Encoders/QoiEncoder.Net8.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if NET8_0
using System;
using QoiSharpNoGC;
using QoiSharpNoGC.Codec;
using SkiaSharp;

namespace PixiEditor.Parser.Skia.Encoders;

public class QoiEncoder : ImageEncoder
{
public override string EncodedFormatName { get; } = "QOI";

public override byte[] Encode(byte[] rawBitmap, int width, int height, bool isSrgb) =>
QoiSharpNoGC.QoiEncoder.Encode(
new QoiImage(rawBitmap, width, height, Channels.RgbWithAlpha, isSrgb ? ColorSpace.SRgb : ColorSpace.Linear));

public override byte[] Decode(byte[] encodedData, out SKImageInfo info)
{
var qoiImage = QoiDecoder.Decode(encodedData);

var colorSpace = qoiImage.ColorSpace switch
{
ColorSpace.SRgb => SKColorSpace.CreateSrgb(),
ColorSpace.Linear => SKColorSpace.CreateSrgbLinear(),
_ => throw new IndexOutOfRangeException($"QOI color space '{qoiImage.ColorSpace}' is not supported.")
};

info = new SKImageInfo(qoiImage.Width, qoiImage.Height, SKColorType.Rgba8888, SKAlphaType.Premul, colorSpace);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why Rgba8888 instead of Bgra8888? What about decoding already encoded images?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also

Image

Does it encode premul?


return qoiImage.Data;
}

public override byte[] Decode(Span<byte> encodedData, out SKImageInfo info) => Decode(encodedData.ToArray(), out info);
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if NETSTANDARD
using System;
using Qoi.NetStandard;
using SkiaSharp;

Expand All @@ -24,3 +25,4 @@ public override byte[] Decode(byte[] encodedData, out SKImageInfo info)

public override byte[] Decode(Span<byte> encodedData, out SKImageInfo info) => Decode(encodedData.ToArray(), out info);
}
#endif
7 changes: 7 additions & 0 deletions src/PixiParser.Skia/PixiParser.Skia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="3.118.0-preview.1.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="QoiSharpNoGC" Version="1.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Qoi.NetStandard" Version="1.0.0" />
</ItemGroup>

Expand Down