使用ImageSharp解码和编码纹理
Contents
[
Hide
]
使用 Aspose.3D for .NET,开发人员使用外部图像编码器和解码器来加载纹理或将纹理保存为不同的图像格式。
使用ImageSharp实现纹理编解码器
使用以下类定义纹理编码器和纹理解码器:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using Aspose.ThreeD.Utilities; | |
using Aspose.ThreeD.Render; | |
using SixLabors.ImageSharp; | |
using SixLabors.ImageSharp.PixelFormats; | |
using System.Runtime.InteropServices; | |
using SixLabors.ImageSharp.Formats; | |
namespace Aspose.ThreeD | |
{ | |
/// <summary> | |
/// Uses ImageSharp for encoding/decoding textures for Aspose.3D textures. | |
/// </summary> | |
public class ImageSharpCodec : ITextureCodec, ITextureDecoder | |
{ | |
class Encoder<T> : ITextureEncoder where T:IImageEncoder, new() | |
{ | |
public string FileExtension { get; } | |
public Encoder(string ext) | |
{ | |
FileExtension = ext; | |
} | |
public void Encode(TextureData texture, Stream stream) | |
{ | |
var bmp = ToBitmap(texture); | |
var encoder = new T(); | |
bmp.Save(stream, encoder); | |
} | |
} | |
/// <summary> | |
/// Convert TextureData to Image | |
/// </summary> | |
/// <param name="td"></param> | |
/// <returns></returns> | |
public unsafe static Image<Bgra32> ToBitmap(TextureData td) | |
{ | |
using(var map = td.MapPixels(PixelMapMode.ReadOnly, PixelFormat.A8R8G8B8)) | |
{ | |
var ret = new Image<Bgra32>(td.Width, td.Height); | |
ret.ProcessPixelRows(accessor => | |
{ | |
fixed (byte* p = map.Data) | |
{ | |
for (int y = 0; y < ret.Height; y++) | |
{ | |
var offset = y * map.Stride; | |
var src = new Span<Bgra32>(p + offset, map.Stride); | |
var dst = accessor.GetRowSpan(y); | |
src.CopyTo(dst); | |
} | |
} | |
}); | |
return ret; | |
} | |
} | |
/// <summary> | |
/// Convert Image to TextureData | |
/// </summary> | |
/// <param name="img"></param> | |
/// <param name="reverseY"></param> | |
/// <returns></returns> | |
public unsafe static TextureData ToTextureData(Image<Bgra32> img, bool reverseY) | |
{ | |
var ret = new TextureData(img.Width, img.Height, PixelFormat.A8R8G8B8); | |
using (var map = ret.MapPixels(PixelMapMode.WriteOnly)) | |
{ | |
img.ProcessPixelRows(accessor => | |
{ | |
fixed (byte* src = map.Data) | |
{ | |
for (int y = 0; y < img.Height; y++) | |
{ | |
var offset = 0; | |
if (reverseY) | |
offset = (map.Height - y - 1) * map.Stride; | |
else | |
offset = y * map.Stride; | |
var dst = new Span<Bgra32>(src + offset, map.Stride); | |
var row = accessor.GetRowSpan(y); | |
row.CopyTo(dst); | |
} | |
} | |
}); | |
} | |
return ret; | |
} | |
/// <summary> | |
/// Decode texture from stream, return null if failed to decode. | |
/// </summary> | |
/// <param name="stream">Texture data source stream</param> | |
/// <param name="reverseY">Flip the texture</param> | |
/// <returns>Decoded texture data or null if not supported.</returns> | |
public unsafe TextureData Decode(Stream stream, bool reverseY) | |
{ | |
using (var img = Image.Load<Bgra32>(stream)) | |
{ | |
return ToTextureData(img, reverseY); | |
} | |
} | |
/// <summary> | |
/// Gets supported texture decoders. | |
/// </summary> | |
/// <returns></returns> | |
public ITextureDecoder[] GetDecoders() | |
{ | |
return new ITextureDecoder[] { this }; | |
} | |
/// <summary> | |
/// Gets supported texture encoders. | |
/// </summary> | |
/// <returns></returns> | |
public ITextureEncoder[] GetEncoders() | |
{ | |
List<ITextureEncoder> ret = new List<ITextureEncoder>(); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Bmp.BmpEncoder>("bmp")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Png.PngEncoder>("png")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder>("jpg")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder>("jpeg")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Pbm.PbmEncoder>("pbm")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Gif.GifEncoder>("gif")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Tga.TgaEncoder>("tga")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Tiff.TiffEncoder>("tif")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Tiff.TiffEncoder>("tiff")); | |
ret.Add(new Encoder<SixLabors.ImageSharp.Formats.Webp.WebpEncoder>("webp")); | |
return ret.ToArray(); | |
} | |
} | |
} |
将其注册到 Aspose。3D
现在让我们将其注册到Aspsoe.3D:
Aspose.ThreeD.Render.TextureCodec.RegisterCodec(new Aspose.ThreeD.ImageSharpCodec());
注册此编解码器后,可以在TextureData.Save中使用所有ImageSharp支持的图像格式。