mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-05 02:32:03 +08:00
reorganized project directory structure.
This commit is contained in:
186
ArcFormats/Crowd/ArcPCK.cs
Normal file
186
ArcFormats/Crowd/ArcPCK.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
//! \file ArcPCK.cs
|
||||
//! \date Thu Jun 11 12:58:00 2015
|
||||
//! \brief Crowd engine resource archive.
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Crowd
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PckOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "PCK"; } }
|
||||
public override string Description { get { return "Crowd engine resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (0);
|
||||
if (count <= 0 || count > 0xfffff)
|
||||
return null;
|
||||
long index_offset = 4;
|
||||
uint index_size = (uint)(0xc * count);
|
||||
if (index_size > file.View.Reserve (index_offset, index_size))
|
||||
return null;
|
||||
var dir = new List<Entry>();
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
var entry = new Entry {
|
||||
Offset = file.View.ReadUInt32 (index_offset+4),
|
||||
Size = file.View.ReadUInt32 (index_offset+8)
|
||||
};
|
||||
if (entry.Offset < index_size || !entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 12;
|
||||
}
|
||||
byte[] name_buf = new byte[260];
|
||||
foreach (var entry in dir)
|
||||
{
|
||||
uint max_len = Math.Min (260u, file.View.Reserve (index_offset, 260));
|
||||
uint n;
|
||||
for (n = 0; n < max_len; ++n)
|
||||
{
|
||||
byte b = file.View.ReadByte (index_offset+n);
|
||||
if (0 == b)
|
||||
break;
|
||||
name_buf[n] = b;
|
||||
}
|
||||
if (0 == n || max_len == n)
|
||||
return null;
|
||||
entry.Name = Encodings.cp932.GetString (name_buf, 0, (int)n);
|
||||
entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
|
||||
index_offset += n+1;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
|
||||
internal class PkwEntry : PackedEntry
|
||||
{
|
||||
public WaveFormat Format;
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PkwOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "PKWV"; } }
|
||||
public override string Description { get { return "Crowd engine audio archive"; } }
|
||||
public override uint Signature { get { return 0x56574b50; } } // 'PKWV'
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public PkwOpener ()
|
||||
{
|
||||
Extensions = new string[] { "PCK" };
|
||||
}
|
||||
|
||||
const uint WaveHeaderSize = 8/*RIFF*/ + 12/*WAVEfmt*/ + 0x10/*fmt*/ + 8/*data*/;
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int format_count = file.View.ReadUInt16 (4);
|
||||
int count = file.View.ReadUInt16 (6);
|
||||
if (0 == format_count || 0 == count)
|
||||
return null;
|
||||
uint index_offset = 8;
|
||||
long base_offset = index_offset + format_count*0x14 + count*0x18;
|
||||
if (base_offset >= file.MaxOffset)
|
||||
return null;
|
||||
var formats = new List<WaveFormat> (format_count);
|
||||
for (int i = 0; i < format_count; ++i)
|
||||
{
|
||||
var format = new WaveFormat
|
||||
{
|
||||
FormatTag = file.View.ReadUInt16 (index_offset),
|
||||
Channels = file.View.ReadUInt16 (index_offset+2),
|
||||
SamplesPerSecond = file.View.ReadUInt32 (index_offset+4),
|
||||
AverageBytesPerSecond = file.View.ReadUInt32 (index_offset+8),
|
||||
BitsPerSample = file.View.ReadUInt16 (index_offset+12),
|
||||
BlockAlign = file.View.ReadUInt16 (index_offset+14),
|
||||
};
|
||||
index_offset += 0x14;
|
||||
formats.Add (format);
|
||||
}
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
int fmt_index = file.View.ReadUInt16 (index_offset);
|
||||
if (fmt_index > formats.Count)
|
||||
return null;
|
||||
string name = file.View.ReadString (index_offset+2, 0x0A);
|
||||
var entry = new PkwEntry
|
||||
{
|
||||
Name = name + ".wav",
|
||||
Type = "audio",
|
||||
Offset = base_offset + file.View.ReadInt64 (index_offset+0x10),
|
||||
Size = file.View.ReadUInt32 (index_offset+0x0C),
|
||||
IsPacked = true,
|
||||
Format = formats[fmt_index],
|
||||
};
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
entry.UnpackedSize = entry.Size + WaveHeaderSize;
|
||||
dir.Add (entry);
|
||||
index_offset += 0x18;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
var went = entry as PkwEntry;
|
||||
if (null == went)
|
||||
return arc.File.CreateStream (entry.Offset, entry.Size);
|
||||
var riff = new byte[0x2c];
|
||||
using (var buf = new MemoryStream (riff))
|
||||
using (var wav = new BinaryWriter (buf))
|
||||
{
|
||||
wav.Write (0x46464952); // 'RIFF'
|
||||
uint total_size = went.UnpackedSize - 8;
|
||||
wav.Write (total_size);
|
||||
wav.Write (0x45564157); // 'WAVE'
|
||||
wav.Write (0x20746d66); // 'fmt '
|
||||
wav.Write (0x10);
|
||||
wav.Write (went.Format.FormatTag);
|
||||
wav.Write (went.Format.Channels);
|
||||
wav.Write (went.Format.SamplesPerSecond);
|
||||
wav.Write (went.Format.AverageBytesPerSecond);
|
||||
wav.Write (went.Format.BlockAlign);
|
||||
wav.Write (went.Format.BitsPerSample);
|
||||
wav.Write (0x61746164); // 'data'
|
||||
wav.Write (went.Size);
|
||||
wav.Flush ();
|
||||
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
||||
return new PrefixStream (riff, input);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
ArcFormats/Crowd/AudioEOG.cs
Normal file
51
ArcFormats/Crowd/AudioEOG.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! \file AudioEOG.cs
|
||||
//! \date Thu Jun 11 12:46:35 2015
|
||||
//! \brief Crowd engine audio file.
|
||||
//
|
||||
// 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;
|
||||
|
||||
namespace GameRes.Formats.Crowd
|
||||
{
|
||||
[Export(typeof(AudioFormat))]
|
||||
public class EogAudio : OggAudio
|
||||
{
|
||||
public override string Tag { get { return "EOG"; } }
|
||||
public override string Description { get { return "Crowd engine audio format (Ogg/Vorbis)"; } }
|
||||
public override uint Signature { get { return 0x004D5243; } } // 'CRM'
|
||||
|
||||
public override SoundInput TryOpen (Stream file)
|
||||
{
|
||||
var ogg = new StreamRegion (file, 8);
|
||||
return new OggInput (ogg);
|
||||
// in case of exception ogg stream is left undisposed
|
||||
}
|
||||
|
||||
public override void Write (SoundInput source, Stream output)
|
||||
{
|
||||
throw new System.NotImplementedException ("EogFormat.Write not implemenented");
|
||||
}
|
||||
}
|
||||
}
|
||||
124
ArcFormats/Crowd/ImageCWL.cs
Normal file
124
ArcFormats/Crowd/ImageCWL.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
//! \file ImageCWL.cs
|
||||
//! \date Sat Jun 13 09:36:33 2015
|
||||
//! \brief Crowd compressed 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;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
using GameRes.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Crowd
|
||||
{
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class CwdFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "CWD"; } }
|
||||
public override string Description { get { return "Crowd hi-color bitmap"; } }
|
||||
public override uint Signature { get { return 0x20647763u; } } // 'cwd '
|
||||
|
||||
static readonly byte[] SignatureText = Encoding.ASCII.GetBytes ("cwd format - version 1.00 -");
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
var header = new byte[0x38];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
if (!header.Take (SignatureText.Length).SequenceEqual (SignatureText))
|
||||
return null;
|
||||
uint key = header[0x34] + 0x259Au;
|
||||
return new ImageMetaData
|
||||
{
|
||||
Width = LittleEndian.ToUInt32 (header, 0x2c) + key,
|
||||
Height = LittleEndian.ToUInt32 (header, 0x30) + key,
|
||||
BPP = 15,
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
stream.Position = 0x38;
|
||||
int size = (int)info.Width * (int)info.Height * 2;
|
||||
var pixels = new byte[size];
|
||||
if (pixels.Length != stream.Read (pixels, 0, pixels.Length))
|
||||
throw new InvalidFormatException ("Unexpected end of file");
|
||||
return ImageData.Create (info, PixelFormats.Bgr555, null, pixels);
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("CwdFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class CwlFormat : CwdFormat
|
||||
{
|
||||
public override string Tag { get { return "CWL"; } }
|
||||
public override string Description { get { return "LZ-compressed Crowd bitmap"; } }
|
||||
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
stream.Position = 0x0e;
|
||||
using (var lz = new LzssReader (stream, 100, 0x38)) // extract CWD header
|
||||
{
|
||||
lz.FrameSize = 0x1000;
|
||||
lz.FrameFill = 0x20;
|
||||
lz.FrameInitPos = 0x1000 - 0x10;
|
||||
lz.Unpack();
|
||||
using (var cwd = new MemoryStream (lz.Data))
|
||||
return base.ReadMetaData (cwd);
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
if (stream.Length > int.MaxValue)
|
||||
throw new FileSizeException();
|
||||
var header = new byte[14];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
throw new InvalidFormatException();
|
||||
int data_length = LittleEndian.ToInt32 (header, 10);
|
||||
int input_length = (int)(stream.Length-stream.Position);
|
||||
using (var lz = new LzssReader (stream, input_length, data_length))
|
||||
{
|
||||
lz.FrameSize = 0x1000;
|
||||
lz.FrameFill = 0x20;
|
||||
lz.FrameInitPos = 0x1000 - 0x10;
|
||||
lz.Unpack();
|
||||
using (var cwd = new MemoryStream (lz.Data))
|
||||
return base.Read (cwd, info);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("CwlFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
}
|
||||
188
ArcFormats/Crowd/ImageCWP.cs
Normal file
188
ArcFormats/Crowd/ImageCWP.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
//! \file ImageCWP.cs
|
||||
//! \date Thu Jun 11 13:43:41 2015
|
||||
//! \brief Crowd engine 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;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Crowd
|
||||
{
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class CwpFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "CWP"; } }
|
||||
public override string Description { get { return "Crowd engine image format"; } }
|
||||
public override uint Signature { get { return 0x50445743; } } // 'CWDP'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
using (var input = new ArcView.Reader (stream))
|
||||
{
|
||||
input.ReadInt32();
|
||||
uint width = Binary.BigEndian (input.ReadUInt32());
|
||||
uint height = Binary.BigEndian (input.ReadUInt32());
|
||||
if (0 == width || 0 == height)
|
||||
return null;
|
||||
int bpp = input.ReadByte();
|
||||
int color_type = input.ReadByte();
|
||||
switch (color_type)
|
||||
{
|
||||
case 2: bpp *= 3; break;
|
||||
case 4: bpp *= 2; break;
|
||||
case 6: bpp *= 4; break;
|
||||
case 3:
|
||||
case 0: break;
|
||||
default: return null;
|
||||
}
|
||||
return new ImageMetaData
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
BPP = bpp,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
var header = new byte[0x15];
|
||||
using (var mem = new MemoryStream((int)(0x14 + stream.Length + 12)))
|
||||
using (var png = new BinaryWriter (mem))
|
||||
{
|
||||
png.Write (0x474E5089u); // png header
|
||||
png.Write (0x0A1A0A0Du);
|
||||
png.Write (0x0D000000u);
|
||||
png.Write (0x52444849u); // 'IHDR'
|
||||
stream.Position = 4;
|
||||
stream.Read (header, 0, header.Length);
|
||||
png.Write (header, 0, header.Length);
|
||||
png.Write (0x54414449u); // 'IDAT'
|
||||
stream.CopyTo (mem);
|
||||
header[1] = 0;
|
||||
header[2] = 0;
|
||||
header[3] = 0;
|
||||
LittleEndian.Pack (0x444E4549, header, 4);
|
||||
LittleEndian.Pack (0x826042AE, header, 8);
|
||||
png.Write (header, 1, 11);
|
||||
mem.Position = 0;
|
||||
var decoder = new PngBitmapDecoder (mem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
||||
BitmapSource frame = decoder.Frames[0];
|
||||
var pixels = new byte[info.Width*info.Height*4];
|
||||
frame.CopyPixels (pixels, (int)info.Width*4, 0);
|
||||
for (int i = 0; i < pixels.Length; i += 4)
|
||||
{
|
||||
byte t = pixels[i];
|
||||
pixels[i] = pixels[i+2];
|
||||
pixels[i+2] = t;
|
||||
}
|
||||
return ImageData.Create (info, PixelFormats.Bgr32, null, pixels);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
var writer = new Writer (image);
|
||||
writer.Write (file);
|
||||
}
|
||||
|
||||
internal class Writer
|
||||
{
|
||||
BitmapSource m_bitmap;
|
||||
byte[] m_buffer = new byte[81920];
|
||||
|
||||
public Writer (ImageData image)
|
||||
{
|
||||
m_bitmap = image.Bitmap;
|
||||
if (m_bitmap.Format.BitsPerPixel < 32)
|
||||
{
|
||||
m_bitmap = new FormatConvertedBitmap (m_bitmap, PixelFormats.Bgr32, null, 0);
|
||||
}
|
||||
int stride = m_bitmap.PixelWidth * 4;
|
||||
var pixels = new byte[stride*m_bitmap.PixelHeight];
|
||||
m_bitmap.CopyPixels (pixels, stride, 0);
|
||||
for (int i = 0; i < pixels.Length; i += 4)
|
||||
{
|
||||
byte t = pixels[i];
|
||||
pixels[i] = pixels[i+2];
|
||||
pixels[i+2] = t;
|
||||
}
|
||||
m_bitmap = BitmapSource.Create (m_bitmap.PixelWidth, m_bitmap.PixelHeight,
|
||||
m_bitmap.DpiX, m_bitmap.DpiY, PixelFormats.Bgra32, null, pixels, stride);
|
||||
}
|
||||
|
||||
public void Write (Stream file)
|
||||
{
|
||||
var encoder = new PngBitmapEncoder();
|
||||
encoder.Frames.Add (BitmapFrame.Create (m_bitmap));
|
||||
using (var png = new MemoryStream())
|
||||
using (var cwp = new BinaryWriter (file, System.Text.Encoding.ASCII, true))
|
||||
{
|
||||
encoder.Save (png);
|
||||
var header = new byte[0x11];
|
||||
png.Position = 0x10;
|
||||
png.Read (header, 0, header.Length);
|
||||
cwp.Write (0x50445743u); // 'CWDP'
|
||||
cwp.Write (header, 0, header.Length);
|
||||
var idat = PngFormat.FindChunk (png, "IDAT");
|
||||
if (-1 == idat)
|
||||
throw new InvalidFormatException ("CWP conversion failed");
|
||||
png.Position = idat;
|
||||
png.Read (header, 0, 8);
|
||||
int chunk_size = BigEndian.ToInt32 (header, 0) + 4;
|
||||
cwp.Write (header, 0, 4);
|
||||
for (;;)
|
||||
{
|
||||
CopyChunk (png, file, chunk_size);
|
||||
if (8 != png.Read (header, 0, 8))
|
||||
throw new InvalidFormatException ("CWP conversion failed");
|
||||
if (Binary.AsciiEqual (header, 4, "IEND"))
|
||||
{
|
||||
cwp.Write ((byte)0);
|
||||
break;
|
||||
}
|
||||
chunk_size = BigEndian.ToInt32 (header, 0) + 4;
|
||||
cwp.Write (header, 0, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CopyChunk (Stream src, Stream dst, int size)
|
||||
{
|
||||
while (size > 0)
|
||||
{
|
||||
int amount = Math.Min (size, m_buffer.Length);
|
||||
int read = src.Read (m_buffer, 0, amount);
|
||||
if (read != amount)
|
||||
throw new InvalidFormatException ("CWP conversion failed");
|
||||
dst.Write (m_buffer, 0, amount);
|
||||
size -= amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
87
ArcFormats/Crowd/ImageZBM.cs
Normal file
87
ArcFormats/Crowd/ImageZBM.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
//! \file ImageZBM.cs
|
||||
//! \date Thu Jun 11 16:24:09 2015
|
||||
//! \brief LZ-compressed bitmap 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;
|
||||
using GameRes.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Crowd
|
||||
{
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class ZbmFormat : BmpFormat
|
||||
{
|
||||
public override string Tag { get { return "ZBM"; } }
|
||||
public override string Description { get { return "LZ-compressed bitmap"; } }
|
||||
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
stream.Position = 0x0e;
|
||||
using (var lz = new LzssReader (stream, 100, 54)) // extract BMP header
|
||||
{
|
||||
lz.FrameSize = 0x1000;
|
||||
lz.FrameFill = 0x20;
|
||||
lz.FrameInitPos = 0x1000 - 0x10;
|
||||
lz.Unpack();
|
||||
var header = lz.Data;
|
||||
for (int i = 0; i < 54; ++i)
|
||||
header[i] ^= 0xff;
|
||||
using (var bmp = new MemoryStream (header))
|
||||
return base.ReadMetaData (bmp);
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
if (stream.Length > int.MaxValue)
|
||||
throw new FileSizeException();
|
||||
var header = new byte[14];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
throw new InvalidFormatException();
|
||||
int data_length = LittleEndian.ToInt32 (header, 10);
|
||||
int input_length = (int)(stream.Length-stream.Position);
|
||||
using (var lz = new LzssReader (stream, input_length, data_length))
|
||||
{
|
||||
lz.FrameSize = 0x1000;
|
||||
lz.FrameFill = 0x20;
|
||||
lz.FrameInitPos = 0x1000 - 0x10;
|
||||
lz.Unpack();
|
||||
var data = lz.Data;
|
||||
int count = Math.Min (100, data.Length);
|
||||
for (int i = 0; i < count; ++i)
|
||||
data[i] ^= 0xff;
|
||||
using (var bmp = new MemoryStream (data))
|
||||
return base.Read (bmp, info);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("ZbmFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user