Browse Source

Merge branch 'master' into webp

pull/1552/head
Brian Popow 5 years ago
committed by GitHub
parent
commit
fb1fb05943
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs
  2. 6
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
  3. 24
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
  4. 6
      src/ImageSharp/Processing/ResizeOptions.cs
  5. 26
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  6. 15
      tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs
  7. 1
      tests/ImageSharp.Tests/TestImages.cs
  8. 2
      tests/Images/External
  9. 3
      tests/Images/Input/Gif/issues/issue1505_argumentoutofrange.png

3
src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs

@ -6,7 +6,6 @@ using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@ -96,7 +95,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
this.octree.Palletize(paletteSpan, this.maxColors, ref paletteIndex);
// Length of reduced palette + transparency.
ReadOnlyMemory<TPixel> result = this.paletteOwner.Memory.Slice(0, Math.Min(paletteIndex + 2, QuantizerConstants.MaxColors));
ReadOnlyMemory<TPixel> result = this.paletteOwner.Memory.Slice(0, Math.Min(paletteIndex + 2, this.maxColors));
this.pixelMap = new EuclideanPixelMap<TPixel>(this.Configuration, result);
this.palette = result;

6
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs

@ -26,6 +26,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.DestinationHeight = size.Height;
this.DestinationRectangle = rectangle;
this.Compand = options.Compand;
this.PremultiplyAlpha = options.PremultiplyAlpha;
}
/// <summary>
@ -53,6 +54,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// </summary>
public bool Compand { get; }
/// <summary>
/// Gets a value indicating whether to premultiply the alpha (if it exists) during the resize operation.
/// </summary>
public bool PremultiplyAlpha { get; }
/// <inheritdoc />
public override ICloningImageProcessor<TPixel> CreatePixelSpecificCloningProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
=> new ResizeProcessor<TPixel>(configuration, this, source, sourceRectangle);

24
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs

@ -21,6 +21,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private readonly IResampler resampler;
private readonly Rectangle destinationRectangle;
private readonly bool compand;
private readonly bool premultiplyAlpha;
private Image<TPixel> destination;
public ResizeProcessor(Configuration configuration, ResizeProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
@ -30,6 +31,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.destinationHeight = definition.DestinationHeight;
this.destinationRectangle = definition.DestinationRectangle;
this.resampler = definition.Sampler;
this.premultiplyAlpha = definition.PremultiplyAlpha;
this.compand = definition.Compand;
}
@ -60,6 +62,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Rectangle sourceRectangle = this.SourceRectangle;
Rectangle destinationRectangle = this.destinationRectangle;
bool compand = this.compand;
bool premultiplyAlpha = this.premultiplyAlpha;
// Handle resize dimensions identical to the original
if (source.Width == destination.Width
@ -128,7 +131,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
sourceRectangle,
destinationRectangle,
interest,
compand);
compand,
premultiplyAlpha);
}
}
@ -159,6 +163,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
in operation);
}
private static PixelConversionModifiers GetModifiers(bool compand, bool premultiplyAlpha)
{
if (premultiplyAlpha)
{
return PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
}
else
{
return PixelConversionModifiers.None.ApplyCompanding(compand);
}
}
private static void ApplyResizeFrameTransform(
Configuration configuration,
ImageFrame<TPixel> source,
@ -168,10 +184,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Rectangle sourceRectangle,
Rectangle destinationRectangle,
Rectangle interest,
bool compand)
bool compand,
bool premultiplyAlpha)
{
PixelConversionModifiers conversionModifiers =
PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
PixelConversionModifiers conversionModifiers = GetModifiers(compand, premultiplyAlpha);
Buffer2DRegion<TPixel> sourceRegion = source.PixelBuffer.GetRegion(sourceRectangle);

6
src/ImageSharp/Processing/ResizeOptions.cs

@ -45,5 +45,11 @@ namespace SixLabors.ImageSharp.Processing
/// Gets or sets the target rectangle to resize into.
/// </summary>
public Rectangle? TargetRectangle { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to premultiply
/// the alpha (if it exists) during the resize operation.
/// </summary>
public bool PremultiplyAlpha { get; set; } = true;
}
}

26
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -216,6 +216,32 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
appendSourceFileOrDescription: false);
}
[Theory]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, false)]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, true)]
public void Resize_PremultiplyAlpha<TPixel>(TestImageProvider<TPixel> provider, bool premultiplyAlpha)
where TPixel : unmanaged, IPixel<TPixel>
{
string details = premultiplyAlpha ? "On" : "Off";
provider.RunValidatingProcessorTest(
x =>
{
var resizeOptions = new ResizeOptions()
{
Size = x.GetCurrentSize() / 2,
Mode = ResizeMode.Crop,
Sampler = KnownResamplers.Bicubic,
Compand = false,
PremultiplyAlpha = premultiplyAlpha
};
x.Resize(resizeOptions);
},
details,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
[Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)

15
tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs

@ -110,6 +110,21 @@ namespace SixLabors.ImageSharp.Tests
}
}
// Test case for issue: https://github.com/SixLabors/ImageSharp/issues/1505
[Theory]
[WithFile(TestImages.Gif.Issues.Issue1505, PixelTypes.Rgba32)]
public void Issue1505<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var octreeQuantizer = new OctreeQuantizer();
IQuantizer<TPixel> quantizer = octreeQuantizer.CreatePixelSpecificQuantizer<TPixel>(Configuration.Default, new QuantizerOptions() { MaxColors = 128 });
ImageFrame<TPixel> frame = image.Frames[0];
quantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds());
}
}
private int GetTransparentIndex<TPixel>(IndexedImageFrame<TPixel> quantized)
where TPixel : unmanaged, IPixel<TPixel>
{

1
tests/ImageSharp.Tests/TestImages.cs

@ -415,6 +415,7 @@ namespace SixLabors.ImageSharp.Tests
public const string BadAppExtLength = "Gif/issues/issue405_badappextlength252.gif";
public const string BadAppExtLength_2 = "Gif/issues/issue405_badappextlength252-2.gif";
public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif";
public const string Issue1505 = "Gif/issues/issue1505_argumentoutofrange.png";
}
public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin, Leo, Ratio4x1, Ratio1x4 };

2
tests/Images/External

@ -1 +1 @@
Subproject commit 8b43d14d21ce9b436af3d12a70d38402cdba176b
Subproject commit 346070e5ba538f1a3bbafc0ea7367404c5f8c9ab

3
tests/Images/Input/Gif/issues/issue1505_argumentoutofrange.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bea0faf09782d0d972e72ad94e53b8ca9c823fd3056fc6a97aba8c43105fcd66
size 102581
Loading…
Cancel
Save