Browse Source

Move ReadSingle() to ExrDecoderCore

pull/3096/head
Brian Popow 4 months ago
parent
commit
0aeaaf7759
  1. 47
      src/ImageSharp/Formats/Exr/ExrDecoderCore.cs
  2. 1
      src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
  3. 25
      src/ImageSharp/IO/BufferedReadStream.cs

47
src/ImageSharp/Formats/Exr/ExrDecoderCore.cs

@ -516,14 +516,14 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
lineOrder = (ExrLineOrder)stream.ReadByte();
break;
case ExrConstants.AttributeNames.PixelAspectRatio:
aspectRatio = stream.ReadSingle(this.buffer);
aspectRatio = this.ReadSingle(stream);
break;
case ExrConstants.AttributeNames.ScreenWindowCenter:
screenWindowCenterX = stream.ReadSingle(this.buffer);
screenWindowCenterY = stream.ReadSingle(this.buffer);
screenWindowCenterX = this.ReadSingle(stream);
screenWindowCenterY = this.ReadSingle(stream);
break;
case ExrConstants.AttributeNames.ScreenWindowWidth:
screenWindowWidth = stream.ReadSingle(this.buffer);
screenWindowWidth = this.ReadSingle(stream);
break;
case ExrConstants.AttributeNames.Tiles:
tileXSize = this.ReadUnsignedInteger(stream);
@ -738,39 +738,72 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
return false;
}
/// <summary>
/// Reads a unsigned long value from the stream.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
/// <returns>The unsigned long value.</returns>
private ulong ReadUnsignedLong(BufferedReadStream stream)
{
int bytesRead = stream.Read(this.buffer, 0, 8);
if (bytesRead != 8)
{
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream!");
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to read a unsigned long from the stream!");
}
return BinaryPrimitives.ReadUInt64LittleEndian(this.buffer);
}
/// <summary>
/// Reads a unsigned integer value from the stream.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
/// <returns>The integer value.</returns>
private uint ReadUnsignedInteger(BufferedReadStream stream)
{
int bytesRead = stream.Read(this.buffer, 0, 4);
if (bytesRead != 4)
{
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream!");
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to read a unsigned int from the stream!");
}
return BinaryPrimitives.ReadUInt32LittleEndian(this.buffer);
}
/// <summary>
/// Reads a signed integer value from the stream.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
/// <returns>The integer value.</returns>
private int ReadSignedInteger(BufferedReadStream stream)
{
int bytesRead = stream.Read(this.buffer, 0, 4);
if (bytesRead != 4)
{
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream!");
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to read a signed int from the stream!");
}
return BinaryPrimitives.ReadInt32LittleEndian(this.buffer);
}
/// <summary>
/// Reads a float value from the stream.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
/// <returns>The float value.</returns>
private float ReadSingle(BufferedReadStream stream)
{
int bytesRead = stream.Read(this.buffer, 0, 4);
if (bytesRead != 4)
{
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to read a float value from the stream!");
}
int intValue = BinaryPrimitives.ReadInt32BigEndian(this.buffer);
return Unsafe.As<int, float>(ref intValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(uint r, uint g, uint b, uint a)
where TPixel : unmanaged, IPixel<TPixel>

1
src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Pbm;

25
src/ImageSharp/IO/BufferedReadStream.cs

@ -167,31 +167,6 @@ internal sealed class BufferedReadStream : Stream
}
}
/// <summary>
/// Reads a float value.
/// </summary>
/// <returns>The value.</returns>
public float ReadSingle(byte[] data)
{
int offset = 0;
int bytesToRead = 4;
while (bytesToRead > 0)
{
int bytesRead = this.Read(data, offset, bytesToRead);
if (bytesRead == 0)
{
throw new ImageFormatException("Not enough data to read a float value from the stream");
}
bytesToRead -= bytesRead;
offset += bytesRead;
}
int intValue = BinaryPrimitives.ReadInt32BigEndian(data);
return Unsafe.As<int, float>(ref intValue);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int Read(byte[] buffer, int offset, int count)

Loading…
Cancel
Save