Decompressing PVRTC
<h2>Task</h2>

<p>For my own OpenGL-Framework i'm trying to decompress PVRTC textures when the plattform isn't supporting it. For this i've downloaded the <a href="http://www.imgtec.com/powervr/insider/sdkdownloads/index.asp" rel="nofollow">PVRTC-SDK</a> and translated the C++-code 1:1 into C#-code with unmanaged code.</p>

<h2>Problem</h2>

<p>To make things short: It isn't working and i don't know the reason for it. I've checked the code twice and it seems to be ok and should work. <strike>Sometimes i get a <code>AccessViolationException</code>, sometimes a <code>LoaderLock</code>.</strike> The <code>AccessViolationException</code> is not longer occuring, has been a error in another part of my code. The colors of the PVRTC is still wrong. It Looks strange, basic strokes are visible, but the colors are wrong.</p>

<h2>Code</h2>

<p>Because of the reason that the code is huge i uploaded it:</p>

<p>[C#]  <a href="http://codeviewer.org/view/code:2a65" rel="nofollow">http://codeviewer.org/view/code:2a65</a></p>

<p>[C++] <a href="http://codeviewer.org/view/code:2a66" rel="nofollow">http://codeviewer.org/view/code:2a66</a></p>

<p>Also the calling code of the C# method, the sizes actually matches the expected values ( So the image values for the source array is exactly <code>width * height * 4bit</code> and the output size is <code>width * height * 4byte</code> ( so the 8x size of the compressed image ).</p>

<pre><code>private static unsafe Image ConvertPVRTC_RGBA8888(Image source)
{
    Int32 mipmapCount = source.HasMipmaps ? Image.GetMipmapCount(source.Width, source.Height) : 1;
    // Create a array with bytes for the target format
    Byte[] bytes = new Byte[RGBA8.GetMipMappedSize(0, mipmapCount, source.Width, source.Height)];


    fixed (Byte* pTarget = bytes)
    {
        fixed (Byte* pSource = source.GetData())
        {
            Int32 sourceOffset = 0;
            Int32 targetOffset = 0;

            for (Int32 i = 0; i &lt; mipmapCount; i++)
            {
                // Calculate offsets
                Int32 sourceSize = source.Format.GetMipMappedSize(i, 1, source.Width, source.Height);
                Int32 targetSize = RGBA8.GetMipMappedSize(i, 1, source.Width, source.Height);

                // Decompress mipmapLevel
                PVRTC.DecompressPVRTC(pSource + sourceOffset, pTarget + targetOffset, GetMipmapSize(source.Width, i), GetMipmapSize(source.Height, i), source.Format.BitsPerPixel == 2);

                // Calculate pointer offset
                sourceOffset += sourceSize;
                targetOffset += targetSize;
            }
        }
    }
    return new Image(bytes, ImageFormat.RGBA8, source.Width, source.Height, source.HasMipmaps, source.IsCubemap);
}
</code></pre>

<h2>Thoughts</h2>

<p>Either the original source-code is not correct or i missed something when i translated the code. Anyway i need a solution and checked the source code twice.</p>

<p>( If there would be any source code for C# i would use it, but i can't find any )</p>
