mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-06-15 07:35:04 +00:00
feature: .dds
image support (#1392)
* Added Pfim as 3rdparty lib * Added support for parsing showing dds and tga images using Pfim --------- Co-authored-by: Snimax <snimax@live.se>
This commit is contained in:
parent
7bba40d03f
commit
a2ca071f08
3 changed files with 91 additions and 0 deletions
|
@ -4,5 +4,6 @@
|
|||
{
|
||||
None = 0,
|
||||
Builtin,
|
||||
Pfim
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Pfim;
|
||||
|
||||
namespace SourceGit.ViewModels
|
||||
{
|
||||
|
@ -27,6 +29,9 @@ namespace SourceGit.ViewModels
|
|||
case ".png":
|
||||
case ".webp":
|
||||
return Models.ImageDecoder.Builtin;
|
||||
case ".tga":
|
||||
case ".dds":
|
||||
return Models.ImageDecoder.Pfim;
|
||||
default:
|
||||
return Models.ImageDecoder.None;
|
||||
}
|
||||
|
@ -70,9 +75,93 @@ namespace SourceGit.ViewModels
|
|||
// 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…
Add table
Add a link
Reference in a new issue