mirror of https://github.com/SixLabors/ImageSharp
Browse Source
# Conflicts: # tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs # tests/ImageSharp.Tests/TestImages.cspull/1729/head
24 changed files with 355 additions and 18 deletions
@ -0,0 +1,69 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'BlackIsZero' photometric interpretation for 32-bit float grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BlackIsZero32FloatTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public BlackIsZero32FloatTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
byte[] buffer = new byte[4]; |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
Array.Reverse(buffer); |
||||
|
float intensity = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
float intensity = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,88 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 32 bits for each channel.
|
||||
|
/// </summary>
|
||||
|
internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RgbFloat323232TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public RgbFloat323232TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
int offset = 0; |
||||
|
byte[] buffer = new byte[4]; |
||||
|
|
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
|
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
Array.Reverse(buffer); |
||||
|
float r = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
Array.Reverse(buffer); |
||||
|
float g = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
Array.Reverse(buffer); |
||||
|
float b = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(r, g, b, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
float r = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
float g = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
float b = BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(r, g, b, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation for 32-bit float grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="WhiteIsZero32FloatTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public WhiteIsZero32FloatTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
byte[] buffer = new byte[4]; |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
Array.Reverse(buffer); |
||||
|
float intensity = 1.0f - BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 4).CopyTo(buffer); |
||||
|
float intensity = 1.0f - BitConverter.ToSingle(buffer, 0); |
||||
|
offset += 4; |
||||
|
|
||||
|
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:af82e07e6298082c91d60a97acb29b67ecabf386bc14371fcb698b248cfd3b40 |
||||
|
size 12814 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:5c918b8aa9968c03c12d85b3bcacd35ae57663a19f5490fc1c351521ed835f30 |
||||
|
size 12814 |
||||
@ -1,3 +0,0 @@ |
|||||
version https://git-lfs.github.com/spec/v1 |
|
||||
oid sha256:514417ead3d6c5c6ca33374ef0bb6ecbe5f875a266519d4cbaa4a6b91033d243 |
|
||||
size 12778 |
|
||||
@ -1,3 +1,3 @@ |
|||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||
oid sha256:64c948aa03bc4a24cd1d68bb18b5031c119936154a90f1cb1d9aaabd854c5d9b |
oid sha256:17139bc00d9fe2905fbac9226120d2823ea17a10a39b140970122153ec23265d |
||||
size 12778 |
size 12778 |
||||
|
|||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:17139bc00d9fe2905fbac9226120d2823ea17a10a39b140970122153ec23265d |
||||
|
size 12778 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:93e06533486f15e33f2435d081713fbecc3ba96c842058b7ba3a5d9116fe5f5c |
||||
|
size 12814 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:800a101f4d23fa2a499fcef036ebfca7d9338ac71b06a32ad05e7eb1905ddae3 |
||||
|
size 12814 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:a1f889baa6f3bb99f15609848cdd47d548d3e2ed1b7b558d428550dfa3bd4bf9 |
||||
|
size 38050 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:2c02be5dff2bcd5d60afbf379ba9095b0c8fd3a7a0063f684ac9ac9119f967a5 |
||||
|
size 38050 |
||||
Loading…
Reference in new issue