2020-04-22 00:30:55 +02:00
|
|
|
using System.IO;
|
2022-05-07 21:39:36 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
2020-04-22 00:30:55 +02:00
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2024-07-19 20:02:28 +02:00
|
|
|
using MLEM.Textures;
|
2020-04-22 00:30:55 +02:00
|
|
|
|
2020-09-16 23:39:01 +02:00
|
|
|
namespace MLEM.Data.Content {
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-04-22 00:30:55 +02:00
|
|
|
public class Texture2DReader : RawContentReader<Texture2D> {
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-04-22 00:30:55 +02:00
|
|
|
protected override Texture2D Read(RawContentManager manager, string assetPath, Stream stream, Texture2D existing) {
|
2024-09-02 19:55:28 +02:00
|
|
|
#if !FNA && !KNI
|
2020-04-22 00:30:55 +02:00
|
|
|
if (existing != null) {
|
|
|
|
existing.Reload(stream);
|
|
|
|
return existing;
|
2022-08-20 11:39:28 +02:00
|
|
|
}
|
2022-12-13 13:11:36 +01:00
|
|
|
#endif
|
2022-08-20 11:39:28 +02:00
|
|
|
|
|
|
|
// 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());
|
2022-05-07 21:39:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 11:39:28 +02:00
|
|
|
return ret;
|
2020-04-22 00:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-04-22 00:30:55 +02:00
|
|
|
public override string[] GetFileExtensions() {
|
|
|
|
return new[] {"png", "bmp", "gif", "jpg", "tif", "dds"};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-17 18:23:47 +02:00
|
|
|
}
|