Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Models/ImageDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum ImageDecoder
{
None = 0,
Builtin,
Pfim
}
}
1 change: 1 addition & 0 deletions src/SourceGit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc5.4" />
<PackageReference Include="OpenAI" Version="2.2.0-beta.4" />
<PackageReference Include="Pfim" Version="0.11.3" />
<PackageReference Include="TextMateSharp" Version="1.0.66" />
<PackageReference Include="TextMateSharp.Grammars" Version="1.0.66" />
</ItemGroup>
Expand Down
89 changes: 89 additions & 0 deletions src/ViewModels/ImageSource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using System.Runtime.InteropServices;
using Avalonia.Media.Imaging;
using Pfim;

namespace SourceGit.ViewModels
{
Expand Down Expand Up @@ -27,6 +29,9 @@ public static Models.ImageDecoder GetDecoder(string file)
case ".png":
case ".webp":
return Models.ImageDecoder.Builtin;
case ".tga":
case ".dds":
return Models.ImageDecoder.Pfim;
default:
return Models.ImageDecoder.None;
}
Expand Down Expand Up @@ -70,9 +75,93 @@ private static ImageSource LoadFromStream(Stream stream, Models.ImageDecoder dec
// Just ignore.
}
}
else if (decoder == Models.ImageDecoder.Pfim)
{
return new ImageSource(LoadWithPfim(stream), size);
}
}

return new ImageSource(null, 0);
}

private static Bitmap LoadWithPfim(Stream stream)
{
var image = Pfim.Pfimage.FromStream(stream);
byte[] data;
int stride;
if (image.Format == ImageFormat.Rgba32)
{
data = image.Data;
stride = image.Stride;
}
else
{
int pixels = image.Width * image.Height;
data = new byte[pixels * 4];
stride = image.Width * 4;

switch (image.Format)
{
case ImageFormat.Rgba16:
case ImageFormat.R5g5b5a1:
{
for (int i = 0; i < pixels; i++)
{
data[i * 4 + 0] = image.Data[i * 4 + 2]; // B
data[i * 4 + 1] = image.Data[i * 4 + 1]; // G
data[i * 4 + 2] = image.Data[i * 4 + 0]; // R
data[i * 4 + 3] = image.Data[i * 4 + 3]; // A
}
}
break;
case ImageFormat.R5g5b5:
case ImageFormat.R5g6b5:
case ImageFormat.Rgb24:
{
for (int i = 0; i < pixels; i++)
{
data[i * 4 + 0] = image.Data[i * 3 + 2]; // B
data[i * 4 + 1] = image.Data[i * 3 + 1]; // G
data[i * 4 + 2] = image.Data[i * 3 + 0]; // R
data[i * 4 + 3] = 255; // A
}
}
break;
case ImageFormat.Rgb8:
{
for (int i = 0; i < pixels; i++)
{
var color = image.Data[i];
data[i * 4 + 0] = color;
data[i * 4 + 1] = color;
data[i * 4 + 2] = color;
data[i * 4 + 3] = 255;
}
}
break;
default:
return null;
}
}

// Pin the array and pass the pointer to Bitmap
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
var bitmap = new Bitmap(
Avalonia.Platform.PixelFormat.Bgra8888,
Avalonia.Platform.AlphaFormat.Unpremul,
ptr,
new Avalonia.PixelSize(image.Width, image.Height),
new Avalonia.Vector(96, 96),
stride);
return bitmap;
}
finally
{
handle.Free();
}
}
}
}
Loading