feat: MAGES PS3/PSV Image Format decoding

This commit is contained in:
ManicSteiner 2023-12-09 23:48:09 +08:00
parent 8188874e81
commit d4cde67f43
3 changed files with 117 additions and 0 deletions

View File

@ -172,6 +172,7 @@
<Compile Include="Macromedia\Palettes.cs" />
<Compile Include="MAGES\ArcARC20.cs" />
<Compile Include="MAGES\ArcLoveOnce.cs" />
<Compile Include="MAGES\ImageBIN.cs" />
<Compile Include="Mugi\ArcBIN.cs" />
<Compile Include="NipponIchi\ArcCASN.cs" />
<Compile Include="NipponIchi\ArcPSFS.cs" />

View File

@ -65,6 +65,16 @@ namespace GameRes.Formats.BGI
return null;
if (0 != stream.ReadInt64())
return null;
if (flag == 0)
{
int stride = (int)width * ((bpp + 7) / 8);
var pixels = new byte[stride * (int)height];
int read = stream.Read(pixels, 0, pixels.Length);
if (read != pixels.Length)
return null;
}
return new BgiMetaData
{
Width = (uint)width,

View File

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace GameRes.Formats.MAGES
{
[Export(typeof(ImageFormat))]
public class BinFormat : ImageFormat
{
public override string Tag { get { return "MAGES PS3/PSV Image Format"; } }
public override string Description { get { return "MAGES PS3/PSV Image Format"; } }
public override uint Signature { get { return 0; } }
public BinFormat()
{
Extensions = new string[] { "" };
}
public override ImageMetaData ReadMetaData(IBinaryStream file)
{
//var header = file.ReadHeader(8);
int width = file.ReadInt16();
int height = file.ReadInt16();
if (width <= 0 || height <= 0)
return null;
int bpp = file.ReadInt16();
if (32 == bpp)
{
uint imagedatasize = (uint)width * (uint)height * 4;
if (file.Length != imagedatasize + 8)
return null;
}
else if (8 == bpp)
{
uint imagedatasize = (uint)width * (uint)height + 256;
if (file.Length != imagedatasize + 8)
return null;
}
else
return null;
return new ImageMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
};
}
public override ImageData Read(IBinaryStream file, ImageMetaData info)
{
if (info == null)
throw new NotSupportedException(string.Format("Not BIN texture format."));
if (info.BPP == 32)
{
uint pixelnum = info.Width * info.Height;
if (file.Length != pixelnum * 4 + 8) throw new NotSupportedException(string.Format("Not BIN 32ARGB texture format."));
file.Position = 8;
//var data = file.ReadBytes(info.iWidth * info.iHeight * 4);
List<byte> pixels = new List<byte>();
for (int i = 0; i < pixelnum; i++)
{
var pixel = file.ReadBytes(4); //ARGB
//BGRA
pixels.Add(pixel[3]);
pixels.Add(pixel[2]);
pixels.Add(pixel[1]);
pixels.Add(pixel[0]);
}
return ImageData.Create(info, PixelFormats.Bgra32, null, pixels.ToArray());
}
else if (info.BPP == 8)
{
uint imagedatasize = info.Width * info.Height + 256;
if (file.Length != imagedatasize + 8) throw new NotSupportedException(string.Format("Not BIN 256colors texture format."));
file.Position = 8;
//var pixelColor = file.ReadBytes(256 * 4);
List<Color> colors = new List<Color>();
for (int i = 0; i < 256; i += 4)
{
Color c = new Color();
var color_b = file.ReadBytes(4); //BGRA
c.B = color_b[0];
c.G = color_b[1];
c.R = color_b[2];
c.A = color_b[3];
colors.Add(c);
}
BitmapPalette palette = new BitmapPalette(colors);
//file.Position += 256 * 4;
var data = file.ReadBytes(info.iWidth * info.iHeight);
return ImageData.Create(info, PixelFormats.Indexed8, palette, data);
}
else
throw new NotSupportedException(string.Format("Not BIN texture format."));
}
public override void Write(Stream file, ImageData image)
{
throw new System.NotImplementedException("BINFormat.Write not implemented");
}
}
}