Skip to content

Commit ba2de68

Browse files
authored
Merge pull request SixLabors#904 from SixLabors/af/non-generic-image-baseclass
Introduce a non-generic Image base class
2 parents 859fd80 + e20a3f2 commit ba2de68

File tree

10 files changed

+120
-48
lines changed

10 files changed

+120
-48
lines changed

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
1212
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
13+
1314
<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0</TargetFrameworks>
15+
1416
<LangVersion>7.3</LangVersion>
1517
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1618
<GenerateDocumentationFile>true</GenerateDocumentationFile>

tests/ImageSharp.Tests/BaseImageOperationsExtensionTest.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using SixLabors.ImageSharp.PixelFormats;
55
using SixLabors.ImageSharp.Processing;
6+
using SixLabors.ImageSharp.Processing.Processors;
67
using SixLabors.Primitives;
78
using Xunit;
89

@@ -33,7 +34,12 @@ public T Verify<T>(int index = 0)
3334

3435
FakeImageOperationsProvider.FakeImageOperations<Rgba32>.AppliedOperation operation = this.internalOperations.Applied[index];
3536

36-
return Assert.IsType<T>(operation.Processor);
37+
if (operation.NonGenericProcessor != null)
38+
{
39+
return Assert.IsType<T>(operation.NonGenericProcessor);
40+
}
41+
42+
return Assert.IsType<T>(operation.GenericProcessor);
3743
}
3844

3945
public T Verify<T>(Rectangle rect, int index = 0)
@@ -43,7 +49,13 @@ public T Verify<T>(Rectangle rect, int index = 0)
4349
FakeImageOperationsProvider.FakeImageOperations<Rgba32>.AppliedOperation operation = this.internalOperations.Applied[index];
4450

4551
Assert.Equal(rect, operation.Rectangle);
46-
return Assert.IsType<T>(operation.Processor);
52+
53+
if (operation.NonGenericProcessor != null)
54+
{
55+
return Assert.IsType<T>(operation.NonGenericProcessor);
56+
}
57+
58+
return Assert.IsType<T>(operation.GenericProcessor);
4759
}
4860
}
4961
}

tests/ImageSharp.Tests/Drawing/DrawImageTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public void ImageShouldDrawTransformedImage<TPixel>(TestImageProvider<TPixel> pr
7979
.AppendTranslation(new PointF(10, 10));
8080

8181
// Apply a background color so we can see the translation.
82-
blend.Mutate(x => x.Transform(builder).BackgroundColor(NamedColors<TPixel>.HotPink));
82+
blend.Mutate(x => x.Transform(builder));
83+
blend.Mutate(x => x.BackgroundColor(NamedColors<TPixel>.HotPink));
8384

8485
// Lets center the matrix so we can tell whether any cut-off issues we may have belong to the drawing processor
8586
var position = new Point((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2);

tests/ImageSharp.Tests/Drawing/RecolorImageTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ImageShouldRecolorYellowToHotPink()
1919

2020
foreach (TestFile file in Files)
2121
{
22-
using (Image<Rgba32> image = file.CreateImage())
22+
using (Image<Rgba32> image = file.CreateRgba32Image())
2323
{
2424
image.Mutate(x => x.Fill(brush));
2525
image.Save($"{path}/{file.FileName}");
@@ -36,7 +36,7 @@ public void ImageShouldRecolorYellowToHotPinkInARectangle()
3636

3737
foreach (TestFile file in Files)
3838
{
39-
using (Image<Rgba32> image = file.CreateImage())
39+
using (Image<Rgba32> image = file.CreateRgba32Image())
4040
{
4141
int imageHeight = image.Height;
4242
image.Mutate(x => x.Fill(brush, new Rectangle(0, imageHeight / 2 - imageHeight / 4, image.Width, imageHeight / 2)));

tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void ImageShouldBeOverlayedByFilledPolygonImage()
9696
new Vector2(50, 300)
9797
};
9898

99-
using (Image<Rgba32> brushImage = TestFile.Create(TestImages.Bmp.Car).CreateImage())
99+
using (Image<Rgba32> brushImage = TestFile.Create(TestImages.Bmp.Car).CreateRgba32Image())
100100
using (var image = new Image<Rgba32>(500, 500))
101101
{
102102
var brush = new ImageBrush<Rgba32>(brushImage);

tests/ImageSharp.Tests/FakeImageOperationsProvider.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,30 @@ public Size GetCurrentSize()
6767
return this.Source.Size();
6868
}
6969

70+
public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle)
71+
{
72+
this.Applied.Add(new AppliedOperation()
73+
{
74+
Rectangle = rectangle,
75+
NonGenericProcessor = processor
76+
});
77+
return this;
78+
}
79+
80+
public IImageProcessingContext ApplyProcessor(IImageProcessor processor)
81+
{
82+
this.Applied.Add(new AppliedOperation()
83+
{
84+
NonGenericProcessor = processor
85+
});
86+
return this;
87+
}
88+
7089
public IImageProcessingContext<TPixel> ApplyProcessor(IImageProcessor<TPixel> processor, Rectangle rectangle)
7190
{
7291
this.Applied.Add(new AppliedOperation
7392
{
74-
Processor = processor,
93+
GenericProcessor = processor,
7594
Rectangle = rectangle
7695
});
7796
return this;
@@ -81,15 +100,17 @@ public IImageProcessingContext<TPixel> ApplyProcessor(IImageProcessor<TPixel> pr
81100
{
82101
this.Applied.Add(new AppliedOperation
83102
{
84-
Processor = processor
103+
GenericProcessor = processor
85104
});
86105
return this;
87106
}
88107

89108
public struct AppliedOperation
90109
{
91110
public Rectangle? Rectangle { get; set; }
92-
public IImageProcessor<TPixel> Processor { get; set; }
111+
public IImageProcessor<TPixel> GenericProcessor { get; set; }
112+
113+
public IImageProcessor NonGenericProcessor { get; set; }
93114
}
94115
}
95116
}

tests/ImageSharp.Tests/ImageOperationTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void MutateCallsImageOperationsProvider_Func_OriginalImage()
4141
this.image.Mutate(x => x.ApplyProcessor(this.processor));
4242

4343
Assert.True(this.provider.HasCreated(this.image));
44-
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor));
44+
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
4545
}
4646

4747
[Fact]
@@ -50,7 +50,7 @@ public void MutateCallsImageOperationsProvider_ListOfProcessors_OriginalImage()
5050
this.image.Mutate(this.processor);
5151

5252
Assert.True(this.provider.HasCreated(this.image));
53-
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor));
53+
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
5454
}
5555

5656
[Fact]
@@ -59,7 +59,7 @@ public void CloneCallsImageOperationsProvider_Func_WithDuplicateImage()
5959
Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processor));
6060

6161
Assert.True(this.provider.HasCreated(returned));
62-
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.Processor));
62+
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor));
6363
}
6464

6565
[Fact]
@@ -68,31 +68,31 @@ public void CloneCallsImageOperationsProvider_ListOfProcessors_WithDuplicateImag
6868
Image<Rgba32> returned = this.image.Clone(this.processor);
6969

7070
Assert.True(this.provider.HasCreated(returned));
71-
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.Processor));
71+
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor));
7272
}
7373

7474
[Fact]
7575
public void CloneCallsImageOperationsProvider_Func_NotOnOrigional()
7676
{
7777
Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processor));
7878
Assert.False(this.provider.HasCreated(this.image));
79-
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor));
79+
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
8080
}
8181

8282
[Fact]
8383
public void CloneCallsImageOperationsProvider_ListOfProcessors_NotOnOrigional()
8484
{
8585
Image<Rgba32> returned = this.image.Clone(this.processor);
8686
Assert.False(this.provider.HasCreated(this.image));
87-
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor));
87+
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
8888
}
8989

9090
[Fact]
9191
public void ApplyProcessors_ListOfProcessors_AppliesAllProcessorsToOperation()
9292
{
9393
var operations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(null, false);
9494
operations.ApplyProcessors(this.processor);
95-
Assert.Contains(this.processor, operations.Applied.Select(x => x.Processor));
95+
Assert.Contains(this.processor, operations.Applied.Select(x => x.GenericProcessor));
9696
}
9797

9898
public void Dispose() => this.image.Dispose();

tests/ImageSharp.Tests/TestFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public string GetFileNameWithoutExtension(object value)
134134
/// <returns>
135135
/// The <see cref="ImageSharp.Image"/>.
136136
/// </returns>
137-
public Image<Rgba32> CreateImage()
137+
public Image<Rgba32> CreateRgba32Image()
138138
{
139139
return this.Image.Clone();
140140
}
@@ -145,9 +145,9 @@ public Image<Rgba32> CreateImage()
145145
/// <returns>
146146
/// The <see cref="ImageSharp.Image"/>.
147147
/// </returns>
148-
public Image<Rgba32> CreateImage(IImageDecoder decoder)
148+
public Image<Rgba32> CreateRgba32Image(IImageDecoder decoder)
149149
{
150-
return ImageSharp.Image.Load(this.Image.GetConfiguration(), this.Bytes, decoder);
150+
return ImageSharp.Image.Load<Rgba32>(this.Image.GetConfiguration(), this.Bytes, decoder);
151151
}
152152
}
153153
}

tests/ImageSharp.Tests/TestFileSystem.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,24 @@ namespace SixLabors.ImageSharp.Tests
1212
/// </summary>
1313
public class TestFileSystem : ImageSharp.IO.IFileSystem
1414
{
15-
16-
public static TestFileSystem Global { get; } = new TestFileSystem();
17-
18-
public static void RegisterGlobalTestFormat()
19-
{
20-
Configuration.Default.FileSystem = Global;
21-
}
22-
23-
Dictionary<string, Stream> fileSystem = new Dictionary<string, Stream>(StringComparer.OrdinalIgnoreCase);
15+
private readonly Dictionary<string, Stream> fileSystem = new Dictionary<string, Stream>(StringComparer.OrdinalIgnoreCase);
2416

2517
public void AddFile(string path, Stream data)
2618
{
27-
fileSystem.Add(path, data);
19+
lock (this.fileSystem)
20+
{
21+
this.fileSystem.Add(path, data);
22+
}
2823
}
2924

3025
public Stream Create(string path)
3126
{
3227
// if we have injected a fake file use it instead
33-
lock (fileSystem)
28+
lock (this.fileSystem)
3429
{
35-
if (fileSystem.ContainsKey(path))
30+
if (this.fileSystem.ContainsKey(path))
3631
{
37-
Stream stream = fileSystem[path];
32+
Stream stream = this.fileSystem[path];
3833
stream.Position = 0;
3934
return stream;
4035
}
@@ -43,15 +38,14 @@ public Stream Create(string path)
4338
return File.Create(path);
4439
}
4540

46-
4741
public Stream OpenRead(string path)
4842
{
4943
// if we have injected a fake file use it instead
50-
lock (fileSystem)
44+
lock (this.fileSystem)
5145
{
52-
if (fileSystem.ContainsKey(path))
46+
if (this.fileSystem.ContainsKey(path))
5347
{
54-
Stream stream = fileSystem[path];
48+
Stream stream = this.fileSystem[path];
5549
stream.Position = 0;
5650
return stream;
5751
}
@@ -60,5 +54,4 @@ public Stream OpenRead(string path)
6054
return File.OpenRead(path);
6155
}
6256
}
63-
}
64-
57+
}

0 commit comments

Comments
 (0)