Skip to content

Commit

Permalink
better Encoding for GM1 Files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaaammmler authored and Gaaammmler committed May 14, 2019
1 parent 44d00d1 commit 1256510
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 76 deletions.
21 changes: 20 additions & 1 deletion Gm1KonverterCrossPlatform/Files/DecodedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public bool DecodeGm1File(byte[] array, String name)
case GM1FileHeader.DataType.Font:
CreateImages(array);
return true;
case GM1FileHeader.DataType.NOCompression:
case GM1FileHeader.DataType.NOCompression1:
CreateNoCompressionImages(array,((GM1FileHeader.DataType)fileHeader.IDataType==GM1FileHeader.DataType.NOCompression1)?0:7);
return true;
case GM1FileHeader.DataType.TilesObject:
CreateTileImage(array);
return true;
Expand All @@ -80,6 +84,8 @@ public bool DecodeGm1File(byte[] array, String name)
return false;
}



/// <summary>
/// Create the new GM1 Files from IMGS and Headers(1. FileHeader,2. Palette,3. OffsetList,4. SizeList,5. ImgHeaderList,6. ImgsasByteList)
/// </summary>
Expand Down Expand Up @@ -136,13 +142,24 @@ private void CreateImages(byte[] array)

CreateOffsetAndSizeInByteArrayList(array);
CreateImgHeader(array);

for (uint i = 0; i < fileHeader.INumberOfPictureinFile; i++)
{
_TGXImage[(int)i].CreateImageFromByteArray(palette);
}
}


private void CreateNoCompressionImages(byte[] array,int offset)
{
CreateOffsetAndSizeInByteArrayList(array);
CreateImgHeader(array);
for (uint i = 0; i < fileHeader.INumberOfPictureinFile; i++)
{
_TGXImage[(int)i].CreateNoComppressionImageFromByteArray(palette, offset);
}

}

private List<TGXImage> newTileList = new List<TGXImage>();
/// <summary>
/// Convert Img To Tiled Diamond Images
Expand Down Expand Up @@ -185,6 +202,8 @@ private void CreateImgHeader(byte[] array)
}




/// <summary>
/// Creates IMGS from TGX and Tile(the IMG consist out of many smaller IMGS)
/// </summary>
Expand Down
56 changes: 52 additions & 4 deletions Gm1KonverterCrossPlatform/Files/TGXImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,40 @@ public unsafe void CreateImageFromByteArray(Palette palette)

}



internal unsafe void CreateNoComppressionImageFromByteArray(Palette palette,int offset)
{
//-7 because the images only height -7 long idk why
bmp = new WriteableBitmap(new Avalonia.PixelSize(width, height - offset), new Avalonia.Vector(100, 100), Avalonia.Platform.PixelFormat.Bgra8888);// Bgra8888 is device-native and much faster.

using (var buf = bmp.Lock())
{

byte r, g, b, a;
uint x = 0;
uint y = 0;
for (int bytePos = 0; bytePos < imgFileAsBytearray.Length; bytePos+=2)
{
Utility.ReadColor(BitConverter.ToUInt16(imgFileAsBytearray, bytePos), out r, out g, out b, out a);
var colorByte = (UInt32)(b | (g << 8) | (r << 16) | (a << 24));
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
if (x==width)
{
y++;
x = 0;
}
}




}
}

/// <summary>
/// Convert imported Imgs without a Pallete to Byte array to safe new GM1 File
/// </summary>
Expand All @@ -242,11 +276,25 @@ public unsafe void CreateImageFromByteArray(Palette palette)
/// <param name="height">Height of the new IMG</param>
internal void ConvertImageWithoutPaletteToByteArray(List<ushort> colors, int width, int height)
{
var array = Utility.ImgWithoutPaletteToGM1ByteArray(colors, width, height,animatedColor,imgFileAsBytearray);

var array = Utility.ImgWithoutPaletteToGM1ByteArray(colors, width, height,animatedColor);


imgFileAsBytearray = array.ToArray();




}

internal void ConvertNoCommpressionImageToByteArray(List<ushort> list, int width, int height)
{
List<byte> newArray = new List<byte>();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
newArray.AddRange(BitConverter.GetBytes(list[y*width+x]));
}
}
imgFileAsBytearray = newArray.ToArray();
}

/// <summary>
Expand Down
Loading

0 comments on commit 1256510

Please sign in to comment.