Skip to content

Commit

Permalink
Merge pull request #791 from b-editor/change-blur-sigma-type
Browse files Browse the repository at this point in the history
Sigmaプロパティの型を変更
  • Loading branch information
yuto-trd authored Oct 8, 2023
2 parents 9589861 + 387f60d commit bad5aa0
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 53 deletions.
11 changes: 11 additions & 0 deletions src/Beutl.Engine/Animation/AnimationSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json.Nodes;

using Beutl.Serialization;
using Beutl.Serialization.Migration;

namespace Beutl.Animation;

Expand All @@ -25,6 +26,11 @@ internal static class AnimationSerializer
{
if (json is JsonObject obj)
{
if (Migration_ChangeSigmaType.ShouldMigrate(property))
{
Migration_ChangeSigmaType.Update(json);
}

if (obj.TryGetDiscriminator(out Type? type)
&& Activator.CreateInstance(type, property) is IAnimation animation)
{
Expand Down Expand Up @@ -83,6 +89,11 @@ internal static class AnimationSerializer
{
if (json is JsonObject obj)
{
if (Migration_ChangeSigmaType.ShouldMigrate(property))
{
Migration_ChangeSigmaType.Update(json);
}

if (obj.TryGetDiscriminator(out Type? type)
&& Activator.CreateInstance(type, property) is IAnimation animation)
{
Expand Down
62 changes: 55 additions & 7 deletions src/Beutl.Engine/Graphics/FilterEffects/Blur.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Nodes;

using Beutl.Language;
using Beutl.Serialization;
using Beutl.Serialization.Migration;

using SkiaSharp;

namespace Beutl.Graphics.Effects;

public sealed class Blur : FilterEffect
{
public static readonly CoreProperty<Vector> SigmaProperty;
private Vector _sigma;
public static readonly CoreProperty<Size> SigmaProperty;
private Size _sigma;

static Blur()
{
SigmaProperty = ConfigureProperty<Vector, Blur>(nameof(Sigma))
SigmaProperty = ConfigureProperty<Size, Blur>(nameof(Sigma))
.Accessor(o => o.Sigma, (o, v) => o.Sigma = v)
.DefaultValue(Vector.Zero)
.DefaultValue(Size.Empty)
.Register();

AffectsRender<Blur>(SigmaProperty);
}

[Display(Name = nameof(Strings.Sigma), ResourceType = typeof(Strings))]
[Range(typeof(Vector), "0,0", "max,max")]
public Vector Sigma
[Range(typeof(Size), "0,0", "max,max")]
public Size Sigma
{
get => _sigma;
set => SetAndRaise(SigmaProperty, ref _sigma, value);
Expand All @@ -36,6 +39,51 @@ public override void ApplyTo(FilterEffectContext context)

public override Rect TransformBounds(Rect bounds)
{
return bounds.Inflate(new Thickness(_sigma.X * 3, _sigma.Y * 3));
return bounds.Inflate(new Thickness(_sigma.Width * 3, _sigma.Height * 3));
}

public override void Deserialize(ICoreSerializationContext context)
{
// Todo: 互換性処理
if (context is IJsonSerializationContext jsonContext)
{
JsonObject json = jsonContext.GetJsonObject();

try
{
JsonNode? animations = json["Animations"] ?? json["animations"];
JsonNode? sigma = animations?[nameof(Sigma)];

if (sigma != null)
{
Migration_ChangeSigmaType.Update(sigma);
}
}
catch
{
}
}

base.Deserialize(context);
}

[ObsoleteSerializationApi]
public override void ReadFromJson(JsonObject json)
{
try
{
JsonNode? animations = json["Animations"] ?? json["animations"];
JsonNode? sigma = animations?[nameof(Sigma)];

if (sigma != null)
{
Migration_ChangeSigmaType.Update(sigma);
}
}
catch
{
}

base.ReadFromJson(json);
}
}
62 changes: 55 additions & 7 deletions src/Beutl.Engine/Graphics/FilterEffects/DropShadow.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Nodes;

using Beutl.Language;
using Beutl.Media;
using Beutl.Serialization;
using Beutl.Serialization.Migration;

namespace Beutl.Graphics.Effects;

public sealed class DropShadow : FilterEffect
{
public static readonly CoreProperty<Point> PositionProperty;
public static readonly CoreProperty<Vector> SigmaProperty;
public static readonly CoreProperty<Size> SigmaProperty;
public static readonly CoreProperty<Color> ColorProperty;
public static readonly CoreProperty<bool> ShadowOnlyProperty;
private Point _position;
private Vector _sigma;
private Size _sigma;
private Color _color;
private bool _shadowOnly;

Expand All @@ -23,9 +26,9 @@ static DropShadow()
.DefaultValue(new Point())
.Register();

SigmaProperty = ConfigureProperty<Vector, DropShadow>(nameof(Sigma))
SigmaProperty = ConfigureProperty<Size, DropShadow>(nameof(Sigma))
.Accessor(o => o.Sigma, (o, v) => o.Sigma = v)
.DefaultValue(Vector.Zero)
.DefaultValue(Size.Empty)
.Register();

ColorProperty = ConfigureProperty<Color, DropShadow>(nameof(Color))
Expand All @@ -49,8 +52,8 @@ public Point Position
}

[Display(Name = nameof(Strings.Sigma), ResourceType = typeof(Strings))]
[Range(typeof(Vector), "0,0", "max,max")]
public Vector Sigma
[Range(typeof(Size), "0,0", "max,max")]
public Size Sigma
{
get => _sigma;
set => SetAndRaise(SigmaProperty, ref _sigma, value);
Expand Down Expand Up @@ -82,8 +85,53 @@ public override Rect TransformBounds(Rect bounds)
{
Rect shadowBounds = bounds
.Translate(_position)
.Inflate(new Thickness(_sigma.X * 3, _sigma.Y * 3));
.Inflate(new Thickness(_sigma.Width * 3, _sigma.Height * 3));

return _shadowOnly ? shadowBounds : bounds.Union(shadowBounds);
}

public override void Deserialize(ICoreSerializationContext context)
{
// Todo: 互換性処理
if (context is IJsonSerializationContext jsonContext)
{
JsonObject json = jsonContext.GetJsonObject();

try
{
JsonNode? animations = json["Animations"] ?? json["animations"];
JsonNode? sigma = animations?[nameof(Sigma)];

if (sigma != null)
{
Migration_ChangeSigmaType.Update(sigma);
}
}
catch
{
}
}

base.Deserialize(context);
}

[ObsoleteSerializationApi]
public override void ReadFromJson(JsonObject json)
{
try
{
JsonNode? animations = json["Animations"] ?? json["animations"];
JsonNode? sigma = animations?[nameof(Sigma)];

if (sigma != null)
{
Migration_ChangeSigmaType.Update(sigma);
}
}
catch
{
}

base.ReadFromJson(json);
}
}
68 changes: 50 additions & 18 deletions src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

using Beutl.Collections.Pooled;
using Beutl.Media;

using Microsoft.Extensions.ObjectPool;

using SkiaSharp;

namespace Beutl.Graphics.Effects;
Expand Down Expand Up @@ -89,43 +91,61 @@ public void AppendSKColorFilter<T>(T data, Func<T, FilterEffectActivator, SKColo
_items.Add(new FEItem_SKColorFilter<T>(data, factory));
}

public void DropShadowOnly(Point position, Vector sigma, Color color)
public void DropShadowOnly(Point position, Size sigma, Color color)
{
AppendSkiaFilter(
data: (position, sigma, color),
factory: static (t, input, _) => SKImageFilter.CreateDropShadowOnly(t.position.X, t.position.Y, t.sigma.X, t.sigma.Y, t.color.ToSKColor(), input),
factory: static (t, input, _) => SKImageFilter.CreateDropShadowOnly(t.position.X, t.position.Y, t.sigma.Width, t.sigma.Height, t.color.ToSKColor(), input),
transformBounds: static (t, bounds) => bounds
.Translate(t.position)
.Inflate(new Thickness(t.sigma.X * 3, t.sigma.Y * 3)));
.Inflate(new Thickness(t.sigma.Width * 3, t.sigma.Height * 3)));
}

public void DropShadow(Point position, Vector sigma, Color color)
[Obsolete("Use DropShadowOnly(Point, Size, Color)")]
public void DropShadowOnly(Point position, Vector sigma, Color color)
{
DropShadowOnly(position, new Size(sigma.X, sigma.Y), color);
}

public void DropShadow(Point position, Size sigma, Color color)
{
AppendSkiaFilter(
data: (position, sigma, color),
factory: static (t, input, _) => SKImageFilter.CreateDropShadow(t.position.X, t.position.Y, t.sigma.X, t.sigma.Y, t.color.ToSKColor(), input),
factory: static (t, input, _) => SKImageFilter.CreateDropShadow(t.position.X, t.position.Y, t.sigma.Width, t.sigma.Height, t.color.ToSKColor(), input),
transformBounds: static (t, bounds) => bounds.Union(bounds
.Translate(t.position)
.Inflate(new Thickness(t.sigma.X * 3, t.sigma.Y * 3))));
.Inflate(new Thickness(t.sigma.Width * 3, t.sigma.Height * 3))));
}

public void Blur(Vector sigma)
[Obsolete("Use DropShadow(Point, Size, Color)")]
public void DropShadow(Point position, Vector sigma, Color color)
{
if (sigma.X < 0)
sigma = sigma.WithX(0);
if (sigma.Y < 0)
sigma = sigma.WithY(0);
DropShadow(position, new Size(sigma.X, sigma.Y), color);
}

public void Blur(Size sigma)
{
if (sigma.Width < 0)
sigma = sigma.WithWidth(0);
if (sigma.Height < 0)
sigma = sigma.WithHeight(0);

AppendSkiaFilter(
data: sigma,
factory: static (sigma, input, _) =>
{
if (sigma.X == 0 && sigma.Y == 0)
if (sigma.Width == 0 && sigma.Height == 0)
return null;
return SKImageFilter.CreateBlur(sigma.X, sigma.Y, input);
return SKImageFilter.CreateBlur(sigma.Width, sigma.Height, input);
},
transformBounds: static (sigma, bounds) => bounds.Inflate(new Thickness(sigma.X * 3, sigma.Y * 3)));
transformBounds: static (sigma, bounds) => bounds.Inflate(new Thickness(sigma.Width * 3, sigma.Height * 3)));
}

[Obsolete("Use Blur(Size)")]
public void Blur(Vector sigma)
{
Blur(new Size(sigma.X, sigma.Y));
}

public void DisplacementMap(
Expand Down Expand Up @@ -154,7 +174,7 @@ public void DisplacementMap(
}

// https://github.com/Shopify/react-native-skia/blob/c7740e30234e6b0a49721ab954c4a848e42d7edb/package/src/dom/nodes/paint/ImageFilters.ts#L25
public void InnerShadow(Point position, Vector sigma, Color color)
public void InnerShadow(Point position, Size sigma, Color color)
{
AppendSkiaFilter(
data: (position, sigma, color),
Expand All @@ -169,7 +189,7 @@ public void InnerShadow(Point position, Vector sigma, Color color)
using var srcOut = SKColorFilter.CreateBlendMode(data.color.ToSKColor(), SKBlendMode.SrcOut);
using var f1 = SKImageFilter.CreateColorFilter(srcOut);
using var f2 = SKImageFilter.CreateOffset(data.position.X, data.position.Y, f1);
using var f3 = SKImageFilter.CreateBlur(data.sigma.X, data.sigma.Y, SKShaderTileMode.Decal, f2);
using var f3 = SKImageFilter.CreateBlur(data.sigma.Width, data.sigma.Height, SKShaderTileMode.Decal, f2);
using var f4 = SKImageFilter.CreateBlendMode(SKBlendMode.SrcIn, sourceAlpha, f3);
using var srcOver = SKImageFilter.CreateBlendMode(SKBlendMode.SrcOver, sourceGraphic, f4);
Expand All @@ -178,7 +198,13 @@ public void InnerShadow(Point position, Vector sigma, Color color)
transformBounds: static (_, bounds) => bounds);
}

public void InnerShadowOnly(Point position, Vector sigma, Color color)
[Obsolete("Use InnerShadow(Point, Size, Color)")]
public void InnerShadow(Point position, Vector sigma, Color color)
{
InnerShadow(position, new Size(sigma.X, sigma.Y), color);
}

public void InnerShadowOnly(Point position, Size sigma, Color color)
{
AppendSkiaFilter(
data: (position, sigma, color),
Expand All @@ -193,12 +219,18 @@ public void InnerShadowOnly(Point position, Vector sigma, Color color)
using var srcOut = SKColorFilter.CreateBlendMode(data.color.ToSKColor(), SKBlendMode.SrcOut);
using var f1 = SKImageFilter.CreateColorFilter(srcOut);
using var f2 = SKImageFilter.CreateOffset(data.position.X, data.position.Y, f1);
using var f3 = SKImageFilter.CreateBlur(data.sigma.X, data.sigma.Y, SKShaderTileMode.Decal, f2);
using var f3 = SKImageFilter.CreateBlur(data.sigma.Width, data.sigma.Height, SKShaderTileMode.Decal, f2);
return SKImageFilter.CreateBlendMode(SKBlendMode.SrcIn, sourceAlpha, f3);
},
transformBounds: static (_, bounds) => bounds);
}

[Obsolete("Use InnerShadowOnly(Point, Size, Color)")]
public void InnerShadowOnly(Point position, Vector sigma, Color color)
{
InnerShadowOnly(position, new Size(sigma.X, sigma.Y), color);
}

public void Transform(Matrix matrix, BitmapInterpolationMode bitmapInterpolationMode)
{
AppendSkiaFilter(
Expand Down
Loading

0 comments on commit bad5aa0

Please sign in to comment.