-
-
Notifications
You must be signed in to change notification settings - Fork 888
Improve accessability of Span<T> methods. #1172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using SixLabors.ImageSharp.Advanced; | ||
|
|
@@ -166,6 +167,39 @@ internal ImageFrame(Configuration configuration, ImageFrame<TPixel> source) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory | ||
| /// at row <paramref name="rowIndex"/> beginning from the first pixel on that row. | ||
| /// </summary> | ||
| /// <param name="rowIndex">The row.</param> | ||
| /// <returns>The <see cref="Span{TPixel}"/></returns> | ||
| public Span<TPixel> GetPixelRowSpan(int rowIndex) | ||
| { | ||
| Guard.MustBeGreaterThanOrEqualTo(rowIndex, 0, nameof(rowIndex)); | ||
| Guard.MustBeLessThan(rowIndex, this.Height, nameof(rowIndex)); | ||
|
|
||
| return this.PixelBuffer.GetRowSpan(rowIndex); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the representation of the pixels as a <see cref="Span{T}"/> in the source image's pixel format | ||
| /// stored in row major order, if the backing buffer is contiguous. | ||
| /// </summary> | ||
| /// <param name="span">The <see cref="Span{T}"/>.</param> | ||
| /// <returns>The <see cref="bool"/>.</returns> | ||
| public bool TryGetSinglePixelSpan(out Span<TPixel> span) | ||
| { | ||
| IMemoryGroup<TPixel> mg = this.GetPixelMemoryGroup(); | ||
| if (mg.Count > 1) | ||
| { | ||
| span = default; | ||
| return false; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @antonfirsov Is there a test somewhere I can tap into to test the false condition?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you can limit
|
||
| } | ||
|
|
||
| span = mg.Single().Span; | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a reference to the pixel at the specified position. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,39 @@ internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable< | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory | ||
| /// at row <paramref name="rowIndex"/> beginning from the first pixel on that row. | ||
| /// </summary> | ||
| /// <param name="rowIndex">The row.</param> | ||
| /// <returns>The <see cref="Span{TPixel}"/></returns> | ||
| public Span<TPixel> GetPixelRowSpan(int rowIndex) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous note. |
||
| { | ||
| Guard.MustBeGreaterThanOrEqualTo(rowIndex, 0, nameof(rowIndex)); | ||
| Guard.MustBeLessThan(rowIndex, this.Height, nameof(rowIndex)); | ||
|
|
||
| return this.PixelSource.PixelBuffer.GetRowSpan(rowIndex); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the representation of the pixels as a <see cref="Span{T}"/> in the source image's pixel format | ||
| /// stored in row major order, if the backing buffer is contiguous. | ||
| /// </summary> | ||
| /// <param name="span">The <see cref="Span{T}"/>.</param> | ||
| /// <returns>The <see cref="bool"/>.</returns> | ||
| public bool TryGetSinglePixelSpan(out Span<TPixel> span) | ||
| { | ||
| IMemoryGroup<TPixel> mg = this.GetPixelMemoryGroup(); | ||
| if (mg.Count > 1) | ||
| { | ||
| span = default; | ||
| return false; | ||
| } | ||
|
|
||
| span = mg.Single().Span; | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Clones the current image | ||
| /// </summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs are missing
<exception>forArgumentOutOfRangeException.