Skip to content

Commit

Permalink
Updates for conversions/mipmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
cmarNYC committed Jun 29, 2018
1 parent 9a4ccbf commit f323fec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 3 additions & 2 deletions s4pi Wrappers/ImageResource/DSTResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected override Stream UnParse()

public void ImportToDST(Stream input)
{
input.Position = 0;
var header = new DDSHeader();
header.Parse(input);
switch (header.pixelFormat.Fourcc)
Expand Down Expand Up @@ -133,11 +134,11 @@ public void ImportToDST(Stream input)
this.Width = this.header.Width;
this.Height = this.header.Height;
Shuffle(this.header, input, ms);
this.isShuffled = true;
this.data = ms.ToArray();
}

}

}

private static void Shuffle(DDSHeader header, Stream input, Stream output)
{
Expand Down
15 changes: 9 additions & 6 deletions s4pi Wrappers/ImageResource/RLEResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class RLEResource : AResource
private RLEInfo info;
private MipHeader[] MipHeaders;
private byte[] data;

public uint MipCount { get { return this.info.mipCount; } }
#endregion

public RLEResource(int APIversion, Stream s) : base(APIversion, s) { if (s == null) { OnResourceChanged(this, EventArgs.Empty); } else { Parse(s); } }
Expand All @@ -52,6 +54,7 @@ public class RLEResource : AResource
public void Parse(Stream s)
{
if (s == null || s.Length == 0) { this.data = new byte[0]; return; }
s.Position = 0;
BinaryReader r = new BinaryReader(s);
info = new RLEInfo(s);
this.MipHeaders = new MipHeader[this.info.mipCount + 1];
Expand Down Expand Up @@ -493,6 +496,7 @@ public Stream ToBlock4DDS()

public void ImportToRLE(Stream input, RLEVersion rleVersion = RLEVersion.RLE2)
{
input.Position = 0;
var fullOpaqueAlpha = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

MemoryStream output = new MemoryStream();
Expand All @@ -501,6 +505,7 @@ public void ImportToRLE(Stream input, RLEVersion rleVersion = RLEVersion.RLE2)

this.info = new RLEInfo();
this.info.Parse(input);
this.info.Version = rleVersion;
if (this.info.pixelFormat.Fourcc != FourCC.DXT5 & this.info.notFourCC != NotFourCC.L8) throw new InvalidDataException(string.Format("Not a DXT5 or L8 format DDS, read FourCC: {0}, format: {0}", this.info.pixelFormat.Fourcc, this.info.notFourCC));

if (this.info.Depth == 0) this.info.Depth = 1;
Expand Down Expand Up @@ -653,8 +658,6 @@ public void ImportToRLE(Stream input, RLEVersion rleVersion = RLEVersion.RLE2)
w.Write(mipHeader.CommandOffset + commandOffset);
w.Write(mipHeader.Offset0 + block0Offset);
}

this.data = output.ToArray();
}
}

Expand Down Expand Up @@ -795,8 +798,6 @@ public void ImportToRLE(Stream input, RLEVersion rleVersion = RLEVersion.RLE2)
w.Write(mipHeader.Offset0 + block0Offset);
w.Write(mipHeader.Offset1 + block1Offset);
}

this.data = output.ToArray();
}
}
else
Expand Down Expand Up @@ -948,10 +949,12 @@ public void ImportToRLE(Stream input, RLEVersion rleVersion = RLEVersion.RLE2)
w.Write(mipHeader.Offset1 + block1Offset);
w.Write(mipHeader.Offset4 + block4Offset);
}

this.data = output.ToArray();
}
}

// this.data = output.ToArray();
output.Position = 0;
Parse(output);
}

private static bool TrueForAny<T>(T[] array, int offset, int count, Predicate<T> match)
Expand Down
5 changes: 4 additions & 1 deletion s4pi Wrappers/ImageResource/ThumbnailResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void TransformToPNG()
using (MemoryStream ms = new MemoryStream(this.rawData))
{
BinaryReader r = new BinaryReader(ms);
Bitmap colorImage = new Bitmap(ms);
Bitmap colorImage;
ms.Position = 0;
r.ReadBytes(24);
if (r.ReadUInt32() == 0x41464C41U)
Expand All @@ -123,13 +123,16 @@ private void TransformToPNG()
using (MemoryStream alphaStream = new MemoryStream(r.ReadBytes(length)))
{
Bitmap alphaImage = new Bitmap(alphaStream);
colorImage = new Bitmap(ms);
if (colorImage.Width != alphaImage.Width || colorImage.Height != alphaImage.Height) throw new InvalidDataException("Not a proper TS4 Thumbnail image");
colorImage = UpdateAlpha(colorImage, alphaImage);

this.image = colorImage;
return;
}
}
ms.Position = 0;
colorImage = new Bitmap(ms);
this.image = colorImage;
}
}
Expand Down

0 comments on commit f323fec

Please sign in to comment.