-
-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathGeneralFormatTests.cs
More file actions
209 lines (183 loc) · 7.14 KB
/
Copy pathGeneralFormatTests.cs
File metadata and controls
209 lines (183 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace SixLabors.ImageSharp.Tests
{
using System;
public class GeneralFormatTests : FileTestBase
{
[Theory]
[WithFileCollection(nameof(DefaultFiles), DefaultPixelType)]
public void ResolutionShouldChange<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.MetaData.VerticalResolution = 150;
image.MetaData.HorizontalResolution = 150;
image.DebugSave(provider);
}
}
[Fact]
public void ImageCanEncodeToString()
{
string path = TestEnvironment.CreateOutputDirectory("ToString");
foreach (TestFile file in Files)
{
using (Image<Rgba32> image = file.CreateImage())
{
string filename = path + "/" + file.FileNameWithoutExtension + ".txt";
File.WriteAllText(filename, image.ToBase64String(ImageFormats.Png));
}
}
}
[Fact]
public void DecodeThenEncodeImageFromStreamShouldSucceed()
{
string path = TestEnvironment.CreateOutputDirectory("Encode");
foreach (TestFile file in Files)
{
using (Image<Rgba32> image = file.CreateImage())
{
image.Save($"{path}/{file.FileName}");
}
}
}
[Fact]
public void QuantizeImageShouldPreserveMaximumColorPrecision()
{
string path = TestEnvironment.CreateOutputDirectory("Quantize");
foreach (TestFile file in Files)
{
using (Image<Rgba32> srcImage = Image.Load<Rgba32>(file.Bytes, out var mimeType))
{
using (Image<Rgba32> image = srcImage.Clone())
{
using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}"))
{
image.Mutate(x => x.Quantize(Quantization.Octree));
image.Save(output, mimeType);
}
}
using (Image<Rgba32> image = srcImage.Clone())
{
using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}"))
{
image.Mutate(x => x.Quantize(Quantization.Wu));
image.Save(output, mimeType);
}
}
using (Image<Rgba32> image = srcImage.Clone())
{
using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}"))
{
image.Mutate(x => x.Quantize(Quantization.Palette));
image.Save(output, mimeType);
}
}
}
}
}
[Fact]
public void ImageCanConvertFormat()
{
string path = TestEnvironment.CreateOutputDirectory("Format");
foreach (TestFile file in Files)
{
using (Image<Rgba32> image = file.CreateImage())
{
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp"))
{
image.SaveAsBmp(output);
}
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg"))
{
image.SaveAsJpeg(output);
}
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png"))
{
image.SaveAsPng(output);
}
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif"))
{
image.SaveAsGif(output);
}
}
}
}
[Fact]
public void ImageShouldPreservePixelByteOrderWhenSerialized()
{
string path = TestEnvironment.CreateOutputDirectory("Serialized");
foreach (TestFile file in Files)
{
byte[] serialized;
using (Image<Rgba32> image = Image.Load(file.Bytes, out IImageFormat mimeType))
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, mimeType);
memoryStream.Flush();
serialized = memoryStream.ToArray();
}
using (Image<Rgba32> image2 = Image.Load<Rgba32>(serialized))
{
image2.Save($"{path}/{file.FileName}");
}
}
}
[Theory]
[InlineData(10, 10, "png")]
[InlineData(100, 100, "png")]
[InlineData(100, 10, "png")]
[InlineData(10, 100, "png")]
[InlineData(10, 10, "gif")]
[InlineData(100, 100, "gif")]
[InlineData(100, 10, "gif")]
[InlineData(10, 100, "gif")]
[InlineData(10, 10, "bmp")]
[InlineData(100, 100, "bmp")]
[InlineData(100, 10, "bmp")]
[InlineData(10, 100, "bmp")]
[InlineData(10, 10, "jpg")]
[InlineData(100, 100, "jpg")]
[InlineData(100, 10, "jpg")]
[InlineData(10, 100, "jpg")]
public void CanIdentifyImageLoadedFromBytes(int width, int height, string format)
{
using (Image<Rgba32> image = Image.LoadPixelData(new Rgba32[width * height], width, height))
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetEncoder(format));
memoryStream.Position = 0;
var imageInfo = Image.Identify(memoryStream);
Assert.Equal(imageInfo.Width, width);
Assert.Equal(imageInfo.Height, height);
}
}
}
private static IImageEncoder GetEncoder(string format)
{
switch (format)
{
case "png":
return new PngEncoder();
case "gif":
return new GifEncoder();
case "bmp":
return new BmpEncoder();
case "jpg":
return new JpegEncoder();
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}
}
}