From d93bc6cca2383095c2a08db85861b88a1c94c43c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 14 Oct 2023 21:51:10 +0200 Subject: [PATCH] Follow up on post-merge discussions in #2551 (#2552) --- src/ImageSharp/Formats/Pbm/BinaryDecoder.cs | 8 ++--- .../Pbm/BufferedReadStreamExtensions.cs | 4 +-- src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs | 14 ++++---- src/ImageSharp/Formats/Pbm/PlainDecoder.cs | 36 +++++++++---------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs index 96564df0ff..ce7e379fc5 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs @@ -71,7 +71,7 @@ private static void ProcessGrayscale(Configuration configuration, Buffer for (int y = 0; y < height; y++) { - if (stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) < rowSpan.Length) { return; } @@ -97,7 +97,7 @@ private static void ProcessWideGrayscale(Configuration configuration, Bu for (int y = 0; y < height; y++) { - if (stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) < rowSpan.Length) { return; } @@ -123,7 +123,7 @@ private static void ProcessRgb(Configuration configuration, Buffer2D(Configuration configuration, Buffer2D for (int y = 0; y < height; y++) { - if (stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) < rowSpan.Length) { return; } diff --git a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs index 8fd1d797d0..3b0e41a02d 100644 --- a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs +++ b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs @@ -15,7 +15,7 @@ internal static class BufferedReadStreamExtensions /// /// The buffered read stream. /// if EOF has been reached while reading the stream; see langword="true"/> otherwise. - public static bool TrySkipWhitespaceAndComments(this BufferedReadStream stream) + public static bool SkipWhitespaceAndComments(this BufferedReadStream stream) { bool isWhitespace; do @@ -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. /// - public static bool TryReadDecimal(this BufferedReadStream stream, out int value) + public static bool ReadDecimal(this BufferedReadStream stream, out int value) { value = 0; while (true) diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs index 84d0acb1b4..3fe339865b 100644 --- a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs @@ -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(); } @@ -171,7 +171,7 @@ private void ProcessHeader(BufferedReadStream stream) this.componentType = PbmComponentType.Byte; } - stream.TrySkipWhitespaceAndComments(); + stream.SkipWhitespaceAndComments(); } else { diff --git a/src/ImageSharp/Formats/Pbm/PlainDecoder.cs b/src/ImageSharp/Formats/Pbm/PlainDecoder.cs index 5a01c9a8af..8748d90fa8 100644 --- a/src/ImageSharp/Formats/Pbm/PlainDecoder.cs +++ b/src/ImageSharp/Formats/Pbm/PlainDecoder.cs @@ -70,9 +70,9 @@ private static void ProcessGrayscale(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; @@ -106,9 +106,9 @@ private static void ProcessWideGrayscale(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; @@ -142,20 +142,20 @@ private static void ProcessRgb(Configuration configuration, Buffer2D(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; @@ -236,10 +236,10 @@ private static void ProcessBlackAndWhite(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;