Skip to content

Commit 11af2d4

Browse files
committed
Added sample limiting to ColorPaletteSampler
1 parent 6fc9786 commit 11af2d4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

components/ColorAnalyzer/src/ColorPaletteSampler/ColorPaletteSampler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
104104

105105
// Read the stream into a a color array
106106
const int bytesPerPixel = 4;
107-
var samples = new Vector3[(int)pixelByteStream.Length / bytesPerPixel];
107+
var samples = new Vector3[sampleCount];
108108

109109
// Iterate through the stream reading a pixel (4 bytes) at a time
110110
// and storing them as a Vector3. Opacity info is dropped.
111111
int colorIndex = 0;
112+
var step = (pixelByteStream.Length / sampleCount);
113+
step -= step % 4;
112114
#if NET7_0_OR_GREATER
113115
Span<byte> pixelBytes = stackalloc byte[bytesPerPixel];
114116
while (pixelByteStream.Read(pixelBytes) == bytesPerPixel)
@@ -124,6 +126,9 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
124126
// Take the red, green, and blue channels to make a floating-point space color.
125127
samples[colorIndex] = new Vector3(pixelBytes[2], pixelBytes[1], pixelBytes[0]) / byte.MaxValue;
126128
colorIndex++;
129+
130+
// Advance by step amount
131+
pixelByteStream.Position += step;
127132
}
128133

129134
// If we skipped any pixels, trim the span

0 commit comments

Comments
 (0)