Skip to content

Commit

Permalink
Improve performance of Tiff decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
IldarKhayrutdinov committed Aug 22, 2019
1 parent e101736 commit 1b32629
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);
for (int x = left; x < left + width; x += 8)
{
byte b = data[offset++];
int maxShift = Math.Min(left + width - x, 8);

for (int shift = 0; shift < maxShift; shift++)
for (int shift = 0, index = x; shift < maxShift; shift++, index++)
{
int bit = (b >> (7 - shift)) & 1;
byte intensity = (bit == 1) ? (byte)255 : (byte)0;
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255));
pixels[x + shift, y] = color;
buffer[index] = color;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -33,17 +34,19 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width - 1; x += 2)
{
byte byteData = data[offset++];

byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255));
pixels[x, y] = color;
buffer[x] = color;

byte intensity2 = (byte)((byteData & 0x0F) * 17);
color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255));
pixels[x + 1, y] = color;
buffer[x + 1] = color;
}

if (isOddWidth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -32,11 +33,13 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
byte intensity = data[offset++];
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255));
pixels[x, y] = color;
buffer[x] = color;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public static void Decode<TPixel>(byte[] data, uint[] bitsPerSample, Buffer2D<TP

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
int value = bitReader.ReadBits(bitsPerSample[0]);
float intensity = ((float)value) / factor;
color.FromVector4(new Vector4(intensity, intensity, intensity, 1.0f));
pixels[x, y] = color;
buffer[x] = color;
}

bitReader.NextRow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public static void Decode<TPixel>(byte[] data, uint[] bitsPerSample, uint[] colo

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
int index = bitReader.ReadBits(bitsPerSample[0]);
pixels[x, y] = palette[index];
buffer[x] = palette[index];
}

bitReader.NextRow();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -32,13 +33,15 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
byte r = data[offset++];
byte g = data[offset++];
byte b = data[offset++];
color.FromRgba32(new Rgba32(r, g, b, 255));
pixels[x, y] = color;
buffer[x] = color;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public static void Decode<TPixel>(byte[][] data, uint[] bitsPerSample, Buffer2D<

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
float r = ((float)rBitReader.ReadBits(bitsPerSample[0])) / rFactor;
float g = ((float)gBitReader.ReadBits(bitsPerSample[1])) / gFactor;
float b = ((float)bBitReader.ReadBits(bitsPerSample[2])) / bFactor;
color.FromVector4(new Vector4(r, g, b, 1.0f));
pixels[x, y] = color;

buffer[x] = color;
}

rBitReader.NextRow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ public static void Decode<TPixel>(byte[] data, uint[] bitsPerSample, Buffer2D<TP

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
float r = ((float)bitReader.ReadBits(bitsPerSample[0])) / rFactor;
float g = ((float)bitReader.ReadBits(bitsPerSample[1])) / gFactor;
float b = ((float)bitReader.ReadBits(bitsPerSample[2])) / bFactor;
color.FromVector4(new Vector4(r, g, b, 1.0f));
pixels[x, y] = color;

buffer[x] = color;
}

bitReader.NextRow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x += 8)
{
byte b = data[offset++];
int maxShift = Math.Min(left + width - x, 8);

for (int shift = 0; shift < maxShift; shift++)
for (int shift = 0, index = x; shift < maxShift; shift++, index++)
{
int bit = (b >> (7 - shift)) & 1;
byte intensity = (bit == 1) ? (byte)0 : (byte)255;
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255));
pixels[x + shift, y] = color;

buffer[index] = color;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -33,17 +34,19 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width - 1; x += 2)
{
byte byteData = data[offset++];

byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255));
pixels[x, y] = color;
buffer[x] = color;

byte intensity2 = (byte)((15 - (byteData & 0x0F)) * 17);
color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255));
pixels[x + 1, y] = color;
buffer[x + 1] = color;
}

if (isOddWidth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -32,11 +33,13 @@ public static void Decode<TPixel>(byte[] data, Buffer2D<TPixel> pixels, int left

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
byte intensity = (byte)(255 - data[offset++]);
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255));
pixels[x, y] = color;
buffer[x] = color;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public static void Decode<TPixel>(byte[] data, uint[] bitsPerSample, Buffer2D<TP

for (int y = top; y < top + height; y++)
{
Span<TPixel> buffer = pixels.GetRowSpan(y);

for (int x = left; x < left + width; x++)
{
int value = bitReader.ReadBits(bitsPerSample[0]);
float intensity = 1.0f - (((float)value) / factor);
color.FromVector4(new Vector4(intensity, intensity, intensity, 1.0f));
pixels[x, y] = color;
buffer[x] = color;
}

bitReader.NextRow();
Expand Down

0 comments on commit 1b32629

Please sign in to comment.