Skip to content

Commit 203c51f

Browse files
authored
feature: ImageSource supports loading psd files (#2304)
1 parent d8916c5 commit 203c51f

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎src/Models/ImageDecoder.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public enum ImageDecoder
66
Builtin,
77
Pfim,
88
Tiff,
9+
StbImage,
910
}
1011
}

‎src/SourceGit.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0" />
6060
<PackageReference Include="OpenAI" Version="2.10.0" />
6161
<PackageReference Include="Pfim" Version="0.11.4" />
62+
<PackageReference Include="StbImageSharp" Version="2.30.15" />
6263

6364
<ProjectReference Include="../depends/AvaloniaEdit/src/AvaloniaEdit.TextMate/AvaloniaEdit.TextMate.csproj" />
6465
</ItemGroup>

‎src/ViewModels/ImageSource.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Avalonia.Platform;
1010
using BitMiracle.LibTiff.Classic;
1111
using Pfim;
12+
using StbImageSharp;
1213

1314
namespace SourceGit.ViewModels
1415
{
@@ -32,6 +33,7 @@ public static Models.ImageDecoder GetDecoder(string file)
3233
".ico" or ".bmp" or ".gif" or ".jpg" or ".jpeg" or ".png" or ".webp" => Models.ImageDecoder.Builtin,
3334
".tga" or ".dds" => Models.ImageDecoder.Pfim,
3435
".tif" or ".tiff" => Models.ImageDecoder.Tiff,
36+
".psd" => Models.ImageDecoder.StbImage,
3537
_ => Models.ImageDecoder.None,
3638
};
3739
}
@@ -77,6 +79,8 @@ private static ImageSource LoadFromStream(Stream stream, Models.ImageDecoder dec
7779
return DecodeWithPfim(stream, size);
7880
case Models.ImageDecoder.Tiff:
7981
return DecodeWithTiff(stream, size);
82+
case Models.ImageDecoder.StbImage:
83+
return DecodeWithStbImage(stream, size);
8084
}
8185
}
8286
catch (Exception e)
@@ -190,5 +194,19 @@ private static ImageSource DecodeWithTiff(Stream stream, long size)
190194
return new ImageSource(bitmap, size);
191195
}
192196
}
197+
198+
private static ImageSource DecodeWithStbImage(Stream stream, long size)
199+
{
200+
var image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
201+
202+
var data = image.Data;
203+
var stride = image.Width * (int)image.Comp;
204+
205+
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
206+
var pixelSize = new PixelSize(image.Width, image.Height);
207+
var dpi = new Vector(96, 96);
208+
var bitmap = new Bitmap(PixelFormat.Rgba8888, AlphaFormat.Unpremul, ptr, pixelSize, dpi, stride);
209+
return new ImageSource(bitmap, size);
210+
}
193211
}
194212
}

0 commit comments

Comments
 (0)