Skip to content

Instantly share code, notes, and snippets.

@antonfirsov
Last active December 11, 2016 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonfirsov/a254a1d9be86a65fc12fc9ab9783c8fa to your computer and use it in GitHub Desktop.
Save antonfirsov/a254a1d9be86a65fc12fc9ab9783c8fa to your computer and use it in GitHub Desktop.
public interface IBytePacker<TColor>
where TColor: struct, IPackedPixel<TPacked>
{
// Better with Span<TColor> and Span<byte> if possible!
// Even better with a sourceRectangle parameter instead of sourceOffset
void PackBytes(TColor* source, byte[] dest, int destOffset, int count, ComponentOrder componentOrder);
}
#if SOLUTION_1
// Image with 3 Type parameters:
public abstract class ImageBase<TColor, TPacked, TBytePacker> : IImageBase<TColor, TPacked>
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct,
where TBytePacker : IBytePacker<TColor>
{
// returns a packer instance:
public TBytePacker Packer => new TPacker();
}
#endif // SOLUTION_1
#if SOLUTION_2
// TColor implements IBytePacker
// Image has only 2 Type parameters, but confusing
public abstract class ImageBase<TColor, TPacked, TBytePacker> : IImageBase<TColor, TPacked>
where TColor : struct, IPackedPixel<TPacked>, IBytePacker<TColor>
where TPacked : struct
{
// returns a packer instance:
public TColor Packer => new TColor();
}
#endif SOLUTION_2
// Sample implementation:
public struct Bgr565 : IPackedPixel<ushort>, IEquatable<Bgr565>
#if SOLUTION_2
,IBytePacker<Bgr565>
#endif
{
...
#if SOLUTION_1
public struct Packer : IBytePacker<Bgr565>
{
// This struct contains no members only, the implementation
void PackBytes(Bgr565* source, byte[] dest, int destOffset, int count, ComponentOrder componentOrder) {}
}
#endif
#if SOLUTION_2
void PackBytes(Bgr565* source, byte[] dest, int destOffset, int count, ComponentOrder componentOrder) {}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment