Пример #1
0
        public void Export(Stream stream)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            byte[] buffer = null;
            switch (m_TextureFormat)
            {
            case TextureFormat.DXT1:
            case TextureFormat.DXT5:
                byte[] dds_header = DDS.CreateHeader(m_Width, m_Height, 32, m_MipMap ? 2 : 0, m_TextureFormat);
                writer.Write(dds_header);
                buffer = image_data;
                break;

            case TextureFormat.RGB24:
                byte[] tga_header = TGA.CreateHeader((ushort)m_Width, (ushort)m_Height, 24);
                writer.Write(tga_header);
                buffer = new byte[image_data.Length];
                for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3)
                {
                    byte b = image_data[j];
                    buffer[j]     = image_data[i];
                    buffer[i]     = b;
                    buffer[i + 1] = image_data[i + 1];
                }
                break;

            case TextureFormat.ARGB32:
                tga_header = TGA.CreateHeader((ushort)m_Width, (ushort)m_Height, 32);
                writer.Write(tga_header);
                buffer = new byte[image_data.Length];
                for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4)
                {
                    byte b = image_data[j];
                    buffer[j] = image_data[i];
                    buffer[i] = b;
                    b         = image_data[l];
                    buffer[l] = image_data[k];
                    buffer[k] = b;
                }
                break;

            case TextureFormat.Alpha8:
                tga_header = TGA.CreateHeader((ushort)m_Width, (ushort)m_Height, 8);
                writer.Write(tga_header);
                buffer = (byte[])image_data.Clone();
                break;

            case TextureFormat.RGBA32:
                tga_header = TGA.CreateHeader((ushort)m_Width, (ushort)m_Height, 32);
                writer.Write(tga_header);
                buffer = (byte[])image_data.Clone();
                break;

            default:
                throw new Exception("Unhandled Texture2D format: " + m_TextureFormat);
            }
            writer.Write(buffer);
        }