177 lines
6.3 KiB
C#
Raw Normal View History

2015-08-17 07:03:43 +04:00
//! \file ImageKG.cs
//! \date Mon Aug 17 01:50:50 2015
//! \brief Interheart image format.
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.ComponentModel.Composition;
using System.IO;
2023-12-29 22:55:57 +06:00
using System.Linq;
using System.Collections.Generic;
using System.Windows;
2015-08-17 07:03:43 +04:00
using System.Windows.Media;
2023-12-29 22:55:57 +06:00
using System.Windows.Media.Imaging;
2015-08-17 07:03:43 +04:00
using GameRes.Utility;
namespace GameRes.Formats.Interheart
{
[Export(typeof(ImageFormat))]
public class KgFormat : ImageFormat
{
public override string Tag { get { return "KG"; } }
public override string Description { get { return "Interheart image format"; } }
public override uint Signature { get { return 0x4B474347; } } // 'GCGK'
2023-12-30 04:01:40 +06:00
public override bool CanWrite { get { return true; } }
2015-08-17 07:03:43 +04:00
2016-10-16 09:22:53 +04:00
public override ImageMetaData ReadMetaData (IBinaryStream stream)
2015-08-17 07:03:43 +04:00
{
2016-10-16 09:22:53 +04:00
stream.Position = 4;
uint width = stream.ReadUInt16();
uint height = stream.ReadUInt16();
int packed_size = stream.ReadInt32();
2015-08-17 07:03:43 +04:00
if (packed_size <= 0)
return null;
return new ImageMetaData
{
Width = width,
Height = height,
BPP = 32,
};
}
2016-10-16 09:22:53 +04:00
public override ImageData Read (IBinaryStream input, ImageMetaData info)
2015-08-17 07:03:43 +04:00
{
byte[] pixels = new byte[info.Width*info.Height*4];
2016-10-16 09:22:53 +04:00
input.Position = 12;
uint[] offset_table = new uint[info.Height];
for (uint i = 0; i < info.Height; ++i)
offset_table[i] = input.ReadUInt32();
2015-08-17 07:03:43 +04:00
2016-10-16 09:22:53 +04:00
long base_offset = input.Position;
int dst = 0;
foreach (var offset in offset_table)
{
input.Position = base_offset + offset;
for (int x = 0; x < info.Width; )
2015-08-17 07:03:43 +04:00
{
2016-10-16 09:22:53 +04:00
byte alpha = input.ReadUInt8();
int count = input.ReadUInt8();
if (0 == count)
count = 0x100;
if (0 == alpha)
2015-08-17 07:03:43 +04:00
{
2016-10-16 09:22:53 +04:00
dst += count * 4;
}
else
{
for (int n = 0; n < count; ++n)
2015-08-17 07:03:43 +04:00
{
2016-10-16 09:22:53 +04:00
pixels[dst+3] = alpha;
pixels[dst+2] = input.ReadUInt8();
pixels[dst+1] = input.ReadUInt8();
pixels[dst] = input.ReadUInt8();
dst += 4;
2015-08-17 07:03:43 +04:00
}
}
2016-10-16 09:22:53 +04:00
x += count;
2015-08-17 07:03:43 +04:00
}
}
2016-10-16 09:22:53 +04:00
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
2015-08-17 07:03:43 +04:00
}
2023-12-29 22:55:57 +06:00
public override void Write (Stream file, ImageData image) {
2023-12-30 04:01:40 +06:00
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);
2023-12-29 22:55:57 +06:00
}
uint[] offset_table = new uint[image.Height];
var kg_data = new List<byte>();
2023-12-30 04:01:40 +06:00
for (int y = 0; y < image.Height; y++) {
offset_table[y] = (uint)kg_data.Count();
2023-12-29 22:55:57 +06:00
2023-12-30 04:01:40 +06:00
bitmap
.AsMemory()
.Slice(y * stride, stride)
2023-12-29 22:55:57 +06:00
.Chunk(4)
2023-12-30 04:01:40 +06:00
.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 }));
2023-12-29 22:55:57 +06:00
2023-12-30 04:01:40 +06:00
if (!chunks.Any()) {
new_chunk();
2023-12-29 22:55:57 +06:00
}
else {
2023-12-30 04:01:40 +06:00
var (alpha, data) = chunks.Last();
if (pixel[3] != data.Last()[3] || data.Count >= 256) {
new_chunk();
2023-12-29 22:55:57 +06:00
}
else {
2023-12-30 04:01:40 +06:00
data.Add(pixel);
2023-12-29 22:55:57 +06:00
}
}
2023-12-30 04:01:40 +06:00
return chunks;
2023-12-29 22:55:57 +06:00
}
)
.ForEach(chunk => {
kg_data.Add(chunk.alpha);
kg_data.Add((byte)chunk.data.Count());
if (chunk.alpha > 0) {
2023-12-30 04:01:40 +06:00
kg_data.AddRange(chunk.data.SelectMany(p => new[] { p[2], p[1], p[0] }));
2023-12-29 22:55:57 +06:00
}
});
}
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 {
2023-12-30 04:01:40 +06:00
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);
2023-12-29 22:55:57 +06:00
}
}
2015-08-17 07:03:43 +04:00
}
}
}