Skip to content

Commit 71547c4

Browse files
Merge pull request #1095 from SixLabors/js/expose-pixel-operations
Expose Pixel Operations and Configuration
2 parents 966dd46 + 6c4050f commit 71547c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+884
-878
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SixLabors.ImageSharp
99
<div align="center">
1010

1111
[![Build Status](https://img.shields.io/github/workflow/status/SixLabors/ImageSharp/Build/master)](https://github.com/SixLabors/ImageSharp/actions)
12+
[![Code coverage](https://codecov.io/gh/SixLabors/ImageSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SixLabors/ImageSharp)
1213
[![GitHub license](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/SixLabors/ImageSharp/master/LICENSE)
1314
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ImageSharp/General?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1415
[![Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=flat&logo=twitter)](https://twitter.com/intent/tweet?hashtags=imagesharp,dotnet,oss&text=ImageSharp.+A+new+cross-platform+2D+graphics+API+in+C%23&url=https%3a%2f%2fgithub.com%2fSixLabors%2fImageSharp&via=sixlabors)
@@ -46,13 +47,6 @@ The **ImageSharp** library is made up of multiple packages:
4647
- Transform methods like Resize, Crop, Skew, Rotate - anything that alters the dimensions of the image
4748
- Non-transform methods like Gaussian Blur, Pixelate, Edge Detection - anything that maintains the original image dimensions
4849

49-
<!--
50-
### Build Status
51-
52-
|Build Status|Code Coverage|
53-
|:----------:|:-----------:|
54-
|[![Build Status](https://img.shields.io/github/workflow/status/SixLabors/ImageSharp/Build/master)](https://github.com/SixLabors/ImageSharp/actions)|[![Code coverage](https://codecov.io/gh/SixLabors/ImageSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SixLabors/ImageSharp)|
55-
-->
5650
### Questions?
5751

5852
- Do you have questions? We are happy to help! Please [join our gitter channel](https://gitter.im/ImageSharp/General), or ask them on [stackoverflow](https://stackoverflow.com) using the `ImageSharp` tag. **Do not** open issues for questions!

src/ImageSharp/Advanced/AdvancedImageExtensions.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ public static void AcceptVisitor(this Image source, IImageVisitor visitor)
2929
/// <param name="source">The source image.</param>
3030
/// <returns>Returns the configuration.</returns>
3131
public static Configuration GetConfiguration(this Image source)
32-
=> GetConfiguration((IConfigurable)source);
32+
=> GetConfiguration((IConfigurationProvider)source);
33+
34+
/// <summary>
35+
/// Gets the configuration for the image frame.
36+
/// </summary>
37+
/// <param name="source">The source image.</param>
38+
/// <returns>Returns the configuration.</returns>
39+
public static Configuration GetConfiguration(this ImageFrame source)
40+
=> GetConfiguration((IConfigurationProvider)source);
41+
42+
/// <summary>
43+
/// Gets the configuration .
44+
/// </summary>
45+
/// <param name="source">The source image</param>
46+
/// <returns>Returns the bounds of the image</returns>
47+
private static Configuration GetConfiguration(IConfigurationProvider source)
48+
=> source?.Configuration ?? Configuration.Default;
3349

3450
/// <summary>
3551
/// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory in the source image's pixel format
@@ -158,17 +174,9 @@ internal static Memory<TPixel> GetPixelRowMemory<TPixel>(this Image<TPixel> sour
158174
/// </summary>
159175
/// <param name="source">The source image.</param>
160176
/// <returns>Returns the configuration.</returns>
161-
internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source)
177+
internal static MemoryAllocator GetMemoryAllocator(this IConfigurationProvider source)
162178
=> GetConfiguration(source).MemoryAllocator;
163179

164-
/// <summary>
165-
/// Gets the configuration.
166-
/// </summary>
167-
/// <param name="source">The source image</param>
168-
/// <returns>Returns the bounds of the image</returns>
169-
private static Configuration GetConfiguration(IConfigurable source)
170-
=> source?.Configuration ?? Configuration.Default;
171-
172180
/// <summary>
173181
/// Returns a reference to the 0th element of the Pixel buffer.
174182
/// Such a reference can be used for pinning but must never be dereferenced.

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private static void Seed<TPixel>()
109109
private static void AotCompileOctreeQuantizer<TPixel>()
110110
where TPixel : struct, IPixel<TPixel>
111111
{
112-
using (var test = new OctreeFrameQuantizer<TPixel>(new OctreeQuantizer(false)))
112+
using (var test = new OctreeFrameQuantizer<TPixel>(Configuration.Default, new OctreeQuantizer(false)))
113113
{
114114
test.AotGetPalette();
115115
}
@@ -122,7 +122,7 @@ private static void AotCompileOctreeQuantizer<TPixel>()
122122
private static void AotCompileWuQuantizer<TPixel>()
123123
where TPixel : struct, IPixel<TPixel>
124124
{
125-
using (var test = new WuFrameQuantizer<TPixel>(Configuration.Default.MemoryAllocator, new WuQuantizer(false)))
125+
using (var test = new WuFrameQuantizer<TPixel>(Configuration.Default, new WuQuantizer(false)))
126126
{
127127
test.QuantizeFrame(new ImageFrame<TPixel>(Configuration.Default, 1, 1));
128128
test.AotGetPalette();

src/ImageSharp/Advanced/IConfigurable.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Advanced
5+
{
6+
/// <summary>
7+
/// Defines the contract for objects that can provide access to configuration.
8+
/// </summary>
9+
internal interface IConfigurationProvider
10+
{
11+
/// <summary>
12+
/// Gets the configuration which allows altering default behaviour or extending the library.
13+
/// </summary>
14+
Configuration Configuration { get; }
15+
}
16+
}

src/ImageSharp/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace SixLabors.ImageSharp
1717
{
1818
/// <summary>
19-
/// Provides configuration code which allows altering default behaviour or extending the library.
19+
/// Provides configuration which allows altering default behaviour or extending the library.
2020
/// </summary>
2121
public sealed class Configuration
2222
{

src/ImageSharp/Formats/Gif/GifEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed class GifEncoder : IImageEncoder, IGifEncoderOptions
2828
public void Encode<TPixel>(Image<TPixel> image, Stream stream)
2929
where TPixel : struct, IPixel<TPixel>
3030
{
31-
var encoder = new GifEncoderCore(image.GetConfiguration().MemoryAllocator, this);
31+
var encoder = new GifEncoderCore(image.GetConfiguration(), this);
3232
encoder.Encode(image, stream);
3333
}
3434
}

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ internal sealed class GifEncoderCore
5353
/// <summary>
5454
/// Initializes a new instance of the <see cref="GifEncoderCore"/> class.
5555
/// </summary>
56-
/// <param name="memoryAllocator">The <see cref="MemoryAllocator"/> to use for buffer allocations.</param>
56+
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
5757
/// <param name="options">The options for the encoder.</param>
58-
public GifEncoderCore(MemoryAllocator memoryAllocator, IGifEncoderOptions options)
58+
public GifEncoderCore(Configuration configuration, IGifEncoderOptions options)
5959
{
60-
this.memoryAllocator = memoryAllocator;
60+
this.configuration = configuration;
61+
this.memoryAllocator = configuration.MemoryAllocator;
6162
this.quantizer = options.Quantizer;
6263
this.colorTableMode = options.ColorTableMode;
6364
}
@@ -74,16 +75,14 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
7475
Guard.NotNull(image, nameof(image));
7576
Guard.NotNull(stream, nameof(stream));
7677

77-
this.configuration = image.GetConfiguration();
78-
7978
ImageMetadata metadata = image.Metadata;
8079
GifMetadata gifMetadata = metadata.GetGifMetadata();
81-
this.colorTableMode = this.colorTableMode ?? gifMetadata.ColorTableMode;
80+
this.colorTableMode ??= gifMetadata.ColorTableMode;
8281
bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global;
8382

8483
// Quantize the image returning a palette.
8584
IQuantizedFrame<TPixel> quantized;
86-
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(image.GetConfiguration()))
85+
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration))
8786
{
8887
quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame);
8988
}
@@ -146,7 +145,7 @@ private void EncodeGlobal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> q
146145
else
147146
{
148147
using (IFrameQuantizer<TPixel> paletteFrameQuantizer =
149-
new PaletteFrameQuantizer<TPixel>(this.quantizer.Diffuser, quantized.Palette))
148+
new PaletteFrameQuantizer<TPixel>(this.configuration, this.quantizer.Diffuser, quantized.Palette))
150149
{
151150
using (IQuantizedFrame<TPixel> paletteQuantized = paletteFrameQuantizer.QuantizeFrame(frame))
152151
{
@@ -172,14 +171,14 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> qu
172171
if (previousFrame != null && previousMeta.ColorTableLength != frameMetadata.ColorTableLength
173172
&& frameMetadata.ColorTableLength > 0)
174173
{
175-
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(image.GetConfiguration(), frameMetadata.ColorTableLength))
174+
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration, frameMetadata.ColorTableLength))
176175
{
177176
quantized = frameQuantizer.QuantizeFrame(frame);
178177
}
179178
}
180179
else
181180
{
182-
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(image.GetConfiguration()))
181+
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration))
183182
{
184183
quantized = frameQuantizer.QuantizeFrame(frame);
185184
}
@@ -202,9 +201,7 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> qu
202201
/// <summary>
203202
/// Returns the index of the most transparent color in the palette.
204203
/// </summary>
205-
/// <param name="quantized">
206-
/// The quantized.
207-
/// </param>
204+
/// <param name="quantized">The quantized frame.</param>
208205
/// <typeparam name="TPixel">The pixel format.</typeparam>
209206
/// <returns>
210207
/// The <see cref="int"/>.

src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
55
using System.Runtime.CompilerServices;
6-
6+
using SixLabors.ImageSharp.Advanced;
77
using SixLabors.ImageSharp.PixelFormats;
88

99
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
@@ -60,7 +60,7 @@ public void Convert(ImageFrame<TPixel> frame, int x, int y)
6060
this.pixelBlock.LoadAndStretchEdges(frame, x, y);
6161

6262
Span<Rgb24> rgbSpan = this.rgbBlock.AsSpanUnsafe();
63-
PixelOperations<TPixel>.Instance.ToRgb24(frame.Configuration, this.pixelBlock.AsSpanUnsafe(), rgbSpan);
63+
PixelOperations<TPixel>.Instance.ToRgb24(frame.GetConfiguration(), this.pixelBlock.AsSpanUnsafe(), rgbSpan);
6464

6565
ref float yBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Y);
6666
ref float cbBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Cb);
@@ -81,4 +81,4 @@ ref Unsafe.Add(ref cbBlockStart, i),
8181
}
8282
}
8383
}
84-
}
84+
}

src/ImageSharp/Image.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ namespace SixLabors.ImageSharp
1616
/// For the non-generic <see cref="Image"/> type, the pixel type is only known at runtime.
1717
/// <see cref="Image"/> is always implemented by a pixel-specific <see cref="Image{TPixel}"/> instance.
1818
/// </summary>
19-
public abstract partial class Image : IImage, IConfigurable
19+
public abstract partial class Image : IImage, IConfigurationProvider
2020
{
2121
private Size size;
22+
private readonly Configuration configuration;
2223

2324
/// <summary>
2425
/// Initializes a new instance of the <see cref="Image"/> class.
2526
/// </summary>
26-
/// <param name="configuration">The <see cref="Configuration"/>.</param>
27+
/// <param name="configuration">
28+
/// The configuration which allows altering default behaviour or extending the library.
29+
/// </param>
2730
/// <param name="pixelType">The <see cref="PixelTypeInfo"/>.</param>
2831
/// <param name="metadata">The <see cref="ImageMetadata"/>.</param>
2932
/// <param name="size">The <see cref="size"/>.</param>
3033
protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size)
3134
{
32-
this.Configuration = configuration ?? Configuration.Default;
35+
this.configuration = configuration ?? Configuration.Default;
3336
this.PixelType = pixelType;
3437
this.size = size;
3538
this.Metadata = metadata ?? new ImageMetadata();
@@ -48,11 +51,6 @@ internal Image(
4851
{
4952
}
5053

51-
/// <summary>
52-
/// Gets the <see cref="Configuration"/>.
53-
/// </summary>
54-
protected Configuration Configuration { get; }
55-
5654
/// <summary>
5755
/// Gets the <see cref="ImageFrameCollection"/> implementing the public <see cref="Frames"/> property.
5856
/// </summary>
@@ -75,10 +73,8 @@ internal Image(
7573
/// </summary>
7674
public ImageFrameCollection Frames => this.NonGenericFrameCollection;
7775

78-
/// <summary>
79-
/// Gets the pixel buffer.
80-
/// </summary>
81-
Configuration IConfigurable.Configuration => this.Configuration;
76+
/// <inheritdoc/>
77+
Configuration IConfigurationProvider.Configuration => this.configuration;
8278

8379
/// <inheritdoc />
8480
public void Dispose()
@@ -108,7 +104,7 @@ public void Save(Stream stream, IImageEncoder encoder)
108104
/// <typeparam name="TPixel2">The pixel format.</typeparam>
109105
/// <returns>The <see cref="Image{TPixel2}"/></returns>
110106
public Image<TPixel2> CloneAs<TPixel2>()
111-
where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.Configuration);
107+
where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.GetConfiguration());
112108

113109
/// <summary>
114110
/// Returns a copy of the image in the given pixel format.

0 commit comments

Comments
 (0)