From b5ce236d1cce4ecf3c645f23e050383c5ac7ff0b Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 5 Jun 2017 23:46:49 +0200 Subject: [PATCH] T4 Rgba32.PixelOperations --- src/ImageSharp/ImageSharp.csproj | 13 +- .../PixelOperations{TPixel}.Generated.cs | 0 .../PixelOperations{TPixel}.Generated.tt | 3 +- .../Rgba32.PixelOperations.Generated.cs | 130 ++++++++++++++++++ .../Rgba32.PixelOperations.Generated.tt | 99 +++++++++++++ .../PixelFormats/Rgba32.PixelOperations.cs | 104 +------------- 6 files changed, 242 insertions(+), 107 deletions(-) rename src/ImageSharp/PixelFormats/{ => Generated}/PixelOperations{TPixel}.Generated.cs (100%) rename src/ImageSharp/PixelFormats/{ => Generated}/PixelOperations{TPixel}.Generated.tt (99%) create mode 100644 src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs create mode 100644 src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 60f88a79c..17f7bf58f 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -55,10 +55,14 @@ TextTemplatingFileGenerator Block8x8F.Generated.cs - + TextTemplatingFileGenerator PixelOperations{TPixel}.Generated.cs + + TextTemplatingFileGenerator + Rgba32.PixelOperations.Generated.cs + @@ -69,10 +73,15 @@ True Block8x8F.Generated.tt - + True True PixelOperations{TPixel}.Generated.tt + + True + True + Rgba32.PixelOperations.Generated.tt + \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.cs b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.cs similarity index 100% rename from src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.cs rename to src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.cs diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt similarity index 99% rename from src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt rename to src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt index 9cff31936..16292489f 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt +++ b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt @@ -10,8 +10,7 @@ <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> -<# - +<# void GenerateToDestFormatMethods(string pixelType) { #> diff --git a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs new file mode 100644 index 000000000..e42c575d8 --- /dev/null +++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs @@ -0,0 +1,130 @@ +// + +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + + using ImageSharp.Memory; + using ImageSharp.PixelFormats; + + /// + /// Provides optimized overrides for bulk operations. + /// + public partial struct Rgba32 + { + internal partial class PixelOperations : PixelOperations + { + + /// + internal override void PackFromRgb24(Span source, Span destPixels, int count) + { + GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); + + ref Rgb24 sourceRef = ref source.DangerousGetPinnableReference(); + ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Rgb24 sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); + Unsafe.As(ref dp) = sp; dp.A = 255; + } + } + + + /// + internal override void ToRgb24(Span sourcePixels, Span dest, int count) + { + GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); + + ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); + ref Rgb24 destRef = ref dest.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgb24 dp = ref Unsafe.Add(ref destRef, i); + dp = Unsafe.As(ref sp); + } + } + + + /// + internal override void PackFromBgr24(Span source, Span destPixels, int count) + { + GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); + + ref Bgr24 sourceRef = ref source.DangerousGetPinnableReference(); + ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); + dp.Bgr = sp; dp.A = 255; + } + } + + + /// + internal override void ToBgr24(Span sourcePixels, Span dest, int count) + { + GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); + + ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); + ref Bgr24 destRef = ref dest.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Bgr24 dp = ref Unsafe.Add(ref destRef, i); + dp = sp.Bgr; + } + } + + + /// + internal override void PackFromBgra32(Span source, Span destPixels, int count) + { + GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); + + ref Bgra32 sourceRef = ref source.DangerousGetPinnableReference(); + ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); + dp = sp.ToRgba32(); + } + } + + + /// + internal override void ToBgra32(Span sourcePixels, Span dest, int count) + { + GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); + + ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); + ref Bgra32 destRef = ref dest.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Bgra32 dp = ref Unsafe.Add(ref destRef, i); + dp = sp.ToBgra32(); + } + } + + + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt new file mode 100644 index 000000000..9c01fa915 --- /dev/null +++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt @@ -0,0 +1,99 @@ +<# +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +#> +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + + void GeneratePackFromMethod(string pixelType, string converterCode) + { + #> + + /// + internal override void PackFrom<#=pixelType#>(Span<<#=pixelType#>> source, Span destPixels, int count) + { + GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); + + ref <#=pixelType#> sourceRef = ref source.DangerousGetPinnableReference(); + ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref <#=pixelType#> sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); + <#=converterCode#> + } + } + + <# + } + + void GenerateConvertToMethod(string pixelType, string converterCode) + { + #> + + /// + internal override void To<#=pixelType#>(Span sourcePixels, Span<<#=pixelType#>> dest, int count) + { + GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); + + ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); + ref <#=pixelType#> destRef = ref dest.DangerousGetPinnableReference(); + + for (int i = 0; i < count; i++) + { + ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); + ref <#=pixelType#> dp = ref Unsafe.Add(ref destRef, i); + <#=converterCode#> + } + } + + <# + } + +#> +// + +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + + using ImageSharp.Memory; + using ImageSharp.PixelFormats; + + /// + /// Provides optimized overrides for bulk operations. + /// + public partial struct Rgba32 + { + internal partial class PixelOperations : PixelOperations + { + <# + GeneratePackFromMethod("Rgb24", "Unsafe.As(ref dp) = sp; dp.A = 255;"); + GenerateConvertToMethod("Rgb24", "dp = Unsafe.As(ref sp);"); + + GeneratePackFromMethod("Bgr24", "dp.Bgr = sp; dp.A = 255;"); + GenerateConvertToMethod("Bgr24", "dp = sp.Bgr;"); + + GeneratePackFromMethod("Bgra32", "dp = sp.ToRgba32();"); + GenerateConvertToMethod("Bgra32", "dp = sp.ToBgra32();"); + #> + + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index f10f0ba27..63e40e9cf 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -21,7 +21,7 @@ namespace ImageSharp /// /// implementation optimized for . /// - internal class PixelOperations : PixelOperations + internal partial class PixelOperations : PixelOperations { /// /// SIMD optimized bulk implementation of @@ -120,43 +120,6 @@ namespace ImageSharp } } - /// - internal override void PackFromRgb24(Span source, Span destPixels, int count) - { - Guard.MustBeSizedAtLeast(source, count, nameof(source)); - Guard.MustBeSizedAtLeast(destPixels, count, nameof(destPixels)); - - ref Rgb24 sourceRef = ref source.DangerousGetPinnableReference(); - ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Rgb24 sp = ref Unsafe.Add(ref sourceRef, i); - ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); - - Unsafe.As(ref dp) = sp; - dp.A = 255; - } - } - - /// - internal override void ToRgb24(Span sourcePixels, Span dest, int count) - { - Guard.MustBeSizedAtLeast(sourcePixels, count, nameof(sourcePixels)); - Guard.MustBeSizedAtLeast(dest, count, nameof(dest)); - - ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); - ref Rgb24 destRef = ref dest.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); - ref Rgb24 dp = ref Unsafe.Add(ref destRef, i); - - dp = Unsafe.As(ref sp); - } - } - /// internal override void PackFromRgba32(Span source, Span destPixels, int count) { @@ -173,71 +136,6 @@ namespace ImageSharp SpanHelper.Copy(sourcePixels, dest, count); } - /// - internal override void PackFromBgr24(Span source, Span destPixels, int count) - { - GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); - - ref Bgr24 sourceRef = ref source.DangerousGetPinnableReference(); - ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); - ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); - dp.Bgr = sp; - dp.A = 255; - } - } - - /// - internal override void ToBgr24(Span sourcePixels, Span dest, int count) - { - GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); - - ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); - ref Bgr24 destRef = ref dest.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); - ref Bgr24 dp = ref Unsafe.Add(ref destRef, i); - dp = sp.Bgr; - } - } - - /// - internal override void PackFromBgra32(Span source, Span destPixels, int count) - { - GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); - - ref Bgra32 sourceRef = ref source.DangerousGetPinnableReference(); - ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); - ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); - dp = sp.ToRgba32(); - } - } - - /// - internal override void ToBgra32(Span sourcePixels, Span dest, int count) - { - GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); - - ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference(); - ref Bgra32 destRef = ref dest.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); - ref Bgra32 dp = ref Unsafe.Add(ref destRef, i); - dp = sp.ToBgra32(); - } - } - /// /// Value type to store -s unpacked into multiple -s. ///