diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3eb5e2..f682a32 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/MLEM.Data/Content/Texture2DReader.cs b/MLEM.Data/Content/Texture2DReader.cs
index 08584c4..e107289 100644
--- a/MLEM.Data/Content/Texture2DReader.cs
+++ b/MLEM.Data/Content/Texture2DReader.cs
@@ -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();
}
///
diff --git a/MLEM.Ui/Parsers/UiParser.cs b/MLEM.Ui/Parsers/UiParser.cs
index e5a4373..ccd3262 100644
--- a/MLEM.Ui/Parsers/UiParser.cs
+++ b/MLEM.Ui/Parsers/UiParser.cs
@@ -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);
diff --git a/MLEM/Textures/TextureExtensions.cs b/MLEM/Textures/TextureExtensions.cs
index 0c58fb7..adaaa69 100644
--- a/MLEM/Textures/TextureExtensions.cs
+++ b/MLEM/Textures/TextureExtensions.cs
@@ -18,6 +18,24 @@ namespace MLEM.Textures {
return new TextureData(texture);
}
+ ///
+ /// Creates and returns a copy of the given with all colors converted to premultiplied alpha, which is the format that MonoGame's content pipeline loads textures in. This method uses for all pixels in the .
+ ///
+ /// The texture of which to create a premultiplied copy.
+ /// The premultiplied copy of the .
+ 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;
+ }
+
///
/// A struct that represents the data of a texture, accessed through .
///