Initial commit

This commit is contained in:
Yukai Li 2021-02-16 01:04:29 -07:00
commit 0f86b0434b
38 changed files with 2370 additions and 0 deletions

View file

@ -0,0 +1,17 @@
using LibDgf.Aqualead.Image;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Image.Conversion
{
public interface IAlImageConverter
{
string FileExtension { get; }
bool HasAlternativeFile(AlImage image);
bool CanConvert(string pixelFormat);
void ConvertFromAl(AlImage image, Stream destStream);
void ConvertFromAlAlt(AlImage image, Stream destStream);
}
}

View file

@ -0,0 +1,70 @@
using LibDgf.Aqualead.Image;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using KtxSharp;
namespace LibDgf.Aqualead.Image.Conversion
{
public class KtxConverter : IAlImageConverter
{
public string FileExtension => ".ktx";
public void ConvertFromAl(AlImage image, Stream destStream)
{
List<byte[]> mips;
if (image.PixelFormat == "ETC1")
{
mips = image.Mipmaps;
}
else if (image.PixelFormat == "EC1A")
{
// Give first half of the mips
mips = new List<byte[]>();
foreach (var mip in image.Mipmaps)
{
byte[] newMip = new byte[mip.Length / 2];
Buffer.BlockCopy(mip, 0, newMip, 0, newMip.Length);
mips.Add(newMip);
}
}
else
{
throw new ArgumentException("Pixel format not supported.", nameof(image));
}
var ktx = KtxCreator.Create(GlDataType.Compressed, GlPixelFormat.GL_RGB, GlInternalFormat.GL_ETC1_RGB8_OES,
image.Width, image.Height, mips, new Dictionary<string, MetadataValue>());
KtxWriter.WriteTo(ktx, destStream);
}
public void ConvertFromAlAlt(AlImage image, Stream destStream)
{
if (image.PixelFormat != "EC1A")
throw new ArgumentException("Pixel format does not have alternate representation.", nameof(image));
// Give second half of the mips
var mips = new List<byte[]>();
foreach (var mip in image.Mipmaps)
{
byte[] newMip = new byte[mip.Length / 2];
Buffer.BlockCopy(mip, newMip.Length, newMip, 0, newMip.Length);
mips.Add(newMip);
}
var ktx = KtxCreator.Create(GlDataType.Compressed, GlPixelFormat.GL_RGB, GlInternalFormat.GL_ETC1_RGB8_OES,
image.Width, image.Height, mips, new Dictionary<string, MetadataValue>());
KtxWriter.WriteTo(ktx, destStream);
}
public bool CanConvert(string pixelFormat)
{
return pixelFormat == "EC1A" || pixelFormat == "ETC1";
}
public bool HasAlternativeFile(AlImage image)
{
return image.PixelFormat == "EC1A";
}
}
}

View file

@ -0,0 +1,78 @@
using LibDgf.Aqualead.Image;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Image.Conversion
{
public class PkmConverter : IAlImageConverter
{
public string FileExtension => ".pkm";
public void ConvertFromAl(AlImage image, Stream destStream)
{
List<byte[]> mips;
if (image.PixelFormat == "ETC1")
{
mips = image.Mipmaps;
}
else if (image.PixelFormat == "EC1A")
{
// Give first half of the mips
mips = new List<byte[]>();
byte[] newMip = new byte[image.Mipmaps[0].Length / 2];
Buffer.BlockCopy(image.Mipmaps[0], 0, newMip, 0, newMip.Length);
mips.Add(newMip);
}
else
{
throw new ArgumentException("Pixel format not supported.", nameof(image));
}
WritePkm(destStream, mips[0], image.Width, image.Height);
}
public void ConvertFromAlAlt(AlImage image, Stream destStream)
{
if (image.PixelFormat != "EC1A")
throw new ArgumentException("Pixel format does not have alternate representation.", nameof(image));
// Give second half of the mips
byte[] newMip = new byte[image.Mipmaps[0].Length / 2];
Buffer.BlockCopy(image.Mipmaps[0], newMip.Length, newMip, 0, newMip.Length);
WritePkm(destStream, newMip, image.Width, image.Height);
}
public bool CanConvert(string pixelFormat)
{
return pixelFormat == "EC1A" || pixelFormat == "ETC1";
}
public bool HasAlternativeFile(AlImage image)
{
return image.PixelFormat == "EC1A";
}
const ushort ETC1_RGB_NO_MIPMAPS = 0;
static void WritePkm(Stream stream, byte[] data, uint width, uint height)
{
BinaryWriter bw = new BinaryWriter(stream);
bw.Write("PKM 10".ToCharArray());
WriteBeUInt16(bw, ETC1_RGB_NO_MIPMAPS);
WriteBeUInt16(bw, (ushort)((width + 3) & ~3));
WriteBeUInt16(bw, (ushort)((height + 3) & ~3));
WriteBeUInt16(bw, (ushort)width);
WriteBeUInt16(bw, (ushort)height);
bw.Write(data);
}
static void WriteBeUInt16(BinaryWriter bw, ushort value)
{
bw.Write((byte)(value >> 8));
bw.Write((byte)value);
}
}
}

View file

@ -0,0 +1,62 @@
using LibDgf.Aqualead.Image;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Image.Conversion
{
public class PngConverter : IAlImageConverter
{
public string FileExtension => ".png";
public void ConvertFromAl(AlImage image, Stream destStream)
{
// Only grab the first mip
var pixels = image.Mipmaps[0];
using (MemoryStream ms = new MemoryStream(pixels))
{
BinaryReader br = new BinaryReader(ms);
using (var img = ConvertBgra32(br, (int)image.Width, (int)image.Height))
{
img.SaveAsPng(destStream);
}
}
}
public bool CanConvert(string pixelFormat)
{
return pixelFormat == "BGRA";
}
public static Image<Bgra32> ConvertBgra32(BinaryReader br, int width, int height)
{
Image<Bgra32> img = new Image<Bgra32>(width, height);
for (int y = 0; y < height; ++y)
{
var row = img.GetPixelRowSpan(y);
for (int x = 0; x < width; ++x)
{
byte b = br.ReadByte();
byte g = br.ReadByte();
byte r = br.ReadByte();
byte a = br.ReadByte();
row[x] = new Bgra32(r, g, b, a);
}
}
return img;
}
public void ConvertFromAlAlt(AlImage image, Stream destStream)
{
throw new NotSupportedException();
}
public bool HasAlternativeFile(AlImage image)
{
return false;
}
}
}