Skip to content

Commit f1834c7

Browse files
committed
Avoid bounds checks
1 parent 522a879 commit f1834c7

12 files changed

+31
-31
lines changed

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434
int offset = 0;
3535
for (int y = top; y < top + height; y++)
3636
{
37-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
37+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
3838
if (this.isBigEndian)
3939
{
40-
for (int x = left; x < left + width; x++)
40+
for (int x = 0; x < pixelRow.Length; x++)
4141
{
4242
ushort intensity = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
4343
offset += 2;
@@ -47,7 +47,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4747
}
4848
else
4949
{
50-
for (int x = left; x < left + width; x++)
50+
for (int x = 0; x < pixelRow.Length; x++)
5151
{
5252
ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
5353
offset += 2;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2424
var l8 = default(L8);
2525
for (int y = top; y < top + height; y++)
2626
{
27-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
28-
for (int x = left; x < left + width; x++)
27+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
28+
for (int x = 0; x < pixelRow.Length; x++)
2929
{
3030
byte intensity = data[offset++];
3131
pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color);

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434

3535
for (int y = top; y < top + height; y++)
3636
{
37-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
38-
for (int x = left; x < left + width; x++)
37+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
38+
for (int x = 0; x < pixelRow.Length; x++)
3939
{
4040
int value = bitReader.ReadBits(this.bitsPerSample0);
4141
float intensity = value / this.factor;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3535

3636
for (int y = top; y < top + height; y++)
3737
{
38-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
39-
for (int x = left; x < left + width; x++)
38+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
39+
for (int x = 0; x < pixelRow.Length; x++)
4040
{
4141
int index = bitReader.ReadBits(this.bitsPerSample0);
4242
pixelRow[x] = this.palette[index];

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3535

3636
for (int y = top; y < top + height; y++)
3737
{
38-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
38+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
3939

4040
if (this.isBigEndian)
4141
{
42-
for (int x = left; x < left + width; x++)
42+
for (int x = 0; x < pixelRow.Length; x++)
4343
{
4444
ulong r = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
4545
offset += 2;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.Buffers;
5-
using System.Numerics;
66
using SixLabors.ImageSharp.Formats.Tiff.Utils;
77
using SixLabors.ImageSharp.Memory;
88
using SixLabors.ImageSharp.PixelFormats;
@@ -32,17 +32,17 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
3232
var color = default(TPixel);
3333
color.FromVector4(TiffUtils.Vector4Default);
3434

35-
System.Span<byte> redData = data[0].GetSpan();
36-
System.Span<byte> greenData = data[1].GetSpan();
37-
System.Span<byte> blueData = data[2].GetSpan();
35+
Span<byte> redData = data[0].GetSpan();
36+
Span<byte> greenData = data[1].GetSpan();
37+
Span<byte> blueData = data[2].GetSpan();
3838

3939
int offset = 0;
4040
for (int y = top; y < top + height; y++)
4141
{
42-
System.Span<TPixel> pixelRow = pixels.GetRowSpan(y);
42+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
4343
if (this.isBigEndian)
4444
{
45-
for (int x = left; x < left + width; x++)
45+
for (int x = 0; x < pixelRow.Length; x++)
4646
{
4747
ulong r = TiffUtils.ConvertToShortBigEndian(redData.Slice(offset, 2));
4848
ulong g = TiffUtils.ConvertToShortBigEndian(greenData.Slice(offset, 2));
@@ -55,7 +55,7 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
5555
}
5656
else
5757
{
58-
for (int x = left; x < left + width; x++)
58+
for (int x = 0; x < pixelRow.Length; x++)
5959
{
6060
ulong r = TiffUtils.ConvertToShortLittleEndian(redData.Slice(offset, 2));
6161
ulong g = TiffUtils.ConvertToShortLittleEndian(greenData.Slice(offset, 2));

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2424
var rgba = default(Rgba32);
2525
for (int y = top; y < top + height; y++)
2626
{
27-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
27+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
2828

29-
for (int x = left; x < left + width; x++)
29+
for (int x = 0; x < pixelRow.Length; x++)
3030
{
3131
byte r = data[offset++];
3232
byte g = data[offset++];

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
5858

5959
for (int y = top; y < top + height; y++)
6060
{
61-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
62-
for (int x = left; x < left + width; x++)
61+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
62+
for (int x = 0; x < pixelRow.Length; x++)
6363
{
6464
float r = rBitReader.ReadBits(this.bitsPerSampleR) / this.rFactor;
6565
float g = gBitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4747

4848
for (int y = top; y < top + height; y++)
4949
{
50-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
51-
for (int x = left; x < left + width; x++)
50+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
51+
for (int x = 0; x < pixelRow.Length; x++)
5252
{
5353
float r = bitReader.ReadBits(this.bitsPerSampleR) / this.rFactor;
5454
float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434
int offset = 0;
3535
for (int y = top; y < top + height; y++)
3636
{
37-
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
37+
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
3838
if (this.isBigEndian)
3939
{
40-
for (int x = left; x < left + width; x++)
40+
for (int x = 0; x < pixelRow.Length; x++)
4141
{
4242
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)));
4343
offset += 2;
@@ -47,7 +47,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4747
}
4848
else
4949
{
50-
for (int x = left; x < left + width; x++)
50+
for (int x = 0; x < pixelRow.Length; x++)
5151
{
5252
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)));
5353
offset += 2;

0 commit comments

Comments
 (0)