Ejemplo n.º 1
0
        /// <summary>
        /// Read a DDS header from an array of bytes.
        /// </summary>
        public DDS(byte[] bytes)
        {
            BinaryReaderEx br = new BinaryReaderEx(false, bytes);

            // dwMagic
            br.AssertASCII("DDS ");
            // dwSize
            br.AssertInt32(0x7C);

            dwFlags             = (DDSD)br.ReadUInt32();
            dwHeight            = br.ReadInt32();
            dwWidth             = br.ReadInt32();
            dwPitchOrLinearSize = br.ReadInt32();
            dwDepth             = br.ReadInt32();
            dwMipMapCount       = br.ReadInt32();

            // dwReserved1
            br.Skip(4 * 11);

            ddspf   = new PIXELFORMAT(br);
            dwCaps  = (DDSCAPS)br.ReadUInt32();
            dwCaps2 = (DDSCAPS2)br.ReadUInt32();

            // dwCaps3, dwCaps4, dwReserved2
            br.Skip(4 * 3);

            if (ddspf.dwFourCC == PIXELFORMAT.FourCCDX10)
            {
                header10 = new HEADER_DXT10(br);
            }
            else
            {
                header10 = null;
            }
        }
Ejemplo n.º 2
0
        public DDS(byte[] bytes)
        {
            BinaryReaderEx br = new BinaryReaderEx(false, bytes);

            br.AssertASCII("DDS ");
            br.AssertInt32(124);
            dwFlags             = br.ReadInt32();
            dwHeight            = br.ReadInt32();
            dwWidth             = br.ReadInt32();
            dwPitchOrLinearSize = br.ReadInt32();
            dwDepth             = br.ReadInt32();
            dwMipMapCount       = br.ReadInt32();

            // dwReserved1
            br.Skip(4 * 11);

            ddspf   = new PIXELFORMAT(br);
            dwCaps  = br.ReadInt32();
            dwCaps2 = br.ReadInt32();

            // dwCaps3, dwCaps4, dwReserved2
            br.Skip(4 * 3);

            if (ddspf.dwFourCC == "DX10")
            {
                header10 = new HEADER_DXT10(br);
            }
            else
            {
                header10 = null;
            }

            dataOffset = br.Position;
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create a new DDS header with default values and no DX10 header.
 /// </summary>
 public DDS()
 {
     dwFlags     = HEADER_FLAGS_TEXTURE;
     dwReserved1 = new int[11];
     ddspf       = new PIXELFORMAT();
     dwCaps      = DDSCAPS.TEXTURE;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a new DDS header with default values and no DX10 header.
 /// </summary>
 public DDS()
 {
     dwFlags             = HEADER_FLAGS_TEXTURE;
     dwHeight            = 0;
     dwWidth             = 0;
     dwPitchOrLinearSize = 0;
     dwDepth             = 0;
     dwMipMapCount       = 0;
     ddspf    = new PIXELFORMAT();
     dwCaps   = DDSCAPS.TEXTURE;
     dwCaps2  = 0;
     header10 = null;
 }
Ejemplo n.º 5
0
        public static byte[] Headerize(TPF.Texture texture)
        {
            if (SFEncoding.ASCII.GetString(texture.Bytes, 0, 4) == "DDS ")
            {
                return(texture.Bytes);
            }

            var   dds      = new DDS();
            byte  format   = texture.Format;
            short width    = texture.Header.Width;
            short height   = texture.Header.Height;
            int   mipCount = texture.Mipmaps;

            TPF.TexType type = texture.Type;

            dds.dwFlags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT | DDSD.MIPMAPCOUNT;
            if (CompressedBPB.ContainsKey(format))
            {
                dds.dwFlags |= DDSD.PITCH;
            }
            else if (UncompressedBPP.ContainsKey(format))
            {
                dds.dwFlags |= DDSD.LINEARSIZE;
            }

            dds.dwHeight = height;
            dds.dwWidth  = width;

            if (CompressedBPB.ContainsKey(format))
            {
                dds.dwPitchOrLinearSize = Math.Max(1, (width + 3) / 4) * CompressedBPB[format];
            }
            else if (UncompressedBPP.ContainsKey(format))
            {
                dds.dwPitchOrLinearSize = (width * UncompressedBPP[format] + 7) / 8;
            }

            if (mipCount == 0)
            {
                mipCount = DetermineMipCount(width, height);
            }
            dds.dwMipMapCount = mipCount;

            dds.dwCaps = DDSCAPS.TEXTURE;
            if (type == TPF.TexType.Cubemap)
            {
                dds.dwCaps |= DDSCAPS.COMPLEX;
            }
            if (mipCount > 1)
            {
                dds.dwCaps |= DDSCAPS.COMPLEX | DDSCAPS.MIPMAP;
            }

            if (type == TPF.TexType.Cubemap)
            {
                dds.dwCaps2 = CUBEMAP_ALLFACES;
            }
            else if (type == TPF.TexType.Volume)
            {
                dds.dwCaps2 = DDSCAPS2.VOLUME;
            }

            PIXELFORMAT ddspf = dds.ddspf;

            if (FourCC.ContainsKey(format) || DX10Formats.Contains(format))
            {
                ddspf.dwFlags = DDPF.FOURCC;
            }
            if (format == 6)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }
            else if (format == 9)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }
            else if (format == 10)
            {
                ddspf.dwFlags |= DDPF.RGB;
            }
            else if (format == 16)
            {
                ddspf.dwFlags |= DDPF.ALPHA;
            }
            else if (format == 105)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }

            if (format == 0 || format == 1)
            {
                ddspf.SetFourCCAsString("DXT1");
            }
            else if (format == 3)
            {
                ddspf.SetFourCCAsString("DXT3");
            }
            else if (format == 5)
            {
                ddspf.SetFourCCAsString("DXT5");
            }
            else
            {
                ddspf.dwFourCC = 0;
            }

            if (format == 6)
            {
                ddspf.dwRGBBitCount = 16;
                ddspf.dwRBitMask    = 0b01111100_00000000;
                ddspf.dwGBitMask    = 0b00000011_11100000;
                ddspf.dwBBitMask    = 0b00000000_00011111;
                ddspf.dwABitMask    = 0b10000000_00000000;
            }
            else if (format == 9)
            {
                ddspf.dwRGBBitCount = 32;
                ddspf.dwRBitMask    = 0x00FF0000;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x000000FF;
                ddspf.dwABitMask    = 0xFF000000;
            }
            else if (format == 10)
            {
                ddspf.dwRGBBitCount = 24;
                ddspf.dwRBitMask    = 0x00FF0000;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x000000FF;
            }
            else if (format == 16)
            {
                ddspf.dwRGBBitCount = 8;
                ddspf.dwABitMask    = 0x000000FF;
            }
            else if (format == 105)
            {
                ddspf.dwRGBBitCount = 32;
                ddspf.dwRBitMask    = 0x000000FF;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x00FF0000;
                ddspf.dwABitMask    = 0xFF000000;
            }

            if (DX10Formats.Contains(format))
            {
                dds.header10            = new HEADER_DXT10();
                dds.header10.dxgiFormat = (DXGI_FORMAT)texture.Header.DXGIFormat;
                if (type == TPF.TexType.Cubemap)
                {
                    dds.header10.miscFlag = DDS_RESOURCE_MISC.TEXTURECUBE;
                }
            }

            byte[] bytes      = texture.Bytes;
            int    imageCount = type == TPF.TexType.Cubemap ? 6 : 1;

            List <Image> images = null;

            if (CompressedBPB.ContainsKey(format))
            {
                images = Image.ReadCompressed(bytes, width, height, imageCount, mipCount, 0x80, CompressedBPB[format]);
            }
            else if (UncompressedBPP.ContainsKey(format))
            {
                images = Image.ReadUncompressed(bytes, width, height, imageCount, mipCount, 0x80, UncompressedBPP[format]);
            }

            if (format == 10)
            {
                int texelSize  = -1;
                int texelWidth = -1;
                if (format == 10)
                {
                    texelSize  = 4;
                    texelWidth = width;
                }

                foreach (Image image in images)
                {
                    for (int i = 0; i < image.MipLevels.Count; i++)
                    {
                        byte[] swizzled   = image.MipLevels[i];
                        byte[] unswizzled = DeswizzlePS3(swizzled, texelSize, texelWidth / (int)Math.Pow(2, i));
                        if (format == 10)
                        {
                            byte[] trimmed = new byte[unswizzled.Length / 4 * 3];
                            for (int j = 0; j < unswizzled.Length / 4; j++)
                            {
                                Array.Reverse(unswizzled, j * 4, 4);
                                Array.Copy(unswizzled, j * 4, trimmed, j * 3, 3);
                            }
                            unswizzled = trimmed;
                        }
                        image.MipLevels[i] = unswizzled;
                    }
                }
            }

            if (images != null)
            {
                bytes = Image.Write(images);
            }

            return(dds.Write(bytes));
        }
Ejemplo n.º 6
0
        public static byte[] Headerize(TPF.Texture texture)
        {
            if (Encoding.ASCII.GetString(texture.Bytes, 0, 4) == "DDS ")
            {
                return(texture.Bytes);
            }

            DDS         dds   = new DDS();
            PIXELFORMAT ddspf = dds.ddspf;

            byte format = texture.Format;

            if (format != 16 && format != 26)
            {
                DDSD dwFlags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT | DDSD.MIPMAPCOUNT;
                if (format == 0 || format == 1 || format == 3 || format == 5)
                {
                    dwFlags |= DDSD.LINEARSIZE;
                }
                else if (format == 9 || format == 10)
                {
                    dwFlags |= DDSD.PITCH;
                }
                dds.dwFlags = dwFlags;

                dds.dwHeight = texture.Header.Height;
                dds.dwWidth  = texture.Header.Width;

                if (format == 9 || format == 10)
                {
                    dds.dwPitchOrLinearSize = (texture.Header.Width * 32 + 7) / 8;
                }
                //else if (format == 0 || format == 1)
                //    pitch = Math.Max(1, (texture.Header.Width + 3) / 4) * (8 * 8 * 8);
                //else if (format == 5)
                //    pitch = Math.Max(1, (texture.Header.Width + 3) / 4) * (16 * 16 * 8);

                dds.dwDepth       = 1;
                dds.dwMipMapCount = texture.Mipmaps;

                DDPF ddspfdwFlags = 0;
                if (format == 0 || format == 1 || format == 3 || format == 5)
                {
                    ddspfdwFlags |= DDPF.FOURCC;
                }
                else if (format == 9)
                {
                    ddspfdwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
                }
                else if (format == 10)
                {
                    ddspfdwFlags |= DDPF.RGB;
                }
                ddspf.dwFlags = ddspfdwFlags;

                if (format == 0 || format == 1)
                {
                    ddspf.SetFourCCAsString("DXT1");
                }
                else if (format == 3)
                {
                    ddspf.SetFourCCAsString("DXT3");
                }
                else if (format == 5)
                {
                    ddspf.SetFourCCAsString("DXT5");
                }
                else
                {
                    ddspf.dwFourCC = 0;
                }

                if (format == 9 || format == 10)
                {
                    ddspf.dwRGBBitCount = 32;
                }

                if (format == 9)
                {
                    ddspf.dwRBitMask = 0x00FF0000;
                    ddspf.dwGBitMask = 0x0000FF00;
                    ddspf.dwBBitMask = 0x000000FF;
                    ddspf.dwABitMask = 0xFF000000;
                }
                else if (format == 10)
                {
                    ddspf.dwRBitMask = 0x000000FF;
                    ddspf.dwGBitMask = 0x0000FF00;
                    ddspf.dwBBitMask = 0x00FF0000;
                    ddspf.dwABitMask = 0x00000000;
                }

                DDSCAPS dwCaps = DDSCAPS.TEXTURE;
                if (texture.Type == TPF.TexType.Cubemap)
                {
                    dwCaps |= DDSCAPS.COMPLEX;
                }
                if (texture.Mipmaps > 1)
                {
                    dwCaps |= DDSCAPS.COMPLEX | DDSCAPS.MIPMAP;
                }
                dds.dwCaps = dwCaps;

                if (texture.Type == TPF.TexType.Cubemap)
                {
                    dds.dwCaps2 = CUBEMAP_ALLFACES;
                }
                else if (texture.Type == TPF.TexType.Volume)
                {
                    dds.dwCaps2 = DDSCAPS2.VOLUME;
                }
            }
            else
            {
                //int a = 0;
            }

            return(dds.Write(texture.Bytes));
        }
Ejemplo n.º 7
0
        public static byte[] Headerize(TPF.Texture texture)
        {
            if (SFEncoding.ASCII.GetString(texture.Bytes, 0, 4) == "DDS ")
            {
                return(texture.Bytes);
            }

            var   dds      = new DDS();
            byte  format   = texture.Format;
            short width    = texture.Header.Width;
            short height   = texture.Header.Height;
            int   mipCount = texture.Mipmaps;

            TPF.TexType type = texture.Type;

            dds.dwFlags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT | DDSD.MIPMAPCOUNT;
            if (CompressedBPB.ContainsKey(format))
            {
                dds.dwFlags |= DDSD.LINEARSIZE;
            }
            else if (UncompressedBPP.ContainsKey(format))
            {
                dds.dwFlags |= DDSD.PITCH;
            }

            dds.dwHeight = height;
            dds.dwWidth  = width;

            if (CompressedBPB.ContainsKey(format))
            {
                dds.dwPitchOrLinearSize = Math.Max(1, (width + 3) / 4) * CompressedBPB[format];
            }
            else if (UncompressedBPP.ContainsKey(format))
            {
                dds.dwPitchOrLinearSize = (width * UncompressedBPP[format] + 7) / 8;
            }

            // This line serves only to remind me that I didn't forget about dwDepth, I left it 0 on purpose.
            dds.dwDepth = 0;

            if (mipCount == 0)
            {
                mipCount = DetermineMipCount(width, height);
            }
            dds.dwMipMapCount = mipCount;

            dds.dwCaps = DDSCAPS.TEXTURE;
            if (type == TPF.TexType.Cubemap)
            {
                dds.dwCaps |= DDSCAPS.COMPLEX;
            }
            if (mipCount > 1)
            {
                dds.dwCaps |= DDSCAPS.COMPLEX | DDSCAPS.MIPMAP;
            }

            if (type == TPF.TexType.Cubemap)
            {
                dds.dwCaps2 = CUBEMAP_ALLFACES;
            }
            else if (type == TPF.TexType.Volume)
            {
                dds.dwCaps2 = DDSCAPS2.VOLUME;
            }

            PIXELFORMAT ddspf = dds.ddspf;

            if (FourCC.ContainsKey(format) || DX10Formats.Contains(format))
            {
                ddspf.dwFlags = DDPF.FOURCC;
            }
            if (format == 6)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }
            else if (format == 9)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }
            else if (format == 10)
            {
                ddspf.dwFlags |= DDPF.RGB;
            }
            else if (format == 16)
            {
                ddspf.dwFlags |= DDPF.ALPHA;
            }
            else if (format == 105)
            {
                ddspf.dwFlags |= DDPF.ALPHAPIXELS | DDPF.RGB;
            }

            if (FourCC.ContainsKey(format))
            {
                ddspf.dwFourCC = FourCC[format];
            }
            else if (DX10Formats.Contains(format))
            {
                ddspf.dwFourCC = "DX10";
            }

            if (format == 6)
            {
                ddspf.dwRGBBitCount = 16;
                ddspf.dwRBitMask    = 0b01111100_00000000;
                ddspf.dwGBitMask    = 0b00000011_11100000;
                ddspf.dwBBitMask    = 0b00000000_00011111;
                ddspf.dwABitMask    = 0b10000000_00000000;
            }
            else if (format == 9)
            {
                ddspf.dwRGBBitCount = 32;
                ddspf.dwRBitMask    = 0x00FF0000;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x000000FF;
                ddspf.dwABitMask    = 0xFF000000;
            }
            else if (format == 10)
            {
                ddspf.dwRGBBitCount = 24;
                ddspf.dwRBitMask    = 0x00FF0000;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x000000FF;
            }
            else if (format == 16)
            {
                ddspf.dwRGBBitCount = 8;
                ddspf.dwABitMask    = 0x000000FF;
            }
            else if (format == 105)
            {
                ddspf.dwRGBBitCount = 32;
                ddspf.dwRBitMask    = 0x000000FF;
                ddspf.dwGBitMask    = 0x0000FF00;
                ddspf.dwBBitMask    = 0x00FF0000;
                ddspf.dwABitMask    = 0xFF000000;
            }

            if (DX10Formats.Contains(format))
            {
                dds.header10            = new HEADER_DXT10();
                dds.header10.dxgiFormat = (DXGI_FORMAT)texture.Header.DXGIFormat;
                if (type == TPF.TexType.Cubemap)
                {
                    dds.header10.miscFlag = RESOURCE_MISC.TEXTURECUBE;
                }
            }

            byte[] bytes = RebuildPixelData(texture.Bytes, format, width, height, mipCount, type);
            return(dds.Write(bytes));
        }