Refactored video frame generator to pure function

This commit is contained in:
Dimitri Vranken 2023-02-20 13:46:58 +01:00
parent 9646c440bb
commit 317ba47dd2

View file

@ -37,11 +37,11 @@ public static Extensions.System.Drawing.Common.BitmapVideoFrameWrapper CreateVid
{ {
var bitmap = new Bitmap(w, h, fmt); var bitmap = new Bitmap(w, h, fmt);
SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => foreach (var (x, y, red, green, blue) in GenerateVideoFramePixels(index, w, h, scaleNoise, offset))
{ {
var color = Color.FromArgb(args.red, args.blue, args.green); var color = Color.FromArgb(red, blue, green);
bitmap.SetPixel(args.x, args.y, color); bitmap.SetPixel(x, y, color);
}); }
return new Extensions.System.Drawing.Common.BitmapVideoFrameWrapper(bitmap); return new Extensions.System.Drawing.Common.BitmapVideoFrameWrapper(bitmap);
} }
@ -51,22 +51,16 @@ public static Extensions.SkiaSharp.BitmapVideoFrameWrapper CreateVideoFrame(int
var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque);
using var bitmapCanvas = new SKCanvas(bitmap); using var bitmapCanvas = new SKCanvas(bitmap);
SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => foreach (var (x, y, red, green, blue) in GenerateVideoFramePixels(index, w, h, scaleNoise, offset))
{ {
var color = new SKColor(args.red, args.blue, args.green); var color = new SKColor(red, blue, green);
bitmapCanvas.DrawPoint(args.x, args.y, color); bitmapCanvas.DrawPoint(x, y, color);
}); }
return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap); return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap);
} }
private static void SetVideoFramePixels( private static IEnumerable<(int x, int y, byte red, byte green, byte blue)> GenerateVideoFramePixels(int index, int w, int h, float scaleNoise, float offset)
int index,
int w,
int h,
float scaleNoise,
float offset,
Action<(int x, int y, byte red, byte green, byte blue)> setPixel)
{ {
offset = offset * index; offset = offset * index;
@ -81,7 +75,7 @@ private static void SetVideoFramePixels(
var value = (byte)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255); var value = (byte)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255);
setPixel((x, y, (byte)(value * xf), (byte)(value * yf), value)); yield return ((x, y, (byte)(value * xf), (byte)(value * yf), value));
} }
} }
} }