Skip to content

Commit

Permalink
Created settings object
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehonti committed Dec 20, 2023
1 parent f899f06 commit 0f80bdd
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Pinta.Effects/Effects/AddNoiseEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,28 @@ private static ImmutableArray<int> CreateLookup ()
return result.MoveToImmutable ();
}

public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<RectangleI> rois)
private sealed record AddNoiseSettings (
RandomSeed seed,
double coverage,
int dev,
int sat);

private AddNoiseSettings CreateSettings ()
{
RandomSeed seed = Data.Seed;
int intensity = Data.Intensity;
int color_saturation = Data.ColorSaturation;
double coverage = 0.01 * Data.Coverage;
var data = Data;
int intensity = data.Intensity;
int color_saturation = data.ColorSaturation;
return new (
seed: data.Seed,
coverage: 0.01 * data.Coverage,
dev: intensity * intensity / 4,
sat: color_saturation * 4096 / 100
);
}

int dev = intensity * intensity / 4;
int sat = color_saturation * 4096 / 100;
public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<RectangleI> rois)
{
AddNoiseSettings settings = CreateSettings ();

ReadOnlySpan<int> localLookup = lookup.AsSpan ();

Expand All @@ -122,7 +135,7 @@ public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<Re
// Reseed the random number generator for each rectangle being rendered.
// This should produce consistent results regardless of the number of threads
// being used to render the effect, but will change if the effect is tiled differently.
var rand = new Random (seed.GetValueForRegion (rect));
var rand = new Random (settings.seed.GetValueForRegion (rect));

int right = rect.Right;

Expand All @@ -133,7 +146,7 @@ public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<Re

for (int x = rect.Left; x <= right; ++x) {

if (rand.NextDouble () > coverage) {
if (rand.NextDouble () > settings.coverage) {
dst_row[x] = src_row[x];
continue;
}
Expand All @@ -144,16 +157,16 @@ public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<Re

int i = (4899 * _r + 9618 * _g + 1867 * _b) >> 14;

int r = i + (((_r - i) * sat) >> 12);
int g = i + (((_g - i) * sat) >> 12);
int b = i + (((_b - i) * sat) >> 12);
int r = i + (((_r - i) * settings.sat) >> 12);
int g = i + (((_g - i) * settings.sat) >> 12);
int b = i + (((_b - i) * settings.sat) >> 12);

ColorBgra src_pixel = src_row[x];

dst_row[x] = ColorBgra.FromBgra (
b: Utility.ClampToByte (src_pixel.B + ((b * dev + 32768) >> 16)),
g: Utility.ClampToByte (src_pixel.G + ((g * dev + 32768) >> 16)),
r: Utility.ClampToByte (src_pixel.R + ((r * dev + 32768) >> 16)),
b: Utility.ClampToByte (src_pixel.B + ((b * settings.dev + 32768) >> 16)),
g: Utility.ClampToByte (src_pixel.G + ((g * settings.dev + 32768) >> 16)),
r: Utility.ClampToByte (src_pixel.R + ((r * settings.dev + 32768) >> 16)),
a: src_pixel.A
);
}
Expand Down

0 comments on commit 0f80bdd

Please sign in to comment.