/// <summary>
        /// Gets a <see cref="Bitmap"/> given an array and mip index.
        /// </summary>
        /// <param name="ArrayIndex">The index of the surface/array. Cubemaps will have 6</param>
        /// <param name="MipLevel">The index of the mip level.</param>
        /// <returns></returns>
        public Bitmap GetBitmap(int ArrayLevel = 0, int MipLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

            byte[] data = GetImageData(ArrayLevel, MipLevel);

            try
            {
                if (data == null)
                {
                    throw new Exception("Data is null!");
                }

                if (PlatformSwizzle == PlatformSwizzle.Platform_3DS && !IsCompressed(Format))
                {
                    var Image = BitmapExtension.GetBitmap(ConvertBgraToRgba(CTR_3DS.DecodeBlock(data, (int)width, (int)height, Format)),
                                                          (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Image.RotateFlip(RotateFlipType.RotateNoneFlipY); //It's upside down for some reason so flip it
                    return(Image);
                }

                switch (Format)
                {
                case TEX_FORMAT.R4G4_UNORM:
                    return(BitmapExtension.GetBitmap(R4G4.Decompress(data, (int)width, (int)height, false),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                case TEX_FORMAT.BC5_SNORM:
                    return(DDSCompressor.DecompressBC5(data, (int)width, (int)height, true));

                case TEX_FORMAT.ETC1:
                    return(BitmapExtension.GetBitmap(ETC1.ETC1Decompress(data, (int)width, (int)height, false),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                case TEX_FORMAT.ETC1_A4:
                    return(BitmapExtension.GetBitmap(ETC1.ETC1Decompress(data, (int)width, (int)height, true),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                default:
                    return(BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
                }
            }
            catch (Exception ex)
            {
                Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);

                try
                {
                    if (Format == TEX_FORMAT.BC1_UNORM)
                    {
                        return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC1_UNORM_SRGB)
                    {
                        return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC3_UNORM_SRGB)
                    {
                        return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC3_UNORM)
                    {
                        return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC4_UNORM)
                    {
                        return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC4_SNORM)
                    {
                        return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC5_UNORM)
                    {
                        return(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, false));
                    }
                    else
                    {
                        if (Runtime.UseOpenGL)
                        {
                            Runtime.OpenTKInitialized = true;
                            LoadOpenGLTexture();
                            return(RenderableTex.GLTextureToBitmap(RenderableTex));
                        }
                    }
                }
                catch
                {
                    Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                }

                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets a <see cref="Bitmap"/> given an array and mip index.
        /// </summary>
        /// <param name="ArrayIndex">The index of the surface/array. Cubemaps will have 6</param>
        /// <param name="MipLevel">The index of the mip level.</param>
        /// <returns></returns>
        public Bitmap GetBitmap(int ArrayLevel = 0, int MipLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

            byte[] data = GetImageData(ArrayLevel, MipLevel);

            try
            {
                if (data == null)
                {
                    throw new Exception("Data is null!");
                }

                if (Format == TEX_FORMAT.BC5_SNORM)
                {
                    return(DDSCompressor.DecompressBC5(data, (int)width, (int)height, true));
                }

                Bitmap bitmap = BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
                                                          (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                return(bitmap);
            }
            catch (Exception ex)
            {
                Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);

                try
                {
                    if (Format == TEX_FORMAT.BC1_UNORM)
                    {
                        return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC1_UNORM_SRGB)
                    {
                        return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC3_UNORM_SRGB)
                    {
                        return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC3_UNORM)
                    {
                        return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC4_UNORM)
                    {
                        return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, false));
                    }
                    else if (Format == TEX_FORMAT.BC4_SNORM)
                    {
                        return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, true));
                    }
                    else if (Format == TEX_FORMAT.BC5_UNORM)
                    {
                        return(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, false));
                    }
                    else
                    {
                        Runtime.OpenTKInitialized = true;
                        LoadOpenGLTexture();
                        return(RenderableTex.GLTextureToBitmap(RenderableTex));
                    }
                }
                catch
                {
                    Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                }

                return(null);
            }



            /*       try
             *     {
             *         Bitmap bitmap = BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
             *             (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
             *
             *         return bitmap;
             *     }
             *     catch (Exception ex)
             *     {
             *        MessageBox.Show($"Failed to texture {Text} \n{DebugInfo()}\n {ex.ToString()}");
             *         return null;
             *     }*/
        }