-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOpenSlideImageExtensions.cs
More file actions
226 lines (209 loc) · 9.6 KB
/
OpenSlideImageExtensions.cs
File metadata and controls
226 lines (209 loc) · 9.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using OpenCvSharp;
using System;
using System.IO;
namespace OpenSlideSharp.OpencvExtensions
{
/// <summary>
///
/// </summary>
public static class OpenSlideImageExtensions
{
/// <summary>
/// Read region mat.
/// </summary>
/// <param name="image"></param>
/// <param name="level"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public unsafe static Mat ReadRegionImage(this OpenSlideImage image, int level, long x, long y, long width, long height)
{
if (image == null) throw new NullReferenceException();
var data = image.ReadRegion(level, x, y, width, height);
fixed (byte* scan0 = data)
{
return new Mat((int)height, (int)width, MatType.CV_8UC4, (IntPtr)scan0, (int)width * 4);
}
}
/// <summary>
/// Read region jpeg stream.
/// </summary>
/// <param name="image"></param>
/// <param name="level"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="quality">For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default
/// value is 95.</param>
/// <returns></returns>
public unsafe static MemoryStream ReadRegionJpegStream(this OpenSlideImage image, int level, long x, long y, long width, long height, int? quality = null)
{
return new MemoryStream(ReadRegionJpeg(image, level, x, y, width, height, quality));
}
/// <summary>
/// Read region jpeg
/// </summary>
/// <param name="image"></param>
/// <param name="level"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="quality">For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default
/// value is 95.</param>
/// <returns></returns>
public unsafe static byte[] ReadRegionJpeg(this OpenSlideImage image, int level, long x, long y, long width, long height, int? quality = null)
{
var prms = quality != null ? new int[] { (int)ImwriteFlags.JpegQuality, quality.Value } : null;
return ReadRegionImage(image, level, x, y, width, height).ToBytes(".jpg", prms);
}
/// <summary>
/// Read region png stream.
/// </summary>
/// <param name="image"></param>
/// <param name="level"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="quality">
/// For PNG, it can be the compression level from 0 to 9. A higher value means a
/// smaller size and longer compression time. Default value is 3.
/// </param>
/// <returns></returns>
public unsafe static MemoryStream ReadRegionPngStream(this OpenSlideImage image, int level, long x, long y, long width, long height, int? quality = null)
{
return new MemoryStream(ReadRegionPng(image, level, x, y, width, height, quality));
}
/// <summary>
/// Read region png.
/// </summary>
/// <param name="image"></param>
/// <param name="level"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="quality">
/// For PNG, it can be the compression level from 0 to 9. A higher value means a
/// smaller size and longer compression time. Default value is 3.</param>
/// <returns></returns>
public unsafe static byte[] ReadRegionPng(this OpenSlideImage image, int level, long x, long y, long width, long height, int? quality = null)
{
var prms = quality != null ? new int[] { (int)ImwriteFlags.PngCompression, quality.Value } : null;
return ReadRegionImage(image, level, x, y, width, height).ToBytes(".png", prms);
}
/// <summary>
/// Generate thumbnail image.
/// </summary>
/// <param name="image"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <returns></returns>
public static Mat GenerateThumbnailImage(this OpenSlideImage image, int maxWidth, int maxHeight)
{
if (image == null) throw new NullReferenceException();
(long width, long height) = image.Dimensions;
// Find the appropriate level
double downsampleWidth = width / (double)maxWidth;
double downsampleHeight = height / (double)maxHeight;
double downsample = Math.Max(downsampleWidth, downsampleHeight);
int level = image.GetBestLevelForDownsample(downsample);
(long levelWidth, long levelHeight) = image.GetLevelDimension(level);
// Calculate target size
int targetWidth, targetHeight;
if (downsampleWidth > downsampleHeight)
{
targetWidth = maxWidth;
targetHeight = (int)(height / downsampleWidth);
}
else
{
targetWidth = (int)(width / downsampleHeight);
targetHeight = maxHeight;
}
var bitmap = ReadRegionImage(image, level, 0, 0, levelWidth, levelHeight);
bitmap.Resize(targetWidth, targetHeight);
return bitmap;
}
/// <summary>
/// Generate thumbnail jpeg stream.
/// </summary>
/// <param name="image"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <param name="quality">For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default
/// value is 95.</param>
/// <returns></returns>
public static MemoryStream GenerateThumbnailJpegStream(this OpenSlideImage image, int maxWidth, int maxHeight, out int targetWidth, out int targetHeight, int? quality = null)
{
return new MemoryStream(GenerateThumbnailJpeg(image, maxWidth, maxHeight, out targetWidth, out targetHeight, quality));
}
/// <summary>
/// Generate thumbnail jpeg.
/// </summary>
/// <param name="image"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <param name="quality">For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default
/// value is 95.</param>
/// <returns></returns>
public static byte[] GenerateThumbnailJpeg(this OpenSlideImage image, int maxWidth, int maxHeight, out int targetWidth, out int targetHeight, int? quality = null)
{
using (var bitmap = GenerateThumbnailImage(image, maxWidth, maxHeight))
{
targetWidth = bitmap.Width;
targetHeight = bitmap.Height;
var prms = quality != null ? new int[] { (int)ImwriteFlags.JpegQuality, quality.Value } : null;
return bitmap.ToBytes(".jpg", prms);
}
}
/// <summary>
/// Generate thumbail png stream.
/// </summary>
/// <param name="image"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <param name="quality">
/// For PNG, it can be the compression level from 0 to 9. A higher value means a
/// smaller size and longer compression time. Default value is 3.
/// </param>
/// <returns></returns>
public static MemoryStream GenerateThumbnailPngStream(this OpenSlideImage image, int maxWidth, int maxHeight, out int targetWidth, out int targetHeight, int? quality = null)
{
return new MemoryStream(GenerateThumbnailPng(image, maxWidth, maxHeight, out targetWidth, out targetHeight, quality));
}
/// <summary>
/// Generate thumbnail png.
/// </summary>
/// <param name="image"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <param name="quality">
/// For PNG, it can be the compression level from 0 to 9. A higher value means a
/// smaller size and longer compression time. Default value is 3.
/// </param>
/// <returns></returns>
public static byte[] GenerateThumbnailPng(this OpenSlideImage image, int maxWidth, int maxHeight, out int targetWidth, out int targetHeight, int? quality = null)
{
using (var bitmap = GenerateThumbnailImage(image, maxWidth, maxHeight))
{
targetWidth = bitmap.Width;
targetHeight = bitmap.Height;
var prms = quality != null ? new int[] { (int)ImwriteFlags.PngCompression, quality.Value } : null;
return bitmap.ToBytes(".png", prms);
}
}
}
}