Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a non-generic Image base class #904

Merged
merged 36 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
36320c6
define IImageVisitor
antonfirsov May 1, 2019
740c2b5
pixel agnostic Mutate/Clone defined
antonfirsov May 1, 2019
75973d4
pixel-agnostic ResizeProcessor
antonfirsov May 1, 2019
91a547b
pixel-agnostic decoder API
antonfirsov May 1, 2019
442e22c
refactor FilterProcessor stuff
antonfirsov May 1, 2019
87495d7
basic fixes after rebase + temporarily comment out target frameworks
antonfirsov May 3, 2019
22fa631
refactor the rest of the FilterProcessor code
antonfirsov May 3, 2019
5091ac5
reached a fully compiling state
antonfirsov May 3, 2019
77a1bba
fix processor invocation tests
antonfirsov May 3, 2019
137a08a
re-add and fix filter invocation tests
antonfirsov May 3, 2019
6554db9
fix warnings and improve xmldocs
antonfirsov May 4, 2019
b758eaf
*ProcessorImplementation<T> ===> *Processor<T>,
antonfirsov May 4, 2019
513825b
validating tests for Convolution processors
antonfirsov May 4, 2019
4b563d9
Merge branch 'af/extend-processor-coverage' into af/non-generic-image…
antonfirsov May 4, 2019
c451ec6
Merge branch 'af/extend-processor-coverage' into af/non-generic-image…
antonfirsov May 4, 2019
ff48c62
refactor BoxBlur and GaussianBlur
antonfirsov May 4, 2019
c198ad5
making filters public
antonfirsov May 4, 2019
a117dbb
Further refactor on Gaussian stuff
antonfirsov May 4, 2019
d050f65
drop IEdgeDetectorProcessor
antonfirsov May 4, 2019
d8bdac6
Refactor edge detection
antonfirsov May 4, 2019
ded79e7
refactor Effects processors
antonfirsov May 4, 2019
0fbf85f
Finished refactoring transforms
antonfirsov May 5, 2019
484c08e
sealed everything
antonfirsov May 5, 2019
a11141d
refactor HistogramEqualization
antonfirsov May 5, 2019
cf3d4b5
cache Image dimensions into a field + re-enable all target frameworks
antonfirsov May 5, 2019
52c4a3d
fix Image.FromStream() + add tests
antonfirsov May 5, 2019
fcaaa6f
full coverage for Image.Load (I hope)
antonfirsov May 5, 2019
6f08424
Merge branch 'master' into af/non-generic-image-baseclass
antonfirsov May 5, 2019
143946e
fix changes applied by mistake
antonfirsov May 5, 2019
c21c8ba
Fix docs for processing extensions
antonfirsov May 5, 2019
7f2b4b0
publish non-generic QuantizeProcessor
antonfirsov May 5, 2019
b563167
Merge branch 'master' into af/non-generic-image-baseclass
antonfirsov May 5, 2019
beb914c
Merge remote-tracking branch 'origin/master' into af/non-generic-imag…
antonfirsov May 9, 2019
ebd5cd4
verbose naming for Histogram Equalization stuff + make it public
antonfirsov May 9, 2019
4575d44
formatting
antonfirsov May 9, 2019
390bda8
made AutoOrientExtensions non-generic
antonfirsov May 9, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ImageSharp.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Quantizer/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>
2 changes: 2 additions & 0 deletions src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>

<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0</TargetFrameworks>

<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
4 changes: 1 addition & 3 deletions src/ImageSharp/Advanced/AdvancedImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public static class AdvancedImageExtensions
/// <summary>
/// Gets the configuration for the image.
/// </summary>
/// <typeparam name="TPixel">The Pixel format.</typeparam>
/// <param name="source">The source image.</param>
/// <returns>Returns the configuration.</returns>
public static Configuration GetConfiguration<TPixel>(this Image<TPixel> source)
where TPixel : struct, IPixel<TPixel>
public static Configuration GetConfiguration(this Image source)
=> GetConfiguration((IConfigurable)source);

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/ImageSharp/Formats/Bmp/BmpDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
return new BmpDecoderCore(configuration, this).Decode<TPixel>(stream);
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);

/// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream)
{
Expand Down
19 changes: 8 additions & 11 deletions src/ImageSharp/Formats/Bmp/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@
// Licensed under the Apache License, Version 2.0.

using System.IO;

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="Image{TPixel}"/> type.
/// Extension methods for the <see cref="Image"/> type.
/// </summary>
public static partial class ImageExtensions
{
/// <summary>
/// Saves the image to the given stream with the bmp format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsBmp<TPixel>(this Image<TPixel> source, Stream stream)
where TPixel : struct, IPixel<TPixel>
=> source.SaveAsBmp(stream, null);
public static void SaveAsBmp(this Image source, Stream stream) => source.SaveAsBmp(stream, null);

/// <summary>
/// Saves the image to the given stream with the bmp format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsBmp<TPixel>(this Image<TPixel> source, Stream stream, BmpEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance));
public static void SaveAsBmp(this Image source, Stream stream, BmpEncoder encoder) =>
source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance));
}
}
}
3 changes: 3 additions & 0 deletions src/ImageSharp/Formats/Gif/GifDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
var decoder = new GifDecoderCore(configuration, this);
return decoder.Identify(stream);
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);
}
}
19 changes: 8 additions & 11 deletions src/ImageSharp/Formats/Gif/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@
// Licensed under the Apache License, Version 2.0.

using System.IO;

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="Image{TPixel}"/> type.
/// Extension methods for the <see cref="Image"/> type.
/// </summary>
public static partial class ImageExtensions
{
/// <summary>
/// Saves the image to the given stream in the gif format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsGif<TPixel>(this Image<TPixel> source, Stream stream)
where TPixel : struct, IPixel<TPixel>
=> source.SaveAsGif(stream, null);
public static void SaveAsGif(this Image source, Stream stream) => source.SaveAsGif(stream, null);

/// <summary>
/// Saves the image to the given stream in the gif format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsGif<TPixel>(this Image<TPixel> source, Stream stream, GifEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance));
public static void SaveAsGif(this Image source, Stream stream, GifEncoder encoder) =>
source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance));
}
}
}
13 changes: 11 additions & 2 deletions src/ImageSharp/Formats/IImageDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ namespace SixLabors.ImageSharp.Formats
public interface IImageDecoder
{
/// <summary>
/// Decodes the image from the specified stream to the <see cref="ImageFrame{TPixel}"/>.
/// Decodes the image from the specified stream to an <see cref="Image{TPixel}"/> of a specific pixel type.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image</returns>
/// <returns>The decoded image of a given pixel type.</returns>
Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>;

/// <summary>
/// Decodes the image from the specified stream to an <see cref="Image"/>.
/// The decoder is free to choose the pixel type.
/// </summary>
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image of a pixel type chosen by the decoder.</returns>
Image Decode(Configuration configuration, Stream stream);
}
}
19 changes: 8 additions & 11 deletions src/ImageSharp/Formats/Jpeg/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@
// Licensed under the Apache License, Version 2.0.

using System.IO;

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="Image{TPixel}"/> type.
/// Extension methods for the <see cref="Image"/> type.
/// </summary>
public static partial class ImageExtensions
{
/// <summary>
/// Saves the image to the given stream with the jpeg format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsJpeg<TPixel>(this Image<TPixel> source, Stream stream)
where TPixel : struct, IPixel<TPixel>
=> SaveAsJpeg(source, stream, null);
public static void SaveAsJpeg(this Image source, Stream stream) => SaveAsJpeg(source, stream, null);

/// <summary>
/// Saves the image to the given stream with the jpeg format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsJpeg<TPixel>(this Image<TPixel> source, Stream stream, JpegEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance));
public static void SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) =>
source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance));
}
}
}
3 changes: 3 additions & 0 deletions src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
return decoder.Identify(stream);
}
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);
}
}
8 changes: 8 additions & 0 deletions src/ImageSharp/Formats/PixelTypeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System.Runtime.CompilerServices;

using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp.Formats
{
/// <summary>
Expand All @@ -21,5 +25,9 @@ internal PixelTypeInfo(int bitsPerPixel)
/// Gets color depth, in number of bits per pixel.
/// </summary>
public int BitsPerPixel { get; }

internal static PixelTypeInfo Create<TPixel>()
where TPixel : struct, IPixel<TPixel> =>
new PixelTypeInfo(Unsafe.SizeOf<TPixel>() * 8);
}
}
18 changes: 7 additions & 11 deletions src/ImageSharp/Formats/Png/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@
// Licensed under the Apache License, Version 2.0.

using System.IO;

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="Image{TPixel}"/> type.
/// Extension methods for the <see cref="Image"/> type.
/// </summary>
public static partial class ImageExtensions
{
/// <summary>
/// Saves the image to the given stream with the png format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsPng<TPixel>(this Image<TPixel> source, Stream stream)
where TPixel : struct, IPixel<TPixel>
=> SaveAsPng(source, stream, null);
public static void SaveAsPng(this Image source, Stream stream) => SaveAsPng(source, stream, null);

/// <summary>
/// Saves the image to the given stream with the png format.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsPng<TPixel>(this Image<TPixel> source, Stream stream, PngEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance));
public static void SaveAsPng(this Image source, Stream stream, PngEncoder encoder) =>
source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance));
}
}
3 changes: 3 additions & 0 deletions src/ImageSharp/Formats/Png/PngDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
var decoder = new PngDecoderCore(configuration, this);
return decoder.Identify(stream);
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);
}
}
22 changes: 22 additions & 0 deletions src/ImageSharp/IImageVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp
{
/// <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"/>.
/// </summary>
internal interface IImageVisitor
{
/// <summary>
/// Provides a pixel-specific implementation for a given operation.
/// </summary>
/// <param name="image">The image.</param>
/// <typeparam name="TPixel">The pixel type.</typeparam>
void Visit<TPixel>(Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>;
}
}
16 changes: 14 additions & 2 deletions src/ImageSharp/Image.Decode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace SixLabors.ImageSharp
/// <content>
/// Adds static methods allowing the decoding of new images.
/// </content>
public static partial class Image
public abstract partial class Image
{
/// <summary>
/// Creates an <see cref="Image{TPixel}"/> instance backed by an uninitialized memory buffer.
/// This is an optimized creation method intended to be used by decoders.
/// The image might be filled with memory garbage.
/// </summary>
/// <typeparam name="TPixel">The pixel type</typeparam>
/// <param name="configuration">The <see cref="Configuration"/></param>
/// <param name="configuration">The <see cref="ImageSharp.Configuration"/></param>
/// <param name="width">The width of the image</param>
/// <param name="height">The height of the image</param>
/// <param name="metadata">The <see cref="ImageMetadata"/></param>
Expand Down Expand Up @@ -103,6 +103,18 @@ private static (Image<TPixel> img, IImageFormat format) Decode<TPixel>(Stream st
return (img, format);
}

private static (Image img, IImageFormat format) Decode(Stream stream, Configuration config)
{
IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format);
if (decoder is null)
{
return (null, null);
}

Image img = decoder.Decode(config, stream);
return (img, format);
}

/// <summary>
/// Reads the raw image information from the specified stream.
/// </summary>
Expand Down
Loading