mirror of https://github.com/SixLabors/ImageSharp
Browse Source
This is an implementation of field_encodings.h. Note that I avoided implementing EnumValid() and Values() functions, as we have dedicated methods in .NET to do exactly that (Enum.IsDefined, Enum.GetValues)pull/3153/head
4 changed files with 65 additions and 1 deletions
@ -1,7 +1,7 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jxl.IO; |
|||
namespace SixLabors.ImageSharp.Formats.Jxl.Fields; |
|||
|
|||
/// <summary>
|
|||
/// Abstracts enumeration of all fields into a visitor.
|
|||
@ -0,0 +1,21 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jxl.Fields; |
|||
|
|||
internal static class JxlFieldExpressions |
|||
{ |
|||
public static JxlU32Distribution Value(uint value) |
|||
{ |
|||
const uint directConstant = JxlU32Distribution.DirectConstant; |
|||
|
|||
return new(value | directConstant); |
|||
} |
|||
|
|||
public static JxlU32Distribution BitsOffset(uint bits, uint offset) |
|||
=> new(((bits - 1u) & 0x1Fu) + ((offset & 0x3FFFFFFu) << 5)); |
|||
|
|||
public static JxlU32Distribution Bits(uint value) => BitsOffset(value, 0u); |
|||
|
|||
public static int MakeBit(int index) => 1 << index; |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jxl.Fields; |
|||
|
|||
internal struct JxlU32Distribution(uint d) |
|||
{ |
|||
public const uint DirectConstant = 0x80000000u; |
|||
|
|||
public readonly bool IsDirect => (d & DirectConstant) != 0; |
|||
|
|||
public readonly uint Direct => d & (DirectConstant - 1u); |
|||
|
|||
public readonly uint ExtraBits => (d & 0x1Fu) + 1u; |
|||
|
|||
public readonly uint Offset => (d >> 5) & 0x3FFFFFF; |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Diagnostics; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jxl.Fields; |
|||
|
|||
internal readonly struct JxlU32Enc |
|||
{ |
|||
private readonly InlineArray4<JxlU32Distribution> d = default; |
|||
|
|||
public JxlU32Enc(JxlU32Distribution d0, JxlU32Distribution d1, JxlU32Distribution d2, JxlU32Distribution d3) |
|||
{ |
|||
this.d[0] = d0; |
|||
this.d[1] = d1; |
|||
this.d[2] = d2; |
|||
this.d[3] = d3; |
|||
} |
|||
|
|||
public JxlU32Distribution GetDistribution(int selector) |
|||
{ |
|||
Debug.Assert(selector < 4, "Selector out of range"); |
|||
|
|||
return this.d[selector]; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue