|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Buffers; |
| 6 | +using System.Numerics; |
| 7 | +using SixLabors.ImageSharp.ColorSpaces; |
| 8 | +using SixLabors.ImageSharp.ColorSpaces.Conversion; |
| 9 | +using SixLabors.ImageSharp.Memory; |
| 10 | +using SixLabors.ImageSharp.PixelFormats; |
| 11 | + |
| 12 | +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Implements decoding pixel data with photometric interpretation of type 'CieLab' with the planar configuration. |
| 16 | + /// </summary> |
| 17 | + internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> |
| 18 | + where TPixel : unmanaged, IPixel<TPixel> |
| 19 | + { |
| 20 | + private static readonly ColorSpaceConverter ColorSpaceConverter = new(); |
| 21 | + |
| 22 | + private const float Inv255 = 1.0f / 255.0f; |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
| 26 | + { |
| 27 | + Span<byte> l = data[0].GetSpan(); |
| 28 | + Span<byte> a = data[1].GetSpan(); |
| 29 | + Span<byte> b = data[2].GetSpan(); |
| 30 | + |
| 31 | + var color = default(TPixel); |
| 32 | + int offset = 0; |
| 33 | + for (int y = top; y < top + height; y++) |
| 34 | + { |
| 35 | + Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); |
| 36 | + for (int x = 0; x < pixelRow.Length; x++) |
| 37 | + { |
| 38 | + var lab = new CieLab((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]); |
| 39 | + var rgb = ColorSpaceConverter.ToRgb(lab); |
| 40 | + |
| 41 | + color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f)); |
| 42 | + pixelRow[x] = color; |
| 43 | + |
| 44 | + offset++; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments