Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIM2 format edit #76

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ArcFormats/ArcFormats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@
<Compile Include="Artemis\ImageNekoPNG.cs" />
<Compile Include="CsWare\AudioWAV.cs" />
<Compile Include="CsWare\ImageGDT.cs" />
<Compile Include="DigitalWorks\ArcPACsingle.cs" />
<Compile Include="DigitalWorks\ArcPACPS2.cs" />
<Compile Include="DigitalWorks\ImageTM2arc.cs" />
<Compile Include="DxLib\HuffmanDecoder.cs" />
<Compile Include="DxLib\WidgetDXA.xaml.cs">
<DependentUpon>WidgetDXA.xaml</DependentUpon>
Expand Down
88 changes: 88 additions & 0 deletions ArcFormats/DigitalWorks/ArcPACsingle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! \file ArcPACPS2.cs
//! \date 2018 Sep 18
//! \brief Digital Works PS2 resource archive.
//
// Copyright (C) 2018 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using GameRes.Compression;

namespace GameRes.Formats.DigitalWorks
{
[Export(typeof(ArchiveFormat))]
public class PacSingleOpener : ArchiveFormat
{
public override string Tag { get { return "PAC/LZS-TIM2"; } }
public override string Description { get { return "LZS-TIM2 Image archive"; } }
public override uint Signature { get { return 0x535A4C; } } // 'LZS'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }

/**
Target games:
Cafe Little Wish SLPM-65294
F Fanatic SLPM-65296
*/

public override ArcFile TryOpen (ArcView file)
{
if (!file.View.AsciiEqual(9, "TIM2"))
return null;
var dir = new List<Entry> (1);
var entry = FormatCatalog.Instance.Create<PackedEntry> (file.Name);
entry.Offset = 0L;
entry.Size = (uint)file.MaxOffset;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);

return new ArcFile (file, this, dir);
}

public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var pent = entry as PackedEntry;
if (null == pent)
return base.OpenEntry (arc, entry);
if (!pent.IsPacked)
{
if (!arc.File.View.AsciiEqual (entry.Offset, "LZS\0"))
return base.OpenEntry (arc, entry);
pent.IsPacked = true;
pent.UnpackedSize = arc.File.View.ReadUInt32 (entry.Offset+4);
}
var input = arc.File.CreateStream (entry.Offset+8, entry.Size-8);
bool embedded_lzs = (input.Signature & ~0xF0u) == 0x535A4C0F; // 'LZS'
var lzs = new LzssStream (input);
if (embedded_lzs)
{
var header = new byte[8];
lzs.Read (header, 0, 8);
pent.UnpackedSize = header.ToUInt32 (4);
lzs = new LzssStream (lzs);
}
return lzs;
}
}
}
51 changes: 47 additions & 4 deletions ArcFormats/DigitalWorks/ImageTM2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// IN THE SOFTWARE.
//

using GameRes.Formats.Strings;
using System;
using System.ComponentModel.Composition;
using System.IO;
Expand All @@ -36,6 +37,7 @@ internal class Tim2MetaData : ImageMetaData
public int PaletteSize;
public int HeaderSize;
public int Colors;
public byte Alpha;
}

[Export(typeof(ImageFormat))]
Expand All @@ -48,8 +50,16 @@ public class Tim2Format : ImageFormat
public Tim2Format ()
{
Extensions = new string[] { "tm2", "ext" };
Settings = new[] { AlphaFormat };
}

FixedSetSetting AlphaFormat = new FixedSetSetting(Properties.Settings.Default)
{
Name = "TIM2AlphaFormat",
Text = arcStrings.Tim2AlphaFormat,
ValuesSet = new[] { "No Alpha", "RGBX", "RGBA" },
};

public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x40);
Expand All @@ -62,13 +72,22 @@ public override ImageMetaData ReadMetaData (IBinaryStream file)
case 5: bpp = 8; break;
default: return null;
}
byte alpha;
switch (AlphaFormat.Get<String>())
{
case "No Alpha": alpha = 0; break;
case "RGBX": alpha = 7; break;
case "RGBA":
default: alpha = 8; break;
}
return new Tim2MetaData {
Width = header.ToUInt16 (0x24),
Height = header.ToUInt16 (0x26),
BPP = bpp,
PaletteSize = header.ToInt32 (0x14),
HeaderSize = header.ToUInt16 (0x1C),
Colors = header.ToUInt16 (0x1E),
Alpha = alpha, //header.ToUInt16(0x30) == 0?// not so sure, there will be omissions
};
}

Expand Down Expand Up @@ -113,9 +132,9 @@ public byte[] Unpack ()
int image_size = (int)m_info.Width * (int)m_info.Height * pixel_size;
var output = m_input.ReadBytes (image_size);
if (pixel_size <= 8 && m_info.Colors > 0)
Palette = ReadPalette (m_info.Colors);
Palette = ReadPalette (m_info.Colors, m_info.Alpha);

if (pixel_size >= 3)
if (pixel_size == 3 || pixel_size == 4 && m_info.Alpha == 8)
{
for (int i = 0; i < image_size; i += pixel_size)
{
Expand All @@ -124,12 +143,36 @@ public byte[] Unpack ()
output[i+2] = r;
}
}
if (pixel_size == 4 && m_info.Alpha == 7)
{
for (int i = 0; i < image_size; i += 4)
{
byte r = output[i];
output[i] = output[i + 2];
output[i + 2] = r;
if (output[i + 3] >= byte.MaxValue / 2)
output[i + 3] = byte.MaxValue;
else
output[i + 3] = (byte)(output[i + 3] << 1);
}
}
if (pixel_size == 4 && m_info.Alpha == 0)
{
for (int i = 0; i < image_size; i += 4)
{
byte r = output[i];
output[i] = output[i + 2];
output[i + 2] = r;
output[i + 3] = byte.MaxValue;
}
}
return output;
}

BitmapPalette ReadPalette (int color_num)
BitmapPalette ReadPalette (int color_num, byte X_A = 8)
{
var source = ImageFormat.ReadColorMap (m_input.AsStream, color_num, PaletteFormat.RgbA);
var source = ImageFormat.ReadColorMap (m_input.AsStream,
color_num, X_A == 7 ? PaletteFormat.RgbA7 : X_A == 0 ? PaletteFormat.RgbX : PaletteFormat.RgbA);
var color_map = new Color[color_num];

int parts = color_num / 32;
Expand Down
53 changes: 53 additions & 0 deletions ArcFormats/DigitalWorks/ImageTM2arc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using GameRes.Compression;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;

namespace GameRes.Formats.DigitalWorks
{
[Export(typeof(ImageFormat))]
public class TM2ArkFormat : Tim2Format
{
public override string Tag { get { return "TIM2/PS2 compressed"; } }
public override string Description { get { return "PlayStation/2 image format with LZSS compress"; } }
public override uint Signature { get { return 0x535A4C; } } // 'LZS'
public TM2ArkFormat()
{
Extensions = new string[] { "tm2" };
Settings = null;
}

public override ImageMetaData ReadMetaData(IBinaryStream stream)
{
stream.Position = 9;
uint real_sign = stream.ReadUInt32();
//Tim2Format tm2raw = new Tim2Format();
if (real_sign != base.Signature)
{
return null;
}
stream.Position = 4;
uint unpacked_size = stream.ReadUInt32();
if (unpacked_size <= 0x20 || unpacked_size > 0x5000000) // ~83MB
return null;
stream.Position = 8;
using (var lzss = new LzssStream(stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream(lzss))
using (var tm2 = new BinaryStream(input, stream.Name))
return base.ReadMetaData(tm2);
}
public override ImageData Read(IBinaryStream stream, ImageMetaData info)
{
stream.Position = 8;
using (var lzss = new LzssStream(stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream(lzss))
using (var tm2 = new BinaryStream(input, stream.Name))
return base.Read(tm2, info);
}
public override void Write(Stream file, ImageData image)
{
throw new System.NotImplementedException("TM2ArkFormat.Write not implemented");
}
}
}
2 changes: 1 addition & 1 deletion ArcFormats/MAGES/ArcARC20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ARC20Opener : ArchiveFormat
public override string Tag { get { return "ARC/Princess Soft ARC20"; } }
public override string Description { get { return "Princess Soft PS2 resource archive"; } }
public override uint Signature { get { return 0x20435241; } } // 'ARC\x20'
public override bool IsHierarchic { get { return false; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }

public override ArcFile TryOpen(ArcView file)
Expand Down
12 changes: 12 additions & 0 deletions ArcFormats/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ArcFormats/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,8 @@
<Setting Name="NexasEncodingCP" Type="System.Int32" Scope="User">
<Value Profile="(Default)">932</Value>
</Setting>
<Setting Name="TIM2AlphaFormat" Type="System.String" Scope="User">
<Value Profile="(Default)">RGBA</Value>
</Setting>
</Settings>
</SettingsFile>
10 changes: 10 additions & 0 deletions ArcFormats/Strings/arcStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ArcFormats/Strings/arcStrings.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,8 @@ Choose encryption scheme or enter a passphrase.</comment>
<data name="ODNAudioSampleRate" xml:space="preserve">
<value>Default audio sampling rate</value>
</data>
<data name="Tim2AlphaFormat" xml:space="preserve">
<value>Tim2画像のAlpha形式を選択してください。
ファイルから正しく読み取ることができません。</value>
</data>
</root>
4 changes: 4 additions & 0 deletions ArcFormats/Strings/arcStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,8 @@ Choose encryption scheme or enter a passphrase.</value>
<data name="ODNAudioSampleRate" xml:space="preserve">
<value>Default audio sampling rate</value>
</data>
<data name="Tim2AlphaFormat" xml:space="preserve">
<value>Choose Tim2 image alpha format.
It can't be read correctly from the file.</value>
</data>
</root>
4 changes: 4 additions & 0 deletions ArcFormats/Strings/arcStrings.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,8 @@
<data name="ODNAudioSampleRate" xml:space="preserve">
<value>默认音频采样率</value>
</data>
<data name="Tim2AlphaFormat" xml:space="preserve">
<value>选择Tim2图片透明度格式。
这无法从文件中正确获取。</value>
</data>
</root>
3 changes: 3 additions & 0 deletions ArcFormats/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
<setting name="NexasEncodingCP" serializeAs="String">
<value>932</value>
</setting>
<setting name="TIM2AlphaFormat" serializeAs="String">
<value>RGBA</value>
</setting>
</GameRes.Formats.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup>
Expand Down
Loading