Expose Span methods on LoadPixelData and SavePixelData #567
Expose Span methods on LoadPixelData and SavePixelData #567JimBobSquarePants merged 26 commits intoSixLabors:masterfrom carbon:memory-rc1
Conversation
Codecov Report
@@ Coverage Diff @@
## master #567 +/- ##
==========================================
+ Coverage 88.62% 88.62% +<.01%
==========================================
Files 843 843
Lines 35549 35550 +1
Branches 2585 2585
==========================================
+ Hits 31504 31506 +2
Misses 3268 3268
+ Partials 777 776 -1
Continue to review full report at Codecov.
|
|
Ready for review. |
tocsoft
left a comment
There was a problem hiding this comment.
just the one change and a comment regards a subtle change in behavior. Everything else looks good to me.
| private static string ToRgbaHex(string hex) | ||
| { | ||
| hex = hex.StartsWith("#") ? hex.Substring(1) : hex; | ||
| if (hex[0] == '#') |
There was a problem hiding this comment.
this will fail if hex is an empty string. we should be checking the length > 0 too. Probably should be defensive over nulls as well.
There was a problem hiding this comment.
This methods only caller is guarding against null and empty. I added some tests to verify this behavior.
| { | ||
| ReadOnlySpan<byte> newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth); | ||
| byte[] pal = this.palette; | ||
| ReadOnlySpan<Rgb24> pal = MemoryMarshal.Cast<byte, Rgb24>(this.palette); |
There was a problem hiding this comment.
won't this fail if this.palette isn't the correct length? just raising this as a subtle change of behavior that might introduce exceptions that previously (potentially incorrectly) allowed.
note: not asking for a change just pointing it out incase someone else thinks its important.
There was a problem hiding this comment.
This should only throw if the we try indexing into the palette outside it's bounds.
…& cleanup messages. Also remove the unused message arguments.
|
Haven't seen this error before or able to reproduce locally. Will force another build to see if it's transient. |
| /// <typeparam name="TPixel">The pixel format.</typeparam> | ||
| /// <returns>A new <see cref="Image{TPixel}"/>.</returns> | ||
| private static Image<TPixel> LoadPixelData<TPixel>(Span<TPixel> data, int width, int height) | ||
| public static Image<TPixel> LoadPixelData<TPixel>(Span<TPixel> data, int width, int height) |
There was a problem hiding this comment.
We should consume ReadOnlySpan in all overloads.
There was a problem hiding this comment.
Agreed. I'll update the PR shortly. Also need to figure out why that test is failing.
|
@JimBobSquarePants @antonfirsov Should we remove the LoadFrom / SaveTo array overloads and just expose Span to simplify the API surface? Any downsides? |
|
Not everyone knows about An other donwside is loosing binary compatiblity, but I don't mind that, because no one cares about binary compatiblity in .NET Core world these days 😆 (At least not before reaching RC.) |
|
@JimBobSquarePants Ready for your final review. |
JimBobSquarePants
left a comment
There was a problem hiding this comment.
Good stuff... One change and a small doco error though please..
| /// <typeparam name="TPixel">The pixel format.</typeparam> | ||
| /// <returns>A new <see cref="Image{TPixel}"/>.</returns> | ||
| private static Image<TPixel> LoadPixelData<TPixel>(Span<byte> data, int width, int height) | ||
| public static Image<TPixel> LoadPixelData<TPixel>(Span<byte> data, int width, int height) |
src/ImageSharp/ImageExtensions.cs
Outdated
|
|
||
| /// <summary> | ||
| /// Saves the raw image to the given bytes. | ||
| /// Saves the raw image to the given byte buffer. |
There was a problem hiding this comment.
Target buffer is of type TPixel
| } | ||
|
|
||
| // Ensure at least 1 frame was added to the frames collection | ||
| if (this.frames.Count == 0) |
There was a problem hiding this comment.
Would we not check this before iterating. Guard.IsTrue(frames.Count > 0)?
There was a problem hiding this comment.
No count on IEnumerable.
There was a problem hiding this comment.
Legit, forgot about IEnumerable.
| (byte)(packedValue >> 16), | ||
| (byte)(packedValue >> 8), | ||
| (byte)(packedValue >> 0)); | ||
| var rgba = new Rgba32(BinaryPrimitives.ReverseEndianness(packedValue)); |
| char a = hex.Length == 3 ? 'F' : hex[3]; | ||
|
|
||
| return string.Concat(red, red, green, green, blue, blue, alpha, alpha); | ||
| return new string(new[] { r, r, g, g, b, b, a, a }); |
|
|
||
| namespace SixLabors.ImageSharp.Tests.Colors | ||
| { | ||
| public class ColorBuilderTests |
| { | ||
| Guard.NotNullOrWhiteSpace(extension, nameof(extension)); | ||
|
|
||
| if (extension[0] == '.') |
Prerequisites
Description