mirror of https://github.com/SixLabors/ImageSharp
13 changed files with 146 additions and 20 deletions
@ -0,0 +1,89 @@ |
|||||
|
// 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 16 bits for each channel.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb242424TiffColor{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 Rgb242424TiffColor(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; |
||||
|
float scale = 1.0f / 0xFFFFFF; |
||||
|
byte[] buffer = new byte[4]; |
||||
|
int bufferStartIdx = this.isBigEndian ? 1 : 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, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
var colorVector = new Vector4(r * scale, g * scale, b * scale, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
|
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(buffer.AsSpan(bufferStartIdx)); |
||||
|
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
var colorVector = new Vector4(r * scale, g * scale, b * scale, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
|
||||
|
pixelRow[x] = color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:c6368a704b0a629239024f6fbfb30723fa317593ef36ddba05d76302530bd974 |
||||
|
size 28568 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:bbb2b4ca6d7eeee4737c6963c99ef68fb6971cf6ccee463427a8246574bc6440 |
||||
|
size 28632 |
||||
Loading…
Reference in new issue