Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion osu.Framework/Graphics/BlendingParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public struct BlendingParameters : IEquatable<BlendingParameters>
AlphaEquation = BlendingEquation.Add,
};

// we can't really get an accurate diffrence blend for alphas with the current blending params, this is a close approximation
public static BlendingParameters Difference => new BlendingParameters
{
Source = BlendingType.One,
Destination = BlendingType.One,
SourceAlpha = BlendingType.Zero,
DestinationAlpha = BlendingType.One,
RGBEquation = BlendingEquation.Subtract,
AlphaEquation = BlendingEquation.Add,
};

public static BlendingParameters Additive => new BlendingParameters
{
Source = BlendingType.SrcAlpha,
Expand All @@ -90,8 +101,65 @@ public struct BlendingParameters : IEquatable<BlendingParameters>
AlphaEquation = BlendingEquation.Add,
};

public static BlendingParameters Subtractive => new BlendingParameters
{
Source = BlendingType.One,
Destination = BlendingType.One,
SourceAlpha = BlendingType.Zero,
DestinationAlpha = BlendingType.One,
RGBEquation = BlendingEquation.ReverseSubtract,
AlphaEquation = BlendingEquation.Add,
};

public static BlendingParameters Screen => new BlendingParameters
{
Source = BlendingType.One,
Destination = BlendingType.OneMinusSrcColor,
SourceAlpha = BlendingType.One,
DestinationAlpha = BlendingType.OneMinusSrcAlpha,
RGBEquation = BlendingEquation.Add,
AlphaEquation = BlendingEquation.Add,
};

public static BlendingParameters Multiplicative => new BlendingParameters
{
Source = BlendingType.DstColor,
Destination = BlendingType.OneMinusSrcAlpha,
SourceAlpha = BlendingType.One,
DestinationAlpha = BlendingType.OneMinusSrcAlpha,
RGBEquation = BlendingEquation.Add,
AlphaEquation = BlendingEquation.Add,
};

public static BlendingParameters Premultiplied => new BlendingParameters
{
Source = BlendingType.One,
Destination = BlendingType.OneMinusSrcAlpha,
SourceAlpha = BlendingType.One,
DestinationAlpha = BlendingType.OneMinusSrcAlpha,
RGBEquation = BlendingEquation.Add,
AlphaEquation = BlendingEquation.Add,
};

#endregion

public static BlendingParameters GetDefaultParameters(DefaultBlendingParameters blendingParameterType)
{
return blendingParameterType switch
{
DefaultBlendingParameters.None => None,
DefaultBlendingParameters.Inherit => Inherit,
DefaultBlendingParameters.Mix => Mixture,
DefaultBlendingParameters.Difference => Difference,
DefaultBlendingParameters.Add => Additive,
DefaultBlendingParameters.Subtract => Subtractive,
DefaultBlendingParameters.Screen => Screen,
DefaultBlendingParameters.Multiply => Multiplicative,
DefaultBlendingParameters.Premultiplied => Premultiplied,
_ => throw new ArgumentOutOfRangeException(nameof(blendingParameterType), blendingParameterType, null),
};
}

/// <summary>
/// Copy all properties that are marked as inherited from a parent <see cref="BlendingParameters"/> object.
/// </summary>
Expand Down Expand Up @@ -238,7 +306,7 @@ private static BlendingFactorSrc translateBlendingFactorSrc(BlendingType factor)
return BlendingFactorSrc.OneMinusDstColor;

case BlendingType.OneMinusSrcAlpha:
return BlendingFactorSrc.OneMinusSrcColor;
return BlendingFactorSrc.OneMinusSrcAlpha;

case BlendingType.SrcAlpha:
return BlendingFactorSrc.SrcAlpha;
Expand Down
18 changes: 18 additions & 0 deletions osu.Framework/Graphics/DefaultBlendingParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Graphics
{
public enum DefaultBlendingParameters
{
None,
Inherit,
Mix,
Difference,
Add,
Subtract,
Screen,
Multiply,
Premultiplied,
}
}
28 changes: 27 additions & 1 deletion osu.Framework/Graphics/Textures/TextureUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using osuTK.Graphics.ES30;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using StbiSharp;

namespace osu.Framework.Graphics.Textures
Expand Down Expand Up @@ -98,10 +99,35 @@ internal static Image<TPixel> LoadFromStream<TPixel>(Stream stream) where TPixel

Logger.Log($"Texture could not be loaded via STB; falling back to ImageSharp: {e.Message}");
stream.Position = initialPos;
return Image.Load<TPixel>(stream);

bool isWebP = TextureUpload.isWebP(stream);
var image = Image.Load<TPixel>(stream);

// a stupid fix for heavily compressed webp images with visible artifacts but it's efficient and works
if (isWebP)
image.Mutate(x => x.BoxBlur(0));

return image;
}
}

private static bool isWebP(Stream stream)
{
long initialPos = stream.Position;

if (stream.Length < 12)
return false;

Span<byte> header = stackalloc byte[12];
stream.ReadExactly(header);
stream.Position = initialPos;

return header[0] == 'R' && header[1] == 'I' &&
header[2] == 'F' && header[3] == 'F' &&
header[8] == 'W' && header[9] == 'E' &&
header[10] == 'B' && header[11] == 'P';
}

/// <summary>
/// Create an empty upload. Used by <see cref="IFrameBuffer"/> for initialisation.
/// </summary>
Expand Down