@@ -17,7 +17,7 @@ public abstract partial class Image
1717 {
1818 /// <summary>
1919 /// 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.
2121 /// </summary>
2222 /// <typeparam name="TPixel">The pixel type</typeparam>
2323 /// <param name="configuration">The <see cref="Configuration"/></param>
@@ -38,14 +38,15 @@ public static Image<TPixel> WrapMemory<TPixel>(
3838 {
3939 Guard . NotNull ( configuration , nameof ( configuration ) ) ;
4040 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" ) ;
4142
4243 var memorySource = MemoryGroup < TPixel > . Wrap ( pixelMemory ) ;
4344 return new Image < TPixel > ( configuration , memorySource , width , height , metadata ) ;
4445 }
4546
4647 /// <summary>
4748 /// 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.
4950 /// </summary>
5051 /// <typeparam name="TPixel">The pixel type</typeparam>
5152 /// <param name="configuration">The <see cref="Configuration"/></param>
@@ -64,7 +65,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
6465
6566 /// <summary>
6667 /// 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.
6869 /// The memory is being observed, the caller remains responsible for managing it's lifecycle.
6970 /// </summary>
7071 /// <typeparam name="TPixel">The pixel type.</typeparam>
@@ -81,7 +82,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
8182
8283 /// <summary>
8384 /// 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.
8586 /// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
8687 /// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
8788 /// It will be disposed together with the result image.
@@ -105,14 +106,15 @@ public static Image<TPixel> WrapMemory<TPixel>(
105106 {
106107 Guard . NotNull ( configuration , nameof ( configuration ) ) ;
107108 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" ) ;
108110
109111 var memorySource = MemoryGroup < TPixel > . Wrap ( pixelMemoryOwner ) ;
110112 return new Image < TPixel > ( configuration , memorySource , width , height , metadata ) ;
111113 }
112114
113115 /// <summary>
114116 /// 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.
116118 /// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
117119 /// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
118120 /// It will be disposed together with the result image.
@@ -134,7 +136,7 @@ public static Image<TPixel> WrapMemory<TPixel>(
134136
135137 /// <summary>
136138 /// 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.
138140 /// The ownership of the <paramref name="pixelMemoryOwner"/> is being transferred to the new <see cref="Image{TPixel}"/> instance,
139141 /// meaning that the caller is not allowed to dispose <paramref name="pixelMemoryOwner"/>.
140142 /// It will be disposed together with the result image.
@@ -150,5 +152,73 @@ public static Image<TPixel> WrapMemory<TPixel>(
150152 int height )
151153 where TPixel : unmanaged, IPixel < TPixel >
152154 => 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 ) ;
153223 }
154224}
0 commit comments