-
-
Notifications
You must be signed in to change notification settings - Fork 886
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
Changes from 5 commits
0b7705c
bd2b067
485571a
d850aee
bf4c0dc
7c8feca
9165efe
9930010
e3704ef
e4e7a5b
a258c47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| 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 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 = this.colorSpaceConverter.ToRgb(lab); | ||
|
|
||
| color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f)); | ||
| pixelRow[x] = color; | ||
|
|
||
| offset++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| 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 readonly ColorSpaceConverter colorSpaceConverter = new(); | ||
brianpopow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 = this.colorSpaceConverter.ToRgb(lab); | ||
|
|
||
| color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f)); | ||
| pixelRow[x] = color; | ||
|
|
||
| offset += 3; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,6 +318,20 @@ public void TiffDecoder_CanDecode_YCbCr_24Bit<TPixel>(TestImageProvider<TPixel> | |
| image.CompareToReferenceOutput(ImageComparer.Exact, provider); | ||
| } | ||
|
|
||
| [Theory] | ||
| [WithFile(CieLab, PixelTypes.Rgba32)] | ||
| [WithFile(CieLabPlanar, PixelTypes.Rgba32)] | ||
| [WithFile(CieLabLzwPredictor, PixelTypes.Rgba32)] | ||
| public void TiffDecoder_CanDecode_CieLab<TPixel>(TestImageProvider<TPixel> provider) | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| // Note: The image from MagickReferenceDecoder does not look right, maybe we are doing something wrong | ||
| // converting the pixel data from Magick.NET to our format with CieLab? | ||
|
Member
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. What's happening here?
Collaborator
Author
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.
Member
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. Ah right.... Yep,
It looks like we should be checking that first and converting on the fly. @dlemstra Am I correct here? |
||
| using Image<TPixel> image = provider.GetImage(); | ||
| image.DebugSave(provider); | ||
| image.CompareToReferenceOutput(ImageComparer.Exact, provider); | ||
| } | ||
|
|
||
| [Theory] | ||
| [WithFile(FlowerRgb101010Contiguous, PixelTypes.Rgba32)] | ||
| [WithFile(FlowerRgb101010Planar, PixelTypes.Rgba32)] | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.