mirror of https://github.com/SixLabors/ImageSharp
200 changed files with 4803 additions and 5794 deletions
@ -1,6 +1,7 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<RuleSet Name="ImageSharp" ToolsVersion="17.0"> |
|||
<Include Path="..\shared-infrastructure\sixlabors.ruleset" Action="Default" /> |
|||
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"> |
|||
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features"> |
|||
<Rule Id="IDE0290" Action="None" /> |
|||
</Rules> |
|||
</RuleSet> |
|||
</RuleSet> |
|||
@ -0,0 +1,120 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Buffers.Binary; |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.Intrinsics; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Tiff.Utils; |
|||
|
|||
/// <summary>
|
|||
/// Helper methods for TIFF decoding.
|
|||
/// </summary>
|
|||
internal static class TiffUtilities |
|||
{ |
|||
private const float Scale24Bit = 1f / 0xFFFFFF; |
|||
private static readonly Vector4 Scale24BitVector = Vector128.Create(Scale24Bit, Scale24Bit, Scale24Bit, 1f).AsVector4(); |
|||
|
|||
private const float Scale32Bit = 1f / 0xFFFFFFFF; |
|||
private static readonly Vector4 Scale32BitVector = Vector128.Create(Scale32Bit, Scale32Bit, Scale32Bit, 1f).AsVector4(); |
|||
|
|||
public static Rgba64 Rgba64Default { get; } = new(0, 0, 0, 0); |
|||
|
|||
public static L16 L16Default { get; } = new(0); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static ushort ConvertToUShortBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16BigEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static ushort ConvertToUShortLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16LittleEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static uint ConvertToUIntBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32BigEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static uint ConvertToUIntLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32LittleEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromRgba64Premultiplied<TPixel>(ushort r, ushort g, ushort b, ushort a) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
if (a == 0) |
|||
{ |
|||
return TPixel.FromRgba64(default); |
|||
} |
|||
|
|||
return TPixel.FromRgba64(new((ushort)(r / a), (ushort)(g / a), (ushort)(b / a), a)); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(uint r, uint g, uint b) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(r, g, b, 1f) * Scale24BitVector); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(uint r, uint g, uint b, uint a) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(r, g, b, a) * Scale24Bit); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(uint r, uint g, uint b, uint a) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit; |
|||
return UnPremultiply<TPixel>(ref colorVector); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(uint r, uint g, uint b) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(r, g, b, 1f) * Scale32BitVector); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(uint r, uint g, uint b, uint a) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(r, g, b, a) * Scale32Bit); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(uint r, uint g, uint b, uint a) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 vector = new Vector4(r, g, b, a) * Scale32Bit; |
|||
return UnPremultiply<TPixel>(ref vector); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(uint intensity) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale24BitVector); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(uint intensity) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale32BitVector); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel UnPremultiply<TPixel>(ref Vector4 vector) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Numerics.UnPremultiply(ref vector); |
|||
return TPixel.FromScaledVector4(vector); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Finds the padding needed to round 'valueToRoundUp' to the next integer multiple of subSampling value.
|
|||
/// </summary>
|
|||
/// <param name="valueToRoundUp">The width or height to round up.</param>
|
|||
/// <param name="subSampling">The sub sampling.</param>
|
|||
/// <returns>The padding.</returns>
|
|||
public static int PaddingToNextInteger(int valueToRoundUp, int subSampling) |
|||
{ |
|||
if (valueToRoundUp % subSampling == 0) |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
return subSampling - (valueToRoundUp % subSampling); |
|||
} |
|||
} |
|||
@ -1,176 +0,0 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Buffers.Binary; |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Tiff.Utils; |
|||
|
|||
/// <summary>
|
|||
/// Helper methods for TIFF decoding.
|
|||
/// </summary>
|
|||
internal static class TiffUtils |
|||
{ |
|||
private const float Scale24Bit = 1.0f / 0xFFFFFF; |
|||
|
|||
private const float Scale32Bit = 1.0f / 0xFFFFFFFF; |
|||
|
|||
public static Rgba64 Rgba64Default { get; } = new(0, 0, 0, 0); |
|||
|
|||
public static L16 L16Default { get; } = new(0); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static ushort ConvertToUShortBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16BigEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static ushort ConvertToUShortLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16LittleEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static uint ConvertToUIntBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32BigEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static uint ConvertToUIntLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32LittleEndian(buffer); |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromL8<TPixel>(L8 l8, byte intensity, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
l8.PackedValue = intensity; |
|||
color.FromL8(l8); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromRgb64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48); |
|||
color.FromRgba64(rgba); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48); |
|||
color.FromRgba64(rgba); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromRgba64Premultiplied<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48); |
|||
var vec = rgba.ToVector4(); |
|||
return UnPremultiply(ref vec, color); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, 1.0f); |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit; |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit; |
|||
return UnPremultiply(ref colorVector, color); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, 1.0f); |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit; |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit; |
|||
return UnPremultiply(ref colorVector, color); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
l16.PackedValue = intensity; |
|||
color.FromL16(l16); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo24Bit<TPixel>(ulong intensity, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
var colorVector = new Vector4(intensity * Scale24Bit, intensity * Scale24Bit, intensity * Scale24Bit, 1.0f); |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel ColorScaleTo32Bit<TPixel>(ulong intensity, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
var colorVector = new Vector4(intensity * Scale32Bit, intensity * Scale32Bit, intensity * Scale32Bit, 1.0f); |
|||
color.FromScaledVector4(colorVector); |
|||
return color; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static TPixel UnPremultiply<TPixel>(ref Vector4 vector, TPixel color) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Numerics.UnPremultiply(ref vector); |
|||
color.FromScaledVector4(vector); |
|||
|
|||
return color; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Finds the padding needed to round 'valueToRoundUp' to the next integer multiple of subSampling value.
|
|||
/// </summary>
|
|||
/// <param name="valueToRoundUp">The width or height to round up.</param>
|
|||
/// <param name="subSampling">The sub sampling.</param>
|
|||
/// <returns>The padding.</returns>
|
|||
public static int PaddingToNextInteger(int valueToRoundUp, int subSampling) |
|||
{ |
|||
if (valueToRoundUp % subSampling == 0) |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
return subSampling - (valueToRoundUp % subSampling); |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
File diff suppressed because it is too large
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue