Skip to content

Commit df53238

Browse files
committed
Add indexer for image pixels that accepts PremultipliedColours
1 parent 39c888b commit df53238

File tree

7 files changed

+37
-18
lines changed

7 files changed

+37
-18
lines changed

osu.Framework.Tests/Visual/Input/TestSceneInputResampler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using osu.Framework.Allocation;
8+
using osu.Framework.Extensions.Color4Extensions;
89
using osu.Framework.Graphics;
910
using osu.Framework.Graphics.Lines;
1011
using osu.Framework.Graphics.Rendering;
@@ -15,7 +16,6 @@
1516
using osu.Framework.Testing;
1617
using osuTK;
1718
using osuTK.Graphics;
18-
using SixLabors.ImageSharp.PixelFormats;
1919

2020
namespace osu.Framework.Tests.Visual.Input
2121
{
@@ -37,7 +37,7 @@ private void load(IRenderer renderer)
3737
for (int i = 0; i < width; ++i)
3838
{
3939
byte brightnessByte = (byte)((float)i / (width - 1) * 255);
40-
image.Premultiplied[i, 0] = new Rgba32(brightnessByte, brightnessByte, brightnessByte);
40+
image[i, 0] = new Color4(brightnessByte, brightnessByte, brightnessByte, 255).ToPremultiplied();
4141
}
4242

4343
gradientTexture.SetData(new TextureUpload(image));

osu.Framework.Tests/Visual/UserInterface/TestSceneCircularBlob.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using osu.Framework.Allocation;
5+
using osu.Framework.Extensions.Color4Extensions;
56
using osu.Framework.Graphics;
67
using osu.Framework.Graphics.Colour;
78
using osu.Framework.Graphics.Containers;
@@ -11,7 +12,6 @@
1112
using osu.Framework.Graphics.UserInterface;
1213
using osuTK;
1314
using osuTK.Graphics;
14-
using SixLabors.ImageSharp.PixelFormats;
1515

1616
namespace osu.Framework.Tests.Visual.UserInterface
1717
{
@@ -38,7 +38,7 @@ private void load()
3838
for (int i = 0; i < width; ++i)
3939
{
4040
float brightness = (float)i / (width - 1);
41-
image.Premultiplied[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255);
41+
image[i, 0] = new Color4((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255).ToPremultiplied();
4242
}
4343

4444
gradientTextureHorizontal.SetData(new TextureUpload(image));
@@ -50,7 +50,7 @@ private void load()
5050
for (int i = 0; i < width; ++i)
5151
{
5252
float brightness = (float)i / (width - 1);
53-
image.Premultiplied[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255);
53+
image[i, 0] = new Color4((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255).ToPremultiplied();
5454
}
5555

5656
gradientTextureVertical.SetData(new TextureUpload(image));
@@ -65,11 +65,11 @@ private void load()
6565
{
6666
float brightness = (float)i / (width - 1);
6767
float brightness2 = (float)j / (width - 1);
68-
image.Premultiplied[i, j] = new Rgba32(
68+
image[i, j] = new Color4(
6969
(byte)(128 + (1 + brightness - brightness2) / 2 * 127),
7070
(byte)(128 + (1 + brightness2 - brightness) / 2 * 127),
7171
(byte)(128 + (brightness + brightness2) / 2 * 127),
72-
255);
72+
255).ToPremultiplied();
7373
}
7474
}
7575

osu.Framework.Tests/Visual/UserInterface/TestSceneCircularProgress.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using osu.Framework.Allocation;
7+
using osu.Framework.Extensions.Color4Extensions;
78
using osu.Framework.Graphics;
89
using osu.Framework.Graphics.Colour;
910
using osu.Framework.Graphics.Containers;
@@ -13,7 +14,6 @@
1314
using osu.Framework.Graphics.UserInterface;
1415
using osuTK;
1516
using osuTK.Graphics;
16-
using SixLabors.ImageSharp.PixelFormats;
1717

1818
namespace osu.Framework.Tests.Visual.UserInterface
1919
{
@@ -44,7 +44,7 @@ private void load()
4444
for (int i = 0; i < width; ++i)
4545
{
4646
float brightness = (float)i / (width - 1);
47-
image.Premultiplied[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255);
47+
image[i, 0] = new Color4((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255).ToPremultiplied();
4848
}
4949

5050
gradientTextureHorizontal.SetData(new TextureUpload(image));
@@ -56,7 +56,7 @@ private void load()
5656
for (int i = 0; i < width; ++i)
5757
{
5858
float brightness = (float)i / (width - 1);
59-
image.Premultiplied[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255);
59+
image[i, 0] = new Color4((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255).ToPremultiplied();
6060
}
6161

6262
gradientTextureVertical.SetData(new TextureUpload(image));
@@ -71,11 +71,11 @@ private void load()
7171
{
7272
float brightness = (float)i / (width - 1);
7373
float brightness2 = (float)j / (width - 1);
74-
image.Premultiplied[i, j] = new Rgba32(
74+
image[i, j] = new Color4(
7575
(byte)(128 + (1 + brightness - brightness2) / 2 * 127),
7676
(byte)(128 + (1 + brightness2 - brightness) / 2 * 127),
7777
(byte)(128 + (brightness + brightness2) / 2 * 127),
78-
255);
78+
255).ToPremultiplied();
7979
}
8080
}
8181

osu.Framework.Tests/Visual/UserInterface/TestSceneDrawablePath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using NUnit.Framework;
88
using osu.Framework.Allocation;
9+
using osu.Framework.Extensions.Color4Extensions;
910
using osu.Framework.Graphics;
1011
using osu.Framework.Graphics.Containers;
1112
using osu.Framework.Graphics.Lines;
@@ -15,7 +16,6 @@
1516
using osu.Framework.Utils;
1617
using osuTK;
1718
using osuTK.Graphics;
18-
using SixLabors.ImageSharp.PixelFormats;
1919

2020
namespace osu.Framework.Tests.Visual.UserInterface
2121
{
@@ -33,7 +33,7 @@ private void load(IRenderer renderer)
3333
for (int i = 0; i < texture_width; ++i)
3434
{
3535
byte brightnessByte = (byte)((float)i / (texture_width - 1) * 255);
36-
image.Premultiplied[i, 0] = new Rgba32(brightnessByte, brightnessByte, brightnessByte, brightnessByte);
36+
image[i, 0] = new Color4(255, 255, 255, brightnessByte).ToPremultiplied();
3737
}
3838

3939
gradientTexture = renderer.CreateTexture(texture_width, 1, true);

osu.Framework/Graphics/Colour/PremultipliedColour.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public PremultipliedColour(float premultipliedR, float premultipliedG, float pre
5555
colour.B * colour.A,
5656
colour.A);
5757

58+
/// <summary>
59+
/// Creates a <see cref="PremultipliedColour"/> from a premultiplied-alpha <see cref="Rgba32"/> colour.
60+
/// </summary>
61+
/// <param name="premultipliedColour">The premultiplied-alpha <see cref="Rgba32"/> colour.</param>
62+
public static PremultipliedColour FromPremultiplied(Rgba32 premultipliedColour)
63+
{
64+
var premultipliedVector = premultipliedColour.ToVector4();
65+
return new PremultipliedColour(premultipliedVector.X, premultipliedVector.Y, premultipliedVector.Z, premultipliedVector.W);
66+
}
67+
5868
public bool Equals(PremultipliedColour other)
5969
=> PremultipliedR == other.PremultipliedR &&
6070
PremultipliedG == other.PremultipliedG &&

osu.Framework/Graphics/Lines/SmoothPath.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using osu.Framework.Graphics.Rendering;
99
using osu.Framework.Graphics.Textures;
1010
using osuTK.Graphics;
11-
using SixLabors.ImageSharp.PixelFormats;
1211

1312
namespace osu.Framework.Graphics.Lines
1413
{
@@ -74,8 +73,7 @@ private void validateTexture()
7473
float progress = (float)i / (textureWidth - 1);
7574

7675
var colour = ColourAt(progress);
77-
float alpha = Math.Min(progress / aa_portion, 1);
78-
raw.Premultiplied[i, 0] = new Rgba32(colour.R * alpha, colour.G * alpha, colour.B * alpha, colour.A * alpha);
76+
raw[i, 0] = colour.Opacity(colour.A * Math.Min(progress / aa_portion, 1)).ToPremultiplied();
7977
}
8078

8179
if (Texture?.Width == textureWidth)

osu.Framework/Graphics/Textures/PremultipliedImage.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace osu.Framework.Graphics.Textures
1212
public class PremultipliedImage : IDisposable
1313
{
1414
/// <summary>
15-
/// The underlying image in <see cref="Image{TPixel}"/> form.
15+
/// The underlying image in <see cref="Image{Rgba32}"/> form.
1616
/// </summary>
1717
public readonly Image<Rgba32> Premultiplied;
1818

@@ -33,6 +33,17 @@ public PremultipliedImage(int width, int height, PremultipliedColour colour)
3333

3434
public PremultipliedImage Clone() => FromPremultiplied(Premultiplied.Clone());
3535

36+
/// <summary>
37+
/// Gets or sets the pixel at the specified position.
38+
/// </summary>
39+
/// <param name="x">The x-coordinate of the pixel.</param>
40+
/// <param name="y">The y-coordinate of the pixel.</param>
41+
public PremultipliedColour this[int x, int y]
42+
{
43+
get => PremultipliedColour.FromPremultiplied(Premultiplied[x, y]);
44+
set => Premultiplied[x, y] = value.ToPremultipliedRgba32();
45+
}
46+
3647
public void Dispose()
3748
{
3849
Premultiplied.Dispose();

0 commit comments

Comments
 (0)