This commit is contained in:
ManicSteiner 2024-02-23 11:22:02 +08:00
commit c1008aba3e
3 changed files with 79 additions and 5 deletions

View File

@ -26,7 +26,11 @@
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.Interheart
@ -37,6 +41,7 @@ namespace GameRes.Formats.Interheart
public override string Tag { get { return "KG"; } }
public override string Description { get { return "Interheart image format"; } }
public override uint Signature { get { return 0x4B474347; } } // 'GCGK'
public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
@ -94,9 +99,78 @@ namespace GameRes.Formats.Interheart
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("KgFormat.Write not implemented");
public override void Write (Stream file, ImageData image) {
int stride = (int)image.Width * 4;
byte[] bitmap = new byte[stride * image.Height];
{
var image_bgra = image.Bitmap.Format != PixelFormats.Bgra32
? new FormatConvertedBitmap(image.Bitmap, PixelFormats.Bgra32, null, 0)
: image.Bitmap;
image_bgra.CopyPixels(bitmap, stride, 0);
}
uint[] offset_table = new uint[image.Height];
var kg_data = new List<byte>();
for (int y = 0; y < image.Height; y++) {
offset_table[y] = (uint)kg_data.Count();
bitmap
.AsMemory()
.Slice(y * stride, stride)
.Chunk(4)
.Aggregate(new List<(byte alpha, List<byte[]> data)> { }, (chunks, x) => {
var pixel = x.ToArray();
void new_chunk () => chunks.Add((alpha: pixel[3], data: new List<byte[]> { pixel }));
if (!chunks.Any()) {
new_chunk();
}
else {
var (alpha, data) = chunks.Last();
if (pixel[3] != data.Last()[3] || data.Count >= 256) {
new_chunk();
}
else {
data.Add(pixel);
}
}
return chunks;
}
)
.ForEach(chunk => {
kg_data.Add(chunk.alpha);
kg_data.Add((byte)chunk.data.Count());
if (chunk.alpha > 0) {
kg_data.AddRange(chunk.data.SelectMany(p => new[] { p[2], p[1], p[0] }));
}
});
}
var bw = new BinaryWriter(file);
bw.Write(new byte[] { 0x47, 0x43, 0x47, 0x4B });
bw.Write((UInt16)image.Width);
bw.Write((UInt16)image.Height);
bw.Write((uint)kg_data.Count);
{
byte[] buf = new byte[offset_table.Length * sizeof(uint)];
Buffer.BlockCopy(offset_table, 0, buf, 0, buf.Length);
bw.Write(buf);
}
bw.Write(kg_data.ToArray());
}
}
static class Extensions {
public static IEnumerable<Memory<T>> Chunk<T>(this Memory<T> arr, int size) {
for (var i = 0; i < arr.Length / size + 1; i++) {
if (i * size < arr.Length) {
yield return arr.Slice(i * size, size);
}
}
}
}
}

Binary file not shown.

View File

@ -101,8 +101,8 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>If $(ConfigurationName) == Release del *.pdb
If $(ConfigurationName) == Release del *.config</PostBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.