@@ -130,6 +130,13 @@ public interface IImage<TUnderlying> : IImage
130
130
/// </summary>
131
131
public interface IImageData : IMemoryOwner < byte > , IStreamFactory
132
132
{
133
+ /// <summary>
134
+ /// Whether the image data is backed by a contiguous range of memory, in which
135
+ /// case the <see cref="IMemoryOwner{T}.Memory"/> member is usable and describable
136
+ /// by <see cref="Scan0"/> and <see cref="Stride"/>.
137
+ /// </summary>
138
+ bool IsContiguous { get ; }
139
+
133
140
/// <summary>
134
141
/// The offset of the top-left pixel in the data.
135
142
/// </summary>
@@ -348,6 +355,9 @@ public ImageData(ImageBase<TUnderlying> image)
348
355
349
356
StreamFactoryAccess IStreamFactory . Access => StreamFactoryAccess . Parallel ;
350
357
358
+ /// <inheritdoc/>
359
+ public abstract bool IsContiguous { get ; }
360
+
351
361
/// <inheritdoc/>
352
362
public abstract int Scan0 { get ; }
353
363
@@ -387,6 +397,9 @@ Stream IStreamFactory.Open()
387
397
388
398
public abstract class MemoryImageData < TUnderlying > : ImageData < TUnderlying > where TUnderlying : class
389
399
{
400
+ /// <inheritdoc/>
401
+ public override bool IsContiguous => true ;
402
+
390
403
/// <inheritdoc/>
391
404
public override int Scan0 { get ; }
392
405
@@ -430,9 +443,18 @@ public MemoryImageData(ImageBase<TUnderlying> image, int stride, int bitDepth) :
430
443
RowSize = ( Width * bitDepth + 7 ) / 8 ;
431
444
}
432
445
446
+ private void CheckContiguous ( )
447
+ {
448
+ if ( ! IsContiguous )
449
+ {
450
+ throw new NotImplementedException ( "This operation is not implemented for non-contiguous memory." ) ;
451
+ }
452
+ }
453
+
433
454
/// <inheritdoc/>
434
455
public override Memory < byte > GetPixel ( int x , int y )
435
456
{
457
+ CheckContiguous ( ) ;
436
458
var pixelSize = Math . DivRem ( BitDepth , 8 , out var rem ) ;
437
459
if ( rem == 0 )
438
460
{
@@ -444,6 +466,7 @@ public override Memory<byte> GetPixel(int x, int y)
444
466
/// <inheritdoc/>
445
467
public override Memory < byte > GetRow ( int y )
446
468
{
469
+ CheckContiguous ( ) ;
447
470
return Memory . Slice ( Scan0 + Stride * y , RowSize ) ;
448
471
}
449
472
0 commit comments