Browse Source

Merge branch 'master' into LightnessProcessor

af/merge-core
ip75 6 years ago
committed by GitHub
parent
commit
dfc094d320
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs
  2. 9
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  3. 10
      src/ImageSharp/Advanced/IImageVisitor.cs
  4. 4
      src/ImageSharp/IImage.cs
  5. 43
      src/ImageSharp/Image.cs
  6. 13
      src/ImageSharp/ImageExtensions.cs
  7. 12
      src/ImageSharp/ImageFrame.cs
  8. 16
      src/ImageSharp/ImageFrame{TPixel}.cs
  9. 36
      src/ImageSharp/Image{TPixel}.cs
  10. 33
      src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs
  11. 7
      src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs
  12. 1
      src/ImageSharp/Processing/Processors/ImageProcessorExtensions.cs
  13. 8
      src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs

1
src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

9
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -15,6 +15,15 @@ namespace SixLabors.ImageSharp.Advanced
/// </summary>
public static class AdvancedImageExtensions
{
/// <summary>
/// Accepts a <see cref="IImageVisitor"/> to implement a double-dispatch pattern in order to
/// apply pixel-specific operations on non-generic <see cref="Image"/> instances
/// </summary>
/// <param name="source">The source.</param>
/// <param name="visitor">The visitor.</param>
public static void AcceptVisitor(this Image source, IImageVisitor visitor)
=> source.Accept(visitor);
/// <summary>
/// Gets the configuration for the image.
/// </summary>

10
src/ImageSharp/IImageVisitor.cs → src/ImageSharp/Advanced/IImageVisitor.cs

@ -3,13 +3,13 @@
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp
namespace SixLabors.ImageSharp.Advanced
{
/// <summary>
/// A visitor to implement double-dispatch pattern in order to apply pixel-specific operations
/// on non-generic <see cref="Image"/> instances. The operation is dispatched by <see cref="Image.AcceptVisitor"/>.
/// A visitor to implement a double-dispatch pattern in order to apply pixel-specific operations
/// on non-generic <see cref="Image"/> instances.
/// </summary>
internal interface IImageVisitor
public interface IImageVisitor
{
/// <summary>
/// Provides a pixel-specific implementation for a given operation.
@ -19,4 +19,4 @@ namespace SixLabors.ImageSharp
void Visit<TPixel>(Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>;
}
}
}

4
src/ImageSharp/IImage.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -11,4 +11,4 @@ namespace SixLabors.ImageSharp
public interface IImage : IImageInfo, IDisposable
{
}
}
}

43
src/ImageSharp/Image.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using SixLabors.ImageSharp.Advanced;
@ -80,21 +81,11 @@ namespace SixLabors.ImageSharp
/// </summary>
Configuration IConfigurable.Configuration => this.Configuration;
/// <summary>
/// Gets a value indicating whether the image instance is disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <inheritdoc />
public void Dispose()
{
if (this.IsDisposed)
{
return;
}
this.IsDisposed = true;
this.DisposeImpl();
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
@ -109,8 +100,7 @@ namespace SixLabors.ImageSharp
Guard.NotNull(encoder, nameof(encoder));
this.EnsureNotDisposed();
EncodeVisitor visitor = new EncodeVisitor(encoder, stream);
this.AcceptVisitor(visitor);
this.AcceptVisitor(new EncodeVisitor(encoder, stream));
}
/// <summary>
@ -130,13 +120,6 @@ namespace SixLabors.ImageSharp
public abstract Image<TPixel2> CloneAs<TPixel2>(Configuration configuration)
where TPixel2 : struct, IPixel<TPixel2>;
/// <summary>
/// Accept a <see cref="IImageVisitor"/>.
/// Implemented by <see cref="Image{TPixel}"/> invoking <see cref="IImageVisitor.Visit{TPixel}"/>
/// with the pixel type of the image.
/// </summary>
internal abstract void AcceptVisitor(IImageVisitor visitor);
/// <summary>
/// Update the size of the image after mutation.
/// </summary>
@ -144,9 +127,23 @@ namespace SixLabors.ImageSharp
protected void UpdateSize(Size size) => this.size = size;
/// <summary>
/// Implements the Dispose logic.
/// Disposes the object and frees resources for the Garbage Collector.
/// </summary>
/// <param name="disposing">Whether to dispose of managed and unmanaged objects.</param>
protected abstract void Dispose(bool disposing);
/// <summary>
/// Throws <see cref="ObjectDisposedException"/> if the image is disposed.
/// </summary>
internal abstract void EnsureNotDisposed();
/// <summary>
/// Accepts a <see cref="IImageVisitor"/>.
/// Implemented by <see cref="Image{TPixel}"/> invoking <see cref="IImageVisitor.Visit{TPixel}"/>
/// with the pixel type of the image.
/// </summary>
protected abstract void DisposeImpl();
/// <param name="visitor">The visitor.</param>
internal abstract void Accept(IImageVisitor visitor);
private class EncodeVisitor : IImageVisitor
{

13
src/ImageSharp/ImageExtensions.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -119,16 +119,5 @@ namespace SixLabors.ImageSharp
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(stream.ToArray())}";
}
}
/// <summary>
/// Throws <see cref="ObjectDisposedException"/> if the image is disposed.
/// </summary>
internal static void EnsureNotDisposed(this Image image)
{
if (image.IsDisposed)
{
throw new ObjectDisposedException(nameof(image), "Trying to execute an operation on a disposed image.");
}
}
}
}

12
src/ImageSharp/ImageFrame.cs

@ -74,7 +74,17 @@ namespace SixLabors.ImageSharp
public Rectangle Bounds() => new Rectangle(0, 0, this.Width, this.Height);
/// <inheritdoc />
public abstract void Dispose();
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the object and frees resources for the Garbage Collector.
/// </summary>
/// <param name="disposing">Whether to dispose of managed and unmanaged objects.</param>
protected abstract void Dispose(bool disposing);
internal abstract void CopyPixelsTo<TDestinationPixel>(Span<TDestinationPixel> destination)
where TDestinationPixel : struct, IPixel<TDestinationPixel>;

16
src/ImageSharp/ImageFrame{TPixel}.cs

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp
/// In all other cases it is the only frame of the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>, IDisposable
public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private bool isDisposed;
@ -196,20 +196,20 @@ namespace SixLabors.ImageSharp
this.UpdateSize(this.PixelBuffer.Size());
}
/// <summary>
/// Disposes the object and frees resources for the Garbage Collector.
/// </summary>
public override void Dispose()
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
this.PixelBuffer?.Dispose();
this.PixelBuffer = null;
if (disposing)
{
this.PixelBuffer?.Dispose();
this.PixelBuffer = null;
}
// Note disposing is done.
this.isDisposed = true;
}

36
src/ImageSharp/Image{TPixel}.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -21,6 +21,8 @@ namespace SixLabors.ImageSharp
public sealed class Image<TPixel> : Image
where TPixel : struct, IPixel<TPixel>
{
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// with the height and the width of the image.
@ -185,19 +187,41 @@ namespace SixLabors.ImageSharp
}
/// <inheritdoc/>
protected override void DisposeImpl() => this.Frames.Dispose();
protected override void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
if (disposing)
{
this.Frames.Dispose();
}
this.isDisposed = true;
}
/// <inheritdoc/>
internal override void EnsureNotDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException("Trying to execute an operation on a disposed image.");
}
}
/// <inheritdoc/>
public override string ToString() => $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}";
/// <inheritdoc />
internal override void AcceptVisitor(IImageVisitor visitor)
internal override void Accept(IImageVisitor visitor)
{
this.EnsureNotDisposed();
visitor.Visit(this);
}
/// <inheritdoc/>
public override string ToString() => $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}";
/// <summary>
/// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer.
/// </summary>

33
src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs

@ -25,8 +25,7 @@ namespace SixLabors.ImageSharp.Processing
Guard.NotNull(operation, nameof(operation));
source.EnsureNotDisposed();
var visitor = new ProcessingVisitor(operation, true);
source.AcceptVisitor(visitor);
source.AcceptVisitor(new ProcessingVisitor(operation, true));
}
/// <summary>
@ -42,8 +41,10 @@ namespace SixLabors.ImageSharp.Processing
Guard.NotNull(operation, nameof(operation));
source.EnsureNotDisposed();
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
.CreateImageProcessingContext(source, true);
IInternalImageProcessingContext<TPixel> operationsRunner
= source.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(source, true);
operation(operationsRunner);
}
@ -60,8 +61,10 @@ namespace SixLabors.ImageSharp.Processing
Guard.NotNull(operations, nameof(operations));
source.EnsureNotDisposed();
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
.CreateImageProcessingContext(source, true);
IInternalImageProcessingContext<TPixel> operationsRunner
= source.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(source, true);
operationsRunner.ApplyProcessors(operations);
}
@ -96,8 +99,10 @@ namespace SixLabors.ImageSharp.Processing
Guard.NotNull(operation, nameof(operation));
source.EnsureNotDisposed();
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
.CreateImageProcessingContext(source, false);
IInternalImageProcessingContext<TPixel> operationsRunner
= source.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(source, false);
operation(operationsRunner);
return operationsRunner.GetResultImage();
}
@ -116,8 +121,10 @@ namespace SixLabors.ImageSharp.Processing
Guard.NotNull(operations, nameof(operations));
source.EnsureNotDisposed();
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
.CreateImageProcessingContext(source, false);
IInternalImageProcessingContext<TPixel> operationsRunner
= source.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(source, false);
operationsRunner.ApplyProcessors(operations);
return operationsRunner.GetResultImage();
}
@ -157,8 +164,10 @@ namespace SixLabors.ImageSharp.Processing
public void Visit<TPixel>(Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
IInternalImageProcessingContext<TPixel> operationsRunner = image.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(image, this.mutate);
IInternalImageProcessingContext<TPixel> operationsRunner =
image.GetConfiguration()
.ImageOperationsProvider.CreateImageProcessingContext(image, this.mutate);
this.operation(operationsRunner);
this.ResultImage = operationsRunner.GetResultImage();
}

7
src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs

@ -15,8 +15,6 @@ namespace SixLabors.ImageSharp.Processing.Processors
internal abstract class CloningImageProcessor<TPixel> : ICloningImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="CloningImageProcessor{TPixel}"/> class.
/// </summary>
@ -104,6 +102,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
@ -160,10 +159,6 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// <param name="disposing">Whether to dispose managed and unmanaged objects.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
this.isDisposed = true;
}
}
}
}

1
src/ImageSharp/Processing/Processors/ImageProcessorExtensions.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

8
src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs

@ -15,8 +15,6 @@ namespace SixLabors.ImageSharp.Processing.Processors
internal abstract class ImageProcessor<TPixel> : IImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="ImageProcessor{TPixel}"/> class.
/// </summary>
@ -97,6 +95,8 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// <inheritdoc/>
public virtual void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
@ -142,10 +142,6 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// <param name="disposing">Whether to dispose managed and unmanaged objects.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
this.isDisposed = true;
}
}
}
}

Loading…
Cancel
Save