-
-
Notifications
You must be signed in to change notification settings - Fork 888
Add support for decoding tiff images with CieLab color space #2127
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
11 commits
Select commit
Hold shift + click to select a range
0b7705c
Add support for decoding tiff images with CieLab color space
brianpopow bd2b067
Undo horizontal predictor for CieLab
brianpopow 485571a
Add support for decoding planar tiff with cielab colorspace
brianpopow d850aee
Add tests for CieLab
brianpopow bf4c0dc
Add CieLab to supported formats for the tiff decoder
brianpopow 7c8feca
Make color converter static
brianpopow 9165efe
Fix build issue
brianpopow 9930010
Merge branch 'main' into bp/cielab
brianpopow e3704ef
Merge branch 'main' into bp/cielab
brianpopow e4e7a5b
Merge branch 'main' into bp/cielab
brianpopow a258c47
Fix file header copyright text
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
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
49 changes: 49 additions & 0 deletions
49
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabPlanarTiffColor{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,49 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Numerics; | ||
| using SixLabors.ImageSharp.ColorSpaces; | ||
| using SixLabors.ImageSharp.ColorSpaces.Conversion; | ||
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation | ||
| { | ||
| /// <summary> | ||
| /// Implements decoding pixel data with photometric interpretation of type 'CieLab' with the planar configuration. | ||
| /// </summary> | ||
| internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| private static readonly ColorSpaceConverter ColorSpaceConverter = new(); | ||
|
|
||
| private const float Inv255 = 1.0f / 255.0f; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) | ||
| { | ||
| Span<byte> l = data[0].GetSpan(); | ||
| Span<byte> a = data[1].GetSpan(); | ||
| Span<byte> b = data[2].GetSpan(); | ||
|
|
||
| var color = default(TPixel); | ||
| int offset = 0; | ||
| for (int y = top; y < top + height; y++) | ||
| { | ||
| Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
| for (int x = 0; x < pixelRow.Length; x++) | ||
| { | ||
| var lab = new CieLab((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]); | ||
| var rgb = ColorSpaceConverter.ToRgb(lab); | ||
|
|
||
| color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f)); | ||
| pixelRow[x] = color; | ||
|
|
||
| offset++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabTiffColor{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,46 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using System; | ||
| using System.Numerics; | ||
| using SixLabors.ImageSharp.ColorSpaces; | ||
| using SixLabors.ImageSharp.ColorSpaces.Conversion; | ||
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation | ||
| { | ||
| /// <summary> | ||
| /// Implements decoding pixel data with photometric interpretation of type 'CieLab'. | ||
| /// </summary> | ||
| internal class CieLabTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| private static readonly ColorSpaceConverter ColorSpaceConverter = new(); | ||
|
|
||
| private const float Inv255 = 1.0f / 255.0f; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) | ||
| { | ||
| var color = default(TPixel); | ||
| int offset = 0; | ||
| for (int y = top; y < top + height; y++) | ||
| { | ||
| Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); | ||
|
|
||
| for (int x = 0; x < pixelRow.Length; x++) | ||
| { | ||
| float l = (data[offset] & 0xFF) * 100f * Inv255; | ||
| var lab = new CieLab(l, (sbyte)data[offset + 1], (sbyte)data[offset + 2]); | ||
| var rgb = ColorSpaceConverter.ToRgb(lab); | ||
|
|
||
| color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f)); | ||
| pixelRow[x] = color; | ||
|
|
||
| offset += 3; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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
3 changes: 3 additions & 0 deletions
3
...ReferenceOutput/TiffDecoderTests/TiffDecoder_CanDecode_CieLab_Rgba32_CieLab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...nceOutput/TiffDecoderTests/TiffDecoder_CanDecode_CieLab_Rgba32_CieLabPlanar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...derTests/TiffDecoder_CanDecode_CieLab_Rgba32_CieLab_lzwcompressed_predictor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
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.
What's happening here?
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.
The image

Tiff\CieLab.tifflooks wrong when using theMagickReferenceDecoder:It should look like this:

ImageMagick itself can decode this image just fine, that's why I thought we maybe doing something wrong in
MagickReferenceDecoder.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.
Ah right.... Yep,
IMagickImage<T>containsColorSpaceenum which we are blindly ignoring when converting. I always assumed they did the same as us and converted color spaces on decode.It looks like we should be checking that first and converting on the fly.
@dlemstra Am I correct here?