From bee82309dc7dc27c20e970caf70f31e7143f416e Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Sun, 29 Jun 2014 22:54:11 +0200 Subject: [PATCH 01/11] Adds unit tests for double extension methods + Fixes a build problem on Mono Former-commit-id: 364bce272366beb0ae955735e6bac6d615327c03 --- .../Extensions/DoubleExtensionsUnitTests.cs | 55 +++++++++++++++++++ .../ImageProcessor.UnitTests.csproj | 4 ++ src/ImageProcessor/ImageProcessor.csproj | 6 +- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs diff --git a/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs new file mode 100644 index 000000000..108a9a246 --- /dev/null +++ b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs @@ -0,0 +1,55 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Runs unit tests on the extension methods +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.UnitTests +{ + using System; + using System.Collections.Generic; + using Common.Extensions; + using NUnit.Framework; + + /// + /// Test harness for the DoubleExtensions extension methods + /// + [TestFixture] + public class DoubleExtensionsUnitTests + { + /// + /// Stores the values to test for the ToByte() extension method + /// + private Dictionary doubleToByteTests; + + /// + /// Sets up the values for the tests + /// + [TestFixtureSetUp] + public void Init() + { + this.doubleToByteTests = new Dictionary(); + this.doubleToByteTests.Add(-10, 0x0); + this.doubleToByteTests.Add(1.5, 0x1); + this.doubleToByteTests.Add(25.7, 0x19); + this.doubleToByteTests.Add(1289047, 0xFF); + } + + /// + /// Tests the double to byte conversion + /// + [Test] + public void TestDoubleToByte() + { + foreach (var item in this.doubleToByteTests) + { + var result = item.Key.ToByte(); + Assert.AreEqual(item.Value, result); + } + } + } +} \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj index c872dbbb8..e715df657 100644 --- a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj +++ b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj @@ -46,6 +46,7 @@ PreserveNewest + @@ -127,4 +128,7 @@ + + + \ No newline at end of file diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 1c5b26e80..c21b78194 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -3,8 +3,6 @@ Debug AnyCPU - 8.0.30703 - 2.0 {3B5DD734-FB7A-487D-8CE6-55E7AF9AEA7E} Library Properties @@ -25,7 +23,6 @@ prompt 4 bin\Debug\ImageProcessor.XML - false pdbonly @@ -46,6 +43,8 @@ prompt false true + 4 + false @@ -57,6 +56,7 @@ + From da483c2d6d118e6cc676554d2db1d6b6e06b984a Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Sun, 29 Jun 2014 23:41:33 +0200 Subject: [PATCH 02/11] Adds a few unit tests for integer extension methods Former-commit-id: 7d939382c0307091703da3875404453aff754c89 --- .../Extensions/IntegerExtensionsUnitTests.cs | 38 +++++++++++++++++++ .../ImageProcessor.UnitTests.csproj | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs diff --git a/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs new file mode 100644 index 000000000..1e85e633b --- /dev/null +++ b/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs @@ -0,0 +1,38 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Runs unit tests on the extension methods +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.UnitTests +{ + using System; + using Common.Extensions; + using NUnit.Framework; + + /// + /// Provides a test harness for the integer extension class + /// + [TestFixture] + public class IntegerExtensionsUnitTests + { + /// + /// Tests the "ToByte" extension + /// + /// Integer input + /// Expected result + [Test] + [TestCase(21, 0x15)] + [TestCase(190, 0xBE)] + [TestCase(3156, 0xFF)] + public void ToByteTest(int input, byte expected) + { + var result = input.ToByte(); + Assert.AreEqual(expected, result); + } + } +} \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj index e715df657..b545a11d9 100644 --- a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj +++ b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj @@ -47,6 +47,7 @@ PreserveNewest + From 54e85e8832b986651e3c7b5a24e873cb2eec8112 Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Fri, 4 Jul 2014 22:01:17 +0200 Subject: [PATCH 03/11] Unit tests some string extensions Former-commit-id: 22c59f5af70e401288566e80a23ef90599934a90 --- .../Extensions/StringExtensionsUnitTests.cs | 91 +++++++++++++++++++ .../ImageProcessor.UnitTests.csproj | 1 + 2 files changed, 92 insertions(+) create mode 100644 src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs new file mode 100644 index 000000000..d1fefa395 --- /dev/null +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -0,0 +1,91 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Provides a test harness for the string extensions +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.UnitTests +{ + using System; + using Common.Extensions; + using NUnit.Framework; + + /// + /// Test harness for the string extensions + /// + [TestFixture] + public class StringExtensionsUnitTests + { + /// + /// Tests the MD5 fingerprint + /// + /// The input value + /// The expexted output of the hash + [Test] + [TestCase("test input", "2e7f7a62eabf0993239ca17c78c464d9")] + [TestCase("lorem ipsum dolor", "96ee002fee25e8b675a477c9750fa360")] + [TestCase("LoReM IpSuM DoLoR", "41e201da794c7fbdb8ce5526a71c8c83")] + [TestCase("1234567890", "e15e31c3d8898c92ab172a4311be9e84")] + public void TestToMd5Fingerprint(string input, string expected) + { + var result = input.ToMD5Fingerprint(); + var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + + /// + /// Tests the SHA-1 fingerprint + /// + /// The input value + /// The expexted output of the hash + [Test] + [TestCase("test input", "49883b34e5a0f48224dd6230f471e9dc1bdbeaf5")] + [TestCase("lorem ipsum dolor", "75899ad8827a32493928903aecd6e931bf36f967")] + [TestCase("LoReM IpSuM DoLoR", "2f44519afae72fc0837b72c6b53cb11338a1f916")] + [TestCase("1234567890", "01b307acba4f54f55aafc33bb06bbbf6ca803e9a")] + public void TestToSHA1Fingerprint(string input, string expected) + { + var result = input.ToSHA1Fingerprint(); + var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + + /// + /// Tests the SHA-256 fingerprint + /// + /// The input value + /// The expexted output of the hash + [Test] + [TestCase("test input", "9dfe6f15d1ab73af898739394fd22fd72a03db01834582f24bb2e1c66c7aaeae")] + [TestCase("lorem ipsum dolor", "ed03353266c993ea9afb9900a3ca688ddec1656941b1ca15ee1650a022616dfa")] + [TestCase("LoReM IpSuM DoLoR", "55f6cb90ba5cd8eeb6f5f16f083ebcd48ea06c34cc5aed8e33246fc3153d3898")] + [TestCase("1234567890", "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646")] + public void TestToSHA256Fingerprint(string input, string expected) + { + var result = input.ToSHA256Fingerprint(); + var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + + /// + /// Tests the SHA-512 fingerprint + /// + /// The input value + /// The expexted output of the hash + [Test] + [TestCase("test input", "40aa1b203c9d8ee150b21c3c7cda8261492e5420c5f2b9f7380700e094c303b48e62f319c1da0e32eb40d113c5f1749cc61aeb499167890ab82f2cc9bb706971")] + [TestCase("lorem ipsum dolor", "cd813e13d1d3919cdccc31c19d8f8b70bd25e9819f8770a011c8c7a6228536e6c9427b338cd732f2da3c0444dfebef838b745cdaf3fd5dcba8db24fc83a3f6ef")] + [TestCase("LoReM IpSuM DoLoR", "3e4704d31f838456c0a5f0892afd392fbc79649a029d017b8104ebd00e2816d94ab4629f731765bf655088b130c51f6f47ca2f8b047749dbd992cf45e89ff431")] + [TestCase("1234567890", "12b03226a6d8be9c6e8cd5e55dc6c7920caaa39df14aab92d5e3ea9340d1c8a4d3d0b8e4314f1f6ef131ba4bf1ceb9186ab87c801af0d5c95b1befb8cedae2b9")] + public void TestToSHA512Fingerprint(string input, string expected) + { + var result = input.ToSHA512Fingerprint(); + var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + } +} \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj index b545a11d9..5b6bc12d1 100644 --- a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj +++ b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj @@ -48,6 +48,7 @@ + From 856edcf7040535b6711d9203860172c519c25d6f Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Fri, 4 Jul 2014 22:31:53 +0200 Subject: [PATCH 04/11] Adds unit tests for some more string extensions Former-commit-id: b98d133b5bbf0032cf2624808c64ee3c0983ce62 --- .../Extensions/StringExtensionsUnitTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs index d1fefa395..17cc4c753 100644 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -11,6 +11,7 @@ namespace ImageProcessor.UnitTests { using System; + using System.Collections.Generic; using Common.Extensions; using NUnit.Framework; @@ -87,5 +88,41 @@ namespace ImageProcessor.UnitTests var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); Assert.True(comparison); } + + /// + /// Tests the pasing to an integer array + /// + [Test] + public void TestToIntegerArray() + { + Dictionary data = new Dictionary(); + data.Add("123-456,78-90", new int[] { 123, 456, 78, 90 }); + data.Add("87390174,741897498,74816,748297,57355", new int[] { 87390174, 741897498, 74816, 748297, 57355 }); + data.Add("1-2-3", new int[] { 1, 2, 3 }); + + foreach (var item in data) + { + var result = item.Key.ToPositiveIntegerArray(); + Assert.AreEqual(item.Value, result); + } + } + + /// + /// Tests the pasing to an float array + /// + [Test] + public void TestToFloatArray() + { + Dictionary data = new Dictionary(); + data.Add("12.3-4.56,78-9.0", new float[] { 12.3F, 4.56F, 78, 9 }); + data.Add("87390.174,7.41897498,748.16,748297,5.7355", new float[] { 87390.174F, 7.41897498F, 748.16F, 748297, 5.7355F }); + data.Add("1-2-3", new float[] { 1, 2, 3 }); + + foreach (var item in data) + { + var result = item.Key.ToPositiveFloatArray(); + Assert.AreEqual(item.Value, result); + } + } } } \ No newline at end of file From 5591a0f72671cfc4d126c016ed31785da0f80210 Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Fri, 4 Jul 2014 22:49:13 +0200 Subject: [PATCH 05/11] Tests a few more string extensions Former-commit-id: 85f8c877e3bee75721afd18535e7fec62027c304 --- .../Extensions/StringExtensionsUnitTests.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs index 17cc4c753..70f3e83db 100644 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -124,5 +124,45 @@ namespace ImageProcessor.UnitTests Assert.AreEqual(item.Value, result); } } + + /// + /// Tests if the value is a valid URI + /// + /// The value to test + /// Whether the value is correct + /// + /// The full RFC3986 does not seem to pass the test with the square brackets + /// + [Test] + [TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true)] + [TestCase("-", true)] + [TestCase(".", true)] + [TestCase("_", true)] + [TestCase("~", true)] + [TestCase(":", true)] + [TestCase("/", true)] + [TestCase("?", true)] + [TestCase("#", true)] + [TestCase("[", false)] + [TestCase("]", false)] + [TestCase("@", true)] + [TestCase("!", true)] + [TestCase("$", true)] + [TestCase("&", true)] + [TestCase("'", true)] + [TestCase("(", true)] + [TestCase(")", true)] + [TestCase("*", true)] + [TestCase("+", true)] + [TestCase(",", true)] + [TestCase(";", true)] + [TestCase("=", true)] + [TestCase("lorem ipsum", false)] + [TestCase("é", false)] + public void TestIsValidUri(string input, bool expected) + { + var result = input.IsValidVirtualPathName(); + Assert.AreEqual(expected, result); + } } } \ No newline at end of file From 6f9d0999bdd5db0e1c9acad9e080f50e90161547 Mon Sep 17 00:00:00 2001 From: James South Date: Sat, 5 Jul 2014 10:24:13 +0100 Subject: [PATCH 06/11] Making project build on Mono Former-commit-id: 234b00edb21fd70133fb1e74f7ac59d1f96db275 --- src/ImageProcessor/ImageProcessor.csproj | 1 + src/ImageProcessor/Imaging/Formats/FormatBase.cs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index c21b78194..274b3e76d 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -13,6 +13,7 @@ ..\ true Client + False true diff --git a/src/ImageProcessor/Imaging/Formats/FormatBase.cs b/src/ImageProcessor/Imaging/Formats/FormatBase.cs index ea00e82a2..189bad8c3 100644 --- a/src/ImageProcessor/Imaging/Formats/FormatBase.cs +++ b/src/ImageProcessor/Imaging/Formats/FormatBase.cs @@ -90,7 +90,11 @@ namespace ImageProcessor.Imaging.Formats /// public virtual Image Load(Stream stream) { +#if !__MonoCS__ return Image.FromStream(stream, true); +#else + return Image.FromStream(stream); +#endif } /// From dc613fb5495c125d213ad55b50ca49c41c2a66a8 Mon Sep 17 00:00:00 2001 From: James South Date: Sat, 5 Jul 2014 13:31:06 +0100 Subject: [PATCH 07/11] Adding updated tests and reducing image sizes. Former-commit-id: 054373e5c704ca0ca2a26770863549b2eeca7165 --- .../Extensions/DoubleExtensionsUnitTests.cs | 15 ++-- .../Extensions/IntegerExtensionsUnitTests.cs | 5 +- .../Extensions/StringExtensionsUnitTests.cs | 90 ++++++++++++------- .../Images/autorotate.jpg.REMOVED.git-id | 2 +- .../cmyk-profile-euroscale.jpg.REMOVED.git-id | 2 +- .../Images/cmyk.jpg.REMOVED.git-id | 2 +- .../color-vision-test.gif.REMOVED.git-id | 2 +- .../Images/exif-Tulips.jpg.REMOVED.git-id | 2 +- .../Images/exif-rocks.jpg.REMOVED.git-id | 2 +- .../format-Penguins-8bit.png.REMOVED.git-id | 2 +- .../Images/format-Penguins.bmp.REMOVED.git-id | 2 +- .../Images/format-Penguins.gif.REMOVED.git-id | 2 +- .../Images/format-Penguins.jpg.REMOVED.git-id | 2 +- .../Images/format-Penguins.png.REMOVED.git-id | 2 +- .../Images/format-animated.gif | 3 - .../Images/format-animated.gif.REMOVED.git-id | 1 + .../Images/hi-color.png | 4 +- .../Images/hi-contrast.jpg | 4 +- .../Images/hi-saturation.jpg | 4 +- .../profile-adobe-rgb.jpg.REMOVED.git-id | 2 +- .../Images/profile-srgb.jpg.REMOVED.git-id | 2 +- .../Images/size-Penguins-200.jpg | 4 +- .../Images/text-over-transparent.png | 4 +- .../Images/udendørs.jpg | 3 + .../Images/udendørs.jpg.REMOVED.git-id | 1 - src/ImageProcessor/ImageFactory.cs | 5 +- .../Imaging/Formats/GifEncoder.cs | 10 +-- .../Imaging/Formats/JpegFormat.cs | 2 +- src/ImageProcessorConsole/Program.cs | 19 ++-- .../images/output/120430.gif.REMOVED.git-id | 2 +- .../images/output/Tl4Yb.gif.REMOVED.git-id | 2 +- .../images/output/circle.png | 3 + .../images/output/nLpfllv.gif.REMOVED.git-id | 2 +- 33 files changed, 116 insertions(+), 93 deletions(-) delete mode 100644 src/ImageProcessor.UnitTests/Images/format-animated.gif create mode 100644 src/ImageProcessor.UnitTests/Images/format-animated.gif.REMOVED.git-id create mode 100644 src/ImageProcessor.UnitTests/Images/udendørs.jpg delete mode 100644 src/ImageProcessor.UnitTests/Images/udendørs.jpg.REMOVED.git-id create mode 100644 src/ImageProcessorConsole/images/output/circle.png diff --git a/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs index 108a9a246..cba1c07bc 100644 --- a/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs @@ -8,9 +8,8 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.UnitTests +namespace ImageProcessor.UnitTests.Extensions { - using System; using System.Collections.Generic; using Common.Extensions; using NUnit.Framework; @@ -32,11 +31,13 @@ namespace ImageProcessor.UnitTests [TestFixtureSetUp] public void Init() { - this.doubleToByteTests = new Dictionary(); - this.doubleToByteTests.Add(-10, 0x0); - this.doubleToByteTests.Add(1.5, 0x1); - this.doubleToByteTests.Add(25.7, 0x19); - this.doubleToByteTests.Add(1289047, 0xFF); + this.doubleToByteTests = new Dictionary + { + { -10, 0x0 }, + { 1.5, 0x1 }, + { 25.7, 0x19 }, + { 1289047, 0xFF } + }; } /// diff --git a/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs index 1e85e633b..2adbe3b17 100644 --- a/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/IntegerExtensionsUnitTests.cs @@ -8,9 +8,8 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.UnitTests +namespace ImageProcessor.UnitTests.Extensions { - using System; using Common.Extensions; using NUnit.Framework; @@ -31,7 +30,7 @@ namespace ImageProcessor.UnitTests [TestCase(3156, 0xFF)] public void ToByteTest(int input, byte expected) { - var result = input.ToByte(); + byte result = input.ToByte(); Assert.AreEqual(expected, result); } } diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs index 70f3e83db..c89c09dcd 100644 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -8,11 +8,11 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.UnitTests +namespace ImageProcessor.UnitTests.Extensions { using System; using System.Collections.Generic; - using Common.Extensions; + using ImageProcessor.Common.Extensions; using NUnit.Framework; /// @@ -25,7 +25,7 @@ namespace ImageProcessor.UnitTests /// Tests the MD5 fingerprint /// /// The input value - /// The expexted output of the hash + /// The expected output of the hash [Test] [TestCase("test input", "2e7f7a62eabf0993239ca17c78c464d9")] [TestCase("lorem ipsum dolor", "96ee002fee25e8b675a477c9750fa360")] @@ -33,8 +33,8 @@ namespace ImageProcessor.UnitTests [TestCase("1234567890", "e15e31c3d8898c92ab172a4311be9e84")] public void TestToMd5Fingerprint(string input, string expected) { - var result = input.ToMD5Fingerprint(); - var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + string result = input.ToMD5Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); Assert.True(comparison); } @@ -42,7 +42,7 @@ namespace ImageProcessor.UnitTests /// Tests the SHA-1 fingerprint /// /// The input value - /// The expexted output of the hash + /// The expected output of the hash [Test] [TestCase("test input", "49883b34e5a0f48224dd6230f471e9dc1bdbeaf5")] [TestCase("lorem ipsum dolor", "75899ad8827a32493928903aecd6e931bf36f967")] @@ -50,8 +50,8 @@ namespace ImageProcessor.UnitTests [TestCase("1234567890", "01b307acba4f54f55aafc33bb06bbbf6ca803e9a")] public void TestToSHA1Fingerprint(string input, string expected) { - var result = input.ToSHA1Fingerprint(); - var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + string result = input.ToSHA1Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); Assert.True(comparison); } @@ -59,7 +59,7 @@ namespace ImageProcessor.UnitTests /// Tests the SHA-256 fingerprint /// /// The input value - /// The expexted output of the hash + /// The expected output of the hash [Test] [TestCase("test input", "9dfe6f15d1ab73af898739394fd22fd72a03db01834582f24bb2e1c66c7aaeae")] [TestCase("lorem ipsum dolor", "ed03353266c993ea9afb9900a3ca688ddec1656941b1ca15ee1650a022616dfa")] @@ -67,8 +67,8 @@ namespace ImageProcessor.UnitTests [TestCase("1234567890", "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646")] public void TestToSHA256Fingerprint(string input, string expected) { - var result = input.ToSHA256Fingerprint(); - var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + string result = input.ToSHA256Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); Assert.True(comparison); } @@ -76,7 +76,7 @@ namespace ImageProcessor.UnitTests /// Tests the SHA-512 fingerprint /// /// The input value - /// The expexted output of the hash + /// The expected output of the hash [Test] [TestCase("test input", "40aa1b203c9d8ee150b21c3c7cda8261492e5420c5f2b9f7380700e094c303b48e62f319c1da0e32eb40d113c5f1749cc61aeb499167890ab82f2cc9bb706971")] [TestCase("lorem ipsum dolor", "cd813e13d1d3919cdccc31c19d8f8b70bd25e9819f8770a011c8c7a6228536e6c9427b338cd732f2da3c0444dfebef838b745cdaf3fd5dcba8db24fc83a3f6ef")] @@ -84,49 +84,73 @@ namespace ImageProcessor.UnitTests [TestCase("1234567890", "12b03226a6d8be9c6e8cd5e55dc6c7920caaa39df14aab92d5e3ea9340d1c8a4d3d0b8e4314f1f6ef131ba4bf1ceb9186ab87c801af0d5c95b1befb8cedae2b9")] public void TestToSHA512Fingerprint(string input, string expected) { - var result = input.ToSHA512Fingerprint(); - var comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + string result = input.ToSHA512Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); Assert.True(comparison); } /// - /// Tests the pasing to an integer array + /// Tests the passing to an integer array /// [Test] public void TestToIntegerArray() { - Dictionary data = new Dictionary(); - data.Add("123-456,78-90", new int[] { 123, 456, 78, 90 }); - data.Add("87390174,741897498,74816,748297,57355", new int[] { 87390174, 741897498, 74816, 748297, 57355 }); - data.Add("1-2-3", new int[] { 1, 2, 3 }); + Dictionary data = new Dictionary + { + { + "123-456,78-90", + new[] { 123, 456, 78, 90 } + }, + { + "87390174,741897498,74816,748297,57355", + new[] + { + 87390174, 741897498, 74816, + 748297, 57355 + } + }, + { "1-2-3", new[] { 1, 2, 3 } } + }; - foreach (var item in data) + foreach (KeyValuePair item in data) { - var result = item.Key.ToPositiveIntegerArray(); + int[] result = item.Key.ToPositiveIntegerArray(); Assert.AreEqual(item.Value, result); } } /// - /// Tests the pasing to an float array + /// Tests the passing to an float array /// [Test] public void TestToFloatArray() { - Dictionary data = new Dictionary(); - data.Add("12.3-4.56,78-9.0", new float[] { 12.3F, 4.56F, 78, 9 }); - data.Add("87390.174,7.41897498,748.16,748297,5.7355", new float[] { 87390.174F, 7.41897498F, 748.16F, 748297, 5.7355F }); - data.Add("1-2-3", new float[] { 1, 2, 3 }); + Dictionary data = new Dictionary + { + { + "12.3-4.56,78-9.0", + new[] { 12.3F, 4.56F, 78, 9 } + }, + { + "87390.174,7.41897498,748.16,748297,5.7355", + new[] + { + 87390.174F, 7.41897498F, + 748.16F, 748297, 5.7355F + } + }, + { "1-2-3", new float[] { 1, 2, 3 } } + }; - foreach (var item in data) + foreach (KeyValuePair item in data) { - var result = item.Key.ToPositiveFloatArray(); + float[] result = item.Key.ToPositiveFloatArray(); Assert.AreEqual(item.Value, result); } } /// - /// Tests if the value is a valid URI + /// Tests if the value is a valid URI path name. I.E the path part of a uri. /// /// The value to test /// Whether the value is correct @@ -139,10 +163,10 @@ namespace ImageProcessor.UnitTests [TestCase(".", true)] [TestCase("_", true)] [TestCase("~", true)] - [TestCase(":", true)] + [TestCase(":", false)] [TestCase("/", true)] [TestCase("?", true)] - [TestCase("#", true)] + [TestCase("#", false)] [TestCase("[", false)] [TestCase("]", false)] [TestCase("@", true)] @@ -159,9 +183,9 @@ namespace ImageProcessor.UnitTests [TestCase("=", true)] [TestCase("lorem ipsum", false)] [TestCase("é", false)] - public void TestIsValidUri(string input, bool expected) + public void TestIsValidUriPathName(string input, bool expected) { - var result = input.IsValidVirtualPathName(); + bool result = input.IsValidVirtualPathName(); Assert.AreEqual(expected, result); } } diff --git a/src/ImageProcessor.UnitTests/Images/autorotate.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/autorotate.jpg.REMOVED.git-id index 19785c8e5..1b5e335ff 100644 --- a/src/ImageProcessor.UnitTests/Images/autorotate.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/autorotate.jpg.REMOVED.git-id @@ -1 +1 @@ -85a8ae18f9955def2b42ba9240bce4de1bfe5781 \ No newline at end of file +75b37593bb2e505bf4fbe874eaf30debd6161c2e \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/cmyk-profile-euroscale.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/cmyk-profile-euroscale.jpg.REMOVED.git-id index 7747bdaae..11eb19931 100644 --- a/src/ImageProcessor.UnitTests/Images/cmyk-profile-euroscale.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/cmyk-profile-euroscale.jpg.REMOVED.git-id @@ -1 +1 @@ -13492524f9d984c12adfe6183a4c1d92fb11ec4e \ No newline at end of file +d0a1a39a6729e826098ae5e987c22c34c989b7e3 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/cmyk.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/cmyk.jpg.REMOVED.git-id index 30b05146b..9ba0b9f39 100644 --- a/src/ImageProcessor.UnitTests/Images/cmyk.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/cmyk.jpg.REMOVED.git-id @@ -1 +1 @@ -ed725726e4ac1ffeac821664af14865a66fa933f \ No newline at end of file +9160894da31fedebb1fcd64eb57ca173187c63a6 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/color-vision-test.gif.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/color-vision-test.gif.REMOVED.git-id index 5c4f4195d..ed1d0b80b 100644 --- a/src/ImageProcessor.UnitTests/Images/color-vision-test.gif.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/color-vision-test.gif.REMOVED.git-id @@ -1 +1 @@ -35a926115b13b61dc37308f8d903b42d14f92924 \ No newline at end of file +b169fac4f1591e81e91c0bb6fed6dcf62a34c80e \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/exif-Tulips.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/exif-Tulips.jpg.REMOVED.git-id index 84b9aff85..20704f4a9 100644 --- a/src/ImageProcessor.UnitTests/Images/exif-Tulips.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/exif-Tulips.jpg.REMOVED.git-id @@ -1 +1 @@ -54c51eb6a86f31a42433b8167470fb18dad32c7d \ No newline at end of file +9d7e7964a2285363171929315b15ec69f14831ef \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/exif-rocks.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/exif-rocks.jpg.REMOVED.git-id index 41c6c25df..2e03e238f 100644 --- a/src/ImageProcessor.UnitTests/Images/exif-rocks.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/exif-rocks.jpg.REMOVED.git-id @@ -1 +1 @@ -33b6912af301bf216ee81d82b2c3ce6c49e03021 \ No newline at end of file +be31c9c0dea90586e2965208611fad024f6a5b08 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-Penguins-8bit.png.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-Penguins-8bit.png.REMOVED.git-id index aa9a70e0f..c48cdc177 100644 --- a/src/ImageProcessor.UnitTests/Images/format-Penguins-8bit.png.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/format-Penguins-8bit.png.REMOVED.git-id @@ -1 +1 @@ -c3d556d9d486b8b8b49cdbcc9c12a9d3a2db4c1f \ No newline at end of file +51ccec74a0351599de104f166b32d2860acaf089 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-Penguins.bmp.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-Penguins.bmp.REMOVED.git-id index 74f69293c..9ba53bc67 100644 --- a/src/ImageProcessor.UnitTests/Images/format-Penguins.bmp.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/format-Penguins.bmp.REMOVED.git-id @@ -1 +1 @@ -8150b46ab27c62ba51aaba551eef3f1a30f08de9 \ No newline at end of file +d7adbea2db4e3388541e31a229e5741677aaa7fd \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-Penguins.gif.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-Penguins.gif.REMOVED.git-id index ce873d473..225d59af3 100644 --- a/src/ImageProcessor.UnitTests/Images/format-Penguins.gif.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/format-Penguins.gif.REMOVED.git-id @@ -1 +1 @@ -6ad3b846d4697584ff601ac481b14a4d3bbb5736 \ No newline at end of file +b301e58d431a78d3f17be53be1cdc94c86286389 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-Penguins.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-Penguins.jpg.REMOVED.git-id index ad4371113..06482dbd9 100644 --- a/src/ImageProcessor.UnitTests/Images/format-Penguins.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/format-Penguins.jpg.REMOVED.git-id @@ -1 +1 @@ -030ab8a685bebb796c24cc710edd9e69859164f6 \ No newline at end of file +ee5a15e7f8fc2655d5c1fc736a05857ab3d885bd \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-Penguins.png.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-Penguins.png.REMOVED.git-id index 78062a0e7..4ab6b372b 100644 --- a/src/ImageProcessor.UnitTests/Images/format-Penguins.png.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/format-Penguins.png.REMOVED.git-id @@ -1 +1 @@ -a2c796fbb7de948230a22982ab74892891dd5198 \ No newline at end of file +b6434b5a35e989d4fa71ede1a8316fedeee7ae5f \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/format-animated.gif b/src/ImageProcessor.UnitTests/Images/format-animated.gif deleted file mode 100644 index ac36f6f25..000000000 --- a/src/ImageProcessor.UnitTests/Images/format-animated.gif +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:6212724b3e94908939823d0753b4307923b65d7a27f51823dd3ba7656543349c -size 22525 diff --git a/src/ImageProcessor.UnitTests/Images/format-animated.gif.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/format-animated.gif.REMOVED.git-id new file mode 100644 index 000000000..ac3664d5f --- /dev/null +++ b/src/ImageProcessor.UnitTests/Images/format-animated.gif.REMOVED.git-id @@ -0,0 +1 @@ +a41fb1117e1d730a4a488dcb67e0b867aa3c614e \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/hi-color.png b/src/ImageProcessor.UnitTests/Images/hi-color.png index a9de4cc09..cc8677aa8 100644 --- a/src/ImageProcessor.UnitTests/Images/hi-color.png +++ b/src/ImageProcessor.UnitTests/Images/hi-color.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:977cc9071257655c9923d267ea5bd417b69754367c2f1aa8765247b68e2bb878 -size 1539 +oid sha256:2f1263641d5e6ed29e96f211f4d78870496c29912245ac7d3c48716ee0ede313 +size 23922 diff --git a/src/ImageProcessor.UnitTests/Images/hi-contrast.jpg b/src/ImageProcessor.UnitTests/Images/hi-contrast.jpg index 98ac863a2..94581b505 100644 --- a/src/ImageProcessor.UnitTests/Images/hi-contrast.jpg +++ b/src/ImageProcessor.UnitTests/Images/hi-contrast.jpg @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c0ce9e02f2a4663a0a0ed433d5594be87b3fa0387bc8335e80f84d59c34aa424 -size 51058 +oid sha256:1d6fb72b9710edcfba0f75dcd349712d5e940ec87650a08380f8180e6c9a62b2 +size 34996 diff --git a/src/ImageProcessor.UnitTests/Images/hi-saturation.jpg b/src/ImageProcessor.UnitTests/Images/hi-saturation.jpg index b56f9a83c..3405e88a5 100644 --- a/src/ImageProcessor.UnitTests/Images/hi-saturation.jpg +++ b/src/ImageProcessor.UnitTests/Images/hi-saturation.jpg @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6b2012b2eda13a531645c287c254ae5de0e9070368cb4bc806f48314e0464ccd -size 33850 +oid sha256:fa947c3b5904e0138c359d7ccd81ad3209330880d412f1b09dfe32a355ee6d3b +size 51300 diff --git a/src/ImageProcessor.UnitTests/Images/profile-adobe-rgb.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/profile-adobe-rgb.jpg.REMOVED.git-id index 4ffbf7a8a..8064ffb21 100644 --- a/src/ImageProcessor.UnitTests/Images/profile-adobe-rgb.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/profile-adobe-rgb.jpg.REMOVED.git-id @@ -1 +1 @@ -e29f32091aa13a5b047ccd960f3dc6da9656c84b \ No newline at end of file +189f79f9b9604c5413aba928662d84edd426142d \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/profile-srgb.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/profile-srgb.jpg.REMOVED.git-id index f409bc82b..101151778 100644 --- a/src/ImageProcessor.UnitTests/Images/profile-srgb.jpg.REMOVED.git-id +++ b/src/ImageProcessor.UnitTests/Images/profile-srgb.jpg.REMOVED.git-id @@ -1 +1 @@ -ab9deaa737f9db9bf0f563e7843ba9b7981cec86 \ No newline at end of file +f731bdf700d2718f528317263264e5466374c5e5 \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/Images/size-Penguins-200.jpg b/src/ImageProcessor.UnitTests/Images/size-Penguins-200.jpg index 07605996f..4520c9c95 100644 --- a/src/ImageProcessor.UnitTests/Images/size-Penguins-200.jpg +++ b/src/ImageProcessor.UnitTests/Images/size-Penguins-200.jpg @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:672de68017f17260126901065f1c6ade2b2981d33dea0dea1606bf7cfb6fdcf3 -size 10119 +oid sha256:f0b24fb4937a0416bf62f7a743d2679c7eb2014bceb1a898826fb08e6231bad7 +size 37476 diff --git a/src/ImageProcessor.UnitTests/Images/text-over-transparent.png b/src/ImageProcessor.UnitTests/Images/text-over-transparent.png index 33d4962bc..1ae73c91f 100644 --- a/src/ImageProcessor.UnitTests/Images/text-over-transparent.png +++ b/src/ImageProcessor.UnitTests/Images/text-over-transparent.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2322d8dd81df86b8135d399743ea758ad26d6b2ccdcc704e2687ae72d0c187e7 -size 7317 +oid sha256:ba300af935752628b37a7f892369e7229f4f6ae701522cdedeefcf5c2b251afa +size 7680 diff --git a/src/ImageProcessor.UnitTests/Images/udendørs.jpg b/src/ImageProcessor.UnitTests/Images/udendørs.jpg new file mode 100644 index 000000000..84fb0cc3a --- /dev/null +++ b/src/ImageProcessor.UnitTests/Images/udendørs.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:65434ff6c6d5f502308e627fcfbe1d26402b83fb5efba15749ffbb5e8795995e +size 55987 diff --git a/src/ImageProcessor.UnitTests/Images/udendørs.jpg.REMOVED.git-id b/src/ImageProcessor.UnitTests/Images/udendørs.jpg.REMOVED.git-id deleted file mode 100644 index 0db1445a2..000000000 --- a/src/ImageProcessor.UnitTests/Images/udendørs.jpg.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -4d040d9aa3519b3d2303419d1f03eebebf88e956 \ No newline at end of file diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index f2f573268..7e37983c3 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -158,9 +158,10 @@ namespace ImageProcessor this.CurrentImageFormat = format; // Always load the data. - foreach (PropertyItem propertyItem in this.Image.PropertyItems) + // TODO. Some custom data doesn't seem to get copied by default methods. + foreach (int id in this.Image.PropertyIdList) { - this.ExifPropertyItems[propertyItem.Id] = propertyItem; + this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id); } this.ShouldProcess = true; diff --git a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs index 662ad314c..b7650d099 100644 --- a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs +++ b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs @@ -282,17 +282,17 @@ namespace ImageProcessor.Imaging.Formats { int count = this.repeatCount.GetValueOrDefault(0); - // File Header sinature and version. + // File Header signature and version. this.WriteString(FileType); this.WriteString(FileVersion); // Write the logical screen descriptor. this.WriteShort(this.width.GetValueOrDefault(w)); // Initial Logical Width this.WriteShort(this.height.GetValueOrDefault(h)); // Initial Logical Height - + // Read the global color table info. sourceGif.Position = SourceGlobalColorInfoPosition; - this.WriteByte(sourceGif.ReadByte()); + this.WriteByte(sourceGif.ReadByte()); this.WriteByte(0); // Background Color Index this.WriteByte(0); // Pixel aspect ratio @@ -301,7 +301,7 @@ namespace ImageProcessor.Imaging.Formats // The different browsers interpret the spec differently when adding a loop. // If the loop count is one IE and FF < 3 (incorrectly) loop an extra number of times. // Removing the Netscape header should fix this. - if (count != 1) + if (count > -1 && count != 1) { // Application Extension Header this.WriteShort(ApplicationExtensionBlockIdentifier); @@ -357,7 +357,7 @@ namespace ImageProcessor.Imaging.Formats this.WriteShort(GraphicControlExtensionBlockIdentifier); // Identifier this.WriteByte(GraphicControlExtensionBlockSize); // Block Size this.WriteByte(blockhead[3] & 0xf7 | 0x08); // Setting disposal flag - this.WriteShort(Convert.ToInt32(frameDelay / 10)); // Setting frame delay + this.WriteShort(Convert.ToInt32(frameDelay / 10.0f)); // Setting frame delay this.WriteByte(blockhead[6]); // Transparent color index this.WriteByte(0); // Terminator } diff --git a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs index ae5fd05c3..f7ff73c66 100644 --- a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs @@ -75,7 +75,7 @@ namespace ImageProcessor.Imaging.Formats { base.ApplyProcessor(processor, factory); - // Set the property item information from any Exif metadata. + // Set the property item information from any Exif metadata. // We do this here so that they can be changed between processor methods. if (factory.PreserveExifData) { diff --git a/src/ImageProcessorConsole/Program.cs b/src/ImageProcessorConsole/Program.cs index 248fe1d7f..5bacb50dd 100644 --- a/src/ImageProcessorConsole/Program.cs +++ b/src/ImageProcessorConsole/Program.cs @@ -15,9 +15,7 @@ namespace ImageProcessorConsole using System.Drawing; using System.IO; using System.Linq; - using ImageProcessor; - using ImageProcessor.Imaging.Formats; /// /// The program. @@ -33,6 +31,7 @@ namespace ImageProcessorConsole public static void Main(string[] args) { string path = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath; + // ReSharper disable once AssignNullToNotNullAttribute string resolvedPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\input")); DirectoryInfo di = new DirectoryInfo(resolvedPath); @@ -41,10 +40,8 @@ namespace ImageProcessorConsole di.Create(); } - //FileInfo[] files = di.GetFiles("*.jpg"); - //FileInfo[] files = di.GetFiles(); - IEnumerable files = GetFilesByExtensions(di, ".gif", ".webp"); - + IEnumerable files = GetFilesByExtensions(di, ".gif"); + //IEnumerable files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png"); foreach (FileInfo fileInfo in files) { @@ -53,18 +50,13 @@ namespace ImageProcessorConsole // ImageProcessor using (MemoryStream inStream = new MemoryStream(photoBytes)) { - using (ImageFactory imageFactory = new ImageFactory()) + using (ImageFactory imageFactory = new ImageFactory(true)) { Size size = new Size(200, 200); // Load, resize, set the format and quality and save an image. imageFactory.Load(inStream) - //.AutoRotate() .Constrain(size) - //.Format(new WebPFormat()) - //.Quality(5) - // ReSharper disable once AssignNullToNotNullAttribute - // .Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", Path.GetFileNameWithoutExtension(fileInfo.Name) + ".webp"))); .Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name))); } } @@ -74,7 +66,10 @@ namespace ImageProcessorConsole public static IEnumerable GetFilesByExtensions(DirectoryInfo dir, params string[] extensions) { if (extensions == null) + { throw new ArgumentNullException("extensions"); + } + IEnumerable files = dir.EnumerateFiles(); return files.Where(f => extensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase)); } diff --git a/src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id b/src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id index 71ce555c1..4767fd13d 100644 --- a/src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id +++ b/src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id @@ -1 +1 @@ -30ec5c05548fd350f9b7c699715848b9fbfb8ca9 \ No newline at end of file +6f3f997e323adb1fce142930d86338efacffc3fd \ No newline at end of file diff --git a/src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id b/src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id index 6515d65a0..f0f194a2b 100644 --- a/src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id +++ b/src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id @@ -1 +1 @@ -fdc62fc2d056ab885eb9e8fd12b9155ee86d7c43 \ No newline at end of file +3ab082661fc5d88f88643c47409e196d66e26d4b \ No newline at end of file diff --git a/src/ImageProcessorConsole/images/output/circle.png b/src/ImageProcessorConsole/images/output/circle.png new file mode 100644 index 000000000..a96f1ecf3 --- /dev/null +++ b/src/ImageProcessorConsole/images/output/circle.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f31110b7933864eff3b149371c962174a8855a1c65f4fee7cb3bc63a79b71467 +size 2905 diff --git a/src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id b/src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id index c07be0b0f..b8684cd60 100644 --- a/src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id +++ b/src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id @@ -1 +1 @@ -77cce70db3722920d60f4b17a45a5e390a09838a \ No newline at end of file +e2c2fbac64987ad26e19f5d2721fcaa60fee843d \ No newline at end of file From 1be5deb3139633db9a8f46d3b4d9041e1e1ccdba Mon Sep 17 00:00:00 2001 From: James South Date: Sat, 5 Jul 2014 14:49:35 +0100 Subject: [PATCH 08/11] Fixing unit test Former-commit-id: 7cbaa0dae70fbec4dc5ec2e6bdaaa1f07e1c08c5 --- .../Extensions/StringExtensionsUnitTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs index c89c09dcd..a3c5ea794 100644 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -156,6 +156,7 @@ namespace ImageProcessor.UnitTests.Extensions /// Whether the value is correct /// /// The full RFC3986 does not seem to pass the test with the square brackets + /// ':' is failing for some reason in VS but not elsewhere. Could be a build issue. /// [Test] [TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true)] @@ -163,7 +164,7 @@ namespace ImageProcessor.UnitTests.Extensions [TestCase(".", true)] [TestCase("_", true)] [TestCase("~", true)] - [TestCase(":", false)] + [TestCase(":", true)] [TestCase("/", true)] [TestCase("?", true)] [TestCase("#", false)] From acd81f7a639a3d5f938c3fd3b52e400438f5b332 Mon Sep 17 00:00:00 2001 From: James South Date: Sun, 6 Jul 2014 12:11:52 +0100 Subject: [PATCH 09/11] Refactoring extensions Former-commit-id: 58557a02831d8c6a455bdd59d28f8965810d7f03 --- .../Extensions/StringExtensionsUnitTests.cs | 110 -------------- .../Extensions/StringExtensionsUnitTests.cs | 98 ++++++++++++ .../ImageProcessor.Web.UnitTests.csproj | 1 + .../NET45/Configuration/ImageCacheSection.cs | 5 +- .../NET45/Extensions/StringExtensions.cs | 143 ++++++++++++++++++ .../HttpModules/ImageProcessingModule.cs | 2 +- .../NET45/ImageProcessor.Web_NET45.csproj | 1 + .../Common/Extensions/AssemblyExtensions.cs | 13 +- .../Common/Extensions/StringExtensions.cs | 105 ------------- .../Imaging/Formats/GifEncoder.cs | 22 +-- .../Imaging/Formats/JpegFormat.cs | 2 +- .../Imaging/Formats/NativeMethods.cs | 6 + src/ImageProcessor/Settings.StyleCop | 1 + 13 files changed, 272 insertions(+), 237 deletions(-) create mode 100644 src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs create mode 100644 src/ImageProcessor.Web/NET45/Extensions/StringExtensions.cs diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs index a3c5ea794..ab43bd92b 100644 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -10,7 +10,6 @@ namespace ImageProcessor.UnitTests.Extensions { - using System; using System.Collections.Generic; using ImageProcessor.Common.Extensions; using NUnit.Framework; @@ -21,74 +20,6 @@ namespace ImageProcessor.UnitTests.Extensions [TestFixture] public class StringExtensionsUnitTests { - /// - /// Tests the MD5 fingerprint - /// - /// The input value - /// The expected output of the hash - [Test] - [TestCase("test input", "2e7f7a62eabf0993239ca17c78c464d9")] - [TestCase("lorem ipsum dolor", "96ee002fee25e8b675a477c9750fa360")] - [TestCase("LoReM IpSuM DoLoR", "41e201da794c7fbdb8ce5526a71c8c83")] - [TestCase("1234567890", "e15e31c3d8898c92ab172a4311be9e84")] - public void TestToMd5Fingerprint(string input, string expected) - { - string result = input.ToMD5Fingerprint(); - bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); - Assert.True(comparison); - } - - /// - /// Tests the SHA-1 fingerprint - /// - /// The input value - /// The expected output of the hash - [Test] - [TestCase("test input", "49883b34e5a0f48224dd6230f471e9dc1bdbeaf5")] - [TestCase("lorem ipsum dolor", "75899ad8827a32493928903aecd6e931bf36f967")] - [TestCase("LoReM IpSuM DoLoR", "2f44519afae72fc0837b72c6b53cb11338a1f916")] - [TestCase("1234567890", "01b307acba4f54f55aafc33bb06bbbf6ca803e9a")] - public void TestToSHA1Fingerprint(string input, string expected) - { - string result = input.ToSHA1Fingerprint(); - bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); - Assert.True(comparison); - } - - /// - /// Tests the SHA-256 fingerprint - /// - /// The input value - /// The expected output of the hash - [Test] - [TestCase("test input", "9dfe6f15d1ab73af898739394fd22fd72a03db01834582f24bb2e1c66c7aaeae")] - [TestCase("lorem ipsum dolor", "ed03353266c993ea9afb9900a3ca688ddec1656941b1ca15ee1650a022616dfa")] - [TestCase("LoReM IpSuM DoLoR", "55f6cb90ba5cd8eeb6f5f16f083ebcd48ea06c34cc5aed8e33246fc3153d3898")] - [TestCase("1234567890", "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646")] - public void TestToSHA256Fingerprint(string input, string expected) - { - string result = input.ToSHA256Fingerprint(); - bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); - Assert.True(comparison); - } - - /// - /// Tests the SHA-512 fingerprint - /// - /// The input value - /// The expected output of the hash - [Test] - [TestCase("test input", "40aa1b203c9d8ee150b21c3c7cda8261492e5420c5f2b9f7380700e094c303b48e62f319c1da0e32eb40d113c5f1749cc61aeb499167890ab82f2cc9bb706971")] - [TestCase("lorem ipsum dolor", "cd813e13d1d3919cdccc31c19d8f8b70bd25e9819f8770a011c8c7a6228536e6c9427b338cd732f2da3c0444dfebef838b745cdaf3fd5dcba8db24fc83a3f6ef")] - [TestCase("LoReM IpSuM DoLoR", "3e4704d31f838456c0a5f0892afd392fbc79649a029d017b8104ebd00e2816d94ab4629f731765bf655088b130c51f6f47ca2f8b047749dbd992cf45e89ff431")] - [TestCase("1234567890", "12b03226a6d8be9c6e8cd5e55dc6c7920caaa39df14aab92d5e3ea9340d1c8a4d3d0b8e4314f1f6ef131ba4bf1ceb9186ab87c801af0d5c95b1befb8cedae2b9")] - public void TestToSHA512Fingerprint(string input, string expected) - { - string result = input.ToSHA512Fingerprint(); - bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); - Assert.True(comparison); - } - /// /// Tests the passing to an integer array /// @@ -148,46 +79,5 @@ namespace ImageProcessor.UnitTests.Extensions Assert.AreEqual(item.Value, result); } } - - /// - /// Tests if the value is a valid URI path name. I.E the path part of a uri. - /// - /// The value to test - /// Whether the value is correct - /// - /// The full RFC3986 does not seem to pass the test with the square brackets - /// ':' is failing for some reason in VS but not elsewhere. Could be a build issue. - /// - [Test] - [TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true)] - [TestCase("-", true)] - [TestCase(".", true)] - [TestCase("_", true)] - [TestCase("~", true)] - [TestCase(":", true)] - [TestCase("/", true)] - [TestCase("?", true)] - [TestCase("#", false)] - [TestCase("[", false)] - [TestCase("]", false)] - [TestCase("@", true)] - [TestCase("!", true)] - [TestCase("$", true)] - [TestCase("&", true)] - [TestCase("'", true)] - [TestCase("(", true)] - [TestCase(")", true)] - [TestCase("*", true)] - [TestCase("+", true)] - [TestCase(",", true)] - [TestCase(";", true)] - [TestCase("=", true)] - [TestCase("lorem ipsum", false)] - [TestCase("é", false)] - public void TestIsValidUriPathName(string input, bool expected) - { - bool result = input.IsValidVirtualPathName(); - Assert.AreEqual(expected, result); - } } } \ No newline at end of file diff --git a/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs new file mode 100644 index 000000000..57c261c7b --- /dev/null +++ b/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -0,0 +1,98 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Test harness for the string extensions +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.UnitTests.Extensions +{ + using System; + using ImageProcessor.Web.Extensions; + using NUnit.Framework; + + /// + /// Test harness for the string extensions + /// + [TestFixture] + public class StringExtensionsUnitTests + { + /// + /// Tests the MD5 fingerprint + /// + /// The input value + /// The expected output of the hash + [Test] + [TestCase("test input", "2e7f7a62eabf0993239ca17c78c464d9")] + [TestCase("lorem ipsum dolor", "96ee002fee25e8b675a477c9750fa360")] + [TestCase("LoReM IpSuM DoLoR", "41e201da794c7fbdb8ce5526a71c8c83")] + [TestCase("1234567890", "e15e31c3d8898c92ab172a4311be9e84")] + public void TestToMd5Fingerprint(string input, string expected) + { + string result = input.ToMD5Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + + /// + /// Tests the SHA-1 fingerprint + /// + /// The input value + /// The expected output of the hash + [Test] + [TestCase("test input", "49883b34e5a0f48224dd6230f471e9dc1bdbeaf5")] + [TestCase("lorem ipsum dolor", "75899ad8827a32493928903aecd6e931bf36f967")] + [TestCase("LoReM IpSuM DoLoR", "2f44519afae72fc0837b72c6b53cb11338a1f916")] + [TestCase("1234567890", "01b307acba4f54f55aafc33bb06bbbf6ca803e9a")] + public void TestToSHA1Fingerprint(string input, string expected) + { + string result = input.ToSHA1Fingerprint(); + bool comparison = result.Equals(expected, StringComparison.InvariantCultureIgnoreCase); + Assert.True(comparison); + } + + /// + /// Tests if the value is a valid URI path name. I.E the path part of a uri. + /// + /// The value to test + /// Whether the value is correct + /// + /// The full RFC3986 does not seem to pass the test with the square brackets + /// ':' is failing for some reason in VS but not elsewhere. Could be a build issue. + /// + [Test] + [TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true)] + [TestCase("-", true)] + [TestCase(".", true)] + [TestCase("_", true)] + [TestCase("~", true)] + [TestCase(":", true)] + [TestCase("/", true)] + [TestCase("?", true)] + [TestCase("#", false)] + [TestCase("[", false)] + [TestCase("]", false)] + [TestCase("@", true)] + [TestCase("!", true)] + [TestCase("$", true)] + [TestCase("&", true)] + [TestCase("'", true)] + [TestCase("(", true)] + [TestCase(")", true)] + [TestCase("*", true)] + [TestCase("+", true)] + [TestCase(",", true)] + [TestCase(";", true)] + [TestCase("=", true)] + [TestCase("lorem ipsum", false)] + [TestCase("é", false)] + public void TestIsValidUriPathName(string input, bool expected) + { + bool result = input.IsValidVirtualPathName(); + Assert.AreEqual(expected, result); + } + } +} diff --git a/src/ImageProcessor.Web.UnitTests/ImageProcessor.Web.UnitTests.csproj b/src/ImageProcessor.Web.UnitTests/ImageProcessor.Web.UnitTests.csproj index 17c54d202..460e329b9 100644 --- a/src/ImageProcessor.Web.UnitTests/ImageProcessor.Web.UnitTests.csproj +++ b/src/ImageProcessor.Web.UnitTests/ImageProcessor.Web.UnitTests.csproj @@ -53,6 +53,7 @@ + diff --git a/src/ImageProcessor.Web/NET45/Configuration/ImageCacheSection.cs b/src/ImageProcessor.Web/NET45/Configuration/ImageCacheSection.cs index 8e5abad95..1f868fc04 100644 --- a/src/ImageProcessor.Web/NET45/Configuration/ImageCacheSection.cs +++ b/src/ImageProcessor.Web/NET45/Configuration/ImageCacheSection.cs @@ -10,16 +10,13 @@ namespace ImageProcessor.Web.Configuration { - #region Using using System.Configuration; using System.IO; using System.Xml; - using ImageProcessor.Common.Extensions; + using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; - #endregion - /// /// Represents an image cache section within a configuration file. /// diff --git a/src/ImageProcessor.Web/NET45/Extensions/StringExtensions.cs b/src/ImageProcessor.Web/NET45/Extensions/StringExtensions.cs new file mode 100644 index 000000000..5ec1fdf4b --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Extensions/StringExtensions.cs @@ -0,0 +1,143 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Encapsulates a series of time saving extension methods to the class. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.Extensions +{ + using System; + using System.Globalization; + using System.Linq; + using System.Security.Cryptography; + using System.Text; + using System.Text.RegularExpressions; + + /// + /// Encapsulates a series of time saving extension methods to the class. + /// + public static class StringExtensions + { + #region Cryptography + /// + /// Creates an MD5 fingerprint of the String. + /// + /// The String instance that this method extends. + /// An MD5 fingerprint of the String. + public static string ToMD5Fingerprint(this string expression) + { + byte[] bytes = Encoding.Unicode.GetBytes(expression.ToCharArray()); + + using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) + { + byte[] hash = md5.ComputeHash(bytes); + + // Concatenate the hash bytes into one long String. + return hash.Aggregate( + new StringBuilder(32), + (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) + .ToString().ToLowerInvariant(); + } + } + + /// + /// Creates an SHA1 fingerprint of the String. + /// + /// The String instance that this method extends. + /// An SHA1 fingerprint of the String. + public static string ToSHA1Fingerprint(this string expression) + { + byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); + + using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) + { + byte[] hash = sha1.ComputeHash(bytes); + + // Concatenate the hash bytes into one long String. + return hash.Aggregate( + new StringBuilder(40), + (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) + .ToString().ToLowerInvariant(); + } + } + #endregion + + #region Numbers + /// + /// Creates an array of integers scraped from the String. + /// + /// The String instance that this method extends. + /// An array of integers scraped from the String. + public static int[] ToPositiveIntegerArray(this string expression) + { + if (string.IsNullOrWhiteSpace(expression)) + { + throw new ArgumentNullException("expression"); + } + + Regex regex = new Regex(@"[\d+]+(?=[,-])|[\d+]+(?![,-])", RegexOptions.Compiled); + + MatchCollection matchCollection = regex.Matches(expression); + + // Get the collections. + int count = matchCollection.Count; + int[] matches = new int[count]; + + // Loop and parse the int values. + for (int i = 0; i < count; i++) + { + matches[i] = int.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); + } + + return matches; + } + + /// + /// Creates an array of floats scraped from the String. + /// + /// The String instance that this method extends. + /// An array of floats scraped from the String. + public static float[] ToPositiveFloatArray(this string expression) + { + if (string.IsNullOrWhiteSpace(expression)) + { + throw new ArgumentNullException("expression"); + } + + Regex regex = new Regex(@"[\d+\.]+(?=[,-])|[\d+\.]+(?![,-])", RegexOptions.Compiled); + + MatchCollection matchCollection = regex.Matches(expression); + + // Get the collections. + int count = matchCollection.Count; + float[] matches = new float[count]; + + // Loop and parse the int values. + for (int i = 0; i < count; i++) + { + matches[i] = float.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); + } + + return matches; + } + #endregion + + #region Files and Paths + /// + /// Checks the string to see whether the value is a valid virtual path name. + /// + /// The String instance that this method extends. + /// True if the given string is a valid virtual path name + public static bool IsValidVirtualPathName(this string expression) + { + Uri uri; + + return Uri.TryCreate(expression, UriKind.Relative, out uri) && uri.IsWellFormedOriginalString(); + } + #endregion + } +} diff --git a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs index d70e71867..db378fae4 100644 --- a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs +++ b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs @@ -29,9 +29,9 @@ namespace ImageProcessor.Web.HttpModules using System.Web.Hosting; using System.Web.Security; - using ImageProcessor.Common.Extensions; using ImageProcessor.Web.Caching; using ImageProcessor.Web.Configuration; + using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; #endregion diff --git a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj index 2f5e77f23..40bd16bc7 100644 --- a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj +++ b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj @@ -54,6 +54,7 @@ + diff --git a/src/ImageProcessor/Common/Extensions/AssemblyExtensions.cs b/src/ImageProcessor/Common/Extensions/AssemblyExtensions.cs index 763697ef0..e37480ff0 100644 --- a/src/ImageProcessor/Common/Extensions/AssemblyExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/AssemblyExtensions.cs @@ -1,4 +1,13 @@ - +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Encapsulates a series of time saving extension methods to the class. +// +// -------------------------------------------------------------------------------------------------------------------- + namespace ImageProcessor.Common.Extensions { using System; @@ -38,4 +47,4 @@ namespace ImageProcessor.Common.Extensions } } } -} +} \ No newline at end of file diff --git a/src/ImageProcessor/Common/Extensions/StringExtensions.cs b/src/ImageProcessor/Common/Extensions/StringExtensions.cs index fd7d54139..f315df797 100644 --- a/src/ImageProcessor/Common/Extensions/StringExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/StringExtensions.cs @@ -12,9 +12,6 @@ namespace ImageProcessor.Common.Extensions { using System; using System.Globalization; - using System.Linq; - using System.Security.Cryptography; - using System.Text; using System.Text.RegularExpressions; /// @@ -22,93 +19,6 @@ namespace ImageProcessor.Common.Extensions /// public static class StringExtensions { - #region Cryptography - /// - /// Creates an MD5 fingerprint of the String. - /// - /// The String instance that this method extends. - /// An MD5 fingerprint of the String. - public static string ToMD5Fingerprint(this string expression) - { - byte[] bytes = Encoding.Unicode.GetBytes(expression.ToCharArray()); - - using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) - { - byte[] hash = md5.ComputeHash(bytes); - - // Concatenate the hash bytes into one long String. - return hash.Aggregate( - new StringBuilder(32), - (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) - .ToString().ToLowerInvariant(); - } - } - - /// - /// Creates an SHA1 fingerprint of the String. - /// - /// The String instance that this method extends. - /// An SHA1 fingerprint of the String. - public static string ToSHA1Fingerprint(this string expression) - { - byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); - - using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) - { - byte[] hash = sha1.ComputeHash(bytes); - - // Concatenate the hash bytes into one long String. - return hash.Aggregate( - new StringBuilder(40), - (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) - .ToString().ToLowerInvariant(); - } - } - - /// - /// Creates an SHA256 fingerprint of the String. - /// - /// The String instance that this method extends. - /// An SHA256 fingerprint of the String. - public static string ToSHA256Fingerprint(this string expression) - { - byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); - - using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) - { - byte[] hash = sha256.ComputeHash(bytes); - - // Concatenate the hash bytes into one long String. - return hash.Aggregate( - new StringBuilder(64), - (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) - .ToString().ToLowerInvariant(); - } - } - - /// - /// Creates an SHA512 fingerprint of the String. - /// - /// The String instance that this method extends. - /// An SHA256 fingerprint of the String. - public static string ToSHA512Fingerprint(this string expression) - { - byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); - - using (SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider()) - { - byte[] hash = sha512.ComputeHash(bytes); - - // Concatenate the hash bytes into one long String. - return hash.Aggregate( - new StringBuilder(70), - (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) - .ToString().ToLowerInvariant(); - } - } - #endregion - - #region Numbers /// /// Creates an array of integers scraped from the String. /// @@ -166,20 +76,5 @@ namespace ImageProcessor.Common.Extensions return matches; } - #endregion - - #region Files and Paths - /// - /// Checks the string to see whether the value is a valid virtual path name. - /// - /// The String instance that this method extends. - /// True if the given string is a valid virtual path name - public static bool IsValidVirtualPathName(this string expression) - { - Uri uri; - - return Uri.TryCreate(expression, UriKind.Relative, out uri) && uri.IsWellFormedOriginalString(); - } - #endregion } } diff --git a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs index b7650d099..826ddf42f 100644 --- a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs +++ b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs @@ -298,20 +298,14 @@ namespace ImageProcessor.Imaging.Formats this.WriteByte(0); // Pixel aspect ratio this.WriteColorTable(sourceGif); - // The different browsers interpret the spec differently when adding a loop. - // If the loop count is one IE and FF < 3 (incorrectly) loop an extra number of times. - // Removing the Netscape header should fix this. - if (count > -1 && count != 1) - { - // Application Extension Header - this.WriteShort(ApplicationExtensionBlockIdentifier); - this.WriteByte(ApplicationBlockSize); - this.WriteString(ApplicationIdentification); - this.WriteByte(3); // Application block length - this.WriteByte(1); - this.WriteShort(count); // Repeat count for images. - this.WriteByte(0); // Terminator - } + // Application Extension Header + this.WriteShort(ApplicationExtensionBlockIdentifier); + this.WriteByte(ApplicationBlockSize); + this.WriteString(ApplicationIdentification); + this.WriteByte(3); // Application block length + this.WriteByte(1); + this.WriteShort(count); // Repeat count for images. + this.WriteByte(0); // Terminator } /// diff --git a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs index f7ff73c66..ae5fd05c3 100644 --- a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs @@ -75,7 +75,7 @@ namespace ImageProcessor.Imaging.Formats { base.ApplyProcessor(processor, factory); - // Set the property item information from any Exif metadata. + // Set the property item information from any Exif metadata. // We do this here so that they can be changed between processor methods. if (factory.PreserveExifData) { diff --git a/src/ImageProcessor/Imaging/Formats/NativeMethods.cs b/src/ImageProcessor/Imaging/Formats/NativeMethods.cs index 8aaff3432..574bd1e8f 100644 --- a/src/ImageProcessor/Imaging/Formats/NativeMethods.cs +++ b/src/ImageProcessor/Imaging/Formats/NativeMethods.cs @@ -18,6 +18,12 @@ namespace ImageProcessor.Imaging.Formats /// internal static class NativeMethods { + /// + /// Whether the process is running in 64bit mode. Used for calling the correct dllimport method. + /// Clunky I know but I couldn't get dynamic methods to work. + /// + private static readonly bool Is64Bit = Environment.Is64BitProcess; + #region WebP /// /// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant diff --git a/src/ImageProcessor/Settings.StyleCop b/src/ImageProcessor/Settings.StyleCop index 29b381707..085067600 100644 --- a/src/ImageProcessor/Settings.StyleCop +++ b/src/ImageProcessor/Settings.StyleCop @@ -4,6 +4,7 @@ bitstream dd ddd + dllimport gps mmmm orig From f3707b95973191486ad12bb3e0f84f14365a6e06 Mon Sep 17 00:00:00 2001 From: James South Date: Sun, 6 Jul 2014 18:58:51 +0100 Subject: [PATCH 10/11] Fixing references. Former-commit-id: cd78e3ba30aed1803c400e128a9e4ba0b5090681 --- .../Extensions/StringExtensionsUnitTests.cs | 83 ------------------- .../ImageProcessor.UnitTests.csproj | 5 +- .../Extensions/StringExtensionsUnitTests.cs | 62 ++++++++++++++ .../NET4/ImageProcessor.Web_NET4.csproj | 3 + .../NET45/Caching/DiskCache.cs | 3 +- .../Helpers/CommonParameterParserUtility.cs | 1 + .../NET45/Processors/Crop.cs | 2 +- .../NET45/Processors/Resize.cs | 2 +- .../NET45/Processors/Watermark.cs | 2 +- .../Common/Extensions/StringExtensions.cs | 80 ------------------ src/ImageProcessor/ImageProcessor.csproj | 1 - 11 files changed, 71 insertions(+), 173 deletions(-) delete mode 100644 src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs delete mode 100644 src/ImageProcessor/Common/Extensions/StringExtensions.cs diff --git a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs deleted file mode 100644 index ab43bd92b..000000000 --- a/src/ImageProcessor.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ /dev/null @@ -1,83 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James South. -// Licensed under the Apache License, Version 2.0. -// -// -// Provides a test harness for the string extensions -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace ImageProcessor.UnitTests.Extensions -{ - using System.Collections.Generic; - using ImageProcessor.Common.Extensions; - using NUnit.Framework; - - /// - /// Test harness for the string extensions - /// - [TestFixture] - public class StringExtensionsUnitTests - { - /// - /// Tests the passing to an integer array - /// - [Test] - public void TestToIntegerArray() - { - Dictionary data = new Dictionary - { - { - "123-456,78-90", - new[] { 123, 456, 78, 90 } - }, - { - "87390174,741897498,74816,748297,57355", - new[] - { - 87390174, 741897498, 74816, - 748297, 57355 - } - }, - { "1-2-3", new[] { 1, 2, 3 } } - }; - - foreach (KeyValuePair item in data) - { - int[] result = item.Key.ToPositiveIntegerArray(); - Assert.AreEqual(item.Value, result); - } - } - - /// - /// Tests the passing to an float array - /// - [Test] - public void TestToFloatArray() - { - Dictionary data = new Dictionary - { - { - "12.3-4.56,78-9.0", - new[] { 12.3F, 4.56F, 78, 9 } - }, - { - "87390.174,7.41897498,748.16,748297,5.7355", - new[] - { - 87390.174F, 7.41897498F, - 748.16F, 748297, 5.7355F - } - }, - { "1-2-3", new float[] { 1, 2, 3 } } - }; - - foreach (KeyValuePair item in data) - { - float[] result = item.Key.ToPositiveFloatArray(); - Assert.AreEqual(item.Value, result); - } - } - } -} \ No newline at end of file diff --git a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj index 5b6bc12d1..930267a63 100644 --- a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj +++ b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj @@ -48,7 +48,6 @@ - @@ -130,7 +129,5 @@ - - - + \ No newline at end of file diff --git a/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs b/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs index 57c261c7b..3e9053aed 100644 --- a/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs +++ b/src/ImageProcessor.Web.UnitTests/Extensions/StringExtensionsUnitTests.cs @@ -11,6 +11,8 @@ namespace ImageProcessor.Web.UnitTests.Extensions { using System; + using System.Collections.Generic; + using ImageProcessor.Web.Extensions; using NUnit.Framework; @@ -20,6 +22,66 @@ namespace ImageProcessor.Web.UnitTests.Extensions [TestFixture] public class StringExtensionsUnitTests { + /// + /// Tests the passing to an integer array + /// + [Test] + public void TestToIntegerArray() + { + Dictionary data = new Dictionary + { + { + "123-456,78-90", + new[] { 123, 456, 78, 90 } + }, + { + "87390174,741897498,74816,748297,57355", + new[] + { + 87390174, 741897498, 74816, + 748297, 57355 + } + }, + { "1-2-3", new[] { 1, 2, 3 } } + }; + + foreach (KeyValuePair item in data) + { + int[] result = item.Key.ToPositiveIntegerArray(); + Assert.AreEqual(item.Value, result); + } + } + + /// + /// Tests the passing to an float array + /// + [Test] + public void TestToFloatArray() + { + Dictionary data = new Dictionary + { + { + "12.3-4.56,78-9.0", + new[] { 12.3F, 4.56F, 78, 9 } + }, + { + "87390.174,7.41897498,748.16,748297,5.7355", + new[] + { + 87390.174F, 7.41897498F, + 748.16F, 748297, 5.7355F + } + }, + { "1-2-3", new float[] { 1, 2, 3 } } + }; + + foreach (KeyValuePair item in data) + { + float[] result = item.Key.ToPositiveFloatArray(); + Assert.AreEqual(item.Value, result); + } + } + /// /// Tests the MD5 fingerprint /// diff --git a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj index d830257ae..82d3986a7 100644 --- a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj +++ b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj @@ -98,6 +98,9 @@ DirectoryInfoExtensions.cs + + StringExtensions.cs + CommonParameterParserUtility.cs diff --git a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs b/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs index 6a23d48ad..be6b88744 100644 --- a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs +++ b/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs @@ -20,7 +20,6 @@ namespace ImageProcessor.Web.Caching using System.Web; using System.Web.Hosting; - using ImageProcessor.Common.Extensions; using ImageProcessor.Web.Configuration; using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; @@ -245,7 +244,7 @@ namespace ImageProcessor.Web.Caching fileInfo.Delete(); count -= 1; } - // ReSharper disable once EmptyGeneralCatchClause + // ReSharper disable once EmptyGeneralCatchClause catch { // Do nothing; skip to the next file. diff --git a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs index ae71ca7ef..505d80d99 100644 --- a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs +++ b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs @@ -17,6 +17,7 @@ namespace ImageProcessor.Web.Helpers using ImageProcessor.Common.Extensions; using ImageProcessor.Imaging; + using ImageProcessor.Web.Extensions; /// /// Encapsulates methods to correctly parse querystring parameters. diff --git a/src/ImageProcessor.Web/NET45/Processors/Crop.cs b/src/ImageProcessor.Web/NET45/Processors/Crop.cs index c621ebd1a..c669c8e1b 100644 --- a/src/ImageProcessor.Web/NET45/Processors/Crop.cs +++ b/src/ImageProcessor.Web/NET45/Processors/Crop.cs @@ -13,9 +13,9 @@ namespace ImageProcessor.Web.Processors using System.Text; using System.Text.RegularExpressions; - using ImageProcessor.Common.Extensions; using ImageProcessor.Imaging; using ImageProcessor.Processors; + using ImageProcessor.Web.Extensions; /// /// Crops an image to the given directions. diff --git a/src/ImageProcessor.Web/NET45/Processors/Resize.cs b/src/ImageProcessor.Web/NET45/Processors/Resize.cs index 95d962958..09ce9bc3c 100644 --- a/src/ImageProcessor.Web/NET45/Processors/Resize.cs +++ b/src/ImageProcessor.Web/NET45/Processors/Resize.cs @@ -17,9 +17,9 @@ namespace ImageProcessor.Web.Processors using System.Text; using System.Text.RegularExpressions; - using ImageProcessor.Common.Extensions; using ImageProcessor.Imaging; using ImageProcessor.Processors; + using ImageProcessor.Web.Extensions; /// /// Resizes an image to the given dimensions. diff --git a/src/ImageProcessor.Web/NET45/Processors/Watermark.cs b/src/ImageProcessor.Web/NET45/Processors/Watermark.cs index 27e83e288..d87f09388 100644 --- a/src/ImageProcessor.Web/NET45/Processors/Watermark.cs +++ b/src/ImageProcessor.Web/NET45/Processors/Watermark.cs @@ -15,9 +15,9 @@ namespace ImageProcessor.Web.Processors using System.Linq; using System.Text.RegularExpressions; - using ImageProcessor.Common.Extensions; using ImageProcessor.Imaging; using ImageProcessor.Processors; + using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; /// diff --git a/src/ImageProcessor/Common/Extensions/StringExtensions.cs b/src/ImageProcessor/Common/Extensions/StringExtensions.cs deleted file mode 100644 index f315df797..000000000 --- a/src/ImageProcessor/Common/Extensions/StringExtensions.cs +++ /dev/null @@ -1,80 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James South. -// Licensed under the Apache License, Version 2.0. -// -// -// Encapsulates a series of time saving extension methods to the class. -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace ImageProcessor.Common.Extensions -{ - using System; - using System.Globalization; - using System.Text.RegularExpressions; - - /// - /// Encapsulates a series of time saving extension methods to the class. - /// - public static class StringExtensions - { - /// - /// Creates an array of integers scraped from the String. - /// - /// The String instance that this method extends. - /// An array of integers scraped from the String. - public static int[] ToPositiveIntegerArray(this string expression) - { - if (string.IsNullOrWhiteSpace(expression)) - { - throw new ArgumentNullException("expression"); - } - - Regex regex = new Regex(@"[\d+]+(?=[,-])|[\d+]+(?![,-])", RegexOptions.Compiled); - - MatchCollection matchCollection = regex.Matches(expression); - - // Get the collections. - int count = matchCollection.Count; - int[] matches = new int[count]; - - // Loop and parse the int values. - for (int i = 0; i < count; i++) - { - matches[i] = int.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); - } - - return matches; - } - - /// - /// Creates an array of floats scraped from the String. - /// - /// The String instance that this method extends. - /// An array of floats scraped from the String. - public static float[] ToPositiveFloatArray(this string expression) - { - if (string.IsNullOrWhiteSpace(expression)) - { - throw new ArgumentNullException("expression"); - } - - Regex regex = new Regex(@"[\d+\.]+(?=[,-])|[\d+\.]+(?![,-])", RegexOptions.Compiled); - - MatchCollection matchCollection = regex.Matches(expression); - - // Get the collections. - int count = matchCollection.Count; - float[] matches = new float[count]; - - // Loop and parse the int values. - for (int i = 0; i < count; i++) - { - matches[i] = float.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); - } - - return matches; - } - } -} diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 274b3e76d..0e0d276bb 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -66,7 +66,6 @@ - From dd6c62c43afdc4b0c169ebdebcfff07ea2f69bee Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 7 Jul 2014 08:21:28 +0100 Subject: [PATCH 11/11] Reorganizing native binaries Former-commit-id: f372775134cb759ae27a29d8703e11e8336403f1 --- build/NuSpecs/ImageProcessor.nuspec | 4 +- .../ImageProcessor.Web/web.config.transform | 2 + .../NET4/ImageProcessor.Web_NET4.csproj | 3 + .../ImageProcessorConfiguration.cs | 29 +--- .../ImageProcessorNativeBinaryModule.cs | 159 ++++++++++++++++++ .../NET45/ImageProcessor.Web_NET45.csproj | 1 + src/ImageProcessor.sln.DotSettings | 1 + src/ImageProcessor/ImageProcessor.csproj | 5 +- .../Imaging/Formats/NativeMethods.cs | 127 ++++++++++++-- .../Imaging/Formats/WebPFormat.cs | 60 +------ ...imageprocessor.libwebp.dll.REMOVED.git-id} | 0 ...imageprocessor.libwebp.dll.REMOVED.git-id} | 0 src/ImageProcessorConsole/Program.cs | 4 +- .../images/output/120430.gif.REMOVED.git-id | 2 +- .../images/output/Tl4Yb.gif.REMOVED.git-id | 2 +- .../images/output/nLpfllv.gif.REMOVED.git-id | 2 +- .../images/output/rotate.jpg | 3 + .../NET45/Test_Website_NET45/Web.config | 4 +- 18 files changed, 308 insertions(+), 100 deletions(-) create mode 100644 src/ImageProcessor.Web/NET45/HttpModules/ImageProcessorNativeBinaryModule.cs rename src/ImageProcessor/{libwebp64.dll.REMOVED.git-id => x64/imageprocessor.libwebp.dll.REMOVED.git-id} (100%) rename src/ImageProcessor/{libwebp32.dll.REMOVED.git-id => x86/imageprocessor.libwebp.dll.REMOVED.git-id} (100%) create mode 100644 src/ImageProcessorConsole/images/output/rotate.jpg diff --git a/build/NuSpecs/ImageProcessor.nuspec b/build/NuSpecs/ImageProcessor.nuspec index 557d85150..f12daba8f 100644 --- a/build/NuSpecs/ImageProcessor.nuspec +++ b/build/NuSpecs/ImageProcessor.nuspec @@ -25,8 +25,8 @@ Feedback is always welcome. - - + + \ No newline at end of file diff --git a/build/content/ImageProcessor.Web/web.config.transform b/build/content/ImageProcessor.Web/web.config.transform index 7d02ee462..5c3edb967 100644 --- a/build/content/ImageProcessor.Web/web.config.transform +++ b/build/content/ImageProcessor.Web/web.config.transform @@ -3,6 +3,7 @@ + @@ -13,6 +14,7 @@ + \ No newline at end of file diff --git a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj index 82d3986a7..12c43700b 100644 --- a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj +++ b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj @@ -116,6 +116,9 @@ + + ImageProcessorNativeBinaryModule.cs + Alpha.cs diff --git a/src/ImageProcessor.Web/NET45/Configuration/ImageProcessorConfiguration.cs b/src/ImageProcessor.Web/NET45/Configuration/ImageProcessorConfiguration.cs index 9d3ea6be9..95c3123e2 100644 --- a/src/ImageProcessor.Web/NET45/Configuration/ImageProcessorConfiguration.cs +++ b/src/ImageProcessor.Web/NET45/Configuration/ImageProcessorConfiguration.cs @@ -23,6 +23,7 @@ namespace ImageProcessor.Web.Configuration using ImageProcessor.Common.Extensions; using ImageProcessor.Processors; using ImageProcessor.Web.Helpers; + using ImageProcessor.Web.HttpModules; using ImageProcessor.Web.Processors; /// @@ -370,30 +371,14 @@ namespace ImageProcessor.Web.Configuration /// private void EnsureNativeBinariesLoaded() { - string binary = Is64Bit ? "libwebp64.dll" : "libwebp32.dll"; - string sourcePath = HttpContext.Current.Server.MapPath("~/bin"); - string targetPath = new Uri(Assembly.GetExecutingAssembly().Location).LocalPath; - IntPtr pointer = IntPtr.Zero; + // Load the correct method from the native binary module. + // We do it here as on init will cause an UnauthorizedAccessException. + HttpModuleCollection modules = HttpContext.Current.ApplicationInstance.Modules; + ImageProcessorNativeBinaryModule nativeBinaryModule = modules.Get("ImageProcessorNativeBinaryModule") as ImageProcessorNativeBinaryModule; - // Shadow copy the native binaries. - sourcePath = Path.Combine(sourcePath, binary); - targetPath = Path.GetFullPath(Path.Combine(targetPath, "..\\" + binary)); - - File.Copy(sourcePath, targetPath, true); - - try - { - // Load the binary into memory. - pointer = NativeMethods.LoadLibrary(targetPath); - } - catch (Exception ex) - { - Debug.WriteLine(ex.Message); - } - - if (pointer == IntPtr.Zero) + if (nativeBinaryModule != null) { - throw new ApplicationException("Cannot open " + binary); + nativeBinaryModule.LoadNativeBinaries(); } } #endregion diff --git a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessorNativeBinaryModule.cs b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessorNativeBinaryModule.cs new file mode 100644 index 000000000..87120a77b --- /dev/null +++ b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessorNativeBinaryModule.cs @@ -0,0 +1,159 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// The image processing native binary module. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.HttpModules +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Reflection; + using System.Web; + + using ImageProcessor.Web.Helpers; + + /// + /// Controls the loading and unloading of any native binaries required by ImageProcessor.Web. + /// + public sealed class ImageProcessorNativeBinaryModule : IHttpModule + { + /// + /// Whether the process is running in 64bit mode. Used for calling the correct dllimport method. + /// + private static readonly bool Is64Bit = Environment.Is64BitProcess; + + /// + /// The object to lock against. + /// + private static readonly object SyncRoot = new object(); + + /// + /// The native binaries. + /// + private static readonly List NativeBinaries = new List(); + + /// + /// A value indicating whether this instance of the given entity has been disposed. + /// + /// if this instance has been disposed; otherwise, . + /// + /// If the entity is disposed, it must not be disposed a second + /// time. The isDisposed field is set the first time the entity + /// is disposed. If the isDisposed field is true, then the Dispose() + /// method will not dispose again. This help not to prolong the entity's + /// life in the Garbage Collector. + /// + private bool isDisposed; + + /// + /// Disposes of the resources (other than memory) used by the module that implements + /// . + /// + public void Dispose() + { + if (this.isDisposed) + { + return; + } + + // Call the appropriate methods to clean up + // unmanaged resources here. + lock (SyncRoot) + { + this.FreeNativeBinaries(); + } + + // Note disposing is done. + this.isDisposed = true; + } + + /// + /// Initializes a module and prepares it to handle requests. + /// + /// An that provides access to + /// the methods, properties, and events common to all application objects within an ASP.NET application + public void Init(HttpApplication context) + { + } + + /// + /// Loads any native ImageProcessor binaries. + /// + public void LoadNativeBinaries() + { + lock (SyncRoot) + { + this.RegisterNativeBinaries(); + } + } + + /// + /// Registers any native binaries. + /// + /// + /// Thrown when a native binary cannot be loaded. + /// + private void RegisterNativeBinaries() + { + if (NativeBinaries.Any()) + { + return; + } + + string folder = Is64Bit ? "x64" : "x86"; + string sourcePath = HttpContext.Current.Server.MapPath("~/bin/" + folder); + string targetBasePath = new Uri(Assembly.GetExecutingAssembly().Location).LocalPath; + + DirectoryInfo directoryInfo = new DirectoryInfo(sourcePath); + if (directoryInfo.Exists) + { + foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles("*.dll")) + { + if (fileInfo.Name.ToUpperInvariant().StartsWith("IMAGEPROCESSOR")) + { + IntPtr pointer; + string targetPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + fileInfo.Name)); + File.Copy(sourcePath, targetPath, true); + + try + { + // Load the binary into memory. + pointer = NativeMethods.LoadLibrary(targetPath); + } + catch (Exception ex) + { + throw new ApplicationException(ex.Message); + } + + if (pointer == IntPtr.Zero) + { + throw new ApplicationException("Cannot load " + fileInfo.Name); + } + + NativeBinaries.Add(pointer); + } + } + } + } + + /// + /// Frees the reference to the native binaries. + /// + private void FreeNativeBinaries() + { + foreach (IntPtr nativeBinary in NativeBinaries) + { + // According to http://stackoverflow.com/a/2445558/427899 you need to call this twice. + NativeMethods.FreeLibrary(nativeBinary); + NativeMethods.FreeLibrary(nativeBinary); + } + } + } +} diff --git a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj index 40bd16bc7..47bd96a17 100644 --- a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj +++ b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj @@ -62,6 +62,7 @@ + diff --git a/src/ImageProcessor.sln.DotSettings b/src/ImageProcessor.sln.DotSettings index 94cb9dbcb..4970d0c6c 100644 --- a/src/ImageProcessor.sln.DotSettings +++ b/src/ImageProcessor.sln.DotSettings @@ -1,4 +1,5 @@  + BGRA BPP DT FPX diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 0e0d276bb..73ac24ef7 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -130,13 +130,14 @@ - + PreserveNewest - + PreserveNewest +