Browse Source

Merge branch 'main' into js/jpeg_JFXX

pull/2482/head
James Jackson-South 3 years ago
committed by GitHub
parent
commit
9f0fba4f05
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
  2. 40
      src/ImageSharp/Formats/Pbm/BinaryEncoder.cs
  3. 20
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6BitReader.cs
  4. 26
      src/ImageSharp/IO/IFileSystem.cs
  5. 20
      src/ImageSharp/IO/LocalFileSystem.cs
  6. 8
      src/ImageSharp/Image.FromFile.cs
  7. 2
      src/ImageSharp/ImageExtensions.cs
  8. 1
      tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
  9. 6
      tests/ImageSharp.Tests/Formats/Pbm/PbmEncoderTests.cs
  10. 109
      tests/ImageSharp.Tests/IO/LocalFileSystemTests.cs
  11. 8
      tests/ImageSharp.Tests/Image/ImageSaveTests.cs
  12. 6
      tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs
  13. 44
      tests/ImageSharp.Tests/TestFileSystem.cs
  14. 1
      tests/ImageSharp.Tests/TestImages.cs
  15. 4
      tests/ImageSharp.Tests/TestUtilities/SingleStreamFileSystem.cs
  16. 3
      tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_issue2477.png
  17. 3
      tests/Images/Input/Pbm/issue2477.pbm

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

@ -152,7 +152,6 @@ internal class BinaryDecoder
{
int width = pixels.Width;
int height = pixels.Height;
int startBit = 0;
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();
@ -162,23 +161,12 @@ internal class BinaryDecoder
for (int x = 0; x < width;)
{
int raw = stream.ReadByte();
int bit = startBit;
startBit = 0;
for (; bit < 8; bit++)
int stopBit = Math.Min(8, width - x);
for (int bit = 0; bit < stopBit; bit++)
{
bool bitValue = (raw & (0x80 >> bit)) != 0;
rowSpan[x] = bitValue ? black : white;
x++;
if (x == width)
{
startBit = (bit + 1) & 7; // Round off to below 8.
if (startBit != 0)
{
stream.Seek(-1, System.IO.SeekOrigin.Current);
}
break;
}
}
}

40
src/ImageSharp/Formats/Pbm/BinaryEncoder.cs

@ -33,10 +33,14 @@ internal class BinaryEncoder
{
WriteGrayscale(configuration, stream, image);
}
else
else if (componentType == PbmComponentType.Short)
{
WriteWideGrayscale(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Grayscale PBM.");
}
}
else if (colorType == PbmColorType.Rgb)
{
@ -44,14 +48,25 @@ internal class BinaryEncoder
{
WriteRgb(configuration, stream, image);
}
else
else if (componentType == PbmComponentType.Short)
{
WriteWideRgb(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Color PBM.");
}
}
else
{
WriteBlackAndWhite(configuration, stream, image);
if (componentType == PbmComponentType.Bit)
{
WriteBlackAndWhite(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Black & White PBM.");
}
}
}
@ -164,8 +179,6 @@ internal class BinaryEncoder
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();
int previousValue = 0;
int startBit = 0;
for (int y = 0; y < height; y++)
{
Span<TPixel> pixelSpan = pixelBuffer.DangerousGetRowSpan(y);
@ -177,8 +190,9 @@ internal class BinaryEncoder
for (int x = 0; x < width;)
{
int value = previousValue;
for (int i = startBit; i < 8; i++)
int value = 0;
int stopBit = Math.Min(8, width - x);
for (int i = 0; i < stopBit; i++)
{
if (rowSpan[x].PackedValue < 128)
{
@ -186,19 +200,9 @@ internal class BinaryEncoder
}
x++;
if (x == width)
{
previousValue = value;
startBit = (i + 1) & 7; // Round off to below 8.
break;
}
}
if (startBit == 0)
{
stream.WriteByte((byte)value);
previousValue = 0;
}
stream.WriteByte((byte)value);
}
}
}

20
src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6BitReader.cs

@ -125,13 +125,29 @@ internal sealed class T6BitReader : T4BitReader
if (value == Len7Code0000000.Code)
{
this.Code = Len7Code0000000;
return false;
// We do not support Extensions1D codes, but some encoders (scanner from epson) write a premature EOL code,
// which at this point cannot be distinguished from the marker, because we read the data bit by bit.
// Read the next 5 bit, if its a EOL code return true, indicating its the end of the image.
if (this.ReadValue(5) == 1)
{
return true;
}
throw new NotSupportedException("ccitt extensions 1D codes are not supported.");
}
if (value == Len7Code0000001.Code)
{
this.Code = Len7Code0000001;
return false;
// Same as above, we do not support Extensions2D codes, but it could be a EOL instead.
if (this.ReadValue(5) == 1)
{
return true;
}
throw new NotSupportedException("ccitt extensions 2D codes are not supported.");
}
if (value == Len7Code0000011.Code)

26
src/ImageSharp/IO/IFileSystem.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.IO;
@ -9,16 +9,32 @@ namespace SixLabors.ImageSharp.IO;
internal interface IFileSystem
{
/// <summary>
/// Returns a readable stream as defined by the path.
/// Opens a file as defined by the path and returns it as a readable stream.
/// </summary>
/// <param name="path">Path to the file to open.</param>
/// <returns>A stream representing the file to open.</returns>
/// <returns>A stream representing the opened file.</returns>
Stream OpenRead(string path);
/// <summary>
/// Creates or opens a file and returns it as a writable stream as defined by the path.
/// Opens a file as defined by the path and returns it as a readable stream
/// that can be used for asynchronous reading.
/// </summary>
/// <param name="path">Path to the file to open.</param>
/// <returns>A stream representing the file to open.</returns>
/// <returns>A stream representing the opened file.</returns>
Stream OpenReadAsynchronous(string path);
/// <summary>
/// Creates or opens a file as defined by the path and returns it as a writable stream.
/// </summary>
/// <param name="path">Path to the file to open.</param>
/// <returns>A stream representing the opened file.</returns>
Stream Create(string path);
/// <summary>
/// Creates or opens a file as defined by the path and returns it as a writable stream
/// that can be used for asynchronous reading and writing.
/// </summary>
/// <param name="path">Path to the file to open.</param>
/// <returns>A stream representing the opened file.</returns>
Stream CreateAsynchronous(string path);
}

20
src/ImageSharp/IO/LocalFileSystem.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.IO;
@ -11,6 +11,24 @@ internal sealed class LocalFileSystem : IFileSystem
/// <inheritdoc/>
public Stream OpenRead(string path) => File.OpenRead(path);
/// <inheritdoc/>
public Stream OpenReadAsynchronous(string path) => File.Open(path, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.Asynchronous,
});
/// <inheritdoc/>
public Stream Create(string path) => File.Create(path);
/// <inheritdoc/>
public Stream CreateAsynchronous(string path) => File.Open(path, new FileStreamOptions
{
Mode = FileMode.Create,
Access = FileAccess.ReadWrite,
Share = FileShare.None,
Options = FileOptions.Asynchronous,
});
}

8
src/ImageSharp/Image.FromFile.cs

@ -72,7 +72,7 @@ public abstract partial class Image
{
Guard.NotNull(options, nameof(options));
using Stream stream = options.Configuration.FileSystem.OpenRead(path);
await using Stream stream = options.Configuration.FileSystem.OpenReadAsynchronous(path);
return await DetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false);
}
@ -144,7 +144,7 @@ public abstract partial class Image
CancellationToken cancellationToken = default)
{
Guard.NotNull(options, nameof(options));
using Stream stream = options.Configuration.FileSystem.OpenRead(path);
await using Stream stream = options.Configuration.FileSystem.OpenReadAsynchronous(path);
return await IdentifyAsync(options, stream, cancellationToken).ConfigureAwait(false);
}
@ -214,7 +214,7 @@ public abstract partial class Image
string path,
CancellationToken cancellationToken = default)
{
using Stream stream = options.Configuration.FileSystem.OpenRead(path);
await using Stream stream = options.Configuration.FileSystem.OpenReadAsynchronous(path);
return await LoadAsync(options, stream, cancellationToken).ConfigureAwait(false);
}
@ -291,7 +291,7 @@ public abstract partial class Image
Guard.NotNull(options, nameof(options));
Guard.NotNull(path, nameof(path));
using Stream stream = options.Configuration.FileSystem.OpenRead(path);
await using Stream stream = options.Configuration.FileSystem.OpenReadAsynchronous(path);
return await LoadAsync<TPixel>(options, stream, cancellationToken).ConfigureAwait(false);
}
}

2
src/ImageSharp/ImageExtensions.cs

@ -70,7 +70,7 @@ public static partial class ImageExtensions
Guard.NotNull(path, nameof(path));
Guard.NotNull(encoder, nameof(encoder));
using Stream fs = source.GetConfiguration().FileSystem.Create(path);
await using Stream fs = source.GetConfiguration().FileSystem.CreateAsynchronous(path);
await source.SaveAsync(fs, encoder, cancellationToken).ConfigureAwait(false);
}

1
tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs

@ -81,6 +81,7 @@ public class PbmDecoderTests
[Theory]
[WithFile(BlackAndWhitePlain, PixelTypes.L8, "pbm")]
[WithFile(BlackAndWhiteBinary, PixelTypes.L8, "pbm")]
[WithFile(Issue2477, PixelTypes.L8, "pbm")]
[WithFile(GrayscalePlain, PixelTypes.L8, "pgm")]
[WithFile(GrayscalePlainNormalized, PixelTypes.L8, "pgm")]
[WithFile(GrayscaleBinary, PixelTypes.L8, "pgm")]

6
tests/ImageSharp.Tests/Formats/Pbm/PbmEncoderTests.cs

@ -26,6 +26,7 @@ public class PbmEncoderTests
{
{ BlackAndWhiteBinary, PbmColorType.BlackAndWhite },
{ BlackAndWhitePlain, PbmColorType.BlackAndWhite },
{ Issue2477, PbmColorType.BlackAndWhite },
{ GrayscaleBinary, PbmColorType.Grayscale },
{ GrayscaleBinaryWide, PbmColorType.Grayscale },
{ GrayscalePlain, PbmColorType.Grayscale },
@ -96,6 +97,11 @@ public class PbmEncoderTests
public void PbmEncoder_P4_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestPbmEncoderCore(provider, PbmColorType.BlackAndWhite, PbmEncoding.Binary);
[Theory]
[WithFile(Issue2477, PixelTypes.Rgb24)]
public void PbmEncoder_P4_Irregular_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestPbmEncoderCore(provider, PbmColorType.BlackAndWhite, PbmEncoding.Binary);
[Theory]
[WithFile(GrayscalePlainMagick, PixelTypes.Rgb24)]
public void PbmEncoder_P2_Works<TPixel>(TestImageProvider<TPixel> provider)

109
tests/ImageSharp.Tests/IO/LocalFileSystemTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.IO;
@ -11,36 +11,113 @@ public class LocalFileSystemTests
public void OpenRead()
{
string path = Path.GetTempFileName();
string testData = Guid.NewGuid().ToString();
File.WriteAllText(path, testData);
try
{
string testData = Guid.NewGuid().ToString();
File.WriteAllText(path, testData);
var fs = new LocalFileSystem();
LocalFileSystem fs = new();
using (var r = new StreamReader(fs.OpenRead(path)))
{
string data = r.ReadToEnd();
using (FileStream stream = (FileStream)fs.OpenRead(path))
using (StreamReader reader = new(stream))
{
Assert.False(stream.IsAsync);
Assert.True(stream.CanRead);
Assert.False(stream.CanWrite);
Assert.Equal(testData, data);
string data = reader.ReadToEnd();
Assert.Equal(testData, data);
}
}
finally
{
File.Delete(path);
}
}
File.Delete(path);
[Fact]
public async Task OpenReadAsynchronous()
{
string path = Path.GetTempFileName();
try
{
string testData = Guid.NewGuid().ToString();
File.WriteAllText(path, testData);
LocalFileSystem fs = new();
await using (FileStream stream = (FileStream)fs.OpenReadAsynchronous(path))
using (StreamReader reader = new(stream))
{
Assert.True(stream.IsAsync);
Assert.True(stream.CanRead);
Assert.False(stream.CanWrite);
string data = await reader.ReadToEndAsync();
Assert.Equal(testData, data);
}
}
finally
{
File.Delete(path);
}
}
[Fact]
public void Create()
{
string path = Path.GetTempFileName();
string testData = Guid.NewGuid().ToString();
var fs = new LocalFileSystem();
try
{
string testData = Guid.NewGuid().ToString();
LocalFileSystem fs = new();
using (FileStream stream = (FileStream)fs.Create(path))
using (StreamWriter writer = new(stream))
{
Assert.False(stream.IsAsync);
Assert.True(stream.CanRead);
Assert.True(stream.CanWrite);
using (var r = new StreamWriter(fs.Create(path)))
writer.Write(testData);
}
string data = File.ReadAllText(path);
Assert.Equal(testData, data);
}
finally
{
r.Write(testData);
File.Delete(path);
}
}
string data = File.ReadAllText(path);
Assert.Equal(testData, data);
[Fact]
public async Task CreateAsynchronous()
{
string path = Path.GetTempFileName();
try
{
string testData = Guid.NewGuid().ToString();
LocalFileSystem fs = new();
await using (FileStream stream = (FileStream)fs.CreateAsynchronous(path))
await using (StreamWriter writer = new(stream))
{
Assert.True(stream.IsAsync);
Assert.True(stream.CanRead);
Assert.True(stream.CanWrite);
await writer.WriteAsync(testData);
}
File.Delete(path);
string data = File.ReadAllText(path);
Assert.Equal(testData, data);
}
finally
{
File.Delete(path);
}
}
}

8
tests/ImageSharp.Tests/Image/ImageSaveTests.cs

@ -44,7 +44,7 @@ public class ImageSaveTests : IDisposable
[Fact]
public void SavePath()
{
var stream = new MemoryStream();
using MemoryStream stream = new();
this.fileSystem.Setup(x => x.Create("path.png")).Returns(stream);
this.image.Save("path.png");
@ -54,7 +54,7 @@ public class ImageSaveTests : IDisposable
[Fact]
public void SavePathWithEncoder()
{
var stream = new MemoryStream();
using MemoryStream stream = new();
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream);
this.image.Save("path.jpg", this.encoderNotInFormat.Object);
@ -73,7 +73,7 @@ public class ImageSaveTests : IDisposable
[Fact]
public void SaveStreamWithMime()
{
var stream = new MemoryStream();
using MemoryStream stream = new();
this.image.Save(stream, this.localImageFormat.Object);
this.encoder.Verify(x => x.Encode(this.image, stream));
@ -82,7 +82,7 @@ public class ImageSaveTests : IDisposable
[Fact]
public void SaveStreamWithEncoder()
{
var stream = new MemoryStream();
using MemoryStream stream = new();
this.image.Save(stream, this.encoderNotInFormat.Object);

6
tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs

@ -122,6 +122,7 @@ public partial class ImageTests
Stream StreamFactory() => this.DataStream;
this.LocalFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(StreamFactory);
this.LocalFileSystemMock.Setup(x => x.OpenReadAsynchronous(this.MockFilePath)).Returns(StreamFactory);
this.topLevelFileSystem.AddFile(this.MockFilePath, StreamFactory);
this.LocalConfiguration.FileSystem = this.LocalFileSystemMock.Object;
this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem;
@ -132,6 +133,11 @@ public partial class ImageTests
// Clean up the global object;
this.localStreamReturnImageRgba32?.Dispose();
this.localStreamReturnImageAgnostic?.Dispose();
if (this.dataStreamLazy.IsValueCreated)
{
this.dataStreamLazy.Value.Dispose();
}
}
protected virtual Stream CreateStream() => this.TestFormat.CreateStream(this.Marker);

44
tests/ImageSharp.Tests/TestFileSystem.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable enable
namespace SixLabors.ImageSharp.Tests;
/// <summary>
@ -8,7 +10,7 @@ namespace SixLabors.ImageSharp.Tests;
/// </summary>
public class TestFileSystem : ImageSharp.IO.IFileSystem
{
private readonly Dictionary<string, Func<Stream>> fileSystem = new Dictionary<string, Func<Stream>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Func<Stream>> fileSystem = new(StringComparer.OrdinalIgnoreCase);
public void AddFile(string path, Func<Stream> data)
{
@ -18,35 +20,39 @@ public class TestFileSystem : ImageSharp.IO.IFileSystem
}
}
public Stream Create(string path)
public Stream Create(string path) => this.GetStream(path) ?? File.Create(path);
public Stream CreateAsynchronous(string path) => this.GetStream(path) ?? File.Open(path, new FileStreamOptions
{
// if we have injected a fake file use it instead
lock (this.fileSystem)
{
if (this.fileSystem.ContainsKey(path))
{
Stream stream = this.fileSystem[path]();
stream.Position = 0;
return stream;
}
}
Mode = FileMode.Create,
Access = FileAccess.ReadWrite,
Share = FileShare.None,
Options = FileOptions.Asynchronous,
});
return File.Create(path);
}
public Stream OpenRead(string path) => this.GetStream(path) ?? File.OpenRead(path);
public Stream OpenReadAsynchronous(string path) => this.GetStream(path) ?? File.Open(path, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.Asynchronous,
});
public Stream OpenRead(string path)
private Stream? GetStream(string path)
{
// if we have injected a fake file use it instead
lock (this.fileSystem)
{
if (this.fileSystem.ContainsKey(path))
if (this.fileSystem.TryGetValue(path, out Func<Stream>? streamFactory))
{
Stream stream = this.fileSystem[path]();
Stream stream = streamFactory();
stream.Position = 0;
return stream;
}
}
return File.OpenRead(path);
return null;
}
}

1
tests/ImageSharp.Tests/TestImages.cs

@ -1039,5 +1039,6 @@ public static class TestImages
public const string RgbPlain = "Pbm/rgb_plain.ppm";
public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm";
public const string RgbPlainMagick = "Pbm/rgb_plain_magick.ppm";
public const string Issue2477 = "Pbm/issue2477.pbm";
}
}

4
tests/ImageSharp.Tests/TestUtilities/SingleStreamFileSystem.cs

@ -13,5 +13,9 @@ internal class SingleStreamFileSystem : IFileSystem
Stream IFileSystem.Create(string path) => this.stream;
Stream IFileSystem.CreateAsynchronous(string path) => this.stream;
Stream IFileSystem.OpenRead(string path) => this.stream;
Stream IFileSystem.OpenReadAsynchronous(string path) => this.stream;
}

3
tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_issue2477.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:670bc844ba878afa0f03574dea23ab774ac0cc5aa371d0f4b4dff7da4d32f916
size 2912

3
tests/Images/Input/Pbm/issue2477.pbm

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d625635f7be760fbea935056c0f6d046832dd74bba33a1597b52ab3dfe0c5e4e
size 4956
Loading…
Cancel
Save