-
-
Notifications
You must be signed in to change notification settings - Fork 887
Add support for decoding tiff's with float pixel data #1727
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3d02cfd
Add support for decoding tiff's with 32bit float rgb pixel data
brianpopow 9d6b7a6
Add support for decoding tiff's with 32bit float gray pixel data with…
brianpopow b29581e
Add support for decoding tiff's with 32bit float gray pixel data with…
brianpopow b842c07
Fix issue with white is zero with 24 and 32 bit
brianpopow 556fbf8
Merge branch 'master' into bp/tiff32float
brianpopow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32FloatTiffColor{TPixel}.cs
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbFloat323232TiffColor{TPixel}.cs
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32FloatTiffColor{TPixel}.cs
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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 we not use
BinaryPrimitives.ReadSingleBigEndianhere and elsewhere?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.
ReadSingleBigEndianis only available withnet5.0and aboveThere 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.
Didn't know that. Looking at the source we could only polyfill back to .NET Core 2.0 also. We'll leave it like this for now.