Skip to content

Commit

Permalink
Merge pull request #12 from WizzardMaker/temporary-file-addition
Browse files Browse the repository at this point in the history
Unit export support and importer changes
  • Loading branch information
WizzardMaker authored Sep 16, 2020
2 parents e3362bf + 4c8f64f commit 3e503af
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 62 deletions.
16 changes: 8 additions & 8 deletions S4GFX/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileRea

ImageData data = new ImageData(map.Height, map.Width);

image.Width = map.Width;
image.Height = map.Height;
//image.Width = map.Width;
//image.Height = map.Height;

BitmapData d = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapData d = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

int size = map.Height * map.Width * 4;

Expand All @@ -386,11 +386,11 @@ private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileRea
int index = 0;
for (int y = 0; y < map.Height; y++) {
for (int x = 0; x < map.Width; x++) {

data.data[index + 0] = argb[index + 1];//r
data.data[index + 1] = argb[index + 2];//g
data.data[index + 2] = argb[index + 3];//b
data.data[index + 3] = argb[index + 0];//a
//so LockBits stores the data as bgra, because fuck you, thats why...
data.data[index + 0] = argb[index + 2];//g1
data.data[index + 1] = argb[index + 1];//r2
data.data[index + 2] = argb[index + 0];//a3
data.data[index + 3] = argb[index + 3];//b0

index += 4;
}
Expand Down
10 changes: 8 additions & 2 deletions S4GFXInterface/ImageGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public class ImageView : IDisposable {
DirectBitmap bitmap;
private ExportedBitmap gridElement;

private bool removeAlpha = true;
private bool onlyShadows;
private bool removeShadows = true;
public static bool removeAlpha, removeShadows, removeGarbage;

ImageData data;

Expand Down Expand Up @@ -74,6 +73,13 @@ private void CreateBitmap() {
byte green = data.data[index + 1];
byte blue = data.data[index + 2];


if (removeGarbage && y > height-2) {
red = 255;
green = 0;
blue = 0;
}

if (red == 255 && green + blue == 0) {
alpha = removeAlpha ? 0 : alpha;
}
Expand Down
46 changes: 36 additions & 10 deletions S4GFXInterface/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</DockPanel.Background>
<uc:HeaderButton x:Name="ExportB" Heading="Export" ImageSource="/Resources/Icons/icons8-exportieren-100.png"/>

<uc:HeaderButton x:Name="ImportB" Heading="Import" ImageSource="/Resources/Icons/icons8-importieren-100.png" IsEnabled="False"/>
<uc:HeaderButton x:Name="ImportB" Heading="Import" ImageSource="/Resources/Icons/icons8-importieren-100.png"/>

<uc:HeaderButton x:Name="SettingsB" Heading="Settings" ImageSource="/Resources/Icons/icons8-einstellungen-100.png"/>

Expand All @@ -26,26 +26,52 @@
<TabItem x:Name="Export" Header="Export" Margin="-2,-2,-2,0">
<DockPanel Margin="0,0,10,0" LastChildFill="False" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<Grid Width="328" Margin="0,0,0,23">
<Button x:Name="ReturnToGroupBut" HorizontalAlignment="Left" VerticalAlignment="Top" Width="117" Margin="201,10,0,0" Height="22" Click="ReturnToGroupBut_Click">
<Grid.RowDefinitions>
<RowDefinition Height="121*"/>
<RowDefinition Height="327*"/>
</Grid.RowDefinitions>
<Button x:Name="ReturnToGroupBut" HorizontalAlignment="Left" VerticalAlignment="Top" Width="113" Margin="205,10,0,0" Height="22" Click="ReturnToGroupBut_Click">
<StackPanel Height="18" Width="133" Orientation="Horizontal">
<Image Source="Resources/Icons/icons8-zurück-96.png" Width="23" Margin="0"/>
<Image Source="Resources/Icons/icons8-zurück-96.png" Width="18" Margin="0"/>
<TextBlock TextWrapping="Wrap" Text="Return to group" Margin="0" Width="101"/>
</StackPanel>
</Button>
<ComboBox x:Name="ExportGroupIDs" HorizontalAlignment="Left" VerticalAlignment="Top" Width="96" Margin="76,10,0,0" SelectionChanged="ExportGroupIDs_SelectionChanged" IsEnabled="{Binding IsNotLoading}"/>
<ComboBox x:Name="ExportGroupIDs" HorizontalAlignment="Left" VerticalAlignment="Top" Width="52" Margin="76,10,0,0" SelectionChanged="ExportGroupIDs_SelectionChanged" IsEnabled="{Binding IsNotLoading}"/>
<Label HorizontalAlignment="Left" Margin="10,6,0,0" VerticalAlignment="Top" Width="66">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Group ID:" VerticalAlignment="Top"/>
</Label>
<Button x:Name="ExportSaveToDiskSingle" Height="22" Content="Save selected to Disk" Margin="174,416,10,10" Click="ExportSaveToDiskSingle_Click" IsEnabled="{Binding CanExportSingle}"/>
<Button x:Name="ExportSaveToDiskGroup" Height="22" Content="Save group to Disk" Margin="10,416,174,10" Click="ExportSaveToDiskGroup_Click" IsEnabled="{Binding CanExportGroup}"/>
<Button x:Name="ExportSaveToDiskAll" Height="22" Content="Save All to Disk" Margin="92,389,92,37" Click="ExportSaveToDiskAll_Click" IsEnabled="{Binding CanExportAll}"/>
<Button x:Name="ExportSaveToDiskSingle" Height="22" Content="Save selected to Disk" Margin="174,294.667,10,10" Click="ExportSaveToDiskSingle_Click" IsEnabled="{Binding CanExportSingle}" Grid.Row="1"/>
<Button x:Name="ExportSaveToDiskGroup" Height="22" Content="Save group to Disk" Margin="10,294.667,174,10" Click="ExportSaveToDiskGroup_Click" IsEnabled="{Binding CanExportGroup}" Grid.Row="1"/>
<Button x:Name="ExportSaveToDiskAll" Height="22" Content="Save All to Disk" Margin="92,267.667,92,37" Click="ExportSaveToDiskAll_Click" IsEnabled="{Binding CanExportAll}" Grid.Row="1"/>
<Button x:Name="ReloadBut" Content="Reload" HorizontalAlignment="Left" VerticalAlignment="Top" Width="58" Margin="137,10,0,0" Height="22" Click="ReloadBut_Click" IsEnabled="{Binding IsNotLoading}"/>
<Label HorizontalAlignment="Left" Margin="5,61,0,0" VerticalAlignment="Top" Width="313">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="303"><Run Text="GFX Settings:"/><Run Text=" "/><Run Text=" "/><Run Text="*changing these requires a reload"/></TextBlock>
</Label>
<ComboBox x:Name="TeamGFXSetting" HorizontalAlignment="Left" VerticalAlignment="Top" Width="84" Margin="78,26,0,0" IsEnabled="{Binding IsNotLoading}" SelectedIndex="0" Grid.Row="1">
<ComboBoxItem Content="Red (0)"/>
<ComboBoxItem Content="Blue (1)"/>
<ComboBoxItem Content="Green (2)"/>
<ComboBoxItem Content="Yellow (3)"/>
<ComboBoxItem Content="Purple (4)"/>
<ComboBoxItem Content="Orange (5)"/>
<ComboBoxItem Content="Cyan (6)"/>
<ComboBoxItem Content="White (7)"/>
</ComboBox>
<Label HorizontalAlignment="Left" Margin="7,22,0,0" VerticalAlignment="Top" Width="71" Grid.Row="1">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Team color:" VerticalAlignment="Top"/>
</Label>
<TextBlock HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="179,27,0,0" Grid.Row="1"><Run Text="*for 20,21,22,"/><Run Text="23"/><Run Text=".gfx"/></TextBlock>
<Label HorizontalAlignment="Left" Margin="5,180.667,0,0" VerticalAlignment="Top" Width="93" Grid.Row="1"/>
<CheckBox x:Name="TransparencyCheck" Content="Use red as transparency" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,87,0,0" Unchecked="TransparencyCheck_Checked" Checked="TransparencyCheck_Checked"/>
<CheckBox x:Name="ShadowCheck" Content="Use green as shadow" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,107,0,0" Grid.RowSpan="2" Unchecked="ShadowCheck_Checked" Checked="ShadowCheck_Checked"/>
<CheckBox x:Name="RemoveGarbageCheck" Content="Remove last line (garbage info)" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,6,0,0" Unchecked="RemoveGarbageCheck_Checked" Checked="RemoveGarbageCheck_Checked" Grid.Row="1"/>
</Grid>
<StatusBar HorizontalContentAlignment="Center" VerticalContentAlignment="Center" DockPanel.Dock="Bottom" BorderThickness="0 1 0 0" BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Margin="-328,0,0,0">
<StatusBarItem>
<TextBlock x:Name="ExportLoadCount" Text="Hello!"/>
<TextBlock x:Name="ExportLoadCount" Text="Hello!" Visibility="Hidden"/>
</StatusBarItem>
<StatusBarItem>
<Image x:Name="LoadingIcon" Source="Resources/Icons/icons8-ladeanimation-bild-1-50.png" Height="22" Width="22">
<Image x:Name="LoadingIcon" Source="Resources/Icons/icons8-ladeanimation-bild-1-50.png" Height="22" Width="22" Visibility="Hidden">
<Image.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
Expand Down Expand Up @@ -86,7 +112,7 @@
<Button x:Name="OpenPath1" Content="Open" HorizontalAlignment="Left" Margin="387,2,0,0" VerticalAlignment="Top" Width="75" Click="OpenPath_Click"/>
</Grid>
</Border>
<CheckBox Content="Include subfolders" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,99,0,0"/>
<CheckBox x:Name="ImportSubfolders" Content="Include subfolders" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,99,0,0"/>
<Button Content="Prepare" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="191,19,0,0"/>
<ComboBox x:Name="ExportGroupIDs_Copy" HorizontalAlignment="Left" VerticalAlignment="Top" Width="96" Margin="76,19,0,0" SelectionChanged="ExportGroupIDs_SelectionChanged" IsEnabled="{Binding IsNotLoading}"/>
<Label HorizontalAlignment="Left" Margin="10,15,0,0" VerticalAlignment="Top" Width="66">
Expand Down
55 changes: 49 additions & 6 deletions S4GFXInterface/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,17 @@ public MainWindow() {
exportImageGrid.ViewModeChanged += CacheExportScrollPosition;
exportImageGrid.SelectionChanged += ExportSelectionChanged;

ExportGroupIDs.SelectedIndex = 0;
CanExport = false;
IsNotLoading = true;
ReturnToGroupBut.Visibility = Visibility.Collapsed;

LoadingIcon.Visibility = Visibility.Hidden;


ImageGrid.ImageView.removeAlpha = TransparencyCheck.IsChecked == false;
ImageGrid.ImageView.removeShadows = ShadowCheck.IsChecked == false;

//ExportGroupIDs.SelectedIndex = 0;
//ExportGrid.Children.Add(new ExportedBitmap());
}

Expand Down Expand Up @@ -209,7 +219,7 @@ private void ExportAllView() {
void GetAllGroups() {
List<string> ids = new List<string>();

foreach (string f in Directory.GetFiles(App.GamePath + "/GFX/", "*.gfx")) {
foreach (string f in Directory.GetFiles(App.GamePath + "/GFX/").Where(f => f.EndsWith(".gfx") || f.EndsWith(".gh5"))) {
ids.Add(System.IO.Path.GetFileNameWithoutExtension(f));
}

Expand Down Expand Up @@ -278,7 +288,7 @@ public ICollectionFileReader DoLoadGH(string fileId) {
var gh = new BinaryReader(File.Open(fileId + ".gh5", FileMode.Open), Encoding.Default, true);
//var gl = new BinaryReader(File.Open(fileId + ".gl5", FileMode.Open), Encoding.Default, true);

gfxFile = new GhFileReader(gh);
gfxFile = new GhFileReader(gh, true);

gh.Close();
//gl.Close();
Expand All @@ -298,8 +308,8 @@ public ICollectionFileReader DoLoad(string fileId, bool usePli, bool useJil) {
paletteIndex = new BinaryReader(File.Open(fileId + ".pil", FileMode.Open), Encoding.Default, true);
palette = new BinaryReader(File.Open(fileId + ".pa6", FileMode.Open), Encoding.Default, true);
} else {
paletteIndex = new BinaryReader(File.Open(fileId + ".pi4", FileMode.Open), Encoding.Default, true);
palette = new BinaryReader(File.Open(fileId + ".p46", FileMode.Open), Encoding.Default, true);
paletteIndex = new BinaryReader(File.Open(fileId + ".pi2", FileMode.Open), Encoding.Default, true);
palette = new BinaryReader(File.Open(fileId + ".p26", FileMode.Open), Encoding.Default, true);
}

if (useJil) {
Expand All @@ -313,6 +323,16 @@ public ICollectionFileReader DoLoad(string fileId, bool usePli, bool useJil) {
var paletteIndexList = new PilFileReader(paletteIndex);
var paletteCollection = new PaletteCollection(palette, paletteIndexList);

switch (fileId.Substring(fileId.Length-2)) {
case ("20"):
case ("21"):
case ("22"):
case ("23"):
paletteCollection.GetPalette().replaceTeamColors = true;
paletteCollection.GetPalette().team = TeamGFXSetting.SelectedIndex;
break;
}

DilFileReader directionIndexList = null;
JilFileReader jobIndexList = null;

Expand Down Expand Up @@ -372,6 +392,7 @@ private void SaveAllBitmaps(string path, ICollectionFileReader file = null) {
Dispatcher.Invoke((Action)delegate {
try {
ExportLoadCount.Text = $"Loaded: {loaded++}/{count}";
ExportLoadCount.Visibility = Visibility.Visible;
LoadingIcon.Visibility = Visibility.Visible;
} catch (Exception e) {
Console.WriteLine(e.Message);
Expand Down Expand Up @@ -428,9 +449,15 @@ public void AddButton(HeaderButton b, object indexObject) {
}

private void ExportGroupIDs_SelectionChanged(object sender, SelectionChangedEventArgs e) {
LoadGroup();
}

private void LoadGroup() {
if (ExportScrollView == null)
return;
ExportScrollView.ScrollToTop();

exportImageGrid.Clear();
exportImageGrid?.Clear();
source.Cancel();

exportImageGrid.SetContainer(int.Parse((string)ExportGroupIDs.SelectedItem));
Expand Down Expand Up @@ -494,6 +521,22 @@ private void ExportSaveToDiskSingle_Click(object sender, RoutedEventArgs e) {
exportImageGrid.selectedItem.SaveBitmapToFile(GetFullPath(exportImageGrid.selectedItem.Group));
}

private void ReloadBut_Click(object sender, RoutedEventArgs e) {
LoadGroup();
}

private void TransparencyCheck_Checked(object sender, RoutedEventArgs e) {
ImageGrid.ImageView.removeAlpha = TransparencyCheck.IsChecked == false;
}

private void ShadowCheck_Checked(object sender, RoutedEventArgs e) {
ImageGrid.ImageView.removeShadows = ShadowCheck.IsChecked == false;
}

private void RemoveGarbageCheck_Checked(object sender, RoutedEventArgs e) {
ImageGrid.ImageView.removeGarbage = ShadowCheck.IsChecked == false;
}

private void ExportSaveToDiskGroup_Click(object sender, RoutedEventArgs e) {
foreach (var i in exportImageGrid.GetImagesInCurrentGroup()) {
i.SaveBitmapToFile(GetFullPath(i.Group));
Expand Down
28 changes: 22 additions & 6 deletions S4GFXLibrary/FileReader/GfxFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,27 @@ public void ChangeImageData(string groupID, int index, ImageData[] newDatas) {
Palette p = paletteCollection.GetPalette();

int palettePosition = 0;
int oldIndex = (GetImage(index) as GfxImage).jobIndex;
int addedLength = 0;
int oldIndex = (GetImage(index) as GfxImage)?.jobIndex ?? 0;
for (int j = 0; j < newDatas.Length; j++) {
if(index + j > images.Length) {
if(index + j >= images.Length) {
Array.Resize(ref images, index + j + 1);

paletteCollection.GetPilFile().Resize(images.Length);
p.AddEntry();

paletteCollection.GetPilFile().SetOffset(index+j, (paletteCollection.GetPilFile().GetOffset(index + j - 1)+256*2));

images[index + j] = new GfxImage(
null,
p,
paletteCollection.GetOffset((GetImage(index + j - 1) as GfxImage).jobIndex));
paletteCollection.GetOffset(index + j));

images[index + j].jobIndex = index + j;

offsetTable.AddOffset();

offsetTable.offsetTable[index+j] = (int)baseStream.Length + addedLength;
}

GfxImage i = images[index + j];
Expand All @@ -71,6 +84,7 @@ public void ChangeImageData(string groupID, int index, ImageData[] newDatas) {
palettePosition += RecreatePaletteAt(start, palettePosition, newDatas[j]);

i.buffer = i.CreateImageData(newDatas[j]);
addedLength += i.buffer.Length;

int nextImageStartOffset = offsetTable.GetImageOffset(index + j + 1); //Get the current offset of the next image
int offset = Math.Max(0, offsetTable.GetImageOffset(index + j) + i.HeaderSize + i.buffer.Length - nextImageStartOffset - 1); //Our data could be larger, calculate how much larger
Expand Down Expand Up @@ -98,8 +112,8 @@ public DataBufferCollection GetDataBufferCollection(string groupID) {

return new DataBufferCollection(fileId)
.AddBuffer(gfxDataBuffer, "gfx")
.AddBuffer(pilDataBuffer, "pi4", "pi2")
.AddBuffer(paletteDataBuffer, "p46", "p26")
.AddBuffer(pilDataBuffer, "pil")//"pi4", "pi2")
.AddBuffer(paletteDataBuffer, "pa5","pa6")//"p46", "p26")
.AddBuffer(offsetTable.GetData(), "gil");
}

Expand Down Expand Up @@ -232,8 +246,10 @@ public int RecreatePaletteAt(int paletteOffset, int additionOffset, ImageData or

if (additionOffset == 0) {
for (int i = 0; i < 256; i++) {
p.SetColor(paletteOffset + i, 9999);
p.SetColor(paletteOffset + i, Palette.RGBToPalette(0,255,255));
}
//p.SetColor(paletteOffset + 0, Palette.RGBToPalette(255, 0, 0));
//p.SetColor(paletteOffset + 1, Palette.RGBToPalette(0, 255, 0));
additionOffset = 1;
}

Expand Down
8 changes: 4 additions & 4 deletions S4GFXLibrary/FileReader/GhFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public DataBufferCollection GetDataBufferCollection(string groupID) {
throw new System.NotImplementedException();
}

public GhFileReader(BinaryReader reader) {
public GhFileReader(BinaryReader reader, bool isGh5 = true) {
ReadResource(reader);

reader.BaseStream.Seek(0, SeekOrigin.Begin);
Expand All @@ -62,10 +62,10 @@ public GhFileReader(BinaryReader reader) {

switch (imageType) {
case 0:
img = new GfxImage16Bit(buffer, 128, rowCount);
img = new GfxImage16Bit(buffer, 128, rowCount, isGh5);
break;
case 1:
img = new GfxImage16Bit(buffer, 256, rowCount);
img = new GfxImage16Bit(buffer, 256, rowCount, isGh5);
break;
case 2:
img = new GfxImageWithPalette(buffer, 128, rowCount);
Expand All @@ -76,7 +76,7 @@ public GhFileReader(BinaryReader reader) {
}

img.Flag1 = flag1;
img.Flag1 = flag2;
img.Flag2 = flag2;
img.DataOffset = filePos + 8;

img.Index = index;
Expand Down
11 changes: 8 additions & 3 deletions S4GFXLibrary/FileReader/GilFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.IO;
using System;
using System.IO;

namespace S4GFXLibrary.FileReader
{
public class GilFileReader : FileReaderBase
{
int[] offsetTable;
public int[] offsetTable;


public int GetImageCount()
Expand All @@ -22,6 +23,10 @@ public int GetImageOffset(int index)
return offsetTable[index];
}

public void AddOffset() {
Array.Resize(ref offsetTable, offsetTable.Length+1);
}

/// <summary>
/// Moves all offsets by offsetToAdd, starting from index startIndex
/// </summary>
Expand Down Expand Up @@ -51,7 +56,7 @@ public GilFileReader(BinaryReader resourceReader)

override public byte[] GetData()
{
byte[] data = new byte[(int)baseStream.Length];
byte[] data = new byte[HeaderSize+offsetTable.Length*4];

using (BinaryWriter writer = new BinaryWriter(new MemoryStream(data)))
{
Expand Down
9 changes: 2 additions & 7 deletions S4GFXLibrary/GFX/GfxImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ void GetImageDataWithRunLengthEncoding(byte[] buffer, uint[] imgData, int pos, i
}
else
{
color = palette.GetColor(paletteOffset + value);
//if (value > 200) {
// color = Palette.RGBToPalette(0, 0, value, value);
//} else {
// color = Palette.RGBToPalette(255, 0, 0);
//}
color = palette.GetColor(paletteOffset + value, value);
}

for (int i = 0; i < count && j < length; i++)
Expand All @@ -118,7 +113,7 @@ public void GetImageDataWithNoEncoding(byte[] buffer, uint[] imgData, int pos, i
int value = pos >= buffer.Length ? 1 : buffer[pos];
pos++;

imgData[j++] = palette.GetColor(paletteOffset + value);
imgData[j++] = palette.GetColor(paletteOffset + value, value);
if (!paletteEntries.Contains(paletteOffset + value))
paletteEntries.Add(paletteOffset + value);
}
Expand Down
Loading

0 comments on commit 3e503af

Please sign in to comment.