Add SetImageTo3DST to 3DSUtil

This commit is contained in:
miku-666
2022-09-25 13:15:41 +02:00
parent f9b3d6f218
commit 5d9ce5539b
2 changed files with 36 additions and 35 deletions

View File

@@ -17,7 +17,7 @@ namespace Ohana3DS_Rebirth.Ohana
/// <param name="height">Height of the Texture</param>
/// <param name="format">Pixel Format of the Texture</param>
/// <returns></returns>
public static Bitmap decode(byte[] data, int width, int height, RenderBase.OTextureFormat format)
public static Bitmap Decode(byte[] data, int width, int height, RenderBase.OTextureFormat format)
{
byte[] output = new byte[width * height * 4];
long dataOffset = 0;
@@ -319,12 +319,12 @@ namespace Ohana3DS_Rebirth.Ohana
/// <param name="img">Input image to be encoded</param>
/// <param name="format">Pixel Format of the Texture</param>
/// <returns></returns>
public static byte[] encode(Bitmap img, RenderBase.OTextureFormat format)
public static byte[] Encode(Bitmap img, RenderBase.OTextureFormat format)
{
byte[] data = TextureUtils.ToArray(img);
byte[] output = new byte[data.Length];
uint outputOffset = 0;
int outputOffset = 0;
switch (format)
{
case RenderBase.OTextureFormat.rgba8:
@@ -336,9 +336,9 @@ namespace Ohana3DS_Rebirth.Ohana
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long dataOffset = ((tX * 8) + x + ((tY * 8 + y) * img.Width)) * 4;
int dataOffset = ((tX * 8) + x + (tY * 8 + y) * img.Width) * 4;
Buffer.BlockCopy(data, (int)dataOffset, output, (int)outputOffset + 1, 3);
Buffer.BlockCopy(data, dataOffset, output, outputOffset + 1, 3);
output[outputOffset] = data[dataOffset + 3];
outputOffset += 4;

View File

@@ -10,18 +10,30 @@ namespace PckStudio.Classes.Utils
{
private static string ReadString(Stream stream, int len)
{
var buffer = new byte[len];
byte[] buffer = new byte[len];
stream.Read(buffer, 0, len);
return Encoding.ASCII.GetString(buffer);
}
private static int ReadInt32(Stream stream)
{
var buffer = new byte[4];
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
return BitConverter.ToInt32(buffer, 0);
}
private static void WriteString(Stream stream, string s)
{
byte[] buffer = Encoding.ASCII.GetBytes(s);
stream.Write(buffer, 0, buffer.Length);
}
private static void WriteInt32(Stream stream, int value)
{
byte[] buffer = BitConverter.GetBytes(value);
stream.Write(buffer, 0, 4);
}
public static int CalcBufferSize(RenderBase.OTextureFormat fmt, int w, int h)
{
switch (fmt)
@@ -56,7 +68,7 @@ namespace PckStudio.Classes.Utils
{
int offset = 32;
stream.Seek(8L, SeekOrigin.Begin);
RenderBase.OTextureFormat otextureFormat = ReadInt32(stream) switch
RenderBase.OTextureFormat format = ReadInt32(stream) switch
{
0 => RenderBase.OTextureFormat.rgba8,
1 => RenderBase.OTextureFormat.rgb8,
@@ -66,13 +78,13 @@ namespace PckStudio.Classes.Utils
9 => RenderBase.OTextureFormat.la4,
_ => RenderBase.OTextureFormat.dontCare,
};
int width = ReadInt32(stream);
int width = ReadInt32(stream);
int height = ReadInt32(stream);
int bufferSize = CalcBufferSize(otextureFormat, width, height);
int bufferSize = CalcBufferSize(format, width, height);
stream.Seek(offset, SeekOrigin.Begin);
byte[] buffer = new byte[bufferSize];
stream.Read(buffer, 0, bufferSize);
var img = TextureCodec.decode(buffer, width, height, otextureFormat);
var img = TextureCodec.Decode(buffer, width, height, format);
img.RotateFlip(RotateFlipType.RotateNoneFlipY);
return img;
}
@@ -81,30 +93,19 @@ namespace PckStudio.Classes.Utils
public static void SetImageTo3DST(Stream stream, Image source, RenderBase.OTextureFormat format = RenderBase.OTextureFormat.rgba8)
{
throw new NotImplementedException();
//if (ReadString(stream, 4) == "3DST")
//{
// int offset = 32;
// stream.Seek(8L, SeekOrigin.Begin);
// RenderBase.OTextureFormat otextureFormat = ReadInt32(stream) switch
// {
// 0 => RenderBase.OTextureFormat.rgba8,
// 1 => RenderBase.OTextureFormat.rgb8,
// 2 => RenderBase.OTextureFormat.rgba5551,
// 3 => RenderBase.OTextureFormat.rgb8,
// 4 => RenderBase.OTextureFormat.rgba4,
// 9 => RenderBase.OTextureFormat.la4,
// _ => RenderBase.OTextureFormat.dontCare,
// };
// int width = ReadInt32(stream);
// int height = ReadInt32(stream);
// int bufferSize = CalcBufferSize(otextureFormat, width, height);
// stream.Seek(offset, SeekOrigin.Begin);
// byte[] buffer = new byte[bufferSize];
// stream.Read(buffer, 0, bufferSize);
// var img = TextureCodec.decode(buffer, width, height, otextureFormat);
// img.RotateFlip(RotateFlipType.RotateNoneFlipY);
//}
// TODO: fix Encoding
WriteString(stream, "3DST"); // 0
WriteInt32(stream, 2); // 4 unknown
WriteInt32(stream, (int)format); // 8
WriteInt32(stream, source.Width); // 12
WriteInt32(stream, source.Height); // 16
WriteInt32(stream, 0); // 20
WriteInt32(stream, 0); // 24
WriteInt32(stream, 0); // 28
WriteInt32(stream, 0); // 32
source.RotateFlip(RotateFlipType.RotateNoneFlipY);
byte[] buffer = TextureCodec.Encode(new Bitmap(source), format);
stream.Write(buffer, 0, buffer.Length);
}
}
}