From 5554aa31985b7debbda0e07f2f92e506e23d83f2 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 15:57:17 -0700 Subject: [PATCH] Use Unsafe.As<> --- src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs | 9 +++++---- .../Profiles/ICC/DataReader/IccDataReader.Primitives.cs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs index d3ea9743f..6d473fd4b 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs @@ -6,6 +6,7 @@ using System.Buffers.Binary; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using SixLabors.ImageSharp.IO; @@ -462,7 +463,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif } } - private unsafe double ConvertToDouble(ReadOnlySpan buffer) + private double ConvertToDouble(ReadOnlySpan buffer) { if (buffer.Length < 8) { @@ -473,7 +474,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif ? BinaryPrimitives.ReadInt64BigEndian(buffer) : BinaryPrimitives.ReadInt64LittleEndian(buffer); - return *((double*)&intValue); + return Unsafe.As(ref intValue); } private uint ConvertToUInt32(ReadOnlySpan buffer) @@ -501,7 +502,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif : BinaryPrimitives.ReadUInt16LittleEndian(buffer); } - private unsafe float ConvertToSingle(ReadOnlySpan buffer) + private float ConvertToSingle(ReadOnlySpan buffer) { if (buffer.Length < 4) { @@ -512,7 +513,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif ? BinaryPrimitives.ReadInt32BigEndian(buffer) : BinaryPrimitives.ReadInt32LittleEndian(buffer); - return *((float*)&intValue); + return Unsafe.As(ref intValue); } private Rational ToRational(ReadOnlySpan buffer) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs index 538a31d6a..5be0060f6 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs @@ -3,6 +3,7 @@ using System; using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Text; namespace SixLabors.ImageSharp.MetaData.Profiles.Icc @@ -70,22 +71,22 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// Reads a float. /// /// the value - public unsafe float ReadSingle() + public float ReadSingle() { int intValue = this.ReadInt32(); - return *((float*)&intValue); + return Unsafe.As(ref intValue); } /// /// Reads a double /// /// the value - public unsafe double ReadDouble() + public double ReadDouble() { long intValue = this.ReadInt64(); - return *((double*)&intValue); + return Unsafe.As(ref intValue); } ///