diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs index 14c31dbd0..7933904b4 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -25,6 +26,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff 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 x = left; x < left + width; x += 8) @@ -35,9 +38,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff for (int shift = 0; shift < maxShift; shift++) { 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs index 39216dff7..7d7532ba5 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -25,19 +26,24 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int offset = 0; bool isOddWidth = (width & 1) == 1; + var rgba = default(Rgba32); 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 intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17); - color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); - pixels[x, y] = color; + rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24)); + color.FromRgba32(rgba); + + pixels[x++, y] = color; byte intensity2 = (byte)((byteData & 0x0F) * 17); - color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255)); - pixels[x + 1, y] = color; + rgba.PackedValue = (uint)(intensity2 | (intensity2 << 8) | (intensity2 << 16) | (0xff << 24)); + color.FromRgba32(rgba); + + pixels[x++, y] = color; } if (isOddWidth) @@ -45,7 +51,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff byte byteData = data[offset++]; 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs index 8f281fd04..a4d0adb2b 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -24,13 +25,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int offset = 0; + var rgba = default(Rgba32); for (int y = top; y < top + height; y++) { for (int x = left; x < left + width; x++) { 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs index 719610a9f..b54fb90bf 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { var palette = new TPixel[colorCount]; - int rOffset = 0; + const int rOffset = 0; int gOffset = colorCount; int bOffset = colorCount * 2; diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs index cc3236b89..6bb34eb19 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -24,6 +25,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int offset = 0; + var rgba = default(Rgba32); for (int y = top; y < top + height; y++) { Span pixelRow = pixels.GetRowSpan(y); @@ -34,7 +36,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff byte g = 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs index 51f84d3c8..956862dca 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -24,6 +25,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff 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 x = left; x < left + width; x += 8) @@ -34,9 +37,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff for (int shift = 0; shift < maxShift; shift++) { 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs index bbee41dcd..c6519cf8a 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -25,19 +26,24 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int offset = 0; bool isOddWidth = (width & 1) == 1; + var rgba = default(Rgba32); 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 intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17); - color.FromRgba32(new Rgba32(intensity1, intensity1, intensity1, 255)); - pixels[x, y] = color; + rgba.PackedValue = (uint)(intensity1 | (intensity1 << 8) | (intensity1 << 16) | (0xff << 24)); + color.FromRgba32(rgba); + + pixels[x++, y] = color; byte intensity2 = (byte)((15 - (byteData & 0x0F)) * 17); - color.FromRgba32(new Rgba32(intensity2, intensity2, intensity2, 255)); - pixels[x + 1, y] = color; + rgba.PackedValue = (uint)(intensity2 | (intensity2 << 8) | (intensity2 << 16) | (0xff << 24)); + color.FromRgba32(rgba); + + pixels[x++, y] = color; } if (isOddWidth) @@ -45,7 +51,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff byte byteData = data[offset++]; 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; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs index 7e192f1af..d6dbd4656 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -24,13 +25,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int offset = 0; + var rgba = default(Rgba32); for (int y = top; y < top + height; y++) { for (int x = left; x < left + width; x++) { 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; } }