Skip to content

Commit

Permalink
Add the plugin and libwebp versions to the save UI
Browse files Browse the repository at this point in the history
This makes it easier for users to determine what version of the plugin
they have installed.
The plugin version is also listed as part of the Paint.NET Diagnostics
(in Settings > Diagnostics), but that is harder to find and use.
  • Loading branch information
0xC0000054 committed Jul 4, 2023
1 parent d76bd54 commit 0c9397d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/IgnoredWords.dic
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ifd
interop
jpeg
kvp
libwebp
metadata
nq
num
Expand Down
4 changes: 4 additions & 0 deletions src/Interop/WebP_ARM64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace WebPFileType.Interop
[System.Security.SuppressUnmanagedCodeSecurity]
internal static unsafe partial class WebP_ARM64
{
[LibraryImport("WebP_ARM64.dll", EntryPoint = "GetLibWebPVersion")]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
public static partial int GetLibWebPVersion();

[LibraryImport("WebP_ARM64.dll", EntryPoint = "WebPGetImageInfo")]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
public static partial VP8StatusCode WebPGetImageInfo(byte* data, UIntPtr dataSize, out ImageInfo info);
Expand Down
4 changes: 4 additions & 0 deletions src/Interop/WebP_x64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace WebPFileType.Interop
[System.Security.SuppressUnmanagedCodeSecurity]
internal static unsafe partial class WebP_x64
{
[LibraryImport("WebP_x64.dll", EntryPoint = "GetLibWebPVersion")]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
public static partial int GetLibWebPVersion();

[LibraryImport("WebP_x64.dll", EntryPoint = "WebPGetImageInfo")]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
public static partial VP8StatusCode WebPGetImageInfo(byte* data, UIntPtr dataSize, out ImageInfo info);
Expand Down
43 changes: 43 additions & 0 deletions src/VersionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-webp, a FileType plugin for Paint.NET
// that loads and saves WebP images.
//
// Copyright (c) 2011-2023 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////

using System;
using System.Diagnostics;

namespace WebPFileType
{
internal static class VersionInfo
{
private static readonly Lazy<string> libwebpVersion = new(GetLibWebPVersion);
private static readonly Lazy<string> pluginVersion = new(GetPluginVersion);

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static string LibWebPVersion => libwebpVersion.Value;

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static string PluginVersion => pluginVersion.Value;

private static string GetLibWebPVersion()
{
int libwebpVersion = WebPNative.GetLibWebPVersion();

int major = (libwebpVersion >> 16) & 0xff;
int minor = (libwebpVersion >> 8) & 0xff;
int revision = libwebpVersion & 0xff;

return $"{major}.{minor}.{revision}";
}

private static string GetPluginVersion()
=> typeof(VersionInfo).Assembly.GetName().Version.ToString();
}
}
8 changes: 8 additions & 0 deletions src/WebP/WebP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
#include "WebP.h"
#include "scoped.h"

DLLEXPORT int __stdcall GetLibWebPVersion()
{
// Each libwebp API set has its own method to get the version number.
// All of the version numbers should be identical for a specific libwebp release,
// so we use the decoder version number;
return WebPGetDecoderVersion();
}

int __stdcall WebPGetImageInfo(const uint8_t* data, size_t dataSize, ImageInfo* info)
{
WebPBitstreamFeatures features;
Expand Down
2 changes: 2 additions & 0 deletions src/WebP/WebP.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ typedef struct ImageInfo
bool hasAnimation;
}ImageInfo;

DLLEXPORT int __stdcall GetLibWebPVersion();

DLLEXPORT int __stdcall WebPGetImageInfo(const uint8_t* data, size_t dataSize, ImageInfo* info);

DLLEXPORT bool __stdcall WebPGetImageMetadata(const uint8_t* data, size_t dataSize, SetDecoderMetadataFn setMetadata);
Expand Down
18 changes: 16 additions & 2 deletions src/WebPFileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private enum PropertyNames
KeepMetadata, // Obsolete, but retained to keep the value from being reused for a different property.
ForumLink,
GitHubLink,
Lossless
Lossless,
PluginVersion,
LibWebPVersion
}

private readonly IWebPStringResourceManager strings;
Expand Down Expand Up @@ -201,7 +203,9 @@ public override PropertyCollection OnCreateSavePropertyCollection()
new Int32Property(PropertyNames.Quality, 95, 0, 100, false),
new BooleanProperty(PropertyNames.Lossless, false),
new UriProperty(PropertyNames.ForumLink, new Uri("https://forums.getpaint.net/topic/21773-webp-filetype/")),
new UriProperty(PropertyNames.GitHubLink, new Uri("https://github.com/0xC0000054/pdn-webp"))
new UriProperty(PropertyNames.GitHubLink, new Uri("https://github.com/0xC0000054/pdn-webp")),
new StringProperty(PropertyNames.PluginVersion),
new StringProperty(PropertyNames.LibWebPVersion),
};

List<PropertyCollectionRule> rules = new()
Expand Down Expand Up @@ -240,6 +244,16 @@ public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = string.Empty;
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = "GitHub"; // GitHub is a brand name that should not be localized.

PropertyControlInfo pluginVersionPCI = info.FindControlForPropertyName(PropertyNames.PluginVersion);
pluginVersionPCI.ControlType.Value = PropertyControlType.Label;
pluginVersionPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = string.Empty;
pluginVersionPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = "WebPFileType v" + VersionInfo.PluginVersion;

PropertyControlInfo libwebpVersionPCI = info.FindControlForPropertyName(PropertyNames.LibWebPVersion);
libwebpVersionPCI.ControlType.Value = PropertyControlType.Label;
libwebpVersionPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = string.Empty;
libwebpVersionPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = "libwebp v" + VersionInfo.LibWebPVersion;

return info;
}

Expand Down
27 changes: 26 additions & 1 deletion src/WebPNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using WebPFileType.Interop;
using WebPFileType.Properties;
Expand All @@ -25,6 +24,32 @@ internal static class WebPNative
{
internal const int WebPMaxDimension = 16383;

/// <summary>
/// Gets the libwebp version number.
/// </summary>
/// <returns>
/// The libwebp version number.
/// </returns>
internal static int GetLibWebPVersion()
{
int version;

if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
version = WebP_x64.GetLibWebPVersion();
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
version = WebP_ARM64.GetLibWebPVersion();
}
else
{
throw new PlatformNotSupportedException();
}

return version;
}

/// <summary>
/// Gets the WebP image information.
/// </summary>
Expand Down

0 comments on commit 0c9397d

Please sign in to comment.