-
-
Notifications
You must be signed in to change notification settings - Fork 890
API proposal: Pixel-agnostic Image base class #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
JimBobSquarePants
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah mate, this is really nice. Very neat way to allow exposing the processors without the implementations.
I think we should go with it. 👍
|
|
||
| public abstract partial class Image : IImage, IConfigurable | ||
| { | ||
| protected readonly Configuration configuration; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private
|
|
||
| namespace SixLabors.ImageSharp | ||
| { | ||
| internal interface IImageVisitor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this.
| } | ||
| } | ||
|
|
||
| public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will become Rgb24 yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will delegate into a method of JpegDecoderCore which can then decide what pixel type to use in the resulting Image<T>. For grayscale jpegs we can return Image<Gray8> (if worth).
Designing the code sharing between the specialized (generic) and the "native pixel type" (non-generic) methods is an interesting question. Probably even more challenging for PngDecoder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. Thinking about it shouldn’t be too bad for png either.
| /// <summary> | ||
| /// Gets the target width. | ||
| /// </summary> | ||
| public int Width => this.parameterSource.Width; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These properties can all be private fields now unless you're expecting inheritance.
|
Cool, thanks for checking! This code is not production ready in any way, I just made the fastest possible route to make the POC work. I will start from scratch and/or rebase. |
| /// Adapted from <see href="http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/filter_rcg.c"/> | ||
| /// </summary> | ||
| /// <typeparam name="TPixel">The pixel format.</typeparam> | ||
| internal class ResizeProcessorImplementation<TPixel> : TransformProcessorBase<TPixel> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JimBobSquarePants I wonder whether I should keep the name ResizeProcessor<TPixel> instead. For certain processors there will be no pixel-agnostic variant, therefore it might be better to use a uniform naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer that over Implementation. File name can be ResizeProcessorTPixel.cs I think and pass stylecop naming rules.
This is an API proposal (+POC), communicated in the form of a pull request, not intended for merging. The target branch contains stripped ImageSharp source code, so we can focus on the important changes.
Please, feel free to review it, any feedback is welcome!
The problem
This proposal aims to solve the following issues with the current API:
Image<TPixel>instance. They just want to apply a processor, and resave the image.TPixel,Rgba32is only a noise for them.Image.Load(...)is decoding images into intoImage<Rgba32>in an arbitrary manner. This is sub-optimal in many cases. Eg. jpegs do not have an alpha-channel, therefore it's better to decode them intoImage<Rgb24>. It would be benefitical to solve this in a way that's transparent to the user.The solution
Imagean abstract, pixel-agnostic base class forImage<TPixel>Image.Load(....)overloads return anImageinstead ofImage<Rgba32>Image.Load<TPixel>(....)overloads keep returningImage<TPixel>IImageProcessorandIImageProcessingContextinterfacesIImageProcessor, it's only responsible to encapsulate the parameters, and create the pixel specificIImageProcessor<TPixel>implementation.ImageandIImageProcessingContext(SeeResizeExtensionsin the PR changes).Impact
This is a breaking change, however it's scope is very limited, most of the users should be able to adapt the new API with minimal or no code changes:
varshould be unaffected:Image<Rgba32>class when usingImage.Load(...)could be easily adapted by replacingImage<Rgba32>withImage: