mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-06-22 10:55:00 +00:00
feature: supports to view .tiff
images
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
cb6d6a233f
commit
05757ebf40
3 changed files with 90 additions and 64 deletions
|
@ -4,6 +4,7 @@
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
Builtin,
|
Builtin,
|
||||||
Pfim
|
Pfim,
|
||||||
|
Tiff,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@
|
||||||
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.2.0" />
|
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.2.0" />
|
||||||
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.2.0" />
|
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.2.0" />
|
||||||
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
|
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
|
||||||
|
<PackageReference Include="BitMiracle.LibTiff.NET" Version="2.4.660" />
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||||
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc5.4" />
|
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc5.4" />
|
||||||
<PackageReference Include="OpenAI" Version="2.2.0-beta.4" />
|
<PackageReference Include="OpenAI" Version="2.2.0-beta.4" />
|
||||||
|
|
|
@ -7,6 +7,7 @@ using Avalonia;
|
||||||
using Avalonia.Media.Imaging;
|
using Avalonia.Media.Imaging;
|
||||||
using Avalonia.Platform;
|
using Avalonia.Platform;
|
||||||
|
|
||||||
|
using BitMiracle.LibTiff.Classic;
|
||||||
using Pfim;
|
using Pfim;
|
||||||
|
|
||||||
namespace SourceGit.ViewModels
|
namespace SourceGit.ViewModels
|
||||||
|
@ -39,6 +40,9 @@ namespace SourceGit.ViewModels
|
||||||
case ".tga":
|
case ".tga":
|
||||||
case ".dds":
|
case ".dds":
|
||||||
return Models.ImageDecoder.Pfim;
|
return Models.ImageDecoder.Pfim;
|
||||||
|
case ".tif":
|
||||||
|
case ".tiff":
|
||||||
|
return Models.ImageDecoder.Tiff;
|
||||||
default:
|
default:
|
||||||
return Models.ImageDecoder.None;
|
return Models.ImageDecoder.None;
|
||||||
}
|
}
|
||||||
|
@ -70,10 +74,22 @@ namespace SourceGit.ViewModels
|
||||||
var size = stream.Length;
|
var size = stream.Length;
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
{
|
{
|
||||||
if (decoder == Models.ImageDecoder.Builtin)
|
try
|
||||||
return DecodeWithAvalonia(stream, size);
|
{
|
||||||
else if (decoder == Models.ImageDecoder.Pfim)
|
switch (decoder)
|
||||||
return DecodeWithPfim(stream, size);
|
{
|
||||||
|
case Models.ImageDecoder.Builtin:
|
||||||
|
return DecodeWithAvalonia(stream, size);
|
||||||
|
case Models.ImageDecoder.Pfim:
|
||||||
|
return DecodeWithPfim(stream, size);
|
||||||
|
case Models.ImageDecoder.Tiff:
|
||||||
|
return DecodeWithTiff(stream, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.Out.WriteLine(e.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ImageSource(null, 0);
|
return new ImageSource(null, 0);
|
||||||
|
@ -81,75 +97,83 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
private static ImageSource DecodeWithAvalonia(Stream stream, long size)
|
private static ImageSource DecodeWithAvalonia(Stream stream, long size)
|
||||||
{
|
{
|
||||||
try
|
var bitmap = new Bitmap(stream);
|
||||||
{
|
return new ImageSource(bitmap, size);
|
||||||
var bitmap = new Bitmap(stream);
|
|
||||||
return new ImageSource(bitmap, size);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return new ImageSource(null, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ImageSource DecodeWithPfim(Stream stream, long size)
|
private static ImageSource DecodeWithPfim(Stream stream, long size)
|
||||||
{
|
{
|
||||||
try
|
using (var pfiImage = Pfimage.FromStream(stream))
|
||||||
{
|
{
|
||||||
using (var pfiImage = Pfimage.FromStream(stream))
|
var data = pfiImage.Data;
|
||||||
|
var stride = pfiImage.Stride;
|
||||||
|
|
||||||
|
var pixelFormat = PixelFormats.Bgra8888;
|
||||||
|
var alphaFormat = AlphaFormat.Opaque;
|
||||||
|
switch (pfiImage.Format)
|
||||||
{
|
{
|
||||||
var data = pfiImage.Data;
|
case ImageFormat.Rgb8:
|
||||||
var stride = pfiImage.Stride;
|
pixelFormat = PixelFormats.Gray8;
|
||||||
|
break;
|
||||||
|
case ImageFormat.R5g5b5:
|
||||||
|
case ImageFormat.R5g5b5a1:
|
||||||
|
pixelFormat = PixelFormats.Bgr555;
|
||||||
|
break;
|
||||||
|
case ImageFormat.R5g6b5:
|
||||||
|
pixelFormat = PixelFormats.Bgr565;
|
||||||
|
break;
|
||||||
|
case ImageFormat.Rgb24:
|
||||||
|
pixelFormat = PixelFormats.Bgr24;
|
||||||
|
break;
|
||||||
|
case ImageFormat.Rgba16:
|
||||||
|
var pixels2 = pfiImage.DataLen / 2;
|
||||||
|
data = new byte[pixels2 * 4];
|
||||||
|
stride = pfiImage.Width * 4;
|
||||||
|
for (var i = 0; i < pixels2; i++)
|
||||||
|
{
|
||||||
|
var src = BitConverter.ToUInt16(pfiImage.Data, i * 2);
|
||||||
|
data[i * 4 + 0] = (byte)Math.Round((src & 0x0F) / 15F * 255); // B
|
||||||
|
data[i * 4 + 1] = (byte)Math.Round(((src >> 4) & 0x0F) / 15F * 255); // G
|
||||||
|
data[i * 4 + 2] = (byte)Math.Round(((src >> 8) & 0x0F) / 15F * 255); // R
|
||||||
|
data[i * 4 + 3] = (byte)Math.Round(((src >> 12) & 0x0F) / 15F * 255); // A
|
||||||
|
}
|
||||||
|
|
||||||
var pixelFormat = PixelFormats.Bgra8888;
|
alphaFormat = AlphaFormat.Premul;
|
||||||
var alphaFormat = AlphaFormat.Opaque;
|
break;
|
||||||
switch (pfiImage.Format)
|
case ImageFormat.Rgba32:
|
||||||
{
|
alphaFormat = AlphaFormat.Premul;
|
||||||
case ImageFormat.Rgb8:
|
break;
|
||||||
pixelFormat = PixelFormats.Gray8;
|
default:
|
||||||
break;
|
return new ImageSource(null, 0);
|
||||||
case ImageFormat.R5g5b5:
|
|
||||||
case ImageFormat.R5g5b5a1:
|
|
||||||
pixelFormat = PixelFormats.Bgr555;
|
|
||||||
break;
|
|
||||||
case ImageFormat.R5g6b5:
|
|
||||||
pixelFormat = PixelFormats.Bgr565;
|
|
||||||
break;
|
|
||||||
case ImageFormat.Rgb24:
|
|
||||||
pixelFormat = PixelFormats.Bgr24;
|
|
||||||
break;
|
|
||||||
case ImageFormat.Rgba16:
|
|
||||||
var pixels2 = pfiImage.DataLen / 2;
|
|
||||||
data = new byte[pixels2 * 4];
|
|
||||||
stride = pfiImage.Width * 4;
|
|
||||||
for (var i = 0; i < pixels2; i++)
|
|
||||||
{
|
|
||||||
var src = BitConverter.ToUInt16(pfiImage.Data, i * 2);
|
|
||||||
data[i * 4 + 0] = (byte)Math.Round((src & 0x0F) / 15F * 255); // B
|
|
||||||
data[i * 4 + 1] = (byte)Math.Round(((src >> 4) & 0x0F) / 15F * 255); // G
|
|
||||||
data[i * 4 + 2] = (byte)Math.Round(((src >> 8) & 0x0F) / 15F * 255); // R
|
|
||||||
data[i * 4 + 3] = (byte)Math.Round(((src >> 12) & 0x0F) / 15F * 255); // A
|
|
||||||
}
|
|
||||||
|
|
||||||
alphaFormat = AlphaFormat.Premul;
|
|
||||||
break;
|
|
||||||
case ImageFormat.Rgba32:
|
|
||||||
alphaFormat = AlphaFormat.Premul;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return new ImageSource(null, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
|
|
||||||
var pixelSize = new PixelSize(pfiImage.Width, pfiImage.Height);
|
|
||||||
var dpi = new Vector(96, 96);
|
|
||||||
var bitmap = new Bitmap(pixelFormat, alphaFormat, ptr, pixelSize, dpi, stride);
|
|
||||||
return new ImageSource(bitmap, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
|
||||||
|
var pixelSize = new PixelSize(pfiImage.Width, pfiImage.Height);
|
||||||
|
var dpi = new Vector(96, 96);
|
||||||
|
var bitmap = new Bitmap(pixelFormat, alphaFormat, ptr, pixelSize, dpi, stride);
|
||||||
|
return new ImageSource(bitmap, size);
|
||||||
}
|
}
|
||||||
catch
|
}
|
||||||
|
|
||||||
|
private static ImageSource DecodeWithTiff(Stream stream, long size)
|
||||||
|
{
|
||||||
|
using (var tiff = Tiff.ClientOpen($"{Guid.NewGuid()}.tif", "r", stream, new TiffStream()))
|
||||||
{
|
{
|
||||||
return new ImageSource(null, 0);
|
if (tiff == null)
|
||||||
|
return new ImageSource(null, 0);
|
||||||
|
|
||||||
|
var width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
|
||||||
|
var height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
|
||||||
|
var pixels = new int[width * height];
|
||||||
|
|
||||||
|
// Currently only supports image when its `BITSPERSAMPLE` is one in [1,2,4,8,16]
|
||||||
|
tiff.ReadRGBAImageOriented(width, height, pixels, Orientation.TOPLEFT);
|
||||||
|
|
||||||
|
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(pixels, 0);
|
||||||
|
var pixelSize = new PixelSize(width, height);
|
||||||
|
var dpi = new Vector(96, 96);
|
||||||
|
var bitmap = new Bitmap(PixelFormats.Rgba8888, AlphaFormat.Premul, ptr, pixelSize, dpi, width * 4);
|
||||||
|
return new ImageSource(bitmap, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue