Skip to content

Commit 10ff24f

Browse files
committed
Loop unroll
1 parent affb143 commit 10ff24f

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,49 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3535
byte b = Unsafe.Add(ref dataRef, offset++);
3636
nint maxShift = Math.Min(left + width - x, 8);
3737

38-
for (int shift = 0; shift < maxShift; shift++)
38+
if (maxShift == 8)
3939
{
40-
int bit = (b >> (7 - shift)) & 1;
40+
int bit = (b >> 7) & 1;
41+
ref TPixel pixel0 = ref Unsafe.Add(ref pixelRowRef, x);
42+
pixel0 = bit == 0 ? colorBlack : colorWhite;
4143

42-
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
43-
pixel = bit == 0 ? colorBlack : colorWhite;
44+
bit = (b >> 6) & 1;
45+
ref TPixel pixel1 = ref Unsafe.Add(ref pixelRowRef, x + 1);
46+
pixel1 = bit == 0 ? colorBlack : colorWhite;
47+
48+
bit = (b >> 5) & 1;
49+
ref TPixel pixel2 = ref Unsafe.Add(ref pixelRowRef, x + 2);
50+
pixel2 = bit == 0 ? colorBlack : colorWhite;
51+
52+
bit = (b >> 4) & 1;
53+
ref TPixel pixel3 = ref Unsafe.Add(ref pixelRowRef, x + 3);
54+
pixel3 = bit == 0 ? colorBlack : colorWhite;
55+
56+
bit = (b >> 3) & 1;
57+
ref TPixel pixel4 = ref Unsafe.Add(ref pixelRowRef, x + 4);
58+
pixel4 = bit == 0 ? colorBlack : colorWhite;
59+
60+
bit = (b >> 2) & 1;
61+
ref TPixel pixel5 = ref Unsafe.Add(ref pixelRowRef, x + 5);
62+
pixel5 = bit == 0 ? colorBlack : colorWhite;
63+
64+
bit = (b >> 1) & 1;
65+
ref TPixel pixel6 = ref Unsafe.Add(ref pixelRowRef, x + 6);
66+
pixel6 = bit == 0 ? colorBlack : colorWhite;
67+
68+
bit = b & 1;
69+
ref TPixel pixel7 = ref Unsafe.Add(ref pixelRowRef, x + 7);
70+
pixel7 = bit == 0 ? colorBlack : colorWhite;
71+
}
72+
else
73+
{
74+
for (int shift = 0; shift < maxShift; shift++)
75+
{
76+
int bit = (b >> (7 - shift)) & 1;
77+
78+
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
79+
pixel = bit == 0 ? colorBlack : colorWhite;
80+
}
4481
}
4582
}
4683
}

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,49 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434
byte b = Unsafe.Add(ref dataRef, offset++);
3535
nint maxShift = Math.Min(left + width - x, 8);
3636

37-
for (int shift = 0; shift < maxShift; shift++)
37+
if (maxShift == 8)
3838
{
39-
int bit = (b >> (7 - shift)) & 1;
39+
int bit = (b >> 7) & 1;
40+
ref TPixel pixel0 = ref Unsafe.Add(ref pixelRowRef, x);
41+
pixel0 = bit == 0 ? colorWhite : colorBlack;
4042

41-
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
42-
pixel = bit == 0 ? colorWhite : colorBlack;
43+
bit = (b >> 6) & 1;
44+
ref TPixel pixel1 = ref Unsafe.Add(ref pixelRowRef, x + 1);
45+
pixel1 = bit == 0 ? colorWhite : colorBlack;
46+
47+
bit = (b >> 5) & 1;
48+
ref TPixel pixel2 = ref Unsafe.Add(ref pixelRowRef, x + 2);
49+
pixel2 = bit == 0 ? colorWhite : colorBlack;
50+
51+
bit = (b >> 4) & 1;
52+
ref TPixel pixel3 = ref Unsafe.Add(ref pixelRowRef, x + 3);
53+
pixel3 = bit == 0 ? colorWhite : colorBlack;
54+
55+
bit = (b >> 3) & 1;
56+
ref TPixel pixel4 = ref Unsafe.Add(ref pixelRowRef, x + 4);
57+
pixel4 = bit == 0 ? colorWhite : colorBlack;
58+
59+
bit = (b >> 2) & 1;
60+
ref TPixel pixel5 = ref Unsafe.Add(ref pixelRowRef, x + 5);
61+
pixel5 = bit == 0 ? colorWhite : colorBlack;
62+
63+
bit = (b >> 1) & 1;
64+
ref TPixel pixel6 = ref Unsafe.Add(ref pixelRowRef, x + 6);
65+
pixel6 = bit == 0 ? colorWhite : colorBlack;
66+
67+
bit = b & 1;
68+
ref TPixel pixel7 = ref Unsafe.Add(ref pixelRowRef, x + 7);
69+
pixel7 = bit == 0 ? colorWhite : colorBlack;
70+
}
71+
else
72+
{
73+
for (int shift = 0; shift < maxShift; shift++)
74+
{
75+
int bit = (b >> (7 - shift)) & 1;
76+
77+
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
78+
pixel = bit == 0 ? colorWhite : colorBlack;
79+
}
4380
}
4481
}
4582
}

0 commit comments

Comments
 (0)