Skip to content

Commit

Permalink
Follow up on post-merge discussions in #2551
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Oct 14, 2023
1 parent d76fe6f commit 45b6c36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void ProcessGrayscale<TPixel>(Configuration configuration, Buffer

for (int y = 0; y < height; y++)
{
if (stream.Read(rowSpan) == 0)
if (stream.Read(rowSpan) < rowSpan.Length)
{
return;
}
Expand All @@ -97,7 +97,7 @@ private static void ProcessWideGrayscale<TPixel>(Configuration configuration, Bu

for (int y = 0; y < height; y++)
{
if (stream.Read(rowSpan) == 0)
if (stream.Read(rowSpan) < rowSpan.Length)
{
return;
}
Expand All @@ -123,7 +123,7 @@ private static void ProcessRgb<TPixel>(Configuration configuration, Buffer2D<TPi

for (int y = 0; y < height; y++)
{
if (stream.Read(rowSpan) == 0)
if (stream.Read(rowSpan) < rowSpan.Length)
{
return;
}
Expand All @@ -149,7 +149,7 @@ private static void ProcessWideRgb<TPixel>(Configuration configuration, Buffer2D

for (int y = 0; y < height; y++)
{
if (stream.Read(rowSpan) == 0)
if (stream.Read(rowSpan) < rowSpan.Length)
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class BufferedReadStreamExtensions
/// </summary>
/// <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>
public static bool TrySkipWhitespaceAndComments(this BufferedReadStream stream)
public static bool SkipWhitespaceAndComments(this BufferedReadStream stream)
{
bool isWhitespace;
do
Expand Down Expand Up @@ -61,7 +61,7 @@ public static bool TrySkipWhitespaceAndComments(this BufferedReadStream stream)
/// 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.
/// </remarks>
public static bool TryReadDecimal(this BufferedReadStream stream, out int value)
public static bool ReadDecimal(this BufferedReadStream stream, out int value)
{
value = 0;
while (true)
Expand Down
14 changes: 7 additions & 7 deletions src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ private void ProcessHeader(BufferedReadStream stream)
throw new InvalidImageContentException("Unknown of not implemented image type encountered.");
}

if (!stream.TrySkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int width) ||
!stream.TrySkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int height) ||
!stream.TrySkipWhitespaceAndComments())
if (!stream.SkipWhitespaceAndComments() ||
!stream.ReadDecimal(out int width) ||
!stream.SkipWhitespaceAndComments() ||
!stream.ReadDecimal(out int height) ||
!stream.SkipWhitespaceAndComments())
{
ThrowPrematureEof();
}

if (this.colorType != PbmColorType.BlackAndWhite)
{
if (!stream.TryReadDecimal(out this.maxPixelValue))
if (!stream.ReadDecimal(out this.maxPixelValue))
{
ThrowPrematureEof();
}
Expand All @@ -171,7 +171,7 @@ private void ProcessHeader(BufferedReadStream stream)
this.componentType = PbmComponentType.Byte;
}

stream.TrySkipWhitespaceAndComments();
stream.SkipWhitespaceAndComments();
}
else
{
Expand Down
36 changes: 18 additions & 18 deletions src/ImageSharp/Formats/Pbm/PlainDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ private static void ProcessGrayscale<TPixel>(Configuration configuration, Buffer
{
for (int x = 0; x < width; x++)
{
stream.TryReadDecimal(out int value);
stream.ReadDecimal(out int value);
rowSpan[x] = new L8((byte)value);
eofReached = !stream.TrySkipWhitespaceAndComments();
eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached)
{
break;
Expand Down Expand Up @@ -106,9 +106,9 @@ private static void ProcessWideGrayscale<TPixel>(Configuration configuration, Bu
{
for (int x = 0; x < width; x++)
{
stream.TryReadDecimal(out int value);
stream.ReadDecimal(out int value);
rowSpan[x] = new L16((ushort)value);
eofReached = !stream.TrySkipWhitespaceAndComments();
eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached)
{
break;
Expand Down Expand Up @@ -142,20 +142,20 @@ private static void ProcessRgb<TPixel>(Configuration configuration, Buffer2D<TPi
{
for (int x = 0; x < width; x++)
{
if (!stream.TryReadDecimal(out int red) ||
!stream.TrySkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int green) ||
!stream.TrySkipWhitespaceAndComments())
if (!stream.ReadDecimal(out int red) ||
!stream.SkipWhitespaceAndComments() ||
!stream.ReadDecimal(out int green) ||
!stream.SkipWhitespaceAndComments())
{
// Reached EOF before reading a full RGB value
eofReached = true;
break;
}

stream.TryReadDecimal(out int blue);
stream.ReadDecimal(out int blue);

rowSpan[x] = new Rgb24((byte)red, (byte)green, (byte)blue);
eofReached = !stream.TrySkipWhitespaceAndComments();
eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached)
{
break;
Expand Down Expand Up @@ -189,20 +189,20 @@ private static void ProcessWideRgb<TPixel>(Configuration configuration, Buffer2D
{
for (int x = 0; x < width; x++)
{
if (!stream.TryReadDecimal(out int red) ||
!stream.TrySkipWhitespaceAndComments() ||
!stream.TryReadDecimal(out int green) ||
!stream.TrySkipWhitespaceAndComments())
if (!stream.ReadDecimal(out int red) ||
!stream.SkipWhitespaceAndComments() ||
!stream.ReadDecimal(out int green) ||
!stream.SkipWhitespaceAndComments())
{
// Reached EOF before reading a full RGB value
eofReached = true;
break;
}

stream.TryReadDecimal(out int blue);
stream.ReadDecimal(out int blue);

rowSpan[x] = new Rgb48((ushort)red, (ushort)green, (ushort)blue);
eofReached = !stream.TrySkipWhitespaceAndComments();
eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached)
{
break;
Expand Down Expand Up @@ -236,10 +236,10 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
{
for (int x = 0; x < width; x++)
{
stream.TryReadDecimal(out int value);
stream.ReadDecimal(out int value);

rowSpan[x] = value == 0 ? White : Black;
eofReached = !stream.TrySkipWhitespaceAndComments();
eofReached = !stream.SkipWhitespaceAndComments();
if (eofReached)
{
break;
Expand Down

0 comments on commit 45b6c36

Please sign in to comment.