Skip to content

Commit

Permalink
Fixed bug that occurred when reading RGBA8 images for Melee because t…
Browse files Browse the repository at this point in the history
…hey're actually stored as AR8GB8.
  • Loading branch information
MeltyPlayer committed Oct 25, 2023
1 parent 2f0a477 commit 58e6f13
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 8 deletions.
1 change: 1 addition & 0 deletions FinModelUtility/Fin/Fin/src/image/PixelFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum PixelFormat {
RGBA5553,
RGBA8888,
RGBA16161616,
AR88GB88,

HILO88,

Expand Down
48 changes: 48 additions & 0 deletions FinModelUtility/Fin/Fin/src/image/io/tile/Ar88Gb88TileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using fin.image.formats;

using schema.binary;

using SixLabors.ImageSharp.PixelFormats;

namespace fin.image.io.tile {
public readonly struct Ar88Gb88TileReader : ITileReader<Rgba32> {
public IImage<Rgba32> CreateImage(int width, int height)
=> new Rgba32Image(PixelFormat.AR88GB88, width, height);

public int TileWidth => 4;
public int TileHeight => 4;

public unsafe void Decode(IBinaryReader br,
Rgba32* scan0,
int tileX,
int tileY,
int imageWidth,
int imageHeight) {
var x = tileX * this.TileWidth;
var y = tileY * this.TileHeight;

for (int k = 0; k < 2; k++) {
for (int y1 = y; y1 < y + this.TileHeight; y1++) {
for (int x1 = x; x1 < x + this.TileWidth; x1++) {
var offset = y1 * imageWidth + x1;
var pixel = br.ReadUInt16();

if (x1 > imageWidth || y1 > imageHeight) {
continue;
}

var rgba = scan0[offset];
if (k == 0) {
rgba.A = (byte) (pixel >> 8);
rgba.R = (byte) (pixel & 0xff);
} else {
rgba.G = (byte) (pixel >> 8);
rgba.B = (byte) (pixel & 0xff);
}
scan0[offset] = rgba;
}
}
}
}
}
}
72 changes: 72 additions & 0 deletions FinModelUtility/Formats/Dat/src/image/DatImageReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using fin.image;
using fin.image.io;
using fin.image.io.image;
using fin.image.io.pixel;
using fin.image.io.tile;

using gx;

using schema.binary;

namespace dat.image {
public class DatImageReader : IImageReader {
private readonly IImageReader impl_;

public DatImageReader(int width, int height, GxTextureFormat format) {
this.impl_ = this.CreateImpl_(width, height, format);
}

private IImageReader CreateImpl_(int width,
int height,
GxTextureFormat format) {
return format switch {
GxTextureFormat.I4 => TiledImageReader.New(
width,
height,
8,
8,
new L2a4PixelReader()),
GxTextureFormat.I8 => TiledImageReader.New(
width,
height,
8,
4,
new L2a8PixelReader()),
GxTextureFormat.A4_I4 => TiledImageReader.New(
width,
height,
8,
4,
new Al8PixelReader()),
GxTextureFormat.A8_I8 => TiledImageReader.New(
width,
height,
4,
4,
new Al16PixelReader()),
GxTextureFormat.R5_G6_B5 => TiledImageReader.New(
width,
height,
4,
4,
new Rgb565PixelReader()),
GxTextureFormat.A3_RGB5 => TiledImageReader.New(
width,
height,
4,
4,
new Rgba5553PixelReader()),
GxTextureFormat.ARGB8 => TiledImageReader.New(
width,
height,
new Ar88Gb88TileReader()),
GxTextureFormat.S3TC1 => new CmprImageReader(width, height),
};
}

public IImage ReadImage(IBinaryReader br) => this.impl_.ReadImage(br);

public IImage ReadImage(byte[] data, Endianness endianness)
=> this.impl_.ReadImage(data, endianness);
}
}
5 changes: 4 additions & 1 deletion FinModelUtility/Formats/Dat/src/schema/PObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ public void Read(IBinaryReader br) {
sbr => sbr.ReadVector2(vertexDescriptor));
break;
}
default: throw new NotImplementedException();
default: {
break;
//throw new NotImplementedException();
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions FinModelUtility/Formats/Dat/src/schema/TObj.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Drawing;

using dat.image;

using fin.image;

using gx;
using gx.image;

using schema.binary;

Expand Down Expand Up @@ -40,7 +41,7 @@ public void Read(IBinaryReader br) {
// TODO: Add support for indexed textures
try {
br.Position = imageDataOffset;
this.Image = new GxImageReader(width, height, format).ReadImage(br);
this.Image = new DatImageReader(width, height, format).ReadImage(br);
} catch {
this.Image = FinImage.Create1x1FromColor(Color.Magenta);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using fin.image.io.image;
using fin.image.io.pixel;

using gx;

using schema.binary;

namespace gx.image {
public class GxImageReader : IImageReader {
namespace j3d.image {
public class J3dImageReader : IImageReader {
private readonly IImageReader impl_;

public GxImageReader(int width, int height, GxTextureFormat format) {
public J3dImageReader(int width, int height, GxTextureFormat format) {
this.impl_ = this.CreateImpl_(width, height, format);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
using fin.util.color;

using gx;
using gx.image;

using j3d.image;

using schema.binary;
using schema.binary.attributes;
Expand Down Expand Up @@ -126,7 +127,7 @@ private void ReadPalettes_(IBinaryReader br) {

public unsafe IImage ToBitmap() {
try {
return new GxImageReader(this.Width, this.Height, this.Format).ReadImage(
return new J3dImageReader(this.Width, this.Height, this.Format).ReadImage(
this.Data,
Endianness.BigEndian);
} catch { }
Expand Down

0 comments on commit 58e6f13

Please sign in to comment.