From d4cde67f43056208238434d3cc6bc06b982416e1 Mon Sep 17 00:00:00 2001 From: ManicSteiner Date: Sat, 9 Dec 2023 23:48:09 +0800 Subject: [PATCH] feat: MAGES PS3/PSV Image Format decoding --- ArcFormats/ArcFormats.csproj | 1 + ArcFormats/Ethornell/ImageBGI.cs | 10 +++ ArcFormats/MAGES/ImageBIN.cs | 106 +++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 ArcFormats/MAGES/ImageBIN.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 4aac478c..28bc40b1 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -172,6 +172,7 @@ + diff --git a/ArcFormats/Ethornell/ImageBGI.cs b/ArcFormats/Ethornell/ImageBGI.cs index 05065503..b4d9ba49 100644 --- a/ArcFormats/Ethornell/ImageBGI.cs +++ b/ArcFormats/Ethornell/ImageBGI.cs @@ -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, diff --git a/ArcFormats/MAGES/ImageBIN.cs b/ArcFormats/MAGES/ImageBIN.cs new file mode 100644 index 00000000..32c2e037 --- /dev/null +++ b/ArcFormats/MAGES/ImageBIN.cs @@ -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 pixels = new List(); + 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 colors = new List(); + 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"); + } + } +}