Browse Source

Merge remote-tracking branch 'origin/master' into af/cancellation

pull/1574/head
Anton Firszov 6 years ago
parent
commit
d28d7d7077
  1. 12
      .github/CONTRIBUTING.md
  2. 4
      .github/ISSUE_TEMPLATE/config.yml
  3. 10
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  4. 5
      src/ImageSharp/IO/BufferedReadStream.cs
  5. 223
      tests/ImageSharp.Tests/IO/BufferedReadStreamTests.cs

12
.github/CONTRIBUTING.md

@ -1,4 +1,4 @@
# How to contribute to ImageSharp
# How to contribute to SixLabors.ImageSharp
#### **Did you find a bug?**
@ -12,11 +12,11 @@
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
* Before submitting, please ensure that your code matches the existing coding patterns and practise as demonstrated in the repository. These follow strict Stylecop rules :cop:.
* Before submitting, please ensure that your code matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules :cop:.
#### **Do you intend to add a new feature or change an existing one?**
* Suggest your change in the [ImageSharp Gitter Chat Room](https://gitter.im/ImageSharp/General) and start writing code.
* Suggest your change in the [Ideas Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas) and start writing code.
* Do not open an issue on GitHub until you have collected positive feedback about the change. GitHub issues are primarily intended for bug reports and fixes.
@ -33,14 +33,12 @@
#### **Do you have questions about consuming the library or the source code?**
* Ask any question about how to use ImageSharp over in the [discussions section](https://github.com/SixLabors/ImageSharp/discussions).
* Ask any question about how to use SixLabors.ImageSharp in the [Help Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp).
#### Code of Conduct
This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community.
For more information, see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).
And please remember. ImageSharp is the work of a very, very, small number of developers who struggle balancing time to contribute to the project with family time and work commitments. We encourage you to pitch in and help make our vision of simple accessible imageprocessing available to all. Open Source can only exist with your help.
And please remember. SixLabors.ImageSharp is the work of a very, very, small number of developers who struggle balancing time to contribute to the project with family time and work commitments. We encourage you to pitch in and help make our vision of simple accessible image processing available to all. Open Source can only exist with your help.
Thanks for reading!
James Jackson-South :heart:

4
.github/ISSUE_TEMPLATE/config.yml

@ -1,8 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Ask a Question
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331980
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp
about: Ask a question about this project.
- name: Feature Request
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331981
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas
about: Share ideas for new features for this project.

10
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -521,6 +521,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
// TODO: thumbnail
if (remaining > 0)
{
if (stream.Position + remaining >= stream.Length)
{
JpegThrowHelper.ThrowInvalidImageContentException("Bad App0 Marker length.");
}
stream.Skip(remaining);
}
}
@ -540,6 +545,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return;
}
if (stream.Position + remaining >= stream.Length)
{
JpegThrowHelper.ThrowInvalidImageContentException("Bad App1 Marker length.");
}
var profile = new byte[remaining];
stream.Read(profile, 0, remaining);

5
src/ImageSharp/IO/BufferedReadStream.cs

@ -50,8 +50,8 @@ namespace SixLabors.ImageSharp.IO
}
this.BaseStream = stream;
this.Position = (int)stream.Position;
this.Length = stream.Length;
this.Position = (int)stream.Position;
this.BufferSize = configuration.StreamProcessingBufferSize;
this.maxBufferIndex = this.BufferSize - 1;
this.readBuffer = ArrayPool<byte>.Shared.Rent(this.BufferSize);
@ -86,6 +86,9 @@ namespace SixLabors.ImageSharp.IO
[MethodImpl(MethodImplOptions.NoInlining)]
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.Position));
Guard.MustBeLessThanOrEqualTo(value, this.Length, nameof(this.Position));
// Only reset readBufferIndex if we are out of bounds of our working buffer
// otherwise we should simply move the value by the diff.
if (this.IsInReadBuffer(value, out long index))

223
tests/ImageSharp.Tests/IO/BufferedReadStreamTests.cs

@ -11,18 +11,27 @@ namespace SixLabors.ImageSharp.Tests.IO
public class BufferedReadStreamTests
{
private readonly Configuration configuration;
private readonly int bufferSize;
public BufferedReadStreamTests()
{
this.configuration = Configuration.Default;
this.bufferSize = this.configuration.StreamProcessingBufferSize;
this.configuration = Configuration.CreateDefaultInstance();
}
[Fact]
public void BufferedStreamCanReadSingleByteFromOrigin()
public static readonly TheoryData<int> BufferSizes =
new TheoryData<int>()
{
1, 2, 4, 8,
16, 97, 503,
719, 1024,
8096, 64768
};
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadSingleByteFromOrigin(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
byte[] expected = stream.ToArray();
using (var reader = new BufferedReadStream(this.configuration, stream))
@ -30,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Equal(expected[0], reader.ReadByte());
// We've read a whole chunk but increment by 1 in our reader.
Assert.Equal(this.bufferSize, stream.Position);
Assert.True(stream.Position >= bufferSize);
Assert.Equal(1, reader.Position);
}
@ -39,13 +48,15 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Fact]
public void BufferedStreamCanReadSingleByteFromOffset()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadSingleByteFromOffset(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
byte[] expected = stream.ToArray();
const int offset = 5;
int offset = expected.Length / 2;
using (var reader = new BufferedReadStream(this.configuration, stream))
{
reader.Position = offset;
@ -53,7 +64,7 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Equal(expected[offset], reader.ReadByte());
// We've read a whole chunk but increment by 1 in our reader.
Assert.Equal(this.bufferSize + offset, stream.Position);
Assert.Equal(bufferSize + offset, stream.Position);
Assert.Equal(offset + 1, reader.Position);
}
@ -61,10 +72,12 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Fact]
public void BufferedStreamCanReadSubsequentSingleByteCorrectly()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadSubsequentSingleByteCorrectly(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
byte[] expected = stream.ToArray();
int i;
@ -75,19 +88,19 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Equal(expected[i], reader.ReadByte());
Assert.Equal(i + 1, reader.Position);
if (i < this.bufferSize)
if (i < bufferSize)
{
Assert.Equal(stream.Position, this.bufferSize);
Assert.Equal(stream.Position, bufferSize);
}
else if (i >= this.bufferSize && i < this.bufferSize * 2)
else if (i >= bufferSize && i < bufferSize * 2)
{
// We should have advanced to the second chunk now.
Assert.Equal(stream.Position, this.bufferSize * 2);
Assert.Equal(stream.Position, bufferSize * 2);
}
else
{
// We should have advanced to the third chunk now.
Assert.Equal(stream.Position, this.bufferSize * 3);
Assert.Equal(stream.Position, bufferSize * 3);
}
}
}
@ -96,10 +109,12 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Fact]
public void BufferedStreamCanReadMultipleBytesFromOrigin()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadMultipleBytesFromOrigin(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
var buffer = new byte[2];
byte[] expected = stream.ToArray();
@ -110,95 +125,127 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Equal(expected[1], buffer[1]);
// We've read a whole chunk but increment by the buffer length in our reader.
Assert.Equal(stream.Position, this.bufferSize);
Assert.True(stream.Position >= bufferSize);
Assert.Equal(buffer.Length, reader.Position);
}
}
}
[Fact]
public void BufferedStreamCanReadSubsequentMultipleByteCorrectly()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadSubsequentMultipleByteCorrectly(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
const int increment = 2;
var buffer = new byte[2];
byte[] expected = stream.ToArray();
using (var reader = new BufferedReadStream(this.configuration, stream))
{
for (int i = 0, o = 0; i < expected.Length / 2; i++, o += 2)
for (int i = 0, o = 0; i < expected.Length / increment; i++, o += increment)
{
Assert.Equal(2, reader.Read(buffer, 0, 2));
// Check values are correct.
Assert.Equal(increment, reader.Read(buffer, 0, increment));
Assert.Equal(expected[o], buffer[0]);
Assert.Equal(expected[o + 1], buffer[1]);
Assert.Equal(o + 2, reader.Position);
Assert.Equal(o + increment, reader.Position);
int offset = i * 2;
if (offset < this.bufferSize)
{
Assert.Equal(stream.Position, this.bufferSize);
}
else if (offset >= this.bufferSize && offset < this.bufferSize * 2)
// These tests ensure that we are correctly reading
// our buffer in chunks of the given size.
int offset = i * increment;
// First chunk.
if (offset < bufferSize)
{
// We should have advanced to the second chunk now.
Assert.Equal(stream.Position, this.bufferSize * 2);
// We've read an entire chunk once and are
// now reading from that chunk.
Assert.True(stream.Position >= bufferSize);
continue;
}
else
// Second chunk
if (offset < bufferSize * 2)
{
// We should have advanced to the third chunk now.
Assert.Equal(stream.Position, this.bufferSize * 3);
Assert.True(stream.Position > bufferSize);
// Odd buffer size with even increments can
// jump to the third chunk on final read.
Assert.True(stream.Position <= bufferSize * 3);
continue;
}
// Third chunk
Assert.True(stream.Position > bufferSize * 2);
}
}
}
}
[Fact]
public void BufferedStreamCanReadSubsequentMultipleByteSpanCorrectly()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanReadSubsequentMultipleByteSpanCorrectly(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
const int increment = 2;
Span<byte> buffer = new byte[2];
byte[] expected = stream.ToArray();
using (var reader = new BufferedReadStream(this.configuration, stream))
{
for (int i = 0, o = 0; i < expected.Length / 2; i++, o += 2)
for (int i = 0, o = 0; i < expected.Length / increment; i++, o += increment)
{
Assert.Equal(2, reader.Read(buffer, 0, 2));
// Check values are correct.
Assert.Equal(increment, reader.Read(buffer, 0, increment));
Assert.Equal(expected[o], buffer[0]);
Assert.Equal(expected[o + 1], buffer[1]);
Assert.Equal(o + 2, reader.Position);
Assert.Equal(o + increment, reader.Position);
int offset = i * 2;
if (offset < this.bufferSize)
{
Assert.Equal(stream.Position, this.bufferSize);
}
else if (offset >= this.bufferSize && offset < this.bufferSize * 2)
// These tests ensure that we are correctly reading
// our buffer in chunks of the given size.
int offset = i * increment;
// First chunk.
if (offset < bufferSize)
{
// We should have advanced to the second chunk now.
Assert.Equal(stream.Position, this.bufferSize * 2);
// We've read an entire chunk once and are
// now reading from that chunk.
Assert.True(stream.Position >= bufferSize);
continue;
}
else
// Second chunk
if (offset < bufferSize * 2)
{
// We should have advanced to the third chunk now.
Assert.Equal(stream.Position, this.bufferSize * 3);
Assert.True(stream.Position > bufferSize);
// Odd buffer size with even increments can
// jump to the third chunk on final read.
Assert.True(stream.Position <= bufferSize * 3);
continue;
}
// Third chunk
Assert.True(stream.Position > bufferSize * 2);
}
}
}
}
[Fact]
public void BufferedStreamCanSkip()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamCanSkip(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 4))
{
byte[] expected = stream.ToArray();
using (var reader = new BufferedReadStream(this.configuration, stream))
{
int skip = 50;
int skip = 1;
int plusOne = 1;
int skip2 = this.bufferSize;
int skip2 = bufferSize;
// Skip
reader.Skip(skip);
@ -221,14 +268,17 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Fact]
public void BufferedStreamReadsSmallStream()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamReadsSmallStream(int bufferSize)
{
this.configuration.StreamProcessingBufferSize = bufferSize;
// Create a stream smaller than the default buffer length
using (MemoryStream stream = this.CreateTestStream(this.bufferSize / 4))
using (MemoryStream stream = this.CreateTestStream(Math.Max(1, bufferSize / 4)))
{
byte[] expected = stream.ToArray();
const int offset = 5;
int offset = expected.Length / 2;
using (var reader = new BufferedReadStream(this.configuration, stream))
{
reader.Position = offset;
@ -236,7 +286,7 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Equal(expected[offset], reader.ReadByte());
// We've read a whole length of the stream but increment by 1 in our reader.
Assert.Equal(this.bufferSize / 4, stream.Position);
Assert.Equal(Math.Max(1, bufferSize / 4), stream.Position);
Assert.Equal(offset + 1, reader.Position);
}
@ -244,10 +294,12 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Fact]
public void BufferedStreamReadsCanReadAllAsSingleByteFromOrigin()
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamReadsCanReadAllAsSingleByteFromOrigin(int bufferSize)
{
using (MemoryStream stream = this.CreateTestStream(this.bufferSize * 3))
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 3))
{
byte[] expected = stream.ToArray();
using (var reader = new BufferedReadStream(this.configuration, stream))
@ -260,6 +312,35 @@ namespace SixLabors.ImageSharp.Tests.IO
}
}
[Theory]
[MemberData(nameof(BufferSizes))]
public void BufferedStreamThrowsOnBadPosition(int bufferSize)
{
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize))
{
using (var reader = new BufferedReadStream(this.configuration, stream))
{
Assert.Throws<ArgumentOutOfRangeException>(() => reader.Position = -stream.Length);
Assert.Throws<ArgumentOutOfRangeException>(() => reader.Position = stream.Length + 1);
}
}
}
[Fact]
public void BufferedStreamCanSetPositionToEnd()
{
var bufferSize = 8;
this.configuration.StreamProcessingBufferSize = bufferSize;
using (MemoryStream stream = this.CreateTestStream(bufferSize * 2))
{
using (var reader = new BufferedReadStream(this.configuration, stream))
{
reader.Position = reader.Length;
}
}
}
private MemoryStream CreateTestStream(int length)
{
var buffer = new byte[length];

Loading…
Cancel
Save