New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for decoding Tiff images with UnassociatedAlphaData #2037
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e1e6c6
Add support for decoding Tiff images with UnassociatedAlphaData
brianpopow bc02ab1
Merge branch 'main' into bp/extrasamples
brianpopow e2bc1af
Add support for decoding Tiff images with UnassociatedAlphaData and p…
brianpopow c08545e
Use stackalloc
brianpopow 5591c6d
Merge branch 'main' into bp/extrasamples
brianpopow 198c1de
Fix issue with 24 bits per channel
brianpopow 53db07a
Use pattern matching for magicFrame.Depth
brianpopow bd70395
Add 5 and 3 to valid magicFrame.Depth
brianpopow 815aee2
Merge branch 'main' into bp/extrasamples
JimBobSquarePants File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
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 an alpha channel and with 16 bits for each channel. | ||
/// </summary> | ||
internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
where TPixel : unmanaged, IPixel<TPixel> | ||
{ | ||
private readonly bool isBigEndian; | ||
|
||
private readonly Configuration configuration; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Rgba16161616TiffColor{TPixel}" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param> | ||
public Rgba16161616TiffColor(Configuration configuration, bool isBigEndian) | ||
{ | ||
this.configuration = configuration; | ||
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 | ||
Rgba64 rgba = TiffUtils.Rgba64Default; | ||
var color = default(TPixel); | ||
color.FromVector4(TiffUtils.Vector4Default); | ||
|
||
int offset = 0; | ||
|
||
for (int y = top; y < top + height; y++) | ||
{ | ||
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
|
||
if (this.isBigEndian) | ||
{ | ||
for (int x = 0; x < pixelRow.Length; x++) | ||
{ | ||
ulong r = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); | ||
offset += 2; | ||
ulong g = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); | ||
offset += 2; | ||
ulong b = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); | ||
offset += 2; | ||
ulong a = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); | ||
offset += 2; | ||
|
||
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, a, color); | ||
} | ||
} | ||
else | ||
{ | ||
int byteCount = pixelRow.Length * 8; | ||
PixelOperations<TPixel>.Instance.FromRgba64Bytes( | ||
this.configuration, | ||
data.Slice(offset, byteCount), | ||
pixelRow, | ||
pixelRow.Length); | ||
|
||
offset += byteCount; | ||
} | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
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 an alpha channel and with 24 bits for each channel. | ||
/// </summary> | ||
internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
where TPixel : unmanaged, IPixel<TPixel> | ||
{ | ||
private readonly bool isBigEndian; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Rgba24242424TiffColor{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 Rgba24242424TiffColor(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]; | ||
int bufferStartIdx = this.isBigEndian ? 1 : 0; | ||
|
||
Span<byte> bufferSpan = buffer.AsSpan(bufferStartIdx); | ||
for (int y = top; y < top + height; y++) | ||
{ | ||
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
|
||
if (this.isBigEndian) | ||
{ | ||
for (int x = 0; x < pixelRow.Length; x++) | ||
{ | ||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong a = TiffUtils.ConvertToUIntBigEndian(buffer); | ||
offset += 3; | ||
|
||
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); | ||
} | ||
} | ||
else | ||
{ | ||
for (int x = 0; x < pixelRow.Length; x++) | ||
{ | ||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); | ||
offset += 3; | ||
|
||
data.Slice(offset, 3).CopyTo(bufferSpan); | ||
ulong a = TiffUtils.ConvertToUIntLittleEndian(buffer); | ||
offset += 3; | ||
|
||
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba32323232TiffColor{TPixel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
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 an alpha channel and with 32 bits for each channel. | ||
/// </summary> | ||
internal class Rgba32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
where TPixel : unmanaged, IPixel<TPixel> | ||
{ | ||
private readonly bool isBigEndian; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Rgba32323232TiffColor{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 Rgba32323232TiffColor(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; | ||
|
||
for (int y = top; y < top + height; y++) | ||
{ | ||
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
|
||
if (this.isBigEndian) | ||
{ | ||
for (int x = 0; x < pixelRow.Length; x++) | ||
{ | ||
ulong r = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong g = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong b = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong a = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); | ||
} | ||
} | ||
else | ||
{ | ||
for (int x = 0; x < pixelRow.Length; x++) | ||
{ | ||
ulong r = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong g = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong b = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
ulong a = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); | ||
offset += 4; | ||
|
||
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using SixLabors.ImageSharp.Memory; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation | ||
{ | ||
/// <summary> | ||
/// Implements the 'RGB' photometric interpretation with an alpha channel and 8 bits per channel. | ||
/// </summary> | ||
internal class Rgba8888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
where TPixel : unmanaged, IPixel<TPixel> | ||
{ | ||
private readonly Configuration configuration; | ||
|
||
public Rgba8888TiffColor(Configuration configuration) => this.configuration = configuration; | ||
|
||
/// <inheritdoc/> | ||
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) | ||
{ | ||
int offset = 0; | ||
|
||
for (int y = top; y < top + height; y++) | ||
{ | ||
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
int byteCount = pixelRow.Length * 4; | ||
PixelOperations<TPixel>.Instance.FromRgba32Bytes( | ||
this.configuration, | ||
data.Slice(offset, byteCount), | ||
pixelRow, | ||
pixelRow.Length); | ||
|
||
offset += byteCount; | ||
} | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaFloat32323232TiffColor{TPixel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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 an alpha channel and with 32 bits for each channel. | ||
/// </summary> | ||
internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
where TPixel : unmanaged, IPixel<TPixel> | ||
{ | ||
private readonly bool isBigEndian; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RgbaFloat32323232TiffColor{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 RgbaFloat32323232TiffColor(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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
||
for (int y = top; y < top + height; y++) | ||
{ | ||
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll be nice when we can use |
||
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; | ||
|
||
data.Slice(offset, 4).CopyTo(buffer); | ||
Array.Reverse(buffer); | ||
float a = BitConverter.ToSingle(buffer, 0); | ||
offset += 4; | ||
|
||
var colorVector = new Vector4(r, g, b, a); | ||
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; | ||
|
||
data.Slice(offset, 4).CopyTo(buffer); | ||
float a = BitConverter.ToSingle(buffer, 0); | ||
offset += 4; | ||
|
||
var colorVector = new Vector4(r, g, b, a); | ||
color.FromVector4(colorVector); | ||
pixelRow[x] = color; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this allocation be avoided?
I.e. turn it into
stackalloc
and use it asSpan<byte>
. MaybeTiffUtils
need to be updated also.