@@ -17,7 +17,7 @@ public abstract partial class Image
17
17
{
18
18
/// <summary>
19
19
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
20
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
20
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
21
21
/// </summary>
22
22
/// <typeparam name="TPixel">The pixel type</typeparam>
23
23
/// <param name="configuration">The <see cref="Configuration"/></param>
@@ -38,14 +38,15 @@ public static Image<TPixel> WrapMemory<TPixel>(
38
38
{
39
39
Guard . NotNull ( configuration , nameof ( configuration ) ) ;
40
40
Guard . NotNull ( metadata , nameof ( metadata ) ) ;
41
+ Guard . IsTrue ( pixelMemory . Length == width * height , nameof ( pixelMemory ) , "The length of the input memory doesn't match the specified image size" ) ;
41
42
42
43
var memorySource = MemoryGroup < TPixel > . Wrap ( pixelMemory ) ;
43
44
return new Image < TPixel > ( configuration , memorySource , width , height , metadata ) ;
44
45
}
45
46
46
47
/// <summary>
47
48
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
48
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
49
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
49
50
/// </summary>
50
51
/// <typeparam name="TPixel">The pixel type</typeparam>
51
52
/// <param name="configuration">The <see cref="Configuration"/></param>
@@ -64,7 +65,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
64
65
65
66
/// <summary>
66
67
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
67
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
68
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
68
69
/// The memory is being observed, the caller remains responsible for managing it's lifecycle.
69
70
/// </summary>
70
71
/// <typeparam name="TPixel">The pixel type.</typeparam>
@@ -81,7 +82,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
81
82
82
83
/// <summary>
83
84
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
84
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
85
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
85
86
/// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
86
87
/// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
87
88
/// It will be disposed together with the result image.
@@ -105,14 +106,15 @@ public static Image<TPixel> WrapMemory<TPixel>(
105
106
{
106
107
Guard . NotNull ( configuration , nameof ( configuration ) ) ;
107
108
Guard . NotNull ( metadata , nameof ( metadata ) ) ;
109
+ Guard . IsTrue ( pixelMemoryOwner . Memory . Length == width * height , nameof ( pixelMemoryOwner ) , "The length of the input memory doesn't match the specified image size" ) ;
108
110
109
111
var memorySource = MemoryGroup < TPixel > . Wrap ( pixelMemoryOwner ) ;
110
112
return new Image < TPixel > ( configuration , memorySource , width , height , metadata ) ;
111
113
}
112
114
113
115
/// <summary>
114
116
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
115
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
117
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
116
118
/// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
117
119
/// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
118
120
/// It will be disposed together with the result image.
@@ -134,7 +136,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
134
136
135
137
/// <summary>
136
138
/// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
137
- /// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
139
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
138
140
/// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
139
141
/// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
140
142
/// It will be disposed together with the result image.
@@ -150,5 +152,73 @@ public static Image<TPixel> WrapMemory<TPixel>(
150
152
int height )
151
153
where TPixel : unmanaged, IPixel < TPixel >
152
154
=> WrapMemory ( Configuration . Default , pixelMemoryOwner , width , height ) ;
155
+
156
+ /// <summary>
157
+ /// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
158
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
159
+ /// </summary>
160
+ /// <typeparam name="TPixel">The pixel type</typeparam>
161
+ /// <param name="configuration">The <see cref="Configuration"/></param>
162
+ /// <param name="byteMemory">The byte memory representing the pixel data.</param>
163
+ /// <param name="width">The width of the memory image.</param>
164
+ /// <param name="height">The height of the memory image.</param>
165
+ /// <param name="metadata">The <see cref="ImageMetadata"/>.</param>
166
+ /// <exception cref="ArgumentNullException">The configuration is null.</exception>
167
+ /// <exception cref="ArgumentNullException">The metadata is null.</exception>
168
+ /// <returns>An <see cref="Image{TPixel}"/> instance</returns>
169
+ public static Image < TPixel > WrapMemory < TPixel > (
170
+ Configuration configuration ,
171
+ Memory < byte > byteMemory ,
172
+ int width ,
173
+ int height ,
174
+ ImageMetadata metadata )
175
+ where TPixel : unmanaged, IPixel < TPixel >
176
+ {
177
+ Guard . NotNull ( configuration , nameof ( configuration ) ) ;
178
+ Guard . NotNull ( metadata , nameof ( metadata ) ) ;
179
+
180
+ var memoryManager = new ByteMemoryManager < TPixel > ( byteMemory ) ;
181
+
182
+ Guard . IsTrue ( memoryManager . Memory . Length == width * height , nameof ( byteMemory ) , "The length of the input memory doesn't match the specified image size" ) ;
183
+
184
+ var memorySource = MemoryGroup < TPixel > . Wrap ( memoryManager . Memory ) ;
185
+ return new Image < TPixel > ( configuration , memorySource , width , height , metadata ) ;
186
+ }
187
+
188
+ /// <summary>
189
+ /// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
190
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
191
+ /// </summary>
192
+ /// <typeparam name="TPixel">The pixel type</typeparam>
193
+ /// <param name="configuration">The <see cref="Configuration"/></param>
194
+ /// <param name="byteMemory">The byte memory representing the pixel data.</param>
195
+ /// <param name="width">The width of the memory image.</param>
196
+ /// <param name="height">The height of the memory image.</param>
197
+ /// <exception cref="ArgumentNullException">The configuration is null.</exception>
198
+ /// <returns>An <see cref="Image{TPixel}"/> instance.</returns>
199
+ public static Image < TPixel > WrapMemory < TPixel > (
200
+ Configuration configuration ,
201
+ Memory < byte > byteMemory ,
202
+ int width ,
203
+ int height )
204
+ where TPixel : unmanaged, IPixel < TPixel >
205
+ => WrapMemory < TPixel > ( configuration , byteMemory , width , height , new ImageMetadata ( ) ) ;
206
+
207
+ /// <summary>
208
+ /// Wraps an existing contiguous memory area of 'width' x 'height' pixels,
209
+ /// allowing to view/manipulate it as an <see cref="Image{TPixel}"/> instance.
210
+ /// The memory is being observed, the caller remains responsible for managing it's lifecycle.
211
+ /// </summary>
212
+ /// <typeparam name="TPixel">The pixel type.</typeparam>
213
+ /// <param name="byteMemory">The byte memory representing the pixel data.</param>
214
+ /// <param name="width">The width of the memory image.</param>
215
+ /// <param name="height">The height of the memory image.</param>
216
+ /// <returns>An <see cref="Image{TPixel}"/> instance.</returns>
217
+ public static Image < TPixel > WrapMemory < TPixel > (
218
+ Memory < byte > byteMemory ,
219
+ int width ,
220
+ int height )
221
+ where TPixel : unmanaged, IPixel < TPixel >
222
+ => WrapMemory < TPixel > ( Configuration . Default , byteMemory , width , height ) ;
153
223
}
154
224
}
0 commit comments