AoMBrgEditor.DdtFile.Get32BitUncompressed C# (CSharp) Method

Get32BitUncompressed() public method

public Get32BitUncompressed ( ) : byte[]
return byte[]
        public byte[] Get32BitUncompressed()
        {
            byte[] pixels = new byte[Width * Height * 4];
            switch (texelFormat)
            {
                case DdtTexelFormat.Grayscale8:
                    {
                        for (int b = 0; b < imageData.Length; ++b)
                        {
                            var gray = imageData[b];
                            pixels[b * 4 + 0] = gray;
                            pixels[b * 4 + 1] = gray;
                            pixels[b * 4 + 2] = gray;
                            pixels[b * 4 + 3] = 255;
                        }
                    } break;
                case DdtTexelFormat.DXT1:
                case DdtTexelFormat.DXT3:
                case DdtTexelFormat.DXT5:
                    {
                        Pixel[] blockPalette = new Pixel[2];
                        Pixel[] block4Palette = new Pixel[4];
                        int stride = 8;
                        int colOff = 4;
                        int colStride = 1;
                        int alpOff = 0;
                        int alpBits = 0;
                        int alpStrideBits = 0;
                        Action<byte[], int, Pixel[]> extractor = null;
                        switch (texelFormat)
                        {
                            case DdtTexelFormat.DXT1: extractor = Convert565ToRGB; stride = 8; colOff = 4; colStride = 1; break;
                            case DdtTexelFormat.DXT3: extractor = Convert555ToRGB; stride = 10; colOff = 6; colStride = 1; alpOff = 4; alpBits = 1; alpStrideBits = 4 * 1; break;
                            case DdtTexelFormat.DXT5: extractor = Convert444ToRGB; stride = 16; colOff = 6; colStride = 3; alpOff = 4; alpBits = 4; alpStrideBits = 6 * 4; break;
                        }
                        for (int x = 0; x < Width / 4; ++x)
                        {
                            for (int y = 0; y < Height / 4; ++y)
                            {
                                int pixStart = stride * (y * Width / 4 + x);
                                int palStart = pixStart;
                                int colStart = pixStart + colOff;
                                int alpStart = pixStart + alpOff;
                                extractor(imageData, palStart, blockPalette);
                                block4Palette[0] = blockPalette[0];
                                block4Palette[2] = blockPalette[0].Lerp(blockPalette[1], 1, 3);
                                block4Palette[3] = blockPalette[0].Lerp(blockPalette[1], 2, 3);
                                block4Palette[1] = blockPalette[1];
                                for (int sy = 0; sy < 4; ++sy)
                                {
                                    int py = y * 4 + sy;
                                    int imgOff = sy * colStride + colStart;
                                    int data = imageData[imgOff];
                                    for (int sx = 0; sx < 4; ++sx)
                                    {
                                        int v = (data >> (sx * 2)) & 0x03;
                                        Pixel c = block4Palette[v];
                                        int px = x * 4 + sx;
                                        int p = (px + py * Width) * 4;
                                        pixels[p + 0] = c.B;
                                        pixels[p + 1] = c.G;
                                        pixels[p + 2] = c.R;
                                        pixels[p + 3] = c.A;
                                    }
                                    if (alpBits > 0)
                                    {
                                        int alpMask = (1 << alpBits) - 1;
                                        int alphaStartBit = alpStart * 8 + sy * alpStrideBits;
                                        for (int sx = 0; sx < 4; ++sx)
                                        {
                                            int alphaBit = alphaStartBit + sx * alpBits;
                                            int alphaByte = alphaBit / 8;
                                            int alphaBitOff = alphaBit - alphaByte * 8;
                                            int alphaData = ((imageData[alphaByte + 1]) << 8) + imageData[alphaByte];
                                            int px = x * 4 + sx;
                                            int p = (px + py * Width) * 4;
                                            pixels[p + 3] = (byte)(((alphaData >> alphaBitOff) & alpMask) * 255 / alpMask);
                                        }
                                    }
                                }
                            }
                        }
                    } break;
                default: return null;
            }
            // Flip the image
            for (int y = 0; y < Height / 2; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    int p0 = x + y * Width;
                    int p1 = x + (Height - y - 1) * Width;
                    for (int c = 0; c < 4; ++c)
                    {
                        byte t = pixels[p0 * 4 + c];
                        pixels[p0 * 4 + c] = pixels[p1 * 4 + c];
                        pixels[p1 * 4 + c] = t;
                    }
                }
            }
            return pixels;
        }