[Peek]Adds QOI file support (#29919)

* Adds QOI support to Peek

* Reduce allocations on QoiImage

* Add to QOI to Peek's NOTICE as well.

* Ensure file stream is closed after reading QOI
This commit is contained in:
Pedro Lamas
2023-12-18 15:54:17 +00:00
committed by GitHub
parent ee22581913
commit 4c3e5348f0
22 changed files with 294 additions and 205 deletions

View File

@@ -2,7 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers.Binary;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
@@ -77,6 +77,31 @@ namespace Peek.Common.Extensions
return size;
}
public static Size? GetQoiSize(this IFileSystemItem item)
{
Size? size = null;
using (FileStream stream = new FileStream(item.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
{
if (stream.Length >= 12)
{
stream.Position = 4;
using (var reader = new BinaryReader(stream))
{
uint widthValue = BinaryPrimitives.ReadUInt32BigEndian(reader.ReadBytes(4));
uint heightValue = BinaryPrimitives.ReadUInt32BigEndian(reader.ReadBytes(4));
if (widthValue > 0 && heightValue > 0)
{
size = new Size(widthValue, heightValue);
}
}
}
}
return size;
}
public static ulong GetSizeInBytes(this IFileSystemItem item)
{
ulong sizeInBytes = 0;