-
-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2127 from SixLabors/bp/cielab
Add support for decoding tiff images with CieLab color space
- Loading branch information
Showing
17 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabPlanarTiffColor{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,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 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 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 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 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 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 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 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 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 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