Skip to content

Commit

Permalink
Add hack to fix PNG format change
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Mar 6, 2022
1 parent b3d1590 commit 1dce7b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions TexTool/Properties/AssemblyInfo.cs
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
[assembly: AssemblyVersion("3.0.1.0")]
[assembly: AssemblyFileVersion("3.0.1.0")]
2 changes: 1 addition & 1 deletion TexTool/TexTool.csproj
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TexTool</RootNamespace>
<AssemblyName>TexTool</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<LangVersion>8</LangVersion>
</PropertyGroup>
Expand Down
29 changes: 22 additions & 7 deletions TexTool/Texture.cs
Expand Up @@ -45,7 +45,9 @@ public static void Convert(string fileName, bool overwrite)

private static void ImgToTex(string texFileName, bool overwrite)
{
using var img = Image.FromFile(texFileName);
var img = Image.FromFile(texFileName);
var width = img.Width;
var height = img.Height;

var rects = new List<Rect>();
var rectsPath = $"{texFileName}.uv.csv";
Expand Down Expand Up @@ -79,7 +81,19 @@ private static void ImgToTex(string texFileName, bool overwrite)
}

using var ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
// Hack to handle PNGs correctly: if the input is PNG, copy-paste it as-is to preserve the format
// TODO: Maybe use proper image processing library
if (!Equals(img.RawFormat, ImageFormat.Png))
{
img.Save(ms, ImageFormat.Png);
img.Dispose();
}
else
{
img.Dispose();
using var fs = File.Open(texFileName, FileMode.Open);
fs.CopyTo(ms);
}
var data = ms.ToArray();

var dirName = Path.GetDirectoryName(texFileName) ?? ".";
Expand All @@ -106,8 +120,8 @@ private static void ImgToTex(string texFileName, bool overwrite)
}
}

bw.Write(img.Width);
bw.Write(img.Height);
bw.Write(width);
bw.Write(height);
bw.Write((int) TextureFormat.Argb32);
bw.Write(data.Length);
bw.Write(data);
Expand Down Expand Up @@ -214,9 +228,10 @@ private static bool ShouldRenameTarget(string fileNameNoExt, string newExt, out

private static void ConvertFromPng(string file, byte[] data, int width, int height, TextureFormat format)
{
using var ms = new MemoryStream(data);
using var img = Image.FromStream(ms);
img.Save(file);
// Save PNG without passing it to Image API
// TODO: Maybe use image processing library to correctly process PNG data
using var fs = File.Create(file);
fs.Write(data, 0, data.Length);
}

private static void ConvertFromDxt(string file, byte[] data, int width, int height, TextureFormat format)
Expand Down

0 comments on commit 1dce7b9

Please sign in to comment.