From 5d9ce5539b1b4238badd6380440397f1c5f03ac4 Mon Sep 17 00:00:00 2001
From: miku-666 <74728189+NessieHax@users.noreply.github.com>
Date: Sun, 25 Sep 2022 13:15:41 +0200
Subject: [PATCH] Add `SetImageTo3DST` to 3DSUtil
---
PCK-Studio/Classes/Texturing/TextureCodec.cs | 10 ++--
PCK-Studio/Classes/Utils/3DS/3DSUtil.cs | 61 ++++++++++----------
2 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/PCK-Studio/Classes/Texturing/TextureCodec.cs b/PCK-Studio/Classes/Texturing/TextureCodec.cs
index 60dad1f8..fa175190 100644
--- a/PCK-Studio/Classes/Texturing/TextureCodec.cs
+++ b/PCK-Studio/Classes/Texturing/TextureCodec.cs
@@ -17,7 +17,7 @@ namespace Ohana3DS_Rebirth.Ohana
/// Height of the Texture
/// Pixel Format of the Texture
///
- 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
/// Input image to be encoded
/// Pixel Format of the Texture
///
- 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;
diff --git a/PCK-Studio/Classes/Utils/3DS/3DSUtil.cs b/PCK-Studio/Classes/Utils/3DS/3DSUtil.cs
index 1b44cac3..bf108095 100644
--- a/PCK-Studio/Classes/Utils/3DS/3DSUtil.cs
+++ b/PCK-Studio/Classes/Utils/3DS/3DSUtil.cs
@@ -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);
}
}
}