From 23388b264a9b13dcffb96131a988707792bf6946 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Thu, 28 Dec 2023 23:00:16 +0100 Subject: [PATCH] Box size predictions --- src/ImageSharp/Formats/Heic/HeicEncoderCore.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Heic/HeicEncoderCore.cs b/src/ImageSharp/Formats/Heic/HeicEncoderCore.cs index e028cde1a6..11bf586a48 100644 --- a/src/ImageSharp/Formats/Heic/HeicEncoderCore.cs +++ b/src/ImageSharp/Formats/Heic/HeicEncoderCore.cs @@ -138,7 +138,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteHandlerBox(AutoExpandingMemory memory, int memoryOffset) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 24); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.hdlr, 0, 0); BinaryPrimitives.WriteUInt32BigEndian(buffer[bytesWritten..], 0); bytesWritten += 4; @@ -155,7 +155,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WritePrimaryItemBox(AutoExpandingMemory memory, int memoryOffset) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 12); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.pitm, 0, 0); BinaryPrimitives.WriteUInt16BigEndian(buffer[bytesWritten..], 1); bytesWritten += 2; @@ -166,7 +166,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteItemInfoBox(AutoExpandingMemory memory, int memoryOffset, List items) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 6 + (items.Count * 9)); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.iinf, 0, 0); BinaryPrimitives.WriteUInt16BigEndian(buffer[bytesWritten..], (ushort)items.Count); bytesWritten += 2; @@ -191,7 +191,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteItemReferenceBox(AutoExpandingMemory memory, int memoryOffset, List links) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 4 + (links.Count << 4)); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.iref, 0, 0); foreach (HeicItemLink link in links) { @@ -255,7 +255,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteSpatialExtentPropertyBox(AutoExpandingMemory memory, int memoryOffset, HeicItem item) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 16); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.ispe); BinaryPrimitives.WriteUInt32BigEndian(buffer[bytesWritten..], (uint)item.Extent.Width); bytesWritten += 4; @@ -268,7 +268,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteItemDataBox(AutoExpandingMemory memory, int memoryOffset) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 10); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.idat); BinaryPrimitives.WriteUInt32BigEndian(buffer, (uint)bytesWritten); @@ -277,7 +277,7 @@ internal sealed class HeicEncoderCore : IImageEncoderInternals private static int WriteItemLocationBox(AutoExpandingMemory memory, int memoryOffset, List items) { - Span buffer = memory.GetSpan(memoryOffset, 100); + Span buffer = memory.GetSpan(memoryOffset, 30 + (items.Count << 3)); int bytesWritten = WriteBoxHeader(buffer, Heic4CharCode.iloc, 1, 0); buffer[bytesWritten++] = 0x44; buffer[bytesWritten++] = 0;