Use BinaryPrimitives

This commit is contained in:
Jason Nelson 2024-03-14 11:34:53 -07:00 committed by BobLd
parent 03fd2832ac
commit 247303983a
2 changed files with 4 additions and 9 deletions

View File

@ -1,6 +1,7 @@
namespace UglyToad.PdfPig.Images.Png
{
using System;
using System.Buffers.Binary;
using System.IO;
using System.IO.Compression;
using System.Text;
@ -155,7 +156,7 @@
return false;
}
var length = StreamHelper.ReadBigEndianInt32(headerBytes, 0);
var length = BinaryPrimitives.ReadInt32BigEndian(headerBytes.AsSpan(0, 4));
var name = Encoding.ASCII.GetString(headerBytes, 4, 4);
@ -195,8 +196,8 @@
throw new InvalidOperationException($"Did not read 4 bytes for the CRC, only found: {read}.");
}
var width = StreamHelper.ReadBigEndianInt32(ihdrBytes, 0);
var height = StreamHelper.ReadBigEndianInt32(ihdrBytes, 4);
var width = BinaryPrimitives.ReadInt32BigEndian(ihdrBytes.AsSpan(0, 4));
var height = BinaryPrimitives.ReadInt32BigEndian(ihdrBytes.AsSpan(4, 4));
var bitDepth = ihdrBytes[8];
var colorType = ihdrBytes[9];
var compressionMethod = ihdrBytes[10];

View File

@ -11,12 +11,6 @@
+ (ReadOrTerminate(stream) << 8) + ReadOrTerminate(stream);
}
public static int ReadBigEndianInt32(byte[] bytes, int offset)
{
return (bytes[0 + offset] << 24) + (bytes[1 + offset] << 16)
+ (bytes[2 + offset] << 8) + bytes[3 + offset];
}
public static void WriteBigEndianInt32(Stream stream, int value)
{
stream.WriteByte((byte)(value >> 24));