/// <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, int DepthLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

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

            data = Platform.DecodeImage(this, data, width, height, ArrayLevel, MipLevel);

            if (Platform.OutputFormat != TexFormat.RGBA8_UNORM)
            {
                data = DecodeBlock(data, width, height, Platform.OutputFormat);
            }
            else if (Platform is DefaultSwizzle || Platform is SwitchSwizzle || Platform is WiiUSwizzle)
            {
                data = ImageUtility.ConvertBgraToRgba(data);
            }

            if (data.Length == 0)
            {
                LoadOpenGLTexture();
                return(RenderableTex.ToBitmap());
            }

            if (IsBCNCompressed())
            {
                width  = ((width + 3) / 4) * 4;
                height = ((height + 3) / 4) * 4;
            }

            return(BitmapExtension.CreateBitmap(data, (int)width, (int)height));
        }
        public void Save(System.IO.Stream stream)
        {
            ImageFormat format = ImageFormat.Png;

            if (FileInfo.Extension == ".jpg")
            {
                format = ImageFormat.Jpeg;
            }
            if (FileInfo.Extension == ".gif")
            {
                format = ImageFormat.Gif;
            }
            if (FileInfo.Extension == ".bmp")
            {
                format = ImageFormat.Bmp;
            }
            if (FileInfo.Extension == ".tiff")
            {
                format = ImageFormat.Tiff;
            }

            var bitmap = BitmapExtension.CreateBitmap(ImageData, (int)Width, (int)Height);

            bitmap.Save(stream, format);
        }
예제 #3
0
        public static Tuple <List <byte[]>, ushort[]> GenerateMipList(byte[] uncompressedData, uint TexWidth, uint TexHeight,
                                                                      uint MipCount, TextureFormats Format, PaletteFormats PaletteFormat)
        {
            Bitmap Image = BitmapExtension.CreateBitmap(uncompressedData, (int)TexWidth, (int)TexHeight);

            return(GenerateMipList(Image, TexWidth, TexHeight, MipCount, Format, PaletteFormat));
        }
예제 #4
0
        private void SetAnimationsToFrame(int Frame)
        {
            var video = VideoFormat.VideoData;

            if (video == null)
            {
                return;
            }

            var frame = video.GetFrame(Frame);

            if (frame != null)
            {
                var bitmap = BitmapExtension.CreateBitmap(frame.GetImageData(), (int)video.Width, (int)video.Height);
                if (frame.FlipImage)
                {
                    bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }
                pictureBox1.Image = bitmap;
            }
        }
예제 #5
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, int DepthLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

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

            Console.WriteLine($"data {data.Length}");
            data = Platform.DecodeImage(this, data, width, height, ArrayLevel, MipLevel);
            Console.WriteLine($"data2 {data.Length}");

            Console.WriteLine($"OutputFormat {Platform.OutputFormat}");

            if (Platform.OutputFormat != TexFormat.RGBA8_UNORM)
            {
                data = DecodeBlock(data, width, height, Platform.OutputFormat);
            }
            else if (Platform is DefaultSwizzle || Platform is TegraX1Swizzle || Platform is WiiUSwizzle)
            {
                data = ImageUtility.ConvertBgraToRgba(data);
            }

            return(BitmapExtension.CreateBitmap(data, (int)width, (int)height));
        }
예제 #6
0
 public static System.Drawing.Bitmap DecodeBlockToBitmap(byte[] Input, int Width, int Height, PICASurfaceFormat picaFormat)
 {
     return(BitmapExtension.CreateBitmap(ImageUtility.ConvertBgraToRgba(DecodeBlock(Input, Width, Height, picaFormat)),
                                         Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
 }
예제 #7
0
        //Much help from encoding thanks to this
        // https://github.com/Cruel/3dstex/blob/master/src/Encoder.cpp
        public static byte[] EncodeBlock(byte[] Input, int Width, int Height, PICASurfaceFormat PicaFormat)
        {
            int ImageSize = CalculateLength(Width, Height, PicaFormat);

            if (PicaFormat == PICASurfaceFormat.ETC1)
            {
                return(SmashForge.RG_ETC1.encodeETC(BitmapExtension.CreateBitmap(Input, Width, Height)));
            }
            else if (PicaFormat == PICASurfaceFormat.ETC1A4)
            {
                return(SmashForge.RG_ETC1.encodeETCa4(BitmapExtension.CreateBitmap(Input, Width, Height)));
            }

            var mem = new System.IO.MemoryStream();

            using (var writer = new FileWriter(mem))
            {
                for (int TY = 0; TY < Height; TY += 8)
                {
                    for (int TX = 0; TX < Width; TX += 8)
                    {
                        for (int Px = 0; Px < 64; Px++)
                        {
                            int X = SwizzleLUT[Px] & 7;
                            int Y = (SwizzleLUT[Px] - X) >> 3;

                            int IOffs = (TX + X + ((TY + Y) * Width)) * 4;

                            if (PicaFormat == PICASurfaceFormat.RGBA8)
                            {
                                writer.Write(Input[IOffs + 3]);
                                writer.Write(Input[IOffs + 0]);
                                writer.Write(Input[IOffs + 1]);
                                writer.Write(Input[IOffs + 2]);
                            }
                            else if (PicaFormat == PICASurfaceFormat.RGB8)
                            {
                                writer.Write(Input[IOffs + 0]);
                                writer.Write(Input[IOffs + 1]);
                                writer.Write(Input[IOffs + 2]);
                            }
                            else if (PicaFormat == PICASurfaceFormat.A8)
                            {
                                writer.Write(Input[IOffs]);
                            }
                            else if (PicaFormat == PICASurfaceFormat.L8)
                            {
                                writer.Write(ConvertBRG8ToL(
                                                 new byte[]
                                {
                                    Input[IOffs + 0],
                                    Input[IOffs + 1],
                                    Input[IOffs + 2]
                                }));
                            }
                            else if (PicaFormat == PICASurfaceFormat.LA8)
                            {
                                writer.Write(Input[IOffs + 3]);
                                writer.Write(ConvertBRG8ToL(
                                                 new byte[]
                                {
                                    Input[IOffs + 0],
                                    Input[IOffs + 1],
                                    Input[IOffs + 2]
                                }));
                            }
                            else if (PicaFormat == PICASurfaceFormat.RGB565)
                            {
                                ushort R = (ushort)(Convert8To5(Input[IOffs + 0]));
                                ushort G = (ushort)(Convert8To6(Input[IOffs + 1]) << 5);
                                ushort B = (ushort)(Convert8To5(Input[IOffs + 2]) << 11);

                                writer.Write((ushort)(R | G | B));
                            }
                            else if (PicaFormat == PICASurfaceFormat.RGBA4)
                            {
                                ushort R = (ushort)(Convert8To4(Input[IOffs]) << 4);
                                ushort G = (ushort)(Convert8To4(Input[IOffs + 1]) << 8);
                                ushort B = (ushort)(Convert8To4(Input[IOffs + 2]) << 12);
                                ushort A = (ushort)(Convert8To4(Input[IOffs + 3]));

                                writer.Write((ushort)(R | G | B | A));
                            }
                            else if (PicaFormat == PICASurfaceFormat.RGBA5551)
                            {
                                ushort R = (ushort)(Convert8To5(Input[IOffs + 0]) << 1);
                                ushort G = (ushort)(Convert8To5(Input[IOffs + 1]) << 6);
                                ushort B = (ushort)(Convert8To5(Input[IOffs + 2]) << 11);
                                ushort A = (ushort)(Convert8To1(Input[IOffs + 3]));

                                writer.Write((ushort)(R | G | B | A));
                            }
                            else if (PicaFormat == PICASurfaceFormat.LA4)
                            {
                                byte A = Input[IOffs + 3];
                                byte L = ConvertBRG8ToL(
                                    new byte[]
                                {
                                    Input[IOffs + 0],
                                    Input[IOffs + 1],
                                    Input[IOffs + 2]
                                });
                                writer.Write((byte)((A >> 4) | (L & 0xF0)));
                            }
                            else if (PicaFormat == PICASurfaceFormat.L4)
                            {
                                //Skip alpha channel
                                byte L1 = ConvertBRG8ToL(
                                    new byte[]
                                {
                                    Input[IOffs + 0],
                                    Input[IOffs + 1],
                                    Input[IOffs + 2]
                                });
                                byte L2 = ConvertBRG8ToL(
                                    new byte[]
                                {
                                    Input[IOffs + 4],
                                    Input[IOffs + 5],
                                    Input[IOffs + 6]
                                });

                                writer.Write((byte)((L1 >> 4) | (L2 & 0xF0)));
                                Px++;
                            }
                            else if (PicaFormat == PICASurfaceFormat.A4)
                            {
                                byte A1 = (byte)(Input[IOffs + 3] >> 4);
                                byte A2 = (byte)(Input[IOffs + 7] & 0xF0);
                                writer.Write((byte)(A1 | A2));
                                Px++;
                            }
                            else if (PicaFormat == PICASurfaceFormat.HiLo8)
                            {
                                writer.Write(Input[IOffs]);
                                writer.Write(Input[IOffs + 1]);
                            }
                        }
                    }
                }
            }

            byte[] newOutput = mem.ToArray();
            //    if (newOutput.Length != ImageSize)
            //       throw new Exception($"Invalid image size! Expected {ImageSize} got {newOutput.Length}");

            if (newOutput.Length > 0)
            {
                return(newOutput);
            }
            else
            {
                return(new byte[CalculateLength(Width, Height, PicaFormat)]);
            }
        }
예제 #8
0
 public static System.Drawing.Bitmap DecodeDataToBitmap(byte[] ImageData, ushort[] PaletteData, uint width, uint height, TextureFormats format, PaletteFormats palleteFormat)
 {
     return(BitmapExtension.CreateBitmap(DecodeData(ImageData, PaletteData, width, height, format, palleteFormat),
                                         (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
 }