Support .pdn in ThumbnailViewer #1708
Some checks failed
MSBuild / build (push) Has been cancelled
MSBuild / publish (push) Has been cancelled

Implementation still under adjustment.
This commit is contained in:
ema
2025-08-02 02:31:05 +08:00
parent 9fe37520d3
commit 5cedcff912
4 changed files with 113 additions and 104 deletions

View File

@@ -1,104 +0,0 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
internal class PdnProvider : AnimationProvider
{
public PdnProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
KeyTime.FromTimeSpan(TimeSpan.Zero)));
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
// Skip thumbnail
return new Task<BitmapSource>(() => null);
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
return new Task<BitmapSource>(() =>
{
try
{
using TextReader reader = new StreamReader(Path.LocalPath, Encoding.UTF8);
string line = reader.ReadLine();
if (!line.StartsWith("PDN"))
return null;
int indexOfStart = line.IndexOf("<");
int indexOfEnd = line.LastIndexOf(">");
if (indexOfStart < 0 || indexOfEnd < 0)
return null;
string xml = line.Substring(indexOfStart, indexOfEnd - indexOfStart + 1);
// <pdnImage>
// <custom>
// <thumb png="..." />
// </custom>
// </pdnImage>
XDocument doc = XDocument.Parse(xml);
var pngBase64 = doc.Root
?.Element("custom")
?.Element("thumb")
?.Attribute("png")
?.Value;
if (pngBase64 != null)
{
byte[] imageBytes = Convert.FromBase64String(pngBase64);
MemoryStream ms = new(imageBytes);
BitmapImage bitmap = new();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = ms;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
return null;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
});
}
public override void Dispose()
{
}
}

View File

@@ -21,6 +21,7 @@ using PureSharpCompress.Readers;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
@@ -30,8 +31,18 @@ namespace QuickLook.Plugin.ThumbnailViewer;
internal static class Handler
{
// List<Pair<formats, type>>
public static List<KeyValuePair<string[], Type>> Providers = [];
public static void Prepare(string path, ContextObject context)
{
// Temporary codes
if (path.EndsWith(".pdn", StringComparison.OrdinalIgnoreCase))
{
new PdnProvider().Prepare(path, context);
return;
}
try
{
using Stream imageData = ViewImage(path);
@@ -47,6 +58,12 @@ internal static class Handler
public static Stream ViewImage(string path)
{
// Temporary codes
if (path.EndsWith(".pdn", StringComparison.OrdinalIgnoreCase))
{
return new PdnProvider().ViewImage(path);
}
try
{
using ZipArchive archive = ZipArchive.Open(path, new());

View File

@@ -34,6 +34,7 @@ public class Plugin : IViewer
".cdr", // CorelDraw
".fig", // Figma
".kra", // Krita
".pdn", // Paint.NET
".pip", ".pix", // Pixso
".sketch", // Sketch
".xd", // AdobeXD
@@ -46,6 +47,8 @@ public class Plugin : IViewer
public void Init()
{
Handler.Providers.Add(new KeyValuePair<string[], Type>(
[".pdn"], typeof(PdnProvider)));
}
public bool CanHandle(string path)

View File

@@ -0,0 +1,93 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.ImageViewer;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace QuickLook.Plugin.ThumbnailViewer;
internal class PdnProvider
{
public void Prepare(string path, ContextObject context)
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception ex)
{
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
context.PreferredSize = new Size { Width = 800, Height = 600 };
}
}
public Stream ViewImage(string path)
{
try
{
using TextReader reader = new StreamReader(path, Encoding.UTF8);
string line = reader.ReadLine();
if (!line.StartsWith("PDN"))
return null;
int indexOfStart = line.IndexOf("<");
int indexOfEnd = line.LastIndexOf(">");
if (indexOfStart < 0 || indexOfEnd < 0)
return null;
string xml = line.Substring(indexOfStart, indexOfEnd - indexOfStart + 1);
// <pdnImage>
// <custom>
// <thumb png="..." />
// </custom>
// </pdnImage>
XDocument doc = XDocument.Parse(xml);
var pngBase64 = doc.Root
?.Element("custom")
?.Element("thumb")
?.Attribute("png")
?.Value;
if (pngBase64 != null)
{
byte[] imageBytes = Convert.FromBase64String(pngBase64);
MemoryStream ms = new(imageBytes);
return ms;
}
return null;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
}
}