Advertisement
SciresM

getIMG_ETC1(CLIM bclim)

Oct 11th, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. private Bitmap getIMG_ETC1(CLIM bclim)
  2.         {
  3.             Bitmap img = new Bitmap(Math.Max(nlpo2(bclim.Width), 16), Math.Max(nlpo2(bclim.Height), 16));
  4.             string dllpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location).Replace('\\','/') + "/ETC1.dll";
  5.             if (!File.Exists(dllpath)) File.WriteAllBytes(dllpath, Properties.Resources.ETC1);
  6.             try
  7.             {
  8.                 /*
  9.                  * Much of this code is taken/modified from Tharsis: http://jul.rustedlogic.net/thread.php?pid=436556#436556 Thank you to Tharsis's creator, xdaniel.
  10.                  */
  11.                
  12.  
  13.                 /* Get compressed data & handle to it */
  14.                 byte[] textureData = bclim.Data;
  15.                 //textureData = switchEndianness(textureData, 0x10);
  16.                 ushort[] input = new ushort[textureData.Length / sizeof(ushort)];
  17.                 Buffer.BlockCopy(textureData, 0, input, 0, textureData.Length);
  18.                 GCHandle pInput = GCHandle.Alloc(input, GCHandleType.Pinned);
  19.  
  20.                 /* Marshal data around, invoke ETC1.dll for conversion, etc */
  21.                 UInt32 size1 = 0, size2 = 0;
  22.                 UInt16 wd = (ushort)img.Width, ht = (ushort)img.Height;
  23.                 ConvertETC1(IntPtr.Zero, ref size1, IntPtr.Zero, ref size2, wd, ht, bclim.FileFormat == 0xB); //true = etc1a4, false = etc1
  24.                 System.Diagnostics.Debug.WriteLine(size1);
  25.                 uint[] output = new uint[size1];
  26.                 GCHandle pOutput = GCHandle.Alloc(output, GCHandleType.Pinned);
  27.                 ConvertETC1(pOutput.AddrOfPinnedObject(), ref size1, pInput.AddrOfPinnedObject(), ref size2, wd, ht, bclim.FileFormat == 0xB);
  28.                 pOutput.Free();
  29.                 pInput.Free();
  30.  
  31.                 /* Unscramble if needed // could probably be done in ETC1.dll, it's probably pretty damn ugly, but whatever... */
  32.                 /* Non-square code blocks could need some cleanup, verification, etc. as well... */
  33.                 uint[] finalized = new uint[output.Length];
  34.  
  35.                 //Act if it's square because BCLIM swizzling is stupid
  36.                 Buffer.BlockCopy(output, 0, finalized, 0, finalized.Length);
  37.  
  38.                 byte[] tmp = new byte[finalized.Length];
  39.                 Buffer.BlockCopy(finalized, 0, tmp, 0, tmp.Length);
  40.                 int h = img.Height;
  41.                 int w = img.Width;
  42.                 byte[] imgData = tmp;
  43.                 System.Diagnostics.Debug.WriteLine("COPIED IMAGE DATA AND SHIT");
  44.                 for (int i = 0; i < img.Width; i++)
  45.                 {
  46.                     for (int j = 0; j < img.Height; j++)
  47.                     {
  48.                         int k = (j + i * img.Height) * 4;
  49.                         img.SetPixel(i, j, Color.FromArgb(imgData[k + 3], imgData[k], imgData[k + 1], imgData[k + 2]));
  50.                     }
  51.                 }
  52.                 //image is 13  instead of 12
  53.                 //         24             34
  54.                 img.RotateFlip(RotateFlipType.Rotate90FlipX);
  55.                 if (wd > ht)
  56.                 {
  57.                     //image is now in appropriate order, but the swizzling done been fucked up. Let's fix that.
  58.                     Bitmap img2 = new Bitmap(Math.Max(nlpo2(bclim.Width), 16), Math.Max(nlpo2(bclim.Height), 16));
  59.                     for (int y = 0; y < Math.Max(nlpo2(bclim.Width), 16); y += 8)
  60.                     {
  61.                         for (int x = 0; x < Math.Max(nlpo2(bclim.Height), 16); x++)
  62.                         {
  63.                             for (int j = 0; j < 8; j++) //treat every 8 vertical pixels as 1 pixel for purposes of calculation, add to offset later.
  64.                             {
  65.                                 int x1 = (x + ((y / 8) * h)) % img2.Width; //reshift x
  66.                                 int y1 = ((x + ((y / 8) * h)) / img2.Width) * 8; //reshift y
  67.                                 img2.SetPixel(x1, y1 + j, img.GetPixel(x, y + j)); //reswizzle
  68.                             }
  69.                         }
  70.                     }
  71.                     return img2;
  72.                 }
  73.                 else if (ht > wd)
  74.                 {
  75.             //image is now in appropriate order, but the swizzling done been fucked up. Let's fix that.
  76.                     Bitmap img2 = new Bitmap(Math.Max(nlpo2(bclim.Width), 16), Math.Max(nlpo2(bclim.Height), 16));
  77.                     for (int y = 0; y < Math.Max(nlpo2(bclim.Width), 16); y += 8)
  78.                     {
  79.                         for (int x = 0; x < Math.Max(nlpo2(bclim.Height), 16); x++)
  80.                         {
  81.                             for (int j = 0; j < 8; j++) //treat every 8 vertical pixels as 1 pixel for purposes of calculation, add to offset later.
  82.                             {
  83.                                 int x1 = x%img2.Width; //reshift x
  84.                                 int y1 = ((x + ((y / 8) * h)) / img2.Width) * 8; //reshift y
  85.                                 img2.SetPixel(x1, y1 + j, img.GetPixel(x, y + j)); //reswizzle
  86.                             }
  87.                         }
  88.                     }
  89.                     return img2;
  90.                 }
  91.             }
  92.             catch (System.IndexOutOfRangeException)
  93.             {
  94.                 //
  95.             }
  96.             catch (System.AccessViolationException)
  97.             {
  98.                 //
  99.             }
  100.             return img;
  101.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement