-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from SixLabors/js/jpeg-quality
Allow the ability to set the quality of jpegs.
- Loading branch information
Showing
12 changed files
with
266 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Microsoft.Extensions.Logging; | ||
using SixLabors.ImageSharp.Advanced; | ||
using SixLabors.ImageSharp.Formats.Jpeg; | ||
using SixLabors.ImageSharp.Web.Commands; | ||
|
||
namespace SixLabors.ImageSharp.Web.Processors | ||
{ | ||
/// <summary> | ||
/// Allows the setting of quality for the jpeg image format. | ||
/// </summary> | ||
public class JpegQualityWebProcessor : IImageWebProcessor | ||
{ | ||
/// <summary> | ||
/// The command constant for quality. | ||
/// </summary> | ||
public const string Quality = "quality"; | ||
|
||
/// <summary> | ||
/// The reusable collection of commands. | ||
/// </summary> | ||
private static readonly IEnumerable<string> QualityCommands | ||
= new[] { Quality }; | ||
|
||
/// <inheritdoc/> | ||
public IEnumerable<string> Commands { get; } = QualityCommands; | ||
|
||
/// <inheritdoc/> | ||
public FormattedImage Process( | ||
FormattedImage image, | ||
ILogger logger, | ||
IDictionary<string, string> commands, | ||
CommandParser parser, | ||
CultureInfo culture) | ||
{ | ||
if (commands.ContainsKey(Quality) && image.Format is JpegFormat) | ||
{ | ||
var reference = | ||
(JpegEncoder)image.Image | ||
.GetConfiguration() | ||
.ImageFormatsManager | ||
.FindEncoder(image.Format); | ||
|
||
// The encoder clamps any values so no validation is required. | ||
int quality = parser.ParseValue<int>(commands.GetValueOrDefault(Quality), culture); | ||
|
||
if (quality != reference.Quality) | ||
{ | ||
image.Encoder = new JpegEncoder() { Quality = quality, Subsample = reference.Subsample }; | ||
} | ||
} | ||
|
||
return image; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/ImageSharp.Web.Tests/Processors/FormattedImageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using SixLabors.ImageSharp.Formats.Jpeg; | ||
using SixLabors.ImageSharp.Formats.Png; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using Xunit; | ||
|
||
namespace SixLabors.ImageSharp.Web.Tests.Processors | ||
{ | ||
public class FormattedImageTests | ||
{ | ||
[Fact] | ||
public void ConstructorSetsProperties() | ||
{ | ||
using var image = new Image<Rgba32>(1, 1); | ||
using var formatted = new FormattedImage(image, JpegFormat.Instance); | ||
|
||
Assert.NotNull(formatted.Image); | ||
Assert.Equal(image, formatted.Image); | ||
|
||
Assert.NotNull(formatted.Format); | ||
Assert.Equal(JpegFormat.Instance, formatted.Format); | ||
|
||
Assert.NotNull(formatted.Encoder); | ||
Assert.Equal(typeof(JpegEncoder), formatted.Encoder.GetType()); | ||
} | ||
|
||
[Fact] | ||
public void CanSetFormat() | ||
{ | ||
using var image = new Image<Rgba32>(1, 1); | ||
using var formatted = new FormattedImage(image, JpegFormat.Instance); | ||
|
||
Assert.NotNull(formatted.Format); | ||
Assert.Equal(JpegFormat.Instance, formatted.Format); | ||
|
||
Assert.Throws<ArgumentNullException>(() => formatted.Format = null); | ||
|
||
formatted.Format = PngFormat.Instance; | ||
Assert.Equal(PngFormat.Instance, formatted.Format); | ||
Assert.Equal(typeof(PngEncoder), formatted.Encoder.GetType()); | ||
} | ||
|
||
[Fact] | ||
public void CanSetEncoder() | ||
{ | ||
using var image = new Image<Rgba32>(1, 1); | ||
using var formatted = new FormattedImage(image, PngFormat.Instance); | ||
|
||
Assert.NotNull(formatted.Format); | ||
Assert.Equal(PngFormat.Instance, formatted.Format); | ||
|
||
Assert.Throws<ArgumentNullException>(() => formatted.Encoder = null); | ||
Assert.Throws<ArgumentException>(() => formatted.Encoder = new JpegEncoder()); | ||
|
||
formatted.Format = JpegFormat.Instance; | ||
Assert.Equal(typeof(JpegEncoder), formatted.Encoder.GetType()); | ||
|
||
JpegSubsample current = ((JpegEncoder)formatted.Encoder).Subsample.GetValueOrDefault(); | ||
|
||
Assert.Equal(JpegSubsample.Ratio444, current); | ||
formatted.Encoder = new JpegEncoder { Subsample = JpegSubsample.Ratio420 }; | ||
|
||
JpegSubsample replacement = ((JpegEncoder)formatted.Encoder).Subsample.GetValueOrDefault(); | ||
|
||
Assert.NotEqual(current, replacement); | ||
Assert.Equal(JpegSubsample.Ratio420, replacement); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/ImageSharp.Web.Tests/Processors/JpegQualityWebProcessorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using SixLabors.ImageSharp.Formats.Jpeg; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using SixLabors.ImageSharp.Web.Commands; | ||
using SixLabors.ImageSharp.Web.Commands.Converters; | ||
using SixLabors.ImageSharp.Web.Processors; | ||
using Xunit; | ||
|
||
namespace SixLabors.ImageSharp.Web.Tests.Processors | ||
{ | ||
public class JpegQualityWebProcessorTests | ||
{ | ||
[Fact] | ||
public void JpegQualityWebProcessor_UpdatesQuality() | ||
{ | ||
var parser = new CommandParser(new[] { new IntegralNumberConverter<int>() }); | ||
CultureInfo culture = CultureInfo.InvariantCulture; | ||
|
||
var commands = new Dictionary<string, string> | ||
{ | ||
{ JpegQualityWebProcessor.Quality, "42" }, | ||
}; | ||
|
||
using var image = new Image<Rgba32>(1, 1); | ||
using var formatted = new FormattedImage(image, JpegFormat.Instance); | ||
Assert.Equal(JpegFormat.Instance, formatted.Format); | ||
Assert.Equal(typeof(JpegEncoder), formatted.Encoder.GetType()); | ||
|
||
new JpegQualityWebProcessor() | ||
.Process(formatted, null, commands, parser, culture); | ||
|
||
Assert.Equal(JpegFormat.Instance, formatted.Format); | ||
Assert.Equal(42, ((JpegEncoder)formatted.Encoder).Quality); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.