-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
ConverterSnippets.cs
91 lines (73 loc) · 2.51 KB
/
ConverterSnippets.cs
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
#pragma warning disable CA1416
#if DEBUG && NET8_0
// ReSharper disable UnusedParameter.Local
public class ConverterSnippets
{
[ModuleInitializer]
public static void RegisterFileConverterType() =>
#region RegisterFileConverterType
VerifierSettings.RegisterFileConverter<Image>(
#region ConverterCanConvert
canConvert: (target, context) => Equals(target.RawFormat, ImageFormat.Tiff),
#endregion
conversion: (image, settings) =>
{
var pages = image.GetFrameCount(FrameDimension.Page);
var targets = new List<Target>();
for (var index = 0; index < pages; index++)
{
image.SelectActiveFrame(FrameDimension.Page, index);
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
targets.Add(new("png", page));
}
return new(
info: new
{
image.PixelFormat,
image.Size
},
targets);
});
#endregion
[Fact]
public async Task Type()
{
#region FileConverterTypeVerify
using var stream = File.OpenRead("sample.tif");
await Verify(Image.FromStream(stream));
#endregion
}
[ModuleInitializer]
public static void ExtensionInit() =>
#region RegisterFileConverterExtension
VerifierSettings.RegisterFileConverter(
fromExtension: "tif",
conversion: (stream, settings) =>
{
using var image = Image.FromStream(stream);
var pages = image.GetFrameCount(FrameDimension.Page);
var targets = new List<Target>();
for (var index = 0; index < pages; index++)
{
image.SelectActiveFrame(FrameDimension.Page, index);
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
targets.Add(new("png", page));
}
return new(
info: new
{
image.PixelFormat,
image.Size
},
targets);
});
#endregion
[Fact]
public async Task Extension() =>
#region FileConverterExtensionVerify
await VerifyFile("sample.tif");
#endregion
}
#endif