1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-21 20:43:29 +01:00

Image loading improvements:

- Added TextureExtensions.PremultipliedCopy for textures
- Create a premultiplied copy of UiParser images to support usage with KNI
This commit is contained in:
Ell 2024-09-14 10:48:04 +02:00
parent a6a34c3937
commit c4bd03ff04
4 changed files with 29 additions and 14 deletions

View file

@ -17,9 +17,14 @@ Jump to version:
## 7.1.1 (In Development)
### MLEM
Additions
- Added TextureExtensions.PremultipliedCopy for textures
### MLEM.Ui
Improvements
- Construct images in UiParser.ParseImage on the main thread to support usage with KNI
- Create a premultiplied copy of UiParser images to support usage with KNI
## 7.1.0

View file

@ -17,18 +17,8 @@ namespace MLEM.Data.Content {
#endif
// premultiply the texture's color to be in line with the pipeline's texture reader
using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream)) {
var ret = new Texture2D(manager.GraphicsDevice, texture.Width, texture.Height);
using (var textureData = texture.GetTextureData()) {
using (var retData = ret.GetTextureData()) {
for (var x = 0; x < ret.Width; x++) {
for (var y = 0; y < ret.Height; y++)
retData[x, y] = Color.FromNonPremultiplied(textureData[x, y].ToVector4());
}
}
}
return ret;
}
using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream))
return texture.PremultipliedCopy();
}
/// <inheritdoc />

View file

@ -156,8 +156,10 @@ namespace MLEM.Ui.Parsers {
if (!bytesNull) {
Texture2D tex;
lock (bytesLock) {
using (var stream = new MemoryStream(bytes))
tex = Texture2D.FromStream(this.GraphicsDevice, stream);
using (var stream = new MemoryStream(bytes)) {
using (var read = Texture2D.FromStream(this.GraphicsDevice, stream))
tex = read.PremultipliedCopy();
}
bytes = null;
}
image = new TextureRegion(tex);

View file

@ -18,6 +18,24 @@ namespace MLEM.Textures {
return new TextureData(texture);
}
/// <summary>
/// Creates and returns a copy of the given <paramref name="texture"/> with all colors converted to premultiplied alpha, which is the format that MonoGame's content pipeline loads textures in. This method uses <see cref="Color.FromNonPremultiplied(Microsoft.Xna.Framework.Vector4)"/> for all pixels in the <paramref name="texture"/>.
/// </summary>
/// <param name="texture">The texture of which to create a premultiplied copy.</param>
/// <returns>The premultiplied copy of the <paramref name="texture"/>.</returns>
public static Texture2D PremultipliedCopy(this Texture2D texture) {
var ret = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height);
using (var textureData = texture.GetTextureData()) {
using (var retData = ret.GetTextureData()) {
for (var x = 0; x < ret.Width; x++) {
for (var y = 0; y < ret.Height; y++)
retData[x, y] = Color.FromNonPremultiplied(textureData[x, y].ToVector4());
}
}
}
return ret;
}
/// <summary>
/// A struct that represents the data of a texture, accessed through <see cref="TextureExtensions.GetTextureData"/>.
/// </summary>