Skip to content

Commit

Permalink
Move CAT Image decoding code to CATImageDecoder class
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialAmber committed Apr 15, 2020
1 parent 4e6dce3 commit c294340
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 74 deletions.
62 changes: 61 additions & 1 deletion ShandalarImageToolbox/File Format Helpers/CATImageDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,70 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ShandalarImageToolbox
{
class CATImageDecoder
public class CATImageDecoder
{
public static Bitmap DecodeCatImage(ShandalarAsset asset)
{
//Console.WriteLine("Decompressing " + asset.filename);
byte[] uncompressedData = Vlc.VlcDecompress(asset.data);

int width = (int)BitConverter.ToUInt32(asset.data, 0x1c);
int height = (int)BitConverter.ToUInt32(asset.data, 0x20);
int smallTableSize = (int)BitConverter.ToUInt32(asset.data, 0x24);

int var_14;
int var_24;

if ((int)BitConverter.ToUInt32(asset.data, 0) != 0)
{
/// var_14
int eax = width;
int ecx = (int)BitConverter.ToUInt32(asset.data, 0x28) - 1;
if (ecx == 0)
var_14 = eax / 2;
else
var_14 = eax;

/// var_24
eax = height;
ecx = (int)BitConverter.ToUInt32(asset.data, 0x28) - 1;
if (ecx == 0)
var_24 = eax / 2;
else
var_24 = eax;
}
else
{
var_14 = width;
var_24 = height;
}

int ptr1 = 0;
int ptr2 = ptr1 + width * width * 4 + 0x80;
int ptr3 = ptr2 + var_14 * var_14 * 4 + 0x80;

int[] tempArray = GeneralUtilityFunctions.ByteArrayToIntArray(uncompressedData, 0);

Wavelet.WaveletDecode(ref tempArray, ptr1, width, smallTableSize);
Wavelet.WaveletDecode(ref tempArray, ptr2 / 4, var_14, smallTableSize);
Wavelet.WaveletDecode(ref tempArray, ptr3 / 4, var_14, smallTableSize);

/// YCbCr -> RGB
Bitmap outputImage = Wavelet.Decode_YCbCrToRGB(tempArray,
ptr1,
width,
height,
ptr2 / 4,
ptr3 / 4,
var_14,
var_24);

// Return image
return outputImage;
}
}
}
75 changes: 2 additions & 73 deletions ShandalarImageToolbox/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void OpenWvlFileTest(string path)
ShandalarAsset asset = new ShandalarAsset(path, data, ImageType.Cat);
asset.filename = Path.GetFileName(path);
path = path.Replace(".tif", "").Replace(".wvl",".png"); //replace both extensions seperately, because a few card image files don't have the tif file extension
DecodeCatImage(asset).Save(path);
CATImageDecoder.DecodeCatImage(asset).Save(path);
}

public void AddPalette(string name)
Expand Down Expand Up @@ -275,7 +275,7 @@ private void LoadCATFileToolStripMenuItem_Click(object sender, EventArgs e)
asset.childIndex = i;
asset.parentName = filepath;
asset.filename = cat.files[i].name;
asset.image = DecodeCatImage(asset);
asset.image = CATImageDecoder.DecodeCatImage(asset);
assetsListBox.Items.Add(cat.files[i].name);
loadedImages.Add(asset);
SetProgressBarValue((int)Math.Round(100f * ((float)i / (float)cat.files.Count)));
Expand Down Expand Up @@ -425,85 +425,14 @@ private void AssetsListBox_SelectedIndexChanged(object sender, EventArgs e)
hexEditor1.LoadData(asset.data);
if (tempText != fileText)
textBox1.Text = fileText;
if(asset.imageType == ImageType.Cat && loadedImages[assetsListBox.SelectedIndex].image == null)
{
asset.image = DecodeCatImage(asset);
ShowImage(asset.image);
}
}
}

public Bitmap DecodeCatImage(ShandalarAsset asset)
{
//Console.WriteLine("Decompressing " + asset.filename);
byte[] uncompressedData = Vlc.VlcDecompress(asset.data);

int width = (int)BitConverter.ToUInt32(asset.data, 0x1c);
int height = (int)BitConverter.ToUInt32(asset.data, 0x20);
int smallTableSize = (int)BitConverter.ToUInt32(asset.data, 0x24);

int var_14;
int var_24;

if ((int)BitConverter.ToUInt32(asset.data, 0) != 0)
{
/// var_14
int eax = width;
int ecx = (int)BitConverter.ToUInt32(asset.data, 0x28) - 1;
if (ecx == 0)
var_14 = eax / 2;
else
var_14 = eax;

/// var_24
eax = height;
ecx = (int)BitConverter.ToUInt32(asset.data, 0x28) - 1;
if (ecx == 0)
var_24 = eax / 2;
else
var_24 = eax;
}
else
{
var_14 = width;
var_24 = height;
}

int ptr1 = 0;
int ptr2 = ptr1 + width * width * 4 + 0x80;
int ptr3 = ptr2 + var_14 * var_14 * 4 + 0x80;

int[] tempArray = GeneralUtilityFunctions.ByteArrayToIntArray(uncompressedData, 0);

Wavelet.WaveletDecode(ref tempArray, ptr1, width, smallTableSize);
Wavelet.WaveletDecode(ref tempArray, ptr2/4, var_14, smallTableSize);
Wavelet.WaveletDecode(ref tempArray, ptr3/4, var_14, smallTableSize);

/// YCbCr -> RGB
Bitmap outputImage = Wavelet.Decode_YCbCrToRGB(tempArray,
ptr1,
width,
height,
ptr2/4,
ptr3/4,
var_14,
var_24);

// Return image
return outputImage;

ShowImage(outputImage);
}

private void CATFileToolStripMenuItem_Click(object sender, EventArgs e)
{

}





private void OpenFileDialog1_FileOk(object sender, CancelEventArgs e)
{

Expand Down

0 comments on commit c294340

Please sign in to comment.