Browse Source

Improve performance tiff colors

pull/1570/head
Ildar Khayrutdinov 5 years ago
parent
commit
7282c7bf6b
  1. 7
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs
  2. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs
  3. 6
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs
  4. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs
  5. 6
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs
  6. 7
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs
  7. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs
  8. 6
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs

7
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -25,6 +26,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
var black = new Rgba32(0, 0, 0, 0xff);
var white = new Rgba32(0xff, 0xff, 0xff, 0xff);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x += 8) for (int x = left; x < left + width; x += 8)
@ -35,9 +38,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
for (int shift = 0; shift < maxShift; shift++) for (int shift = 0; shift < maxShift; shift++)
{ {
int bit = (b >> (7 - shift)) & 1; int bit = (b >> (7 - shift)) & 1;
byte intensity = (bit == 1) ? (byte)255 : (byte)0;
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255)); color.FromRgba32(bit == 0 ? black : white);
pixels[x + shift, y] = color; pixels[x + shift, y] = color;
} }
} }

20
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -25,19 +26,24 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
bool isOddWidth = (width & 1) == 1; bool isOddWidth = (width & 1) == 1;
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width - 1; x += 2) for (int x = left; x < left + width - 1;)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17); byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24));
pixels[x, y] = color; color.FromRgba32(rgba);
pixels[x++, y] = color;
byte intensity2 = (byte)((byteData & 0x0F) * 17); byte intensity2 = (byte)((byteData & 0x0F) * 17);
color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255)); rgba.PackedValue = (uint)(intensity2 | (intensity2 << 8) | (intensity2 << 16) | (0xff << 24));
pixels[x + 1, y] = color; color.FromRgba32(rgba);
pixels[x++, y] = color;
} }
if (isOddWidth) if (isOddWidth)
@ -45,7 +51,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
byte byteData = data[offset++]; byte byteData = data[offset++];
byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17); byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24));
color.FromRgba32(rgba);
pixels[left + width - 1, y] = color; pixels[left + width - 1, y] = color;
} }
} }

6
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -24,13 +25,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x++) for (int x = left; x < left + width; x++)
{ {
byte intensity = data[offset++]; byte intensity = data[offset++];
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255)); rgba.PackedValue = (uint)(intensity | (intensity << 8) | (intensity << 16) | (0xff << 24));
color.FromRgba32(rgba);
pixels[x, y] = color; pixels[x, y] = color;
} }
} }

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs

@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
{ {
var palette = new TPixel[colorCount]; var palette = new TPixel[colorCount];
int rOffset = 0; const int rOffset = 0;
int gOffset = colorCount; int gOffset = colorCount;
int bOffset = colorCount * 2; int bOffset = colorCount * 2;

6
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -24,6 +25,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRow = pixels.GetRowSpan(y); Span<TPixel> pixelRow = pixels.GetRowSpan(y);
@ -34,7 +36,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
byte g = data[offset++]; byte g = data[offset++];
byte b = data[offset++]; byte b = data[offset++];
color.FromRgba32(new Rgba32(r, g, b, 255)); rgba.PackedValue = (uint)(r | (g << 8) | (b << 16) | (0xff << 24));
color.FromRgba32(rgba);
pixelRow[x] = color; pixelRow[x] = color;
} }
} }

7
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -24,6 +25,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
var black = new Rgba32(0, 0, 0, 0xff);
var white = new Rgba32(0xff, 0xff, 0xff, 0xff);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x += 8) for (int x = left; x < left + width; x += 8)
@ -34,9 +37,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
for (int shift = 0; shift < maxShift; shift++) for (int shift = 0; shift < maxShift; shift++)
{ {
int bit = (b >> (7 - shift)) & 1; int bit = (b >> (7 - shift)) & 1;
byte intensity = (bit == 1) ? (byte)0 : (byte)255;
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255)); color.FromRgba32(bit == 0 ? white : black);
pixels[x + shift, y] = color; pixels[x + shift, y] = color;
} }
} }

20
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -25,19 +26,24 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
bool isOddWidth = (width & 1) == 1; bool isOddWidth = (width & 1) == 1;
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width - 1; x += 2) for (int x = left; x < left + width - 1;)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17); byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24));
pixels[x, y] = color; color.FromRgba32(rgba);
pixels[x++, y] = color;
byte intensity2 = (byte)((15 - (byteData & 0x0F)) * 17); byte intensity2 = (byte)((15 - (byteData & 0x0F)) * 17);
color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255)); rgba.PackedValue = (uint)(intensity2 | (intensity2 << 8) | (intensity2 << 16) | (0xff << 24));
pixels[x + 1, y] = color; color.FromRgba32(rgba);
pixels[x++, y] = color;
} }
if (isOddWidth) if (isOddWidth)
@ -45,7 +51,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
byte byteData = data[offset++]; byte byteData = data[offset++];
byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17); byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17);
color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24));
color.FromRgba32(rgba);
pixels[left + width - 1, y] = color; pixels[left + width - 1, y] = color;
} }
} }

6
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -24,13 +25,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
int offset = 0; int offset = 0;
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x++) for (int x = left; x < left + width; x++)
{ {
byte intensity = (byte)(255 - data[offset++]); byte intensity = (byte)(255 - data[offset++]);
color.FromRgba32(new Rgba32(intensity, intensity, intensity, 255)); rgba.PackedValue = (uint)(intensity | (intensity << 8) | (intensity << 16) | (0xff << 24));
color.FromRgba32(rgba);
pixels[x, y] = color; pixels[x, y] = color;
} }
} }

Loading…
Cancel
Save