1010
1111namespace SixLabors . ImageSharp . Formats . Webp . Lossless ;
1212
13- internal sealed unsafe class Vp8LHistogram : IDisposable
13+ internal abstract unsafe class Vp8LHistogram
1414{
1515 private const uint NonTrivialSym = 0xffffffff ;
16- private readonly IMemoryOwner < uint > ? bufferOwner ;
17- private readonly Memory < uint > buffer ;
18- private readonly MemoryHandle bufferHandle ;
19-
2016 private readonly uint * red ;
2117 private readonly uint * blue ;
2218 private readonly uint * alpha ;
@@ -35,32 +31,21 @@ internal sealed unsafe class Vp8LHistogram : IDisposable
3531 /// <summary>
3632 /// Initializes a new instance of the <see cref="Vp8LHistogram"/> class.
3733 /// </summary>
38- /// <remarks>
39- /// This constructor should be used when the histogram is a member of a <see cref="Vp8LHistogramSet"/>.
40- /// </remarks>
41- /// <param name="buffer">The backing buffer.</param>
34+ /// <param name="basePointer">The base pointer to the backing memory.</param>
4235 /// <param name="refs">The backward references to initialize the histogram with.</param>
4336 /// <param name="paletteCodeBits">The palette code bits.</param>
44- public Vp8LHistogram ( Memory < uint > buffer , Vp8LBackwardRefs refs , int paletteCodeBits )
45- : this ( buffer , paletteCodeBits ) => this . StoreRefs ( refs ) ;
37+ protected Vp8LHistogram ( uint * basePointer , Vp8LBackwardRefs refs , int paletteCodeBits )
38+ : this ( basePointer , paletteCodeBits ) => this . StoreRefs ( refs ) ;
4639
4740 /// <summary>
4841 /// Initializes a new instance of the <see cref="Vp8LHistogram"/> class.
4942 /// </summary>
50- /// <remarks>
51- /// This constructor should be used when the histogram is a member of a <see cref="Vp8LHistogramSet"/>.
52- /// </remarks>
53- /// <param name="buffer">The backing buffer.</param>
43+ /// <param name="basePointer">The base pointer to the backing memory.</param>
5444 /// <param name="paletteCodeBits">The palette code bits.</param>
55- /// <param name="bufferOwner">Optional buffer owner to dispose.</param>
56- public Vp8LHistogram ( Memory < uint > buffer , int paletteCodeBits , IMemoryOwner < uint > ? bufferOwner = null )
45+ protected Vp8LHistogram ( uint * basePointer , int paletteCodeBits )
5746 {
58- this . bufferOwner = bufferOwner ;
59- this . buffer = buffer ;
60- this . bufferHandle = this . buffer . Pin ( ) ;
6147 this . PaletteCodeBits = paletteCodeBits ;
62-
63- this . red = ( uint * ) this . bufferHandle . Pointer ;
48+ this . red = basePointer ;
6449 this . blue = this . red + RedSize ;
6550 this . alpha = this . blue + BlueSize ;
6651 this . distance = this . alpha + AlphaSize ;
@@ -109,27 +94,6 @@ public Vp8LHistogram(Memory<uint> buffer, int paletteCodeBits, IMemoryOwner<uint
10994
11095 private Span < uint > TotalSpan => new ( this . red , BufferSize ) ;
11196
112- public bool IsDisposed { get ; set ; }
113-
114- /// <summary>
115- /// Creates an <see cref="Vp8LHistogram"/> that is not a member of a <see cref="Vp8LHistogramSet"/>.
116- /// </summary>
117- public static Vp8LHistogram Create ( MemoryAllocator memoryAllocator , int paletteCodeBits )
118- {
119- IMemoryOwner < uint > bufferOwner = memoryAllocator . Allocate < uint > ( BufferSize , AllocationOptions . Clean ) ;
120- return new Vp8LHistogram ( bufferOwner . Memory , paletteCodeBits , bufferOwner ) ;
121- }
122-
123- /// <summary>
124- /// Creates an <see cref="Vp8LHistogram"/> that is not a member of a <see cref="Vp8LHistogramSet"/>.
125- /// </summary>
126- public static Vp8LHistogram Create ( MemoryAllocator memoryAllocator , Vp8LBackwardRefs refs , int paletteCodeBits )
127- {
128- Vp8LHistogram histogram = Create ( memoryAllocator , paletteCodeBits ) ;
129- histogram . StoreRefs ( refs ) ;
130- return histogram ;
131- }
132-
13397 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
13498 public bool IsUsed ( int index ) => this . IsUsedSpan [ index ] == 1u ;
13599
@@ -621,14 +585,57 @@ private static void AddVector(Span<uint> a, Span<uint> b, Span<uint> output, int
621585 }
622586 }
623587 }
588+ }
589+
590+ internal sealed unsafe class OwnedVp8LHistogram : Vp8LHistogram , IDisposable
591+ {
592+ private readonly IMemoryOwner < uint > bufferOwner ;
593+ private MemoryHandle bufferHandle ;
594+ private bool isDisposed ;
595+
596+ private OwnedVp8LHistogram (
597+ IMemoryOwner < uint > bufferOwner ,
598+ ref MemoryHandle bufferHandle ,
599+ uint * basePointer ,
600+ int paletteCodeBits )
601+ : base ( basePointer , paletteCodeBits )
602+ {
603+ this . bufferOwner = bufferOwner ;
604+ this . bufferHandle = bufferHandle ;
605+ }
606+
607+ /// <summary>
608+ /// Creates an <see cref="OwnedVp8LHistogram"/> that is not a member of a <see cref="Vp8LHistogramSet"/>.
609+ /// </summary>
610+ /// <param name="memoryAllocator">The memory allocator.</param>
611+ /// <param name="paletteCodeBits">The palette code bits.</param>
612+ public static OwnedVp8LHistogram Create ( MemoryAllocator memoryAllocator , int paletteCodeBits )
613+ {
614+ IMemoryOwner < uint > bufferOwner = memoryAllocator . Allocate < uint > ( BufferSize , AllocationOptions . Clean ) ;
615+ MemoryHandle bufferHandle = bufferOwner . Memory . Pin ( ) ;
616+ return new OwnedVp8LHistogram ( bufferOwner , ref bufferHandle , ( uint * ) bufferHandle . Pointer , paletteCodeBits ) ;
617+ }
618+
619+ /// <summary>
620+ /// Creates an <see cref="OwnedVp8LHistogram"/> that is not a member of a <see cref="Vp8LHistogramSet"/>.
621+ /// </summary>
622+ /// <param name="memoryAllocator">The memory allocator.</param>
623+ /// <param name="refs">The backward references to initialize the histogram with.</param>
624+ /// <param name="paletteCodeBits">The palette code bits.</param>
625+ public static OwnedVp8LHistogram Create ( MemoryAllocator memoryAllocator , Vp8LBackwardRefs refs , int paletteCodeBits )
626+ {
627+ OwnedVp8LHistogram histogram = Create ( memoryAllocator , paletteCodeBits ) ;
628+ histogram . StoreRefs ( refs ) ;
629+ return histogram ;
630+ }
624631
625632 public void Dispose ( )
626633 {
627- if ( ! this . IsDisposed )
634+ if ( ! this . isDisposed )
628635 {
629636 this . bufferHandle . Dispose ( ) ;
630- this . bufferOwner ? . Dispose ( ) ;
631- this . IsDisposed = true ;
637+ this . bufferOwner . Dispose ( ) ;
638+ this . isDisposed = true ;
632639 }
633640 }
634641}
0 commit comments