Browse Source

Merge branch 'main' into js/webp-allocations

pull/2546/head
James Jackson-South 2 years ago
committed by GitHub
parent
commit
74a09bfcce
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
  2. 4
      src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
  3. 14
      src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
  4. 36
      src/ImageSharp/Formats/Pbm/PlainDecoder.cs

8
src/ImageSharp/Formats/Pbm/BinaryDecoder.cs

@ -71,7 +71,7 @@ internal class BinaryDecoder
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
if (stream.Read(rowSpan) == 0) if (stream.Read(rowSpan) < rowSpan.Length)
{ {
return; return;
} }
@ -97,7 +97,7 @@ internal class BinaryDecoder
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
if (stream.Read(rowSpan) == 0) if (stream.Read(rowSpan) < rowSpan.Length)
{ {
return; return;
} }
@ -123,7 +123,7 @@ internal class BinaryDecoder
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
if (stream.Read(rowSpan) == 0) if (stream.Read(rowSpan) < rowSpan.Length)
{ {
return; return;
} }
@ -149,7 +149,7 @@ internal class BinaryDecoder
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
if (stream.Read(rowSpan) == 0) if (stream.Read(rowSpan) < rowSpan.Length)
{ {
return; return;
} }

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

@ -15,7 +15,7 @@ internal static class BufferedReadStreamExtensions
/// </summary> /// </summary>
/// <param name="stream">The buffered read stream.</param> /// <param name="stream">The buffered read stream.</param>
/// <returns><see langword="false"/> if EOF has been reached while reading the stream; see langword="true"/> otherwise.</returns> /// <returns><see langword="false"/> if EOF has been reached while reading the stream; see langword="true"/> otherwise.</returns>
public static bool TrySkipWhitespaceAndComments(this BufferedReadStream stream) public static bool SkipWhitespaceAndComments(this BufferedReadStream stream)
{ {
bool isWhitespace; bool isWhitespace;
do do
@ -61,7 +61,7 @@ internal static class BufferedReadStreamExtensions
/// A 'false' return value doesn't mean that the parsing has been failed, since it's possible to reach EOF while reading the last decimal in the file. /// A 'false' return value doesn't mean that the parsing has been failed, since it's possible to reach EOF while reading the last decimal in the file.
/// It's up to the call site to handle such a situation. /// It's up to the call site to handle such a situation.
/// </remarks> /// </remarks>
public static bool TryReadDecimal(this BufferedReadStream stream, out int value) public static bool ReadDecimal(this BufferedReadStream stream, out int value)
{ {
value = 0; value = 0;
while (true) while (true)

14
src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs

@ -146,18 +146,18 @@ internal sealed class PbmDecoderCore : IImageDecoderInternals
throw new InvalidImageContentException("Unknown of not implemented image type encountered."); throw new InvalidImageContentException("Unknown of not implemented image type encountered.");
} }
if (!stream.TrySkipWhitespaceAndComments() || if (!stream.SkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int width) || !stream.ReadDecimal(out int width) ||
!stream.TrySkipWhitespaceAndComments() || !stream.SkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int height) || !stream.ReadDecimal(out int height) ||
!stream.TrySkipWhitespaceAndComments()) !stream.SkipWhitespaceAndComments())
{ {
ThrowPrematureEof(); ThrowPrematureEof();
} }
if (this.colorType != PbmColorType.BlackAndWhite) if (this.colorType != PbmColorType.BlackAndWhite)
{ {
if (!stream.TryReadDecimal(out this.maxPixelValue)) if (!stream.ReadDecimal(out this.maxPixelValue))
{ {
ThrowPrematureEof(); ThrowPrematureEof();
} }
@ -171,7 +171,7 @@ internal sealed class PbmDecoderCore : IImageDecoderInternals
this.componentType = PbmComponentType.Byte; this.componentType = PbmComponentType.Byte;
} }
stream.TrySkipWhitespaceAndComments(); stream.SkipWhitespaceAndComments();
} }
else else
{ {

36
src/ImageSharp/Formats/Pbm/PlainDecoder.cs

@ -70,9 +70,9 @@ internal class PlainDecoder
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
stream.TryReadDecimal(out int value); stream.ReadDecimal(out int value);
rowSpan[x] = new L8((byte)value); rowSpan[x] = new L8((byte)value);
eofReached = !stream.TrySkipWhitespaceAndComments(); eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached) if (eofReached)
{ {
break; break;
@ -106,9 +106,9 @@ internal class PlainDecoder
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
stream.TryReadDecimal(out int value); stream.ReadDecimal(out int value);
rowSpan[x] = new L16((ushort)value); rowSpan[x] = new L16((ushort)value);
eofReached = !stream.TrySkipWhitespaceAndComments(); eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached) if (eofReached)
{ {
break; break;
@ -142,20 +142,20 @@ internal class PlainDecoder
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if (!stream.TryReadDecimal(out int red) || if (!stream.ReadDecimal(out int red) ||
!stream.TrySkipWhitespaceAndComments() || !stream.SkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int green) || !stream.ReadDecimal(out int green) ||
!stream.TrySkipWhitespaceAndComments()) !stream.SkipWhitespaceAndComments())
{ {
// Reached EOF before reading a full RGB value // Reached EOF before reading a full RGB value
eofReached = true; eofReached = true;
break; break;
} }
stream.TryReadDecimal(out int blue); stream.ReadDecimal(out int blue);
rowSpan[x] = new Rgb24((byte)red, (byte)green, (byte)blue); rowSpan[x] = new Rgb24((byte)red, (byte)green, (byte)blue);
eofReached = !stream.TrySkipWhitespaceAndComments(); eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached) if (eofReached)
{ {
break; break;
@ -189,20 +189,20 @@ internal class PlainDecoder
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if (!stream.TryReadDecimal(out int red) || if (!stream.ReadDecimal(out int red) ||
!stream.TrySkipWhitespaceAndComments() || !stream.SkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int green) || !stream.ReadDecimal(out int green) ||
!stream.TrySkipWhitespaceAndComments()) !stream.SkipWhitespaceAndComments())
{ {
// Reached EOF before reading a full RGB value // Reached EOF before reading a full RGB value
eofReached = true; eofReached = true;
break; break;
} }
stream.TryReadDecimal(out int blue); stream.ReadDecimal(out int blue);
rowSpan[x] = new Rgb48((ushort)red, (ushort)green, (ushort)blue); rowSpan[x] = new Rgb48((ushort)red, (ushort)green, (ushort)blue);
eofReached = !stream.TrySkipWhitespaceAndComments(); eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached) if (eofReached)
{ {
break; break;
@ -236,10 +236,10 @@ internal class PlainDecoder
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
stream.TryReadDecimal(out int value); stream.ReadDecimal(out int value);
rowSpan[x] = value == 0 ? White : Black; rowSpan[x] = value == 0 ? White : Black;
eofReached = !stream.TrySkipWhitespaceAndComments(); eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached) if (eofReached)
{ {
break; break;

Loading…
Cancel
Save