2019-05-13 21:29:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2019-07-16 23:35:21 +02:00
|
|
|
|
namespace Toolbox.Library
|
2019-05-13 21:29:40 +02:00
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// A class which can create generic texture instances from bitmaps for usage in opengl and image editors.
|
|
|
|
|
//
|
|
|
|
|
public class GenericBitmapTexture : STGenericTexture
|
|
|
|
|
{
|
|
|
|
|
public override bool CanEdit { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
public override TEX_FORMAT[] SupportedFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new TEX_FORMAT[] { TEX_FORMAT.R8G8B8A8_UNORM };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] ImageData;
|
|
|
|
|
|
|
|
|
|
public GenericBitmapTexture(byte[] FileData, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Format = TEX_FORMAT.R8G8B8A8_UNORM;
|
|
|
|
|
Width = (uint)width;
|
|
|
|
|
Height = (uint)height;
|
|
|
|
|
|
|
|
|
|
ImageData = DDSCompressor.CompressBlock(FileData, width, height,
|
2019-11-07 23:40:48 +01:00
|
|
|
|
DDS.DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, false);
|
2019-05-13 21:29:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GenericBitmapTexture(string FileName) {
|
|
|
|
|
LoadBitmap(new Bitmap(FileName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GenericBitmapTexture(Bitmap Image) {
|
|
|
|
|
LoadBitmap(Image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadBitmap(Bitmap Image)
|
|
|
|
|
{
|
|
|
|
|
Image = BitmapExtension.SwapBlueRedChannels(Image);
|
|
|
|
|
|
|
|
|
|
Format = TEX_FORMAT.R8G8B8A8_UNORM;
|
|
|
|
|
Width = (uint)Image.Width;
|
|
|
|
|
Height = (uint)Image.Height;
|
|
|
|
|
MipCount = 1;
|
|
|
|
|
|
2019-05-19 20:25:37 +02:00
|
|
|
|
ImageData = GenerateMipsAndCompress(Image, MipCount, Format);
|
2019-05-13 21:29:40 +02:00
|
|
|
|
|
|
|
|
|
if (ImageData == null || ImageData.Length <= 0)
|
|
|
|
|
throw new Exception("Image failed to encode!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetImageData(Bitmap bitmap, int ArrayLevel)
|
|
|
|
|
{
|
|
|
|
|
byte[] Data = BitmapExtension.ImageToByte(bitmap);
|
|
|
|
|
Width = (uint)bitmap.Width;
|
|
|
|
|
Height = (uint)bitmap.Height;
|
|
|
|
|
|
|
|
|
|
ImageData = DDSCompressor.EncodePixelBlock(Data, bitmap.Width, bitmap.Width, DDS.DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
|
|
|
|
|
{
|
|
|
|
|
return ImageData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|