Skip to content

Commit 493e422

Browse files
Merge pull request #1286 from SixLabors/js/fix-1276
Make stream buffer size configurable.
2 parents ef5e21b + 6aa6472 commit 493e422

File tree

20 files changed

+273
-148
lines changed

20 files changed

+273
-148
lines changed

.github/CONTRIBUTING.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# How to contribute to ImageSharp
1+
# How to contribute to SixLabors.ImageSharp
22

33
#### **Did you find a bug?**
44

@@ -12,11 +12,11 @@
1212

1313
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
1414

15-
* 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:.
15+
* 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:.
1616

1717
#### **Do you intend to add a new feature or change an existing one?**
1818

19-
* Suggest your change in the [ImageSharp Gitter Chat Room](https://gitter.im/ImageSharp/General) and start writing code.
19+
* Suggest your change in the [Ideas Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas) and start writing code.
2020

2121
* 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.
2222

@@ -33,14 +33,12 @@
3333

3434
#### **Do you have questions about consuming the library or the source code?**
3535

36-
* Ask any question about how to use ImageSharp over in the [discussions section](https://github.com/SixLabors/ImageSharp/discussions).
36+
* Ask any question about how to use SixLabors.ImageSharp in the [Help Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp).
3737

3838
#### Code of Conduct
3939
This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community.
4040
For more information, see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).
4141

42-
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.
42+
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.
4343

4444
Thanks for reading!
45-
46-
James Jackson-South :heart:

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
blank_issues_enabled: false
22
contact_links:
33
- name: Ask a Question
4-
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331980
4+
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp
55
about: Ask a question about this project.
66
- name: Feature Request
7-
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331981
7+
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas
88
about: Share ideas for new features for this project.

src/ImageSharp/Configuration.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
7-
using System.Net.Http;
87
using SixLabors.ImageSharp.Formats;
98
using SixLabors.ImageSharp.Formats.Bmp;
109
using SixLabors.ImageSharp.Formats.Gif;
@@ -26,7 +25,8 @@ public sealed class Configuration
2625
/// A lazily initialized configuration default instance.
2726
/// </summary>
2827
private static readonly Lazy<Configuration> Lazy = new Lazy<Configuration>(CreateDefaultInstance);
29-
28+
private const int DefaultStreamProcessingBufferSize = 8096;
29+
private int streamProcessingBufferSize = DefaultStreamProcessingBufferSize;
3030
private int maxDegreeOfParallelism = Environment.ProcessorCount;
3131

3232
/// <summary>
@@ -75,6 +75,24 @@ public int MaxDegreeOfParallelism
7575
}
7676
}
7777

78+
/// <summary>
79+
/// Gets or sets the size of the buffer to use when working with streams.
80+
/// Intitialized with <see cref="DefaultStreamProcessingBufferSize"/> by default.
81+
/// </summary>
82+
public int StreamProcessingBufferSize
83+
{
84+
get => this.streamProcessingBufferSize;
85+
set
86+
{
87+
if (value <= 0)
88+
{
89+
throw new ArgumentOutOfRangeException(nameof(this.StreamProcessingBufferSize));
90+
}
91+
92+
this.streamProcessingBufferSize = value;
93+
}
94+
}
95+
7896
/// <summary>
7997
/// Gets a set of properties for the Congiguration.
8098
/// </summary>
@@ -145,6 +163,7 @@ public Configuration Clone()
145163
return new Configuration
146164
{
147165
MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
166+
StreamProcessingBufferSize = this.StreamProcessingBufferSize,
148167
ImageFormatsManager = this.ImageFormatsManager,
149168
MemoryAllocator = this.MemoryAllocator,
150169
ImageOperationsProvider = this.ImageOperationsProvider,

src/ImageSharp/Formats/Bmp/BmpDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3939

4040
try
4141
{
42-
using var bufferedStream = new BufferedReadStream(stream);
42+
using var bufferedStream = new BufferedReadStream(configuration, stream);
4343
return decoder.Decode<TPixel>(bufferedStream);
4444
}
4545
catch (InvalidMemoryOperationException ex)
@@ -64,7 +64,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
6464

6565
try
6666
{
67-
using var bufferedStream = new BufferedReadStream(stream);
67+
using var bufferedStream = new BufferedReadStream(configuration, stream);
6868
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
6969
}
7070
catch (InvalidMemoryOperationException ex)
@@ -84,7 +84,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
8484
{
8585
Guard.NotNull(stream, nameof(stream));
8686

87-
using var bufferedStream = new BufferedReadStream(stream);
87+
using var bufferedStream = new BufferedReadStream(configuration, stream);
8888
return new BmpDecoderCore(configuration, this).Identify(bufferedStream);
8989
}
9090

@@ -93,7 +93,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
9393
{
9494
Guard.NotNull(stream, nameof(stream));
9595

96-
using var bufferedStream = new BufferedReadStream(stream);
96+
using var bufferedStream = new BufferedReadStream(configuration, stream);
9797
return new BmpDecoderCore(configuration, this).IdentifyAsync(bufferedStream);
9898
}
9999
}

src/ImageSharp/Formats/Gif/GifDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3333

3434
try
3535
{
36-
using var bufferedStream = new BufferedReadStream(stream);
36+
using var bufferedStream = new BufferedReadStream(configuration, stream);
3737
return decoder.Decode<TPixel>(bufferedStream);
3838
}
3939
catch (InvalidMemoryOperationException ex)
@@ -59,7 +59,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
5959

6060
try
6161
{
62-
using var bufferedStream = new BufferedReadStream(stream);
62+
using var bufferedStream = new BufferedReadStream(configuration, stream);
6363
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
6464
}
6565
catch (InvalidMemoryOperationException ex)
@@ -84,7 +84,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
8484

8585
var decoder = new GifDecoderCore(configuration, this);
8686

87-
using var bufferedStream = new BufferedReadStream(stream);
87+
using var bufferedStream = new BufferedReadStream(configuration, stream);
8888
return decoder.Identify(bufferedStream);
8989
}
9090

@@ -95,7 +95,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
9595

9696
var decoder = new GifDecoderCore(configuration, this);
9797

98-
using var bufferedStream = new BufferedReadStream(stream);
98+
using var bufferedStream = new BufferedReadStream(configuration, stream);
9999
return decoder.IdentifyAsync(bufferedStream);
100100
}
101101
}

src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
2626
using var decoder = new JpegDecoderCore(configuration, this);
2727
try
2828
{
29-
using var bufferedStream = new BufferedReadStream(stream);
29+
using var bufferedStream = new BufferedReadStream(configuration, stream);
3030
return decoder.Decode<TPixel>(bufferedStream);
3131
}
3232
catch (InvalidMemoryOperationException ex)
@@ -53,7 +53,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
5353
using var decoder = new JpegDecoderCore(configuration, this);
5454
try
5555
{
56-
using var bufferedStream = new BufferedReadStream(stream);
56+
using var bufferedStream = new BufferedReadStream(configuration, stream);
5757
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
5858
}
5959
catch (InvalidMemoryOperationException ex)
@@ -77,7 +77,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
7777
Guard.NotNull(stream, nameof(stream));
7878

7979
using var decoder = new JpegDecoderCore(configuration, this);
80-
using var bufferedStream = new BufferedReadStream(stream);
80+
using var bufferedStream = new BufferedReadStream(configuration, stream);
8181

8282
return decoder.Identify(bufferedStream);
8383
}
@@ -88,7 +88,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
8888
Guard.NotNull(stream, nameof(stream));
8989

9090
using var decoder = new JpegDecoderCore(configuration, this);
91-
using var bufferedStream = new BufferedReadStream(stream);
91+
using var bufferedStream = new BufferedReadStream(configuration, stream);
9292

9393
return decoder.IdentifyAsync(bufferedStream);
9494
}

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ private void ProcessApplicationHeaderMarker(BufferedReadStream stream, int remai
514514
// TODO: thumbnail
515515
if (remaining > 0)
516516
{
517+
if (stream.Position + remaining >= stream.Length)
518+
{
519+
JpegThrowHelper.ThrowInvalidImageContentException("Bad App0 Marker length.");
520+
}
521+
517522
stream.Skip(remaining);
518523
}
519524
}
@@ -533,6 +538,11 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
533538
return;
534539
}
535540

541+
if (stream.Position + remaining >= stream.Length)
542+
{
543+
JpegThrowHelper.ThrowInvalidImageContentException("Bad App1 Marker length.");
544+
}
545+
536546
var profile = new byte[remaining];
537547
stream.Read(profile, 0, remaining);
538548

src/ImageSharp/Formats/Png/PngDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
2525

2626
try
2727
{
28-
using var bufferedStream = new BufferedReadStream(stream);
28+
using var bufferedStream = new BufferedReadStream(configuration, stream);
2929
return decoder.Decode<TPixel>(bufferedStream);
3030
}
3131
catch (InvalidMemoryOperationException ex)
@@ -50,7 +50,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
5050

5151
try
5252
{
53-
using var bufferedStream = new BufferedReadStream(stream);
53+
using var bufferedStream = new BufferedReadStream(configuration, stream);
5454
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
5555
}
5656
catch (InvalidMemoryOperationException ex)
@@ -71,15 +71,15 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
7171
public IImageInfo Identify(Configuration configuration, Stream stream)
7272
{
7373
var decoder = new PngDecoderCore(configuration, this);
74-
using var bufferedStream = new BufferedReadStream(stream);
74+
using var bufferedStream = new BufferedReadStream(configuration, stream);
7575
return decoder.Identify(bufferedStream);
7676
}
7777

7878
/// <inheritdoc/>
7979
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream)
8080
{
8181
var decoder = new PngDecoderCore(configuration, this);
82-
using var bufferedStream = new BufferedReadStream(stream);
82+
using var bufferedStream = new BufferedReadStream(configuration, stream);
8383
return decoder.IdentifyAsync(bufferedStream);
8484
}
8585
}

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111
using System.Text;
12-
using System.Threading.Tasks;
1312
using SixLabors.ImageSharp.Formats.Png.Chunks;
1413
using SixLabors.ImageSharp.Formats.Png.Filters;
1514
using SixLabors.ImageSharp.Formats.Png.Zlib;
@@ -1027,7 +1026,7 @@ private void ReadInternationalTextChunk(PngMetadata metadata, ReadOnlySpan<byte>
10271026
private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, out string value)
10281027
{
10291028
using (var memoryStream = new MemoryStream(compressedData.ToArray()))
1030-
using (var bufferedStream = new BufferedReadStream(memoryStream))
1029+
using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream))
10311030
using (var inflateStream = new ZlibInflateStream(bufferedStream))
10321031
{
10331032
if (!inflateStream.AllocateNewBytes(compressedData.Length, false))

src/ImageSharp/Formats/Tga/TgaDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
2424

2525
try
2626
{
27-
using var bufferedStream = new BufferedReadStream(stream);
27+
using var bufferedStream = new BufferedReadStream(configuration, stream);
2828
return decoder.Decode<TPixel>(bufferedStream);
2929
}
3030
catch (InvalidMemoryOperationException ex)
@@ -52,7 +52,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
5252

5353
try
5454
{
55-
using var bufferedStream = new BufferedReadStream(stream);
55+
using var bufferedStream = new BufferedReadStream(configuration, stream);
5656
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
5757
}
5858
catch (InvalidMemoryOperationException ex)
@@ -75,7 +75,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
7575
{
7676
Guard.NotNull(stream, nameof(stream));
7777

78-
using var bufferedStream = new BufferedReadStream(stream);
78+
using var bufferedStream = new BufferedReadStream(configuration, stream);
7979
return new TgaDecoderCore(configuration, this).Identify(bufferedStream);
8080
}
8181

@@ -84,7 +84,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
8484
{
8585
Guard.NotNull(stream, nameof(stream));
8686

87-
using var bufferedStream = new BufferedReadStream(stream);
87+
using var bufferedStream = new BufferedReadStream(configuration, stream);
8888
return new TgaDecoderCore(configuration, this).IdentifyAsync(bufferedStream);
8989
}
9090
}

0 commit comments

Comments
 (0)