Skip to content

Commit

Permalink
Support for Animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaaammmler authored and Gaaammmler committed May 27, 2019
1 parent f2416a0 commit 6d18f8d
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Gm1KonverterCrossPlatform/Files/TGXImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ public unsafe void CreateImageFromByteArray(Palette palette)

}

internal void ConvertImageWithPaletteToByteArray(List<ushort> colors, int width, int height, Palette palette, List<ushort>[] colorsImages = null)
{
var array = Utility.ImgToGM1ByteArray(colors, width, height, animatedColor, palette, colorsImages);

imgFileAsBytearray = array.ToArray();
}

internal unsafe void CreateNoComppressionImageFromByteArray(Palette palette,int offset)
{
Expand Down
113 changes: 105 additions & 8 deletions Gm1KonverterCrossPlatform/HelperClasses/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ internal static List<UInt16> LoadImage(String filename, out int width, out int h
var pixel = image[j, i];
byte a = (animatedColor >= 1
|| ((GM1FileHeader.DataType)type) == GM1FileHeader.DataType.TilesObject
|| ((GM1FileHeader.DataType)type) == GM1FileHeader.DataType.Animations
|| ((GM1FileHeader.DataType)type) == GM1FileHeader.DataType.TGXConstSize
|| ((GM1FileHeader.DataType)type) == GM1FileHeader.DataType.NOCompression
|| ((GM1FileHeader.DataType)type) == GM1FileHeader.DataType.NOCompression1) ? byte.MaxValue : byte.MinValue;
Expand Down Expand Up @@ -117,9 +118,8 @@ internal static List<UInt16> LoadImage(String filename, out int width, out int h
/// <param name="height">The Height from the IMG</param>
/// <param name="animatedColor">Needed for Alpha is 1 or 0</param>
/// <returns></returns>
internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int height, int animatedColor, Palette palette = null)
internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int height, int animatedColor, Palette palette = null, List<ushort>[] paletteImages = null)
{

int transparent = 32767;
ushort alpha=(animatedColor==0)?(ushort)0b1000_0000_0000_0000: (ushort)0b0;
List<byte> array = new List<byte>();
Expand Down Expand Up @@ -260,8 +260,16 @@ internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int
array.Add((byte)(header | length));

var color = (ushort)(colors[j + i * width + 1] | alpha);
if (palette == null)
{
array.AddRange(BitConverter.GetBytes(color));

}
else
{
byte positioninColortable = FindColorPositionInPalette(color, j + i * width + 1, palette, paletteImages);
array.Add(positioninColortable);
}

dummy -= 32;
}
if (dummy != 0)
Expand All @@ -270,8 +278,18 @@ internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int
array.Add((byte)(header | length));

var color = (ushort)(colors[j + i * width + 1] | alpha);
if (palette==null)
{
array.AddRange(BitConverter.GetBytes(color));

}
else
{
byte positioninColortable = FindColorPositionInPalette(color, j + i * width + 1, palette, paletteImages);
array.Add(positioninColortable);
}



}
}
else
Expand All @@ -286,8 +304,16 @@ internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int
for (int a = 0; a < 32; a++)
{
var color = (ushort)(colors[j + zaehler + i * width] | alpha);
array.AddRange(BitConverter.GetBytes(color));

if (palette == null)
{
array.AddRange(BitConverter.GetBytes(color));
}
else
{
byte positioninColortable = FindColorPositionInPalette(color, j + zaehler + i * width, palette, paletteImages);
array.Add(positioninColortable);
}

dummy--;
zaehler++;
}
Expand All @@ -299,8 +325,16 @@ internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int
for (int a = 0; a < dummy; a++)
{
var color = (ushort)(colors[j + zaehler + i * width] | alpha);
array.AddRange(BitConverter.GetBytes(color));

if (palette == null)
{
array.AddRange(BitConverter.GetBytes(color));
}
else
{
byte positioninColortable = FindColorPositionInPalette(color, j + zaehler + i * width, palette, paletteImages);
array.Add(positioninColortable);
}

zaehler++;
}
}
Expand All @@ -324,6 +358,69 @@ internal static List<byte> ImgToGM1ByteArray(List<ushort> colors, int width, int
return array;
}

private static byte FindColorPositionInPalette(ushort color,int position, Palette palette, List<ushort>[] paletteImages)
{

byte newPosition = 0;
if (paletteImages==null)//not orginal Stronghold Files do not need to check all
{
for (byte i = 0; i < byte.MaxValue; i++)
{
if (color == palette.ArrayPaletten[0,i])
{
newPosition = i;
break;
}

}
}
else // Orginal ones
{
List<byte> positions = new List<byte>();
for (byte i = 0; i < byte.MaxValue; i++)
{
if (color == palette.ArrayPaletten[0, i])
{
positions.Add(i);
}
}
if (positions.Count>1|| positions.Count==0)
{

for (int j = 0; j < 9; j++)//other 9 pictures check for only one position
{
List<byte> otherPositions = new List<byte>();
for (byte i = 0; i < byte.MaxValue; i++)
{
if (paletteImages[j][position] == palette.ArrayPaletten[j+1, i])
{
otherPositions.Add(i);
}
}
if (otherPositions.Count==1)
{
newPosition = otherPositions[0];
break;
}else if (otherPositions.Count > 1)
{
newPosition = otherPositions[0];
}
}

}
else
{

newPosition = positions[0];
}
}
if (newPosition==0)
{

}
return newPosition;
}

private static bool CheckIfAllLinesUnderTransparent(List<ushort> colors, int transparent, int offset)
{
for (int i = offset; i < colors.Count; i++)
Expand Down
3 changes: 3 additions & 0 deletions Gm1KonverterCrossPlatform/Languages/Language.de-DE.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@
<x:String x:Key="ExportGif">Exportiere Gif</x:String>
<x:String x:Key="SelectGif">Selektiere Bilder mit STRG danach klicke auf den Export Button</x:String>
<x:String x:Key="Delay">Verzögerung:</x:String>

<x:String x:Key="ImportOrginalStrongholdAnimation">Importiere Orginal Stronghold Animation</x:String>
<x:String x:Key="ExportOrginalStrongholdAnimation">Exportiere Orginal Stronghold Animation</x:String>
</controls:ResourceDictionary>
3 changes: 3 additions & 0 deletions Gm1KonverterCrossPlatform/Languages/Language.ru-RU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@
<x:String x:Key="ExportGif">Экспортировать Гиф</x:String>
<x:String x:Key="SelectGif">Выберите изображения с помощью CTRL, затем нажмите кнопку «Экспорт».</x:String>
<x:String x:Key="Delay">Задержка:</x:String>

<x:String x:Key="ImportOrginalStrongholdAnimation">Import Orginal Stronghold Animation</x:String>
<x:String x:Key="ExportOrginalStrongholdAnimation">Export Orginal Stronghold Animation</x:String>
</controls:ResourceDictionary>
4 changes: 3 additions & 1 deletion Gm1KonverterCrossPlatform/Languages/Language.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@
<x:String x:Key="ExportGif">Export Gif</x:String>
<x:String x:Key="SelectGif">Select Images with CTRL than click on the Export Button</x:String>
<x:String x:Key="Delay">Delay:</x:String>


<x:String x:Key="ImportOrginalStrongholdAnimation">Import Orginal Stronghold Animation</x:String>
<x:String x:Key="ExportOrginalStrongholdAnimation">Export Orginal Stronghold Animation</x:String>
</controls:ResourceDictionary>
11 changes: 11 additions & 0 deletions Gm1KonverterCrossPlatform/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ public bool ColorButtonsEnabled
get => colorButtonsEnabled;
set => this.RaiseAndSetIfChanged(ref colorButtonsEnabled, value);
}



private bool orginalStrongholdAnimationButtonEnabled = false;
public bool OrginalStrongholdAnimationButtonEnabled
{

get => orginalStrongholdAnimationButtonEnabled;
set => this.RaiseAndSetIfChanged(ref orginalStrongholdAnimationButtonEnabled, value);
}

private bool importButtonEnabled = false;
public bool ImportButtonEnabled
{
Expand Down
4 changes: 4 additions & 0 deletions Gm1KonverterCrossPlatform/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<Separator/>
<MenuItem Header="{DynamicResource ExportImages}" Name="ExportImagesMenueItem" IsEnabled="{Binding ButtonsEnabled}"/>
<MenuItem Header="{DynamicResource ImportImages}" Name="ImportImagesMenueItem" IsEnabled="{Binding ImportButtonEnabled}"/>
<Separator/>
<Separator/>
<MenuItem Header="{DynamicResource ExportOrginalStrongholdAnimation}" Name="ExportOrginalStrongholdAnimation" IsEnabled="{Binding OrginalStrongholdAnimationButtonEnabled}"/>
<MenuItem Header="{DynamicResource ImportOrginalStrongholdAnimation}" Name="ImportOrginalStrongholdAnimation" IsEnabled="{Binding OrginalStrongholdAnimationButtonEnabled}"/>
</MenuItem>
<MenuItem Header="{DynamicResource Options}">
<MenuItem Header="{DynamicResource Workfolder}" Name="WorkfolderMenueItem"/>
Expand Down
Loading

0 comments on commit 6d18f8d

Please sign in to comment.