From 06b3c4b4e7f2fadbe20b07a0229e61e73bd1279f Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 1 Jan 2017 19:06:34 +0000 Subject: [PATCH] Dropped ImageSharpd.Formats We now default load default formats using reflection only for default configuration, not manual. --- ImageSharp.sln | 10 +-- .../ConfigurationExtensions.cs | 32 -------- .../ImageSharp.Formats.xproj | 25 ------ .../Properties/AssemblyInfo.cs | 6 -- src/ImageSharp.Formats/project.json | 76 ------------------- src/ImageSharp.Formats/stylecop.json | 9 --- src/ImageSharp/Configuration.cs | 52 ++++++++++++- tests/ImageSharp.Benchmarks/project.json | 14 +++- tests/ImageSharp.Tests/ConfigurationTests.cs | 8 ++ tests/ImageSharp.Tests/TestBase.cs | 5 -- tests/ImageSharp.Tests/TestFile.cs | 15 +--- tests/ImageSharp.Tests/project.json | 14 +++- 12 files changed, 89 insertions(+), 177 deletions(-) delete mode 100644 src/ImageSharp.Formats/ConfigurationExtensions.cs delete mode 100644 src/ImageSharp.Formats/ImageSharp.Formats.xproj delete mode 100644 src/ImageSharp.Formats/Properties/AssemblyInfo.cs delete mode 100644 src/ImageSharp.Formats/project.json delete mode 100644 src/ImageSharp.Formats/stylecop.json diff --git a/ImageSharp.sln b/ImageSharp.sln index 6bddeaf56..a5e1e12b0 100644 --- a/ImageSharp.sln +++ b/ImageSharp.sln @@ -37,8 +37,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{E919DF0B EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "build", "build\build.xproj", "{575A5002-DD9F-4335-AA47-1DD87FA13645}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ImageSharp.Formats", "src\ImageSharp.Formats\ImageSharp.Formats.xproj", "{C72B8906-260A-4688-9B7A-5CD4BDF419EC}" -EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ImageSharp.Formats.Bmp", "src\ImageSharp.Formats.Bmp\ImageSharp.Formats.Bmp.xproj", "{C77661B9-F793-422E-8E27-AC60ECC5F215}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ImageSharp.Formats.Gif", "src\ImageSharp.Formats.Gif\ImageSharp.Formats.Gif.xproj", "{27AD4B5F-ECC4-4C63-9ECB-04EC772FDB6F}" @@ -50,6 +48,9 @@ EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ImageSharp.Processing", "src\ImageSharp.Processing\ImageSharp.Processing.xproj", "{A623CFE9-9D2B-4528-AD1F-2E834B061134}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{9E574A07-F879-4811-9C41-5CBDC6BAFDB7}" + ProjectSection(SolutionItems) = preProject + src\Shared\AssemblyInfo.Common.cs = src\Shared\AssemblyInfo.Common.cs + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -77,10 +78,6 @@ Global {575A5002-DD9F-4335-AA47-1DD87FA13645}.Debug|Any CPU.Build.0 = Debug|Any CPU {575A5002-DD9F-4335-AA47-1DD87FA13645}.Release|Any CPU.ActiveCfg = Release|Any CPU {575A5002-DD9F-4335-AA47-1DD87FA13645}.Release|Any CPU.Build.0 = Release|Any CPU - {C72B8906-260A-4688-9B7A-5CD4BDF419EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C72B8906-260A-4688-9B7A-5CD4BDF419EC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C72B8906-260A-4688-9B7A-5CD4BDF419EC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C72B8906-260A-4688-9B7A-5CD4BDF419EC}.Release|Any CPU.Build.0 = Release|Any CPU {C77661B9-F793-422E-8E27-AC60ECC5F215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C77661B9-F793-422E-8E27-AC60ECC5F215}.Debug|Any CPU.Build.0 = Debug|Any CPU {C77661B9-F793-422E-8E27-AC60ECC5F215}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -111,7 +108,6 @@ Global {299D8E18-102C-42DE-ADBF-79098EE706A8} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC} {2E33181E-6E28-4662-A801-E2E7DC206029} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {575A5002-DD9F-4335-AA47-1DD87FA13645} = {E919DF0B-2607-4462-8FC0-5C98FE50F8C9} - {C72B8906-260A-4688-9B7A-5CD4BDF419EC} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {C77661B9-F793-422E-8E27-AC60ECC5F215} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {27AD4B5F-ECC4-4C63-9ECB-04EC772FDB6F} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {7213767C-0003-41CA-AB18-0223CFA7CE4B} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} diff --git a/src/ImageSharp.Formats/ConfigurationExtensions.cs b/src/ImageSharp.Formats/ConfigurationExtensions.cs deleted file mode 100644 index f867bcbf6..000000000 --- a/src/ImageSharp.Formats/ConfigurationExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.IO; - - using Formats; - - /// - /// Extension methods for the type. - /// - public static partial class ConfigurationExtensions - { - /// - /// Adds the common formats, PNG, JPEG, GIF and BMP. - /// - /// The config. - /// The config - public static Configuration AddCommonFormats(this Configuration config) - { - return config - .AddPngFormat() - .AddJpegFormat() - .AddGifFormat() - .AddBmpFormat(); - } - } -} diff --git a/src/ImageSharp.Formats/ImageSharp.Formats.xproj b/src/ImageSharp.Formats/ImageSharp.Formats.xproj deleted file mode 100644 index 80a0b84a6..000000000 --- a/src/ImageSharp.Formats/ImageSharp.Formats.xproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - c72b8906-260a-4688-9b7a-5cd4bdf419ec - ImageSharp.Formats - .\obj - .\bin\ - v4.5.1 - - - 2.0 - - - True - - - - - - \ No newline at end of file diff --git a/src/ImageSharp.Formats/Properties/AssemblyInfo.cs b/src/ImageSharp.Formats/Properties/AssemblyInfo.cs deleted file mode 100644 index d6e98c6d6..000000000 --- a/src/ImageSharp.Formats/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,6 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -// Common values read from `AssemblyInfo.Common.cs` \ No newline at end of file diff --git a/src/ImageSharp.Formats/project.json b/src/ImageSharp.Formats/project.json deleted file mode 100644 index a495ad4a9..000000000 --- a/src/ImageSharp.Formats/project.json +++ /dev/null @@ -1,76 +0,0 @@ -{ - "version": "1.0.0-alpha1-*", - "title": "ImageSharp.Formats", - "description": "A cross-platform library for the processing of image files; written in C#", - "authors": [ - "James Jackson-South and contributors" - ], - "packOptions": { - "owners": [ - "James Jackson-South and contributors" - ], - "projectUrl": "https://github.com/JimBobSquarePants/ImageSharp", - "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0", - "iconUrl": "https://raw.githubusercontent.com/JimBobSquarePants/ImageSharp/master/build/icons/imagesharp-logo-128.png", - "requireLicenseAcceptance": false, - "repository": { - "type": "git", - "url": "https://github.com/JimBobSquarePants/ImageSharp" - }, - "tags": [ - "Image Resize Crop Gif Jpg Jpeg Bitmap Png Core" - ] - }, - "buildOptions": { - "allowUnsafe": true, - "xmlDoc": true, - "additionalArguments": [ "/additionalfile:stylecop.json", "/ruleset:../../ImageSharp.ruleset" ], - "compile": [ - "../Shared/*.cs" - ] - }, - "configurations": { - "Release": { - "buildOptions": { - "warningsAsErrors": true, - "optimize": true - } - } - }, - "dependencies": { - "ImageSharp": { - "target": "project", - "version": "1.0.0-*" - }, - "ImageSharp.Formats.Png": { - "target": "project", - "version": "1.0.0-*" - }, - "ImageSharp.Formats.Bmp": { - "target": "project", - "version": "1.0.0-*" - }, - "ImageSharp.Formats.Jpeg": { - "target": "project", - "version": "1.0.0-*" - }, - "ImageSharp.Formats.Gif": { - "target": "project", - "version": "1.0.0-*" - }, - "StyleCop.Analyzers": { - "version": "1.1.0-beta001", - "type": "build" - } - }, - "frameworks": { - "netstandard1.1": { - "dependencies": { - } - }, - "net45": { - "dependencies": { - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp.Formats/stylecop.json b/src/ImageSharp.Formats/stylecop.json deleted file mode 100644 index df8f120a5..000000000 --- a/src/ImageSharp.Formats/stylecop.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", - "settings": { - "documentationRules": { - "companyName": "James Jackson-South", - "copyrightText": "Copyright (c) James Jackson-South and contributors.\nLicensed under the Apache License, Version 2.0." - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 013c13495..d339042d0 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -21,7 +21,7 @@ namespace ImageSharp /// /// A lazily initialized configuration default instance. /// - private static readonly Lazy Lazy = new Lazy(() => new Configuration()); + private static readonly Lazy Lazy = new Lazy(() => new Configuration(true)); /// /// An object that can be used to synchronize access to the . @@ -33,6 +33,30 @@ namespace ImageSharp /// private readonly List imageFormatsList = new List(); + /// + /// Initializes a new instance of the class. + /// + public Configuration() + : this(false) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// if set to true [autoload wellknown formats]. + internal Configuration(bool autoloadWellknownFormats) + { + if (autoloadWellknownFormats) + { + // lets try auto loading the known image formats + this.TryAddImageFormat("ImageSharp.Formats.PngFormat, ImageSharp.Formats.Png"); + this.TryAddImageFormat("ImageSharp.Formats.JpegFormat, ImageSharp.Formats.Jpeg"); + this.TryAddImageFormat("ImageSharp.Formats.GifFormat, ImageSharp.Formats.Gif"); + this.TryAddImageFormat("ImageSharp.Formats.BmpFormat, ImageSharp.Formats.Bmp"); + } + } + /// /// Gets the default instance. /// @@ -69,6 +93,32 @@ namespace ImageSharp this.AddImageFormatLocked(format); } + /// + /// Tries the add image format. + /// + /// Name of the type. + /// True if type discoverd and is a valid + internal bool TryAddImageFormat(string typeName) + { + Type type = Type.GetType(typeName, false); + if (type != null) + { + IImageFormat format = Activator.CreateInstance(type) as IImageFormat; + if (format != null + && format.Encoder != null + && format.Decoder != null + && !string.IsNullOrEmpty(format.MimeType) + && format.SupportedExtensions?.Any() == true) + { + // we can use the locked version as we have already validated in the if. + this.AddImageFormatLocked(format); + return true; + } + } + + return false; + } + /// /// Adds image format. The class is locked to make it thread safe. /// diff --git a/tests/ImageSharp.Benchmarks/project.json b/tests/ImageSharp.Benchmarks/project.json index 329a58ec1..10790bc15 100644 --- a/tests/ImageSharp.Benchmarks/project.json +++ b/tests/ImageSharp.Benchmarks/project.json @@ -23,7 +23,19 @@ "target": "project", "version": "1.0.0-*" }, - "ImageSharp.Formats": { + "ImageSharp.Formats.Jpeg": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Png": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Bmp": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Gif": { "target": "project", "version": "1.0.0-*" }, diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 74a077341..d1618fd18 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -18,6 +18,14 @@ namespace ImageSharp.Tests /// public class ConfigurationTests { + [Fact] + public void IfAutoloadWellknwonFormatesIsTrueAllFormateAreLoaded() + { + var configuration = new Configuration(true); + + Assert.Equal(4, configuration.ImageFormats.Count); + } + /// /// Test that the default configuration is not null. /// diff --git a/tests/ImageSharp.Tests/TestBase.cs b/tests/ImageSharp.Tests/TestBase.cs index dba93958b..2b92591eb 100644 --- a/tests/ImageSharp.Tests/TestBase.cs +++ b/tests/ImageSharp.Tests/TestBase.cs @@ -13,11 +13,6 @@ namespace ImageSharp.Tests /// public abstract class TestBase { - protected TestBase() - { - Configuration.Default.AddCommonFormats(); - } - /// /// Creates the image output directory. /// diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index 2618549f1..61a6a9f64 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -35,20 +35,7 @@ namespace ImageSharp.Tests /// The file. /// private readonly string file; - - /// - /// Initializes static members of the class. - /// - static TestFile() - { - // Register the individual image formats. - // TODO: Is this the best place to do this? - Configuration.Default.AddImageFormat(new PngFormat()); - Configuration.Default.AddImageFormat(new JpegFormat()); - Configuration.Default.AddImageFormat(new BmpFormat()); - Configuration.Default.AddImageFormat(new GifFormat()); - } - + /// /// Initializes a new instance of the class. /// diff --git a/tests/ImageSharp.Tests/project.json b/tests/ImageSharp.Tests/project.json index b8f19f041..9f9c0c715 100644 --- a/tests/ImageSharp.Tests/project.json +++ b/tests/ImageSharp.Tests/project.json @@ -30,7 +30,19 @@ "target": "project", "version": "1.0.0-*" }, - "ImageSharp.Formats": { + "ImageSharp.Formats.Png": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Jpeg": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Bmp": { + "target": "project", + "version": "1.0.0-*" + }, + "ImageSharp.Formats.Gif": { "target": "project", "version": "1.0.0-*" },