Skip to content

Commit f15bc74

Browse files
authored
Merge pull request #1961 from SixLabors/bp/webplossydefault
Webp encoder now defaults to lossy, if nothing else is specified
2 parents ffa35c3 + ebf8b44 commit f15bc74

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal interface IWebpEncoderOptions
1010
{
1111
/// <summary>
1212
/// Gets the webp file format used. Either lossless or lossy.
13+
/// Defaults to lossy.
1314
/// </summary>
1415
WebpFileFormatType? FileFormat { get; }
1516

src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
7070

7171
/// <summary>
7272
/// Indicating what file format compression should be used.
73+
/// Defaults to lossy.
7374
/// </summary>
7475
private readonly WebpFileFormatType? fileFormat;
7576

@@ -112,43 +113,43 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
112113
Guard.NotNull(stream, nameof(stream));
113114

114115
this.configuration = image.GetConfiguration();
115-
bool lossy;
116+
bool lossless;
116117
if (this.fileFormat is not null)
117118
{
118-
lossy = this.fileFormat == WebpFileFormatType.Lossy;
119+
lossless = this.fileFormat == WebpFileFormatType.Lossless;
119120
}
120121
else
121122
{
122123
WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata();
123-
lossy = webpMetadata.FileFormat == WebpFileFormatType.Lossy;
124+
lossless = webpMetadata.FileFormat == WebpFileFormatType.Lossless;
124125
}
125126

126-
if (lossy)
127+
if (lossless)
127128
{
128-
using var enc = new Vp8Encoder(
129+
using var enc = new Vp8LEncoder(
129130
this.memoryAllocator,
130131
this.configuration,
131132
image.Width,
132133
image.Height,
133134
this.quality,
134135
this.method,
135-
this.entropyPasses,
136-
this.filterStrength,
137-
this.spatialNoiseShaping);
136+
this.transparentColorMode,
137+
this.nearLossless,
138+
this.nearLosslessQuality);
138139
enc.Encode(image, stream);
139140
}
140141
else
141142
{
142-
using var enc = new Vp8LEncoder(
143+
using var enc = new Vp8Encoder(
143144
this.memoryAllocator,
144145
this.configuration,
145146
image.Width,
146147
image.Height,
147148
this.quality,
148149
this.method,
149-
this.transparentColorMode,
150-
this.nearLossless,
151-
this.nearLosslessQuality);
150+
this.entropyPasses,
151+
this.filterStrength,
152+
this.spatialNoiseShaping);
152153
enc.Encode(image, stream);
153154
}
154155
}

tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class WebpEncoderTests
1818
private static string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Lossy.NoFilter06);
1919

2020
[Theory]
21-
[WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] // if its not a webp input image, it should default to lossless.
21+
[WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossy)] // If its not a webp input image, it should default to lossy.
2222
[WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)]
2323
[WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)]
2424
public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)

0 commit comments

Comments
 (0)