Ejemplo n.º 1
0
        public static SBSurface FromBitmap(Bitmap bmp)
        {
            SBSurface surface = new SBSurface();

            surface.Name           = "";
            surface.Width          = bmp.Width;
            surface.Height         = bmp.Height;
            surface.InternalFormat = InternalFormat.Rgba;
            surface.PixelFormat    = PixelFormat.Bgra;
            surface.PixelType      = PixelType.UnsignedByte;

            var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var length     = bitmapData.Stride * bitmapData.Height;

            byte[] bytes = new byte[length];

            Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
            bmp.UnlockBits(bitmapData);

            var mip = new MipArray();

            mip.Mipmaps.Add(bytes);

            surface.Arrays.Add(mip);

            return(surface);
        }
Ejemplo n.º 2
0
        public override void Open()
        {
            surface = Open(AbsolutePath);
            // HACK: Empty streams?
            if (surface == null)
            {
                using (var image = Image.FromFile("DefaultTextures/default_black.png"))
                {
                    surface      = SBSurface.FromBitmap((Bitmap)image);
                    surface.Name = Path.GetFileNameWithoutExtension(AbsolutePath);
                }
            }

            TexName = surface.Name;
        }
Ejemplo n.º 3
0
        public NutexbNode(string path) : base(path, "texture", true)
        {
            var surface = SBSurface.FromNutexb(AbsolutePath);

            TexName = surface?.Name ?? Path.GetFileNameWithoutExtension(AbsolutePath);

            if (surface == null)
            {
                Renderable = new Lazy <IRenderable>(new RTexture(DefaultTextures.Instance.Value.DefaultBlack, false));
            }
            else
            {
                Renderable = new Lazy <IRenderable>(new RTexture(surface.GetRenderTexture(), surface.IsSRGB));
            }
        }
Ejemplo n.º 4
0
        public static void Export(string FileName, SBSurface surface)
        {
            using (BinaryWriter writer = new BinaryWriter(new FileStream(FileName, FileMode.Create)))
            {
                writer.Write(SwitchSwizzler.CreateBuffer(surface));

                uint ImageSize = (uint)writer.BaseStream.Position;

                foreach (var mip in surface.Arrays)
                {
                    foreach (var m in mip.Mipmaps)
                    {
                        writer.Write(m.Length);
                    }
                    for (int i = mip.Mipmaps.Count; i < 0x10; i++)
                    {
                        writer.Write(0);
                    }
                }

                writer.Write(new char[] { ' ', 'X', 'N', 'T' });
                writer.Write(surface.Name.ToCharArray());
                writer.Write(new byte[0x40 - surface.Name.Length]);
                writer.Write(surface.Width);
                writer.Write(surface.Height);
                writer.Write(surface.Depth);
                writer.Write((byte)TexFormatByInternalFormat(surface.InternalFormat)); // format
                writer.Write((byte)4);                                                 // unknown usually 4
                writer.Write((short)0);                                                // pad
                writer.Write(surface.IsCubeMap ? 9 : 4);                               // unknown usually 4 9 for cubemap
                writer.Write(surface.Arrays[0].Mipmaps.Count);
                writer.Write(0x1000);                                                  // alignment
                writer.Write(surface.Arrays.Count);                                    // array count
                writer.Write(ImageSize);

                writer.Write(new char[] { ' ', 'X', 'E', 'T' });
                writer.Write((short)1); // version major
                writer.Write((short)2); // version minor
            }
        }
Ejemplo n.º 5
0
        public static SBSurface Open(string FilePath)
        {
            using (BinaryReader reader = new BinaryReader(new FileStream(FilePath, FileMode.Open)))
            {
                // TODO: Why are there empty streams?
                if (reader.BaseStream.Length == 0)
                {
                    return(null);
                }

                SBSurface surface = new SBSurface();

                reader.BaseStream.Position = reader.BaseStream.Length - 0xB0;

                int[] mipmapSizes = new int[16];
                for (int i = 0; i < mipmapSizes.Length; i++)
                {
                    mipmapSizes[i] = reader.ReadInt32();
                }

                reader.ReadChars(4); // TNX magic

                string texName = ReadTexName(reader);
                surface.Name = texName;

                surface.Width  = reader.ReadInt32();
                surface.Height = reader.ReadInt32();
                surface.Depth  = reader.ReadInt32();

                var Format = (NUTEX_FORMAT)reader.ReadByte();

                reader.ReadByte();

                ushort Padding = reader.ReadUInt16();
                reader.ReadUInt32();

                int MipCount  = reader.ReadInt32();
                int Alignment = reader.ReadInt32();
                surface.ArrayCount = reader.ReadInt32();
                int    ImageSize    = reader.ReadInt32();
                char[] Magic        = reader.ReadChars(4);
                int    MajorVersion = reader.ReadInt16();
                int    MinorVersion = reader.ReadInt16();

                if (pixelFormatByNuTexFormat.ContainsKey(Format))
                {
                    surface.PixelFormat = pixelFormatByNuTexFormat[Format];
                }

                if (internalFormatByNuTexFormat.ContainsKey(Format))
                {
                    surface.InternalFormat = internalFormatByNuTexFormat[Format];
                }

                surface.PixelType = GetPixelType(Format);

                reader.BaseStream.Position = 0;
                byte[] ImageData = reader.ReadBytes(ImageSize);

                for (int array = 0; array < surface.ArrayCount; array++)
                {
                    MipArray arr = new MipArray();
                    for (int i = 0; i < MipCount; i++)
                    {
                        byte[] deswiz = SwitchSwizzler.GetImageData(surface, ImageData, array, i, MipCount);
                        arr.Mipmaps.Add(deswiz);
                    }
                    surface.Arrays.Add(arr);
                }

                return(surface);
            }
        }
Ejemplo n.º 6
0
 public override void Open()
 {
     surface = Open(AbsolutePath);
     TexName = surface.Name;
 }