Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Last active April 25, 2022 04:01
Show Gist options
  • Save donnaken15/cef8255e396d30115acedb7f9b103b12 to your computer and use it in GitHub Desktop.
Save donnaken15/cef8255e396d30115acedb7f9b103b12 to your computer and use it in GitHub Desktop.
/*
* Created by SharpDevelop.
* User: Wesley
* Date: 4/20/2022
* Time: 6:04 PM
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using DDS;
using System.Drawing;
namespace imggen
{
class Program
{
private static BinaryWriter img;
public static byte[] STH(string hex)
{
List<byte> list = new List<byte>();
for (int i = 0; i < hex.Length - 1; i += 2)
{
list.Add(byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber));
}
return list.ToArray();
}
public static uint[] crctable = new uint[256];
public static void initCRC32()
{
uint crc, poly = 0xEDB88320;
for (uint i = 0; i < 256; i++)
{
crc = i;
for (int j = 0; j < 8; j++)
{
if ((crc & 1) == 1)
crc = crc >> 1 ^ poly;
else
crc >>= 1;
}
crctable[i] = crc;
}
}
public static uint crc32(string text)
{
if (text.Length == 8)
try {
byte[] alreadykey = STH(text);
Array.Reverse(alreadykey);
return BitConverter.ToUInt32(alreadykey, 0);
}
catch
{
}
uint crc = 0xFFFFFFFF;
int len = text.Length;
for (uint i = 0; i < len; i++)
{
crc = crc >> 8 & 0x00FFFFFF ^ crctable[(crc ^ text.ToCharArray()[i]) & 0xFF];
}
return crc;
}
public static void ToIMG(string fname)
{
byte[] imagebytes = new byte[0];
Image image = null;
DDSImage dds = new DDSImage();
if (Path.GetExtension(fname) != ".dds")
{
imagebytes = File.ReadAllBytes(fname);
image = Image.FromFile(fname);
}
else
{
imagebytes = File.ReadAllBytes(fname);
dds = DDSImage.Load(imagebytes);
}
uint crcname = crc32(Path.GetFileNameWithoutExtension(fname));
img = new BinaryWriter(File.OpenWrite(Path.GetDirectoryName(fname) + '\\' + crcname.ToString("X8") + ".img.xen"));
img.Write(STH("0A281100"));
img.Write(0);
if (Path.GetExtension(fname) != ".dds")
{
img.Write(STH(image.Width.ToString("X4")));
img.Write(STH(image.Height.ToString("X4")));
img.Write(STH("0001"));
img.Write(STH(image.Width.ToString("X4")));
img.Write(STH(image.Height.ToString("X4")));
}
else
{
img.Write(STH(dds.Header.dwWidth.ToString("X4")));
img.Write(STH(dds.Header.dwHeight.ToString("X4")));
img.Write(STH("0001"));
img.Write(STH(dds.Header.dwWidth.ToString("X4")));
img.Write(STH(dds.Header.dwHeight.ToString("X4")));
}
//img.Write(STH(dds.Header.dwMipMapCount.ToString("X2"))); // dont know where this value is
//img.Write(STH("00"));
img.Write(STH("00010108050000000000"));
// ^ ^
// forgot where it is but its here somewhere
// who uses mipmaps anyway
int imageoffset = 0x28;
int imagelength = imagebytes.Length;
img.Write(STH(imageoffset.ToString("X8")));
img.Write(STH(imagelength.ToString("X8")));
img.Write(STH("00000000"));
img.Write(imagebytes);
img.Close();
}
public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("imggen, another lazy\n\nimggen.exe [dds file]+\noutput: (CRC of filename).img.xen\n");
}
else
{
initCRC32();
foreach (string a in args)
{
string dir = Path.GetDirectoryName(a);
if (dir == string.Empty)
dir = Directory.GetCurrentDirectory();
foreach (string f in Directory.GetFiles(dir, Path.GetFileName(a)))
{
Console.WriteLine("Processing " + f + "...");
ToIMG(f);
}
}
Console.WriteLine("Done");
}
}
}
}
@donnaken15
Copy link
Author

and YES, it support PNGs
thanks aspyr
my brain was btfo when i was shown this was possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment