Skip to content

Commit

Permalink
Add a check box for lossless compression
Browse files Browse the repository at this point in the history
This makes it simpler for users to discover how to use lossless
compression.
When lossless compression is enabled, the quality slider controls how
much effort libwebp will put into compressing the image data; smaller
values are faster but produce larger files.

This replaces the previous behavior where lossless compression would be
silently used when the quality slider was set to 100.

Fixes #12
  • Loading branch information
0xC0000054 committed Jun 8, 2022
1 parent 54ed0f1 commit 6b066ed
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,7 @@
<data name="UnsupportedWebPFeature" xml:space="preserve">
<value>The image uses a WebP feature that is not supported.</value>
</data>
<data name="Lossless_Description" xml:space="preserve">
<value>Lossless</value>
</data>
</root>
2 changes: 1 addition & 1 deletion src/WebP/WebP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int __stdcall WebPSave(
config.method = 6; // 6 is the highest quality encoding
config.thread_level = 1;

if (encodeOptions->quality == 100)
if (encodeOptions->lossless)
{
config.lossless = 1;
pic->use_argb = 1;
Expand Down
1 change: 1 addition & 0 deletions src/WebP/WebP.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct EncodeParams
{
float quality;
int preset;
bool lossless;
}EncParams;

enum MetadataType
Expand Down
7 changes: 6 additions & 1 deletion src/WebPFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ internal static unsafe Surface Load(byte[] webpBytes)
/// <param name="output">The output Stream.</param>
/// <param name="quality">The WebP save quality.</param>
/// <param name="preset">The WebP encoding preset.</param>
/// <param name="lossless">
/// <see langword="true"/> if lossless encoding should be used; otherwise, <see langword="false"/>.
/// </param>
/// <param name="scratchSurface">The scratch surface.</param>
/// <param name="progressCallback">The progress callback.</param>
/// <exception cref="FormatException">The image exceeds 16383 pixels in width and/or height.</exception>
Expand All @@ -133,6 +136,7 @@ internal static void Save(
Stream output,
int quality,
WebPPreset preset,
bool lossless,
Surface scratchSurface,
ProgressEventHandler progressCallback)
{
Expand All @@ -144,7 +148,8 @@ internal static void Save(
WebPNative.EncodeParams encParams = new()
{
quality = quality,
preset = preset
preset = preset,
lossless = lossless
};

scratchSurface.Clear();
Expand Down
10 changes: 8 additions & 2 deletions src/WebPFileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ private enum PropertyNames
Quality,
KeepMetadata, // Obsolete, but retained to keep the value from being reused for a different property.
ForumLink,
GitHubLink
GitHubLink,
Lossless
}

private readonly IWebPStringResourceManager strings;
Expand Down Expand Up @@ -201,6 +202,7 @@ public override PropertyCollection OnCreateSavePropertyCollection()
{
StaticListChoiceProperty.CreateForEnum(PropertyNames.Preset, WebPPreset.Photo, false),
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"))
};
Expand All @@ -224,6 +226,9 @@ public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)

info.SetPropertyControlValue(PropertyNames.Quality, ControlInfoPropertyNames.DisplayName, GetString("Quality_DisplayName"));

info.SetPropertyControlValue(PropertyNames.Lossless, ControlInfoPropertyNames.DisplayName, string.Empty);
info.SetPropertyControlValue(PropertyNames.Lossless, ControlInfoPropertyNames.Description, GetString("Lossless_Description"));

PropertyControlInfo forumLinkPCI = info.FindControlForPropertyName(PropertyNames.ForumLink);
forumLinkPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = GetString("ForumLink_DisplayName");
forumLinkPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = GetString("ForumLink_Description");
Expand All @@ -239,8 +244,9 @@ protected override void OnSaveT(Document input, Stream output, PropertyBasedSave
{
int quality = token.GetProperty<Int32Property>(PropertyNames.Quality).Value;
WebPPreset preset = (WebPPreset)token.GetProperty(PropertyNames.Preset).Value;
bool lossless = token.GetProperty<BooleanProperty>(PropertyNames.Lossless).Value;

WebPFile.Save(input, output, quality, preset, scratchSurface, progressCallback);
WebPFile.Save(input, output, quality, preset, lossless, scratchSurface, progressCallback);
}
}
}
2 changes: 2 additions & 0 deletions src/WebPNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ internal sealed class EncodeParams
public float quality;
[MarshalAs(UnmanagedType.I4)]
public WebPPreset preset;
[MarshalAs(UnmanagedType.U1)]
public bool lossless;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down

0 comments on commit 6b066ed

Please sign in to comment.