From bf8c17cb83a59dd1bd38b85def966c2ca9d1b3da Mon Sep 17 00:00:00 2001 From: Johannes Bildstein Date: Thu, 24 Aug 2017 16:19:57 +0200 Subject: [PATCH 1/4] handle CultureNotFoundException when reading MultilocalizedUnicodeTagDataEntry --- .../DataReader/IccDataReader.TagDataEntry.cs | 44 +++++++---- .../TestDataIcc/IccTestDataTagDataEntry.cs | 73 +++++++++++++++---- 2 files changed, 88 insertions(+), 29 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs index 711de818b..0fe03284f 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs @@ -476,19 +476,7 @@ namespace ImageSharp string languageCode = this.ReadAsciiString(2); string countryCode = this.ReadAsciiString(2); - if (string.IsNullOrWhiteSpace(languageCode)) - { - culture[i] = CultureInfo.InvariantCulture; - } - else if (string.IsNullOrWhiteSpace(countryCode)) - { - culture[i] = new CultureInfo(languageCode); - } - else - { - culture[i] = new CultureInfo($"{languageCode}-{countryCode}"); - } - + culture[i] = ReadCulture(languageCode, countryCode); length[i] = this.ReadUInt32(); offset[i] = this.ReadUInt32(); } @@ -500,6 +488,36 @@ namespace ImageSharp } return new IccMultiLocalizedUnicodeTagDataEntry(text); + + CultureInfo ReadCulture(string language, string country) + { + if (string.IsNullOrWhiteSpace(language)) + { + return CultureInfo.InvariantCulture; + } + else if (string.IsNullOrWhiteSpace(country)) + { + try + { + return new CultureInfo(language); + } + catch (CultureNotFoundException) + { + return CultureInfo.InvariantCulture; + } + } + else + { + try + { + return new CultureInfo($"{language}-{country}"); + } + catch (CultureNotFoundException) + { + return ReadCulture(language, null); + } + } + } } /// diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs index 05942ab61..377cf24e7 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs @@ -455,15 +455,46 @@ namespace ImageSharp.Tests #region MultiLocalizedUnicodeTagDataEntry - private static readonly IccLocalizedString LocalizedString_Rand_enUs = new IccLocalizedString(new CultureInfo("en-US"), IccTestDataPrimitives.Unicode_ValRand2); - private static readonly IccLocalizedString LocalizedString_Rand_deDE = new IccLocalizedString(new CultureInfo("de-DE"), IccTestDataPrimitives.Unicode_ValRand3); - private static readonly IccLocalizedString LocalizedString_Rand2_deDE = new IccLocalizedString(new CultureInfo("de-DE"), IccTestDataPrimitives.Unicode_ValRand2); - private static readonly IccLocalizedString LocalizedString_Rand_en = new IccLocalizedString(new CultureInfo("en"), IccTestDataPrimitives.Unicode_ValRand2); + private static readonly IccLocalizedString LocalizedString_Rand_enUS = CreateLocalizedString("en", "US", IccTestDataPrimitives.Unicode_ValRand2); + private static readonly IccLocalizedString LocalizedString_Rand_deDE = CreateLocalizedString("de", "DE", IccTestDataPrimitives.Unicode_ValRand3); + private static readonly IccLocalizedString LocalizedString_Rand2_deDE = CreateLocalizedString("de", "DE", IccTestDataPrimitives.Unicode_ValRand2); + private static readonly IccLocalizedString LocalizedString_Rand2_esXL = CreateLocalizedString("es", "XL", IccTestDataPrimitives.Unicode_ValRand2); + private static readonly IccLocalizedString LocalizedString_Rand2_xyXL = CreateLocalizedString("xy", "XL", IccTestDataPrimitives.Unicode_ValRand2); + private static readonly IccLocalizedString LocalizedString_Rand_en = CreateLocalizedString("en", null, IccTestDataPrimitives.Unicode_ValRand2); private static readonly IccLocalizedString LocalizedString_Rand_Invariant = new IccLocalizedString(CultureInfo.InvariantCulture, IccTestDataPrimitives.Unicode_ValRand3); - private static readonly IccLocalizedString[] LocalizedString_RandArr_enUs_deDE = new IccLocalizedString[] + private static IccLocalizedString CreateLocalizedString(string language, string country, string text) { - LocalizedString_Rand_enUs, + CultureInfo culture; + if (country == null) + { + try + { + culture = new CultureInfo(language); + } + catch (CultureNotFoundException) + { + culture = CultureInfo.InvariantCulture; + } + } + else + { + try + { + culture = new CultureInfo($"{language}-{country}"); + } + catch (CultureNotFoundException) + { + return CreateLocalizedString(language, null, text); + } + } + + return new IccLocalizedString(culture, text); + } + + private static readonly IccLocalizedString[] LocalizedString_RandArr_enUS_deDE = new IccLocalizedString[] + { + LocalizedString_Rand_enUS, LocalizedString_Rand_deDE, }; private static readonly IccLocalizedString[] LocalizedString_RandArr_en_Invariant = new IccLocalizedString[] @@ -471,13 +502,15 @@ namespace ImageSharp.Tests LocalizedString_Rand_en, LocalizedString_Rand_Invariant, }; - private static readonly IccLocalizedString[] LocalizedString_SameArr_enUs_deDE = new IccLocalizedString[] + private static readonly IccLocalizedString[] LocalizedString_SameArr_enUS_deDE_esXL_xyXL = new IccLocalizedString[] { - LocalizedString_Rand_enUs, - LocalizedString_Rand2_deDE + LocalizedString_Rand_enUS, + LocalizedString_Rand2_deDE, + LocalizedString_Rand2_esXL, + LocalizedString_Rand2_xyXL, }; - public static readonly IccMultiLocalizedUnicodeTagDataEntry MultiLocalizedUnicode_Val = new IccMultiLocalizedUnicodeTagDataEntry(LocalizedString_RandArr_enUs_deDE); + public static readonly IccMultiLocalizedUnicodeTagDataEntry MultiLocalizedUnicode_Val = new IccMultiLocalizedUnicodeTagDataEntry(LocalizedString_RandArr_enUS_deDE); public static readonly byte[] MultiLocalizedUnicode_Arr = ArrayHelper.Concat ( IccTestDataPrimitives.UInt32_2, @@ -530,19 +563,27 @@ namespace ImageSharp.Tests IccTestDataPrimitives.Unicode_Rand3 ); - public static readonly IccMultiLocalizedUnicodeTagDataEntry MultiLocalizedUnicode_Val3 = new IccMultiLocalizedUnicodeTagDataEntry(LocalizedString_SameArr_enUs_deDE); + public static readonly IccMultiLocalizedUnicodeTagDataEntry MultiLocalizedUnicode_Val3 = new IccMultiLocalizedUnicodeTagDataEntry(LocalizedString_SameArr_enUS_deDE_esXL_xyXL); public static readonly byte[] MultiLocalizedUnicode_Arr3 = ArrayHelper.Concat ( - IccTestDataPrimitives.UInt32_2, + IccTestDataPrimitives.UInt32_4, new byte[] { 0x00, 0x00, 0x00, 0x0C }, // 12 new byte[] { (byte)'e', (byte)'n', (byte)'U', (byte)'S' }, new byte[] { 0x00, 0x00, 0x00, 0x0C }, // 12 - new byte[] { 0x00, 0x00, 0x00, 0x28 }, // 40 + new byte[] { 0x00, 0x00, 0x00, 0x40 }, // 64 new byte[] { (byte)'d', (byte)'e', (byte)'D', (byte)'E' }, new byte[] { 0x00, 0x00, 0x00, 0x0C }, // 12 - new byte[] { 0x00, 0x00, 0x00, 0x28 }, // 40 + new byte[] { 0x00, 0x00, 0x00, 0x40 }, // 64 + + new byte[] { (byte)'e', (byte)'s', (byte)'X', (byte)'L' }, + new byte[] { 0x00, 0x00, 0x00, 0x0C }, // 12 + new byte[] { 0x00, 0x00, 0x00, 0x40 }, // 64 + + new byte[] { (byte)'x', (byte)'y', (byte)'X', (byte)'L' }, + new byte[] { 0x00, 0x00, 0x00, 0x0C }, // 12 + new byte[] { 0x00, 0x00, 0x00, 0x40 }, // 64 IccTestDataPrimitives.Unicode_Rand2 ); @@ -670,8 +711,8 @@ namespace ImageSharp.Tests ( new IccProfileSequenceIdentifier[] { - new IccProfileSequenceIdentifier(IccTestDataNonPrimitives.ProfileId_ValRand, LocalizedString_RandArr_enUs_deDE), - new IccProfileSequenceIdentifier(IccTestDataNonPrimitives.ProfileId_ValRand, LocalizedString_RandArr_enUs_deDE), + new IccProfileSequenceIdentifier(IccTestDataNonPrimitives.ProfileId_ValRand, LocalizedString_RandArr_enUS_deDE), + new IccProfileSequenceIdentifier(IccTestDataNonPrimitives.ProfileId_ValRand, LocalizedString_RandArr_enUS_deDE), } ); public static readonly byte[] ProfileSequenceIdentifier_Arr = ArrayHelper.Concat From 7810b388264fdb55cb3e4b7e2567ff3b9447184f Mon Sep 17 00:00:00 2001 From: JimBobSquarePants Date: Fri, 25 Aug 2017 13:51:23 +1000 Subject: [PATCH 2/4] Update all dependencies --- src/ImageSharp.Drawing/ImageSharp.Drawing.csproj | 4 ++-- src/ImageSharp/ImageSharp.csproj | 12 ++++++------ .../General/PixelConversion_ConvertFromRgba32.cs | 2 +- .../ImageSharp.Benchmarks.csproj | 5 +++-- .../ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj | 4 ++-- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 8 ++++---- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index 10562b08c..ddf18e428 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -36,8 +36,8 @@ - - + + All diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 8733131a7..44734ddec 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -35,16 +35,16 @@ - + All - - + + - - + + - + ..\..\ImageSharp.ruleset diff --git a/tests/ImageSharp.Benchmarks/General/PixelConversion_ConvertFromRgba32.cs b/tests/ImageSharp.Benchmarks/General/PixelConversion_ConvertFromRgba32.cs index e096fd828..0f025c9a4 100644 --- a/tests/ImageSharp.Benchmarks/General/PixelConversion_ConvertFromRgba32.cs +++ b/tests/ImageSharp.Benchmarks/General/PixelConversion_ConvertFromRgba32.cs @@ -142,7 +142,7 @@ namespace ImageSharp.Benchmarks.General [Params(32)] public int Count { get; set; } - [Setup] + [GlobalSetup] public void Setup() { this.compatibleMemLayoutRunner = new ConversionRunner(this.Count); diff --git a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj index 72593a0da..ae0fff103 100644 --- a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj +++ b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj @@ -9,14 +9,15 @@ false + - + - + diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index b9124afc6..c62f0519a 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -17,8 +17,8 @@ - - + + diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index b0429d9ed..a070dbdb7 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -10,10 +10,10 @@ - - - - + + + + From 1594bdbae00ae6fa4248f88cfd28b5456d701036 Mon Sep 17 00:00:00 2001 From: JimBobSquarePants Date: Fri, 25 Aug 2017 13:54:38 +1000 Subject: [PATCH 3/4] Fix png decode span bug on Core 2.0 --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index d9df44a09..633d151b6 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -272,7 +272,7 @@ namespace ImageSharp.Formats /// is less than or equals than zero. private static Span ToArrayByBitsLength(Span source, int bytesPerScanline, int bits) { - Guard.NotNull(source, nameof(source)); + Guard.MustBeGreaterThan(source.Length, 0, nameof(source)); Guard.MustBeGreaterThan(bits, 0, nameof(bits)); if (bits >= 8) From 761996d3a9cacb4cf2d1f4f2185e14871b54226c Mon Sep 17 00:00:00 2001 From: JimBobSquarePants Date: Fri, 25 Aug 2017 14:09:26 +1000 Subject: [PATCH 4/4] Fix BMDN dependency issue --- tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj index ae0fff103..f488e0546 100644 --- a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj +++ b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj @@ -9,12 +9,11 @@ false - - +