Skip to content

Commit

Permalink
Version 1.0.2.0
Browse files Browse the repository at this point in the history
Added new option to premultiply alpha when converting to XNB (now on by default).
  • Loading branch information
trigger-segfault committed Aug 3, 2018
1 parent 72b93bf commit 76d4584
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 63 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

A combination tool for managing Terraria content resources. Convert, extract, backup, and restore. The unofficial sequel to TExtract.

![Window Preview](http://i.imgur.com/oTuVrGQ.png)
![Window Preview](https://i.imgur.com/oTuVrGQ.png)

### [Wiki](https://github.com/trigger-death/TConvert/wiki) | [Credits](https://github.com/trigger-death/TConvert/wiki/Credits) | [Image Album](http://imgur.com/a/QaoPd)
### [Wiki](https://github.com/trigger-death/TConvert/wiki) | [Credits](https://github.com/trigger-death/TConvert/wiki/Credits) | [Image Album](https://imgur.com/a/QaoPd)

### [![Get TConvert](http://i.imgur.com/4BGRFF0.png)](https://github.com/trigger-death/TConvert/releases/latest)

## About

* **Created By:** Robert Jordan
* **Version:** 1.0.1.0
* **Version:** 1.0.2.0
* **Language:** C#, WPF

## Requirements for Running
Expand Down
3 changes: 3 additions & 0 deletions TConvert/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<setting name="ExtractAllowFonts" serializeAs="String">
<value>True</value>
</setting>
<setting name="PremultiplyAlpha" serializeAs="String">
<value>True</value>
</setting>
</TConvert.Properties.Settings>
</userSettings>
</configuration>
2 changes: 1 addition & 1 deletion TConvert/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Assembly OnResolveAssemblies(object sender, ResolveEventArgs args) {

string path = assemblyName.Name + ".dll";
if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false) {
path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
path = string.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
}

using (Stream stream = executingAssembly.GetManifestResourceStream(path)) {
Expand Down
30 changes: 27 additions & 3 deletions TConvert/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ private enum ArgTypes {
//AutoCloseDef= 0x4000,
//KeepOpenDef = 0x8000,
Compress = 0x10000,
DontCompress= 0x20000
DontCompress= 0x20000,
Premultiply = 0x40000,
DontPremultiply = 0x80000,
}

/**<summary>Information about a command line option.</summary>*/
Expand Down Expand Up @@ -96,6 +98,8 @@ public string OptionsToString() {

{ ArgTypes.Compress,new OptionInfo(ProcessCompress, "Compress", "Images will be compressed.", null, "-ic", "--compress") },
{ ArgTypes.DontCompress,new OptionInfo(ProcessDontCompress, "Don't Compress", "Images will not be compressed.", null, "-dc", "--dont-compress") },
{ ArgTypes.Premultiply, new OptionInfo(ProcessPremultiply, "Premultiply", "RGB will be multiplied by alpha.", null, "-pr", "--premult") },
{ ArgTypes.DontPremultiply, new OptionInfo(ProcessDontPremultiply, "Don't Premultiply", "RGB will not be modified.", null, "-dp", "--dont-premult") },
};

#endregion
Expand Down Expand Up @@ -124,6 +128,8 @@ public string OptionsToString() {
private static bool autoClose = Settings.Default.AutoCloseCmdProgress;
/**<summary>True if images are compressed.</summary>*/
private static bool compress = Settings.Default.CompressImages && XCompress.IsAvailable;
/**<summary>True if alpha is premultiplied.</summary>*/
private static bool premultiply = Settings.Default.PremultiplyAlpha;
/**<summary>True if a sound is played upon completion.</summary>*/
private static bool sound = Settings.Default.CompletionSound;
/**<summary>True if in console-only mode.</summary>*/
Expand Down Expand Up @@ -335,12 +341,12 @@ public static void ProcessFiles() {
});
#if !(CONSOLE)
if (!console) {
Processing.StartProgressThread(null, "Processing Files...", autoClose, compress, sound, thread);
Processing.StartProgressThread(null, "Processing Files...", autoClose, compress, sound, premultiply, thread);
}
else
#endif
{
Processing.StartConsoleThread("Processing Files...", silent, compress, sound, thread);
Processing.StartConsoleThread("Processing Files...", silent, compress, sound, premultiply, thread);
}
}

Expand Down Expand Up @@ -554,6 +560,24 @@ private static void ProcessDontCompress() {
else
compress = false;
}
/**<summary>Processes the Premultiply option.</summary>*/
private static void ProcessPremultiply() {
if (passedArgs.HasFlag(ArgTypes.Premultiply))
LogOptionAlreadySpecified(ArgTypes.Premultiply);
if (passedArgs.HasFlag(ArgTypes.DontPremultiply))
LogOptionAlreadySpecified(ArgTypes.DontPremultiply);
else
premultiply = true;
}
/**<summary>Processes the Premultiply option.</summary>*/
private static void ProcessDontPremultiply() {
if (passedArgs.HasFlag(ArgTypes.Premultiply))
LogOptionAlreadySpecified(ArgTypes.Premultiply);
if (passedArgs.HasFlag(ArgTypes.DontPremultiply))
LogOptionAlreadySpecified(ArgTypes.DontPremultiply);
else
premultiply = false;
}

#endregion
//--------------------------------
Expand Down
4 changes: 4 additions & 0 deletions TConvert/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static class Config {
public static bool CompressImages { get; set; }
/**<summary>True if a sound is played on completion.</summary>*/
public static bool CompletionSound { get; set; }
/**<summary>True if alpha is premultiplied by default when converting to xnb.</summary>*/
public static bool PremultiplyAlpha { get; set; }

#endregion
//=========== CLASSES ============
Expand Down Expand Up @@ -181,6 +183,7 @@ public static void Load() {
AutoCloseCmdProgress = Settings.Default.AutoCloseCmdProgress;
CompressImages = Settings.Default.CompressImages && XCompress.IsAvailable;
CompletionSound = Settings.Default.CompletionSound;
PremultiplyAlpha = Settings.Default.PremultiplyAlpha;

Extract.FolderInput = Settings.Default.ExtractFolderInput;
Extract.FolderOutput = Settings.Default.ExtractFolderOutput;
Expand Down Expand Up @@ -224,6 +227,7 @@ public static void Save() {
Settings.Default.AutoCloseCmdProgress = AutoCloseCmdProgress;
Settings.Default.CompressImages = CompressImages;
Settings.Default.CompletionSound = CompletionSound;
Settings.Default.PremultiplyAlpha = PremultiplyAlpha;

Settings.Default.ExtractFolderInput = Extract.FolderInput;
Settings.Default.ExtractFolderOutput = Extract.FolderOutput;
Expand Down
38 changes: 27 additions & 11 deletions TConvert/Convert/PngConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class PngConverter {
#region Converting

/**<summary>Converts the specified input file and writes it to the output file.</summary>*/
public static bool Convert(string inputFile, string outputFile, bool changeExtension, bool compressed, bool reach) {
public static bool Convert(string inputFile, string outputFile, bool changeExtension, bool compressed, bool reach, bool premultiply) {
if (changeExtension) {
outputFile = Path.ChangeExtension(outputFile, ".xnb");
}
Expand All @@ -80,11 +80,11 @@ public static bool Convert(string inputFile, string outputFile, bool changeExten
}
writer.Write(flagBits); // flag-bits; 00=reach, 01=hiprofile, 80=compressed, 00=uncompressed
if (compressed) {
WriteCompressedData(writer, bmp);
WriteCompressedData(writer, bmp, premultiply);
}
else {
writer.Write(MetadataSize + bmp.Width * bmp.Height * 4); // compressed file size
WriteData(bmp, writer);
WriteData(bmp, writer, premultiply);
}
}
}
Expand All @@ -97,11 +97,11 @@ public static bool Convert(string inputFile, string outputFile, bool changeExten
#region Writing

/**<summary>Write compressed image data.</summary>*/
private static void WriteCompressedData(BinaryWriter writer, Bitmap png) {
private static void WriteCompressedData(BinaryWriter writer, Bitmap png, bool premultiply) {
using (MemoryStream stream = new MemoryStream()) {
byte[] uncompressedData;
using (BinaryWriter writer2 = new BinaryWriter(stream)) {
WriteData(png, writer2);
WriteData(png, writer2, premultiply);
uncompressedData = stream.ToArray();
}
byte[] compressedData = XCompress.Compress(uncompressedData);
Expand All @@ -111,7 +111,7 @@ private static void WriteCompressedData(BinaryWriter writer, Bitmap png) {
}
}
/**<summary>Write uncompressed image data.</summary>*/
private static void WriteData(Bitmap bmp, BinaryWriter writer) {
private static void WriteData(Bitmap bmp, BinaryWriter writer, bool premultiply) {
writer.Write7BitEncodedInt(1); // type-reader-count
writer.Write7BitEncodedString(Texture2DType); // type-reader-name
writer.Write((int)0); // reader version number
Expand All @@ -130,14 +130,30 @@ private static void WriteData(Bitmap bmp, BinaryWriter writer) {
}
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
try {
var length = bitmapData.Stride * bitmapData.Height;
int length = bitmapData.Stride * bitmapData.Height;
byte[] bytes = new byte[length];
Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
// Swap the R and B channels
for (int i = 0; i < bytes.Length; i += 4) {
byte b = bytes[i];
bytes[i] = bytes[i + 2];
bytes[i + 2] = b;
// Always swap red and blue channels premultiply alpha if requested
int a = bytes[i + 3];
if (!premultiply || a == 255) {
// No premultiply necessary
byte b = bytes[i];
bytes[i] = bytes[i + 2];
bytes[i + 2] = b;
}
else if (a != 0) {
byte b = bytes[i];
bytes[i] = (byte) (bytes[i + 2] * a / 255);
bytes[i + 1] = (byte) (bytes[i + 1] * a / 255);
bytes[i + 2] = (byte) (b * a / 255);
}
else {
// alpha is zero, so just zero everything
bytes[i] = 0;
bytes[i + 1] = 0;
bytes[i + 2] = 0;
}
}
writer.Write(bytes);
}
Expand Down
3 changes: 1 addition & 2 deletions TConvert/Convert/WavConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public static bool Convert(string inputFile, string outputFile, bool changeExten
throw new WavException("Average bytes per second number incorrect.");
if (nBlockAlign != (nChannels * (wBitsPerSample / 8)))
throw new WavException("Block align number incorrect.");



inputStream.Position = chunkSize;

format = new string(reader.ReadChars(4));
Expand Down
2 changes: 0 additions & 2 deletions TConvert/Extract/Ffmpeg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public static bool Convert(string input, string output) {
"-acodec pcm_s16le " +
"-nostdin " +
"-ab 128k " +
//"-fflags +bitexact -flags:a +bitexact " +
//"-metadata title=\"\" " +
"-map_metadata -1 " +
"-y " +
"\"" + Path.GetFullPath(output) + "\"";
Expand Down
5 changes: 5 additions & 0 deletions TConvert/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
</MenuItem>
</MenuItem>
<MenuItem Header="Options">
<MenuItem x:Name="menuItemPremultiply" Header="Premultiply Alpha" IsCheckable="True">
<MenuItem.Icon>
<Image Source="Resources/Icons/Premultiply.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="menuItemCompressImages" Header="Compress Images" Click="OnCompressImagesChecked" IsCheckable="True">
<MenuItem.Icon>
<Image Source="Resources/Icons/Compression.png"/>
Expand Down
18 changes: 12 additions & 6 deletions TConvert/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private void LoadConfig() {

menuItemCompressImages.IsEnabled = XCompress.IsAvailable;
menuItemCompressImages.IsChecked = Config.CompressImages;
menuItemPremultiply.IsChecked = Config.PremultiplyAlpha;
menuItemCompletionSound.IsChecked = Config.CompletionSound;
menuItemAutoCloseProgress.IsChecked = Config.AutoCloseProgress;
menuItemAutoCloseDropProgress.IsChecked = Config.AutoCloseDropProgress;
Expand Down Expand Up @@ -251,7 +252,7 @@ private void OnExtract(object sender, RoutedEventArgs e) {
Processing.ExtractSingleFile(input, output);
});
}
Processing.StartProgressThread(this, "Extracting...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Extracting...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
private void OnExtractModeChanged(object sender, SelectionChangedEventArgs e) {
if (!loaded)
Expand Down Expand Up @@ -374,7 +375,7 @@ private void OnConvert(object sender, RoutedEventArgs e) {
Processing.ConvertSingleFile(input, output);
});
}
Processing.StartProgressThread(this, "Converting...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Converting...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
private void OnConvertModeChanged(object sender, SelectionChangedEventArgs e) {
if (!loaded)
Expand Down Expand Up @@ -471,7 +472,7 @@ private void OnBackup(object sender, RoutedEventArgs e) {
Thread thread = new Thread(() => {
Processing.BackupFiles(input, output);
});
Processing.StartProgressThread(this, "Backing Up...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Backing Up...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
private void OnRestore(object sender, RoutedEventArgs e) {
string input = Config.Backup.FolderBackup;
Expand All @@ -495,7 +496,7 @@ private void OnRestore(object sender, RoutedEventArgs e) {
Thread thread = new Thread(() => {
Processing.RestoreFiles(input, output);
});
Processing.StartProgressThread(this, "Restoring...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Restoring...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
private void OnBackupChangeContent(object sender, RoutedEventArgs e) {
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
Expand Down Expand Up @@ -556,7 +557,7 @@ private void OnRunScript(object sender, RoutedEventArgs e) {
thread = new Thread(() => {
Processing.RunScript(input);
});
Processing.StartProgressThread(this, "Running Script...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Running Script...", Config.AutoCloseProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
private void OnChangeScript(object sender, RoutedEventArgs e) {
OpenFileDialog dialog = new OpenFileDialog();
Expand Down Expand Up @@ -648,7 +649,7 @@ private void OnFileDrop(object sender, DragEventArgs e) {
Thread thread = new Thread(() => {
Processing.ProcessDropFiles(extractFiles.ToArray(), convertFiles.ToArray(), scriptFiles.ToArray());
});
Processing.StartProgressThread(this, "Processing Drop Files...", Config.AutoCloseDropProgress, Config.CompressImages, Config.CompletionSound, thread);
Processing.StartProgressThread(this, "Processing Drop Files...", Config.AutoCloseDropProgress, Config.CompressImages, Config.CompletionSound, Config.PremultiplyAlpha, thread);
}
}
}
Expand Down Expand Up @@ -730,6 +731,11 @@ private void OnExit(object sender, RoutedEventArgs e) {
Close();
}

private void OnPremultiplyAlphaChecked(object sender, RoutedEventArgs e) {
if (!loaded)
return;
Config.PremultiplyAlpha = menuItemPremultiply.IsChecked;
}
private void OnCompressImagesChecked(object sender, RoutedEventArgs e) {
if (!loaded)
return;
Expand Down
Loading

0 comments on commit 76d4584

Please sign in to comment.