Skip to content

Instantly share code, notes, and snippets.

@antonfirsov
Last active May 1, 2019 11:39
Show Gist options
  • Save antonfirsov/ff48cf3491cedb2c3a5393bfcfb2cca5 to your computer and use it in GitHub Desktop.
Save antonfirsov/ff48cf3491cedb2c3a5393bfcfb2cca5 to your computer and use it in GitHub Desktop.
// No longer a static utility class, but a base class for the pixel-type aware Image<TPixel>.
// Nevertheless, it contains all the static methods, just as before.
public abstract class Image
{
// The generic methods load pixel-aware images:
public static Image<TPixel> Load<TPixel>(....);
// The non-generic methods load pixel-agnostic images:
public static Image Load(...);
}
// For many operations, users still need the pixel-aware variant. This includes:
// - Working with pixel buffers (.GetPixelSpan() etc.)
// - Drawing
public class Image<TPixel> : Image
{
}
public interface IImageProcessingContext
{
}
public interface IImageProcessingContext<TPixel> : IImageProcessingContext
{
}
public static class ProcessingExtensions
{
public static void Mutate(this Image image, Action<IImageProcessingContext> ctx);
public static void Mutate<TPixel>(this Image<TPixel> image, Action<IImageProcessingContext<TPixel>> ctx);
}
public interface IImageProcessor
{
// The bridge method:
IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>();
}
public interface IImageProcessor<TPixel> : IImageProcessor
{
void Apply(Image<TPixel> source, Rectangle sourceRectangle);
}
// The non-generic processor is responsible for:
// - Encapsulating the parameters of the processor
// - Implementing a factory method to create the pixel-specific processor that contains the implementation
public class ResizeProcessor : IImageProcessor
{
public IResampler Resampler { get; }
public bool Compand { get; }
// etc.
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>() => new ResizeProcessor<TPixel>(this);
}
// The generic variant is responsible for the implementation.
// We can keep them internal until the code is mature enough.
internal class ResizeProcessor<TPixel> : IImageProcessor<TPixel>
{
}
// For certain processors there is no pixel-agnostic variant,
// because the user has to be aware of the pixel type.
// (Maybe in the future we can overcome this-)
internal class FillProcessor<TPixel> : IImageProcessor<TPixel>
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment