Ejemplo n.º 1
0
        public static SpriteFont Compile(GraphicsDevice g, string file, out IDisposable gR)
        {
            FontDescriptionImporter  ei = new FontDescriptionImporter();
            FontDescription          ec = ei.Import(file, new XNADynImporterContext());
            FontDescriptionProcessor ep = new FontDescriptionProcessor();
            var cec = ep.Process(ec, new XNADynProcessorContext());

            // Get Private Texture
            Texture2DContent texC   = sfcTexture.GetValue(cec) as Texture2DContent;
            MipmapChain      o      = txcMipmaps.GetValue(texC, null) as MipmapChain;
            BitmapContent    texBMP = o[0];
            SurfaceFormat    sf;

            if (!texBMP.TryGetFormat(out sf))
            {
                throw new InvalidContentException("Could Not Obtain The Surface Format Of The SpriteFont");
            }
            Texture2D texture = new Texture2D(g, texBMP.Width, texBMP.Height, false, sf);

            texture.SetData(texBMP.GetPixelData());

            // Get Private Glyph Data
            List <Rectangle> glyphs    = sfcGlyphs.GetValue(cec) as List <Rectangle>;
            List <Rectangle> cropping  = sfcCropping.GetValue(cec) as List <Rectangle>;
            List <char>      charMap   = sfcCharMap.GetValue(cec) as List <char>;
            int            lineSpacing = (int)sfcLineSpacing.GetValue(cec);
            float          spacing     = (float)sfcSpacing.GetValue(cec);
            List <Vector3> kerning     = sfcKerning.GetValue(cec) as List <Vector3>;
            char?          defaultChar = sfcDefaultChar.GetValue(cec) as char?;

            // Invoke Private SpriteFont Constructor
            gR = texture;
            return(sfConstructor.Invoke(new object[] { texture, glyphs, cropping, charMap, lineSpacing, spacing, kerning, defaultChar }) as SpriteFont);
        }
Ejemplo n.º 2
0
        public override Texture2DContent Process(List <byte[]> input, ContentProcessorContext context)
        {
            for (int p = 0; p < input.Count; ++p)
            {
                if (input[p].Length != Height * Width * sizeof(float) * 3 / (1 << (2 * p)))
                {
                    throw new InvalidContentException("The number of bytes in one or more of the images does not correlate with the product of the Height and Width properties.");
                }
            }

            MipmapChain imageChain = new MipmapChain();
            int         mip        = 0;

            for (; mip < input.Count; ++mip)
            {
                byte[] paddedBytes = new byte[input[mip].Length / 3 * 4];
                int    srcIndex    = 0;
                int    destIndex   = 0;
                while (srcIndex < input[mip].Length)
                {
                    paddedBytes[destIndex++] = input[mip][srcIndex++];
                    if (srcIndex % 12 == 0)
                    {
                        for (int x = 0; x < 4; ++x)
                        {
                            paddedBytes[destIndex++] = 0;
                        }
                    }
                }

                int           mipReduction = 1 << mip;
                BitmapContent image        = new PixelBitmapContent <Vector4>(Width / mipReduction, Height / mipReduction);
                image.SetPixelData(paddedBytes);
                imageChain.Add(image);
            }

            // Check to see if this is a partial mipmap chain:
            if (imageChain.Count > 1)
            {
                // Just fill the rest of the chain with anything to satisfy the validator that the chain is complete.
                while ((Math.Max(Height, Width) >> (mip - 1)) > 1)
                {
                    int           mipReduction = 1 << mip;
                    int           mipHeight    = Math.Max(Height / mipReduction, 1);
                    int           mipWidth     = Math.Max(Width / mipReduction, 1);
                    byte[]        bytes        = new byte[mipHeight * mipWidth * sizeof(float) * 4];
                    BitmapContent image        = new PixelBitmapContent <Vector4>(mipWidth, mipHeight);
                    image.SetPixelData(bytes);
                    imageChain.Add(image);
                    ++mip;
                }
            }

            Texture2DContent outputTC = new Texture2DContent();

            outputTC.Mipmaps = imageChain;

            return(outputTC);
        }
Ejemplo n.º 3
0
        private static void RenderAtlas(TextureAtlasContent output)
        {
            var outputBmp = new PixelBitmapContent <Color>(output.Width, output.Height);

            foreach (var sprite in output.DestinationSprites)
            {
                var srcBmp  = sprite.Texture.Faces[0][0];
                var srcRect = new Rectangle(0, 0, srcBmp.Width, srcBmp.Height);
                BitmapContent.Copy(srcBmp, srcRect, outputBmp, sprite.Bounds);
            }
            var mipmapChain = new MipmapChain(outputBmp);

            output.Texture.Mipmaps = mipmapChain;
        }
Ejemplo n.º 4
0
        public void TextureCubeContent()
        {
            var content = new TextureCubeContent();

            Assert.NotNull(content.Faces);
            Assert.AreEqual(6, content.Faces.Count);
            Assert.NotNull(content.Faces[0]);
            Assert.NotNull(content.Faces[1]);
            Assert.NotNull(content.Faces[2]);
            Assert.NotNull(content.Faces[3]);
            Assert.NotNull(content.Faces[4]);
            Assert.NotNull(content.Faces[5]);
            Assert.AreEqual(0, content.Faces[0].Count);
            Assert.AreEqual(0, content.Faces[1].Count);
            Assert.AreEqual(0, content.Faces[2].Count);
            Assert.AreEqual(0, content.Faces[3].Count);
            Assert.AreEqual(0, content.Faces[4].Count);
            Assert.AreEqual(0, content.Faces[5].Count);

            var face0 = new MipmapChain(new PixelBitmapContent <Color>(2, 2));

            content.Faces[0] = face0;
            Assert.AreEqual(face0, content.Faces[0]);
            Assert.AreEqual(1, content.Faces[0].Count);

            content.Faces[0].Add(new PixelBitmapContent <Color>(1, 1));
            Assert.AreEqual(2, content.Faces[0].Count);

            content.Faces[1].Add(new PixelBitmapContent <Color>(2, 2));
            content.Faces[2].Add(new PixelBitmapContent <Color>(2, 2));
            content.Faces[3].Add(new PixelBitmapContent <Color>(2, 2));
            content.Faces[4].Add(new PixelBitmapContent <Color>(2, 2));
            content.Faces[5].Add(new PixelBitmapContent <Color>(2, 2));
            Assert.AreEqual(1, content.Faces[1].Count);
            Assert.AreEqual(1, content.Faces[2].Count);
            Assert.AreEqual(1, content.Faces[3].Count);
            Assert.AreEqual(1, content.Faces[4].Count);
            Assert.AreEqual(1, content.Faces[5].Count);

            Assert.Throws <NotSupportedException>(() => content.Faces.Clear());
            Assert.Throws <NotSupportedException>(() => content.Faces.RemoveAt(0));
            Assert.Throws <NotSupportedException>(() => content.Faces.Add(new MipmapChain()));
            Assert.Throws <NotSupportedException>(() => content.Faces.Insert(0, new MipmapChain()));
            Assert.Throws <NotSupportedException>(() => content.Faces.Remove(content.Faces[0]));
        }
        public override TextureContent Process(TextureContent input, ContentProcessorContext context)
        {
            TextureContent texContent = base.Process(input, context);

            texContent.ConvertBitmapType(typeof(PixelBitmapContent <Color>));

            for (int face = 0; face < texContent.Faces.Count; face++)
            {
                MipmapChain mipChain = texContent.Faces[face];
                for (int mipLevel = 0; mipLevel < mipChain.Count; mipLevel++)
                {
                    PixelBitmapContent <Color> image = (PixelBitmapContent <Color>)input.Faces[face][mipLevel];
                    Color toReplace = new Color(81, 92, 164);
                    image.ReplaceColor(toReplace, Color.Yellow);
                }
            }

            return(texContent);
        }
Ejemplo n.º 6
0
        public override TextureCubeContent Process(TextureCubeContent input, ContentProcessorContext context)
        {
//            System.Diagnostics.Debugger.Launch();
            TextureCubeContent tc = new TextureCubeContent();

            tc.Name     = input.Name;
            tc.Identity = input.Identity;
            int i = 0;

            foreach (var item in input.Faces)
            {
                PixelBitmapContent <Color> bmpInput = (PixelBitmapContent <Color>)item[0];

                // Create Intermediate Content
                Texture2DContent texMipMap = new Texture2DContent();

                // Add decoded Vector4
                texMipMap.Mipmaps.Add(Decode2(bmpInput));

                // Generate Mip Maps
                texMipMap.GenerateMipmaps(true);
                MipmapChain mc = new MipmapChain();
                // Convert each bitmap to Gamma Encoded SurfaceFormat.Color
                for (int mi = 0; mi < texMipMap.Mipmaps.Count; mi++)
                {
                    // Get Mip Map
                    PixelBitmapContent <Rgba1010102> bmpMipMap = (PixelBitmapContent <Rgba1010102>)texMipMap.Mipmaps[mi];
                    if (EncodeAfter)
                    {
                        PixelBitmapContent <Rgba1010102> bmpColor = Encode2(bmpMipMap);
                        mc.Add(bmpColor);
                    }
                    else
                    {
                        mc.Add(bmpMipMap);
                    }
                }

                tc.Faces[i++] = mc;
            }

            return(tc);
        }
Ejemplo n.º 7
0
        public void Texture3DContent()
        {
            var content = new Texture3DContent();

            Assert.NotNull(content.Faces);
            Assert.AreEqual(0, content.Faces.Count);

            var face0 = new MipmapChain(new PixelBitmapContent <Color>(2, 2));

            content.Faces.Add(face0);
            Assert.AreEqual(1, content.Faces.Count);
            Assert.AreEqual(face0, content.Faces[0]);
            Assert.AreEqual(1, content.Faces[0].Count);

            content.Faces[0].Add(new PixelBitmapContent <Color>(1, 1));
            Assert.AreEqual(2, content.Faces[0].Count);

            var face2 = new MipmapChain(new PixelBitmapContent <Color>(2, 2));

            content.Faces.Add(face2);
            Assert.AreEqual(face2, content.Faces[1]);
            Assert.AreEqual(2, content.Faces.Count);

            var face1 = new MipmapChain(new PixelBitmapContent <Color>(2, 2));

            content.Faces.Insert(1, face1);
            Assert.AreEqual(face1, content.Faces[1]);
            Assert.AreEqual(3, content.Faces.Count);

            content.Faces.RemoveAt(0);
            Assert.AreEqual(2, content.Faces.Count);
            Assert.AreEqual(face1, content.Faces[0]);
            Assert.AreEqual(face2, content.Faces[1]);

            content.Faces.Remove(face1);
            Assert.AreEqual(1, content.Faces.Count);
            Assert.AreEqual(face2, content.Faces[0]);

            content.Faces.Clear();
            Assert.AreEqual(0, content.Faces.Count);
        }
Ejemplo n.º 8
0
 protected virtual void NormalizeMipmaps(MipmapChain mmc)
 {
     unchecked
     {
         foreach (PixelBitmapContent <Color> bmc in mmc)
         {
             for (int y = 0, yy = bmc.Height; y != yy; ++y)
             {
                 for (int x = 0, xx = bmc.Width; x != xx; ++x)
                 {
                     Color   c = bmc.GetPixel(x, y);
                     Vector3 v = new Vector3((c.R * 2f) / 255f - 1f,
                                             (c.G * 2f) / 255f - 1f,
                                             (c.B * 2f) / 255f - 1f);
                     v.Normalize();
                     v = v * 0.5f + new Vector3(0.5f, 0.5f, 0.5f);
                     bmc.SetPixel(x, y, new Color(v));
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
        public void Texture3DContent()
        {
            var content = new Texture3DContent();

            Assert.NotNull(content.Faces);
            Assert.AreEqual(0, content.Faces.Count);

            var face0 = new MipmapChain(new PixelBitmapContent<Color>(2, 2));
            content.Faces.Add(face0);
            Assert.AreEqual(1, content.Faces.Count);
            Assert.AreEqual(face0, content.Faces[0]);
            Assert.AreEqual(1, content.Faces[0].Count);

            content.Faces[0].Add(new PixelBitmapContent<Color>(1, 1));
            Assert.AreEqual(2, content.Faces[0].Count);

            var face2 = new MipmapChain(new PixelBitmapContent<Color>(2, 2));
            content.Faces.Add(face2);
            Assert.AreEqual(face2, content.Faces[1]);
            Assert.AreEqual(2, content.Faces.Count);

            var face1 = new MipmapChain(new PixelBitmapContent<Color>(2, 2));
            content.Faces.Insert(1, face1);
            Assert.AreEqual(face1, content.Faces[1]);
            Assert.AreEqual(3, content.Faces.Count);

            content.Faces.RemoveAt(0);
            Assert.AreEqual(2, content.Faces.Count);
            Assert.AreEqual(face1, content.Faces[0]);
            Assert.AreEqual(face2, content.Faces[1]);

            content.Faces.Remove(face1);
            Assert.AreEqual(1, content.Faces.Count);
            Assert.AreEqual(face2, content.Faces[0]);

            content.Faces.Clear();
            Assert.AreEqual(0, content.Faces.Count);
        }
Ejemplo n.º 10
0
        public override Texture2DContent Process(Texture2DContent input, ContentProcessorContext context)
        {
            // Note: In the shader the color is in the w component after sampling.

            input.ConvertBitmapType(typeof(PixelBitmapContent <Vector4>));
            MipmapChain mipmapChain = input.Faces[0];

            foreach (PixelBitmapContent <Vector4> bitmap in mipmapChain)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Vector4 pixel = bitmap.GetPixel(x, y);
                        bitmap.SetPixel(x, y, new Vector4(0, 0, 0, pixel.X));
                    }
                }
            }

            input.ConvertBitmapType(typeof(PixelBitmapContent <Alpha8>));
            input.GenerateMipmaps(false);

            return(input);
        }
        public override TextureContent Process(TextureContent input, ContentProcessorContext context)
        {
            TextureContent texContent = base.Process(input, context);

            texContent.ConvertBitmapType(typeof(PixelBitmapContent <Color>));

            for (int face = 0; face < input.Faces.Count; face++)
            {
                MipmapChain mipChain = input.Faces[face];
                for (int mipLevel = 0; mipLevel < mipChain.Count; mipLevel++)
                {
                    PixelBitmapContent <Color> oldImage  = (PixelBitmapContent <Color>)input.Faces[face][mipLevel];
                    PixelBitmapContent <Color> grayImage = new PixelBitmapContent <Color>(oldImage.Width, oldImage.Height);

                    for (int x = 0; x < oldImage.Width; x++)
                    {
                        for (int y = 0; y < oldImage.Height; y++)
                        {
                            Color oldColor  = oldImage.GetPixel(x, y);
                            float grayValue = oldColor.R * 0.299f / 255.0f;
                            grayValue += oldColor.G * 0.596f / 255.0f;
                            grayValue += oldColor.B * 0.211f / 255.0f;
                            float alpha = oldColor.A / 255.0f;

                            Color grayColor = new Color(grayValue, grayValue, grayValue, alpha);
                            Color newColor  = Color.Lerp(oldColor, grayColor, interpolation);
                            grayImage.SetPixel(x, y, newColor);
                        }
                    }

                    input.Faces[face][mipLevel] = grayImage;
                }
            }

            return(texContent);
        }
Ejemplo n.º 12
0
        internal static TextureContent Import(string filename, ContentImporterContext context)
        {
            var            identity = new ContentIdentity(filename);
            TextureContent output   = null;

            using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
                using (var reader = new BinaryReader(fileStream))
                {
                    // Signature ("DDS ")
                    if (reader.ReadByte() != 0x44 ||
                        reader.ReadByte() != 0x44 ||
                        reader.ReadByte() != 0x53 ||
                        reader.ReadByte() != 0x20)
                    {
                        throw new ContentLoadException("Invalid file signature");
                    }

                    var header = new DdsHeader
                    {
                        // Read DDS_HEADER
                        dwSize = reader.ReadUInt32()
                    };

                    if (header.dwSize != 124)
                    {
                        throw new ContentLoadException("Invalid DDS_HEADER dwSize value");
                    }

                    header.dwFlags             = (Ddsd)reader.ReadUInt32();
                    header.dwHeight            = reader.ReadUInt32();
                    header.dwWidth             = reader.ReadUInt32();
                    header.dwPitchOrLinearSize = reader.ReadUInt32();
                    header.dwDepth             = reader.ReadUInt32();
                    header.dwMipMapCount       = reader.ReadUInt32();
                    // The next 11 DWORDs are reserved and unused
                    for (int i = 0; i < 11; ++i)
                    {
                        reader.ReadUInt32();
                    }

                    // Read DDS_PIXELFORMAT
                    header.ddspf.dwSize = reader.ReadUInt32();
                    if (header.ddspf.dwSize != 32)
                    {
                        throw new ContentLoadException("Invalid DDS_PIXELFORMAT dwSize value");
                    }

                    header.ddspf.dwFlags       = (Ddpf)reader.ReadUInt32();
                    header.ddspf.dwFourCC      = (FourCC)reader.ReadUInt32();
                    header.ddspf.dwRgbBitCount = reader.ReadUInt32();
                    header.ddspf.dwRBitMask    = reader.ReadUInt32();
                    header.ddspf.dwGBitMask    = reader.ReadUInt32();
                    header.ddspf.dwBBitMask    = reader.ReadUInt32();
                    header.ddspf.dwABitMask    = reader.ReadUInt32();

                    // Continue reading DDS_HEADER
                    header.dwCaps  = (DdsCaps)reader.ReadUInt32();
                    header.dwCaps2 = (DdsCaps2)reader.ReadUInt32();

                    reader.ReadUInt32(); // dwCaps3 unused
                    reader.ReadUInt32(); // dwCaps4 unused
                    reader.ReadUInt32(); // dwReserved2 unused

                    // Check for the existence of the DDS_HEADER_DXT10 struct next
                    if (header.ddspf.dwFlags == Ddpf.FourCC && header.ddspf.dwFourCC == FourCC.Dx10)
                    {
                        throw new ContentLoadException("Unsupported DDS_HEADER_DXT10 struct found");
                    }

                    int faceCount   = 1;
                    int mipMapCount = (int)(header.dwCaps.HasFlag(DdsCaps.MipMap) ? header.dwMipMapCount : 1);
                    if (header.dwCaps2.HasFlag(DdsCaps2.Cubemap))
                    {
                        if (!header.dwCaps2.HasFlag(DdsCaps2.CubemapAllFaces))
                        {
                            throw new ContentLoadException("Incomplete cubemap in DDS file");
                        }
                        faceCount = 6;
                        output    = new TextureCubeContent()
                        {
                            Identity = identity
                        };
                    }
                    else
                    {
                        output = new Texture2DContent()
                        {
                            Identity = identity
                        };
                    }

                    var format = GetSurfaceFormat(ref header.ddspf, out bool rbSwap);

                    for (int f = 0; f < faceCount; ++f)
                    {
                        int w       = (int)header.dwWidth;
                        int h       = (int)header.dwHeight;
                        var mipMaps = new MipmapChain();
                        for (int m = 0; m < mipMapCount; ++m)
                        {
                            var content   = CreateBitmapContent(format, w, h);
                            var byteCount = GetBitmapSize(format, w, h);
                            // A 24-bit format is slightly different
                            if (header.ddspf.dwRgbBitCount == 24)
                            {
                                byteCount = 3 * w * h;
                            }

                            var bytes = reader.ReadBytes(byteCount);
                            if (rbSwap)
                            {
                                switch (format)
                                {
                                case SurfaceFormat.Bgr565:
                                    ByteSwapBGR565(bytes);
                                    break;

                                case SurfaceFormat.Bgra4444:
                                    ByteSwapBGRA4444(bytes);
                                    break;

                                case SurfaceFormat.Bgra5551:
                                    ByteSwapBGRA5551(bytes);
                                    break;

                                case SurfaceFormat.Rgba32:
                                    if (header.ddspf.dwRgbBitCount == 32)
                                    {
                                        ByteSwapRGBX(bytes);
                                    }
                                    else if (header.ddspf.dwRgbBitCount == 24)
                                    {
                                        ByteSwapRGB(bytes);
                                    }
                                    break;
                                }
                            }
                            if ((format == SurfaceFormat.Rgba32) &&
                                header.ddspf.dwFlags.HasFlag(Ddpf.Rgb) &&
                                !header.ddspf.dwFlags.HasFlag(Ddpf.AlphaPixels))
                            {
                                // Fill or add alpha with opaque
                                if (header.ddspf.dwRgbBitCount == 32)
                                {
                                    ByteFillAlpha(bytes);
                                }
                                else if (header.ddspf.dwRgbBitCount == 24)
                                {
                                    ByteExpandAlpha(ref bytes);
                                }
                            }
                            content.SetPixelData(bytes);
                            mipMaps.Add(content);
                            w = Math.Max(1, w / 2);
                            h = Math.Max(1, h / 2);
                        }
                        output.Faces[f] = mipMaps;
                    }
                }

            return(output);
        }
Ejemplo n.º 13
0
        static internal TextureContent Import(string filename, ContentImporterContext context)
        {
            var            identity = new ContentIdentity(filename);
            TextureContent output   = null;

            using (var reader = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read)))
            {
                // Read signature ("DDS ")
                var valid = reader.ReadByte() == 0x44;
                valid = valid && reader.ReadByte() == 0x44;
                valid = valid && reader.ReadByte() == 0x53;
                valid = valid && reader.ReadByte() == 0x20;
                if (!valid)
                {
                    throw new ContentLoadException("Invalid file signature");
                }

                var header = new DdsHeader();

                // Read DDS_HEADER
                header.dwSize = reader.ReadUInt32();
                if (header.dwSize != 124)
                {
                    throw new ContentLoadException("Invalid DDS_HEADER dwSize value");
                }
                header.dwFlags             = (Ddsd)reader.ReadUInt32();
                header.dwHeight            = reader.ReadUInt32();
                header.dwWidth             = reader.ReadUInt32();
                header.dwPitchOrLinearSize = reader.ReadUInt32();
                header.dwDepth             = reader.ReadUInt32();
                header.dwMipMapCount       = reader.ReadUInt32();
                // The next 11 DWORDs are reserved and unused
                for (int i = 0; i < 11; ++i)
                {
                    reader.ReadUInt32();
                }
                // Read DDS_PIXELFORMAT
                header.ddspf.dwSize = reader.ReadUInt32();
                if (header.ddspf.dwSize != 32)
                {
                    throw new ContentLoadException("Invalid DDS_PIXELFORMAT dwSize value");
                }
                header.ddspf.dwFlags       = (Ddpf)reader.ReadUInt32();
                header.ddspf.dwFourCC      = (FourCC)reader.ReadUInt32();
                header.ddspf.dwRgbBitCount = reader.ReadUInt32();
                header.ddspf.dwRBitMask    = reader.ReadUInt32();
                header.ddspf.dwGBitMask    = reader.ReadUInt32();
                header.ddspf.dwBBitMask    = reader.ReadUInt32();
                header.ddspf.dwABitMask    = reader.ReadUInt32();
                // Continue reading DDS_HEADER
                header.dwCaps  = (DdsCaps)reader.ReadUInt32();
                header.dwCaps2 = (DdsCaps2)reader.ReadUInt32();
                // dwCaps3 unused
                reader.ReadUInt32();
                // dwCaps4 unused
                reader.ReadUInt32();
                // dwReserved2 unused
                reader.ReadUInt32();

                // Check for the existence of the DDS_HEADER_DXT10 struct next
                if (header.ddspf.dwFlags == Ddpf.FourCC && header.ddspf.dwFourCC == FourCC.Dx10)
                {
                    throw new ContentLoadException("Unsupported DDS_HEADER_DXT10 struct found");
                }

                int faceCount   = 1;
                int mipMapCount = (int)(header.dwCaps.HasFlag(DdsCaps.MipMap) ? header.dwMipMapCount : 1);
                if (header.dwCaps.HasFlag(DdsCaps.Complex))
                {
                    if (header.dwCaps2.HasFlag(DdsCaps2.Cubemap))
                    {
                        if (!header.dwCaps2.HasFlag(DdsCaps2.CubemapAllFaces))
                        {
                            throw new ContentLoadException("Incomplete cubemap in DDS file");
                        }
                        faceCount = 6;
                        output    = new TextureCubeContent()
                        {
                            Identity = identity
                        };
                    }
                    else
                    {
                        output = new Texture2DContent()
                        {
                            Identity = identity
                        };
                    }
                }
                else
                {
                    output = new Texture2DContent()
                    {
                        Identity = identity
                    };
                }

                bool rbSwap;
                var  format = GetSurfaceFormat(ref header.ddspf, out rbSwap);

                for (int f = 0; f < faceCount; ++f)
                {
                    var w       = (int)header.dwWidth;
                    var h       = (int)header.dwHeight;
                    var mipMaps = new MipmapChain();
                    for (int m = 0; m < mipMapCount; ++m)
                    {
                        var content   = CreateBitmapContent(format, w, h);
                        var byteCount = GetBitmapSize(format, w, h);
                        var bytes     = reader.ReadBytes(byteCount);
                        content.SetPixelData(bytes);
                        mipMaps.Add(content);
                        w = MathHelper.Max(1, w / 2);
                        h = MathHelper.Max(1, h / 2);
                    }
                    output.Faces[f] = mipMaps;
                }
            }

            return(output);
        }
Ejemplo n.º 14
0
        static internal TextureContent Import(string filename, ContentImporterContext context)
        {
            var identity = new ContentIdentity(filename);
            TextureContent output = null;

            using (var reader = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read)))
            {
                // Read signature ("DDS ")
                var valid = reader.ReadByte() == 0x44;
                valid = valid && reader.ReadByte() == 0x44;
                valid = valid && reader.ReadByte() == 0x53;
                valid = valid && reader.ReadByte() == 0x20;
                if (!valid)
                    throw new ContentLoadException("Invalid file signature");

                var header = new DdsHeader();

                // Read DDS_HEADER
                header.dwSize = reader.ReadUInt32();
                if (header.dwSize != 124)
                    throw new ContentLoadException("Invalid DDS_HEADER dwSize value");
                header.dwFlags = (Ddsd)reader.ReadUInt32();
                header.dwHeight = reader.ReadUInt32();
                header.dwWidth = reader.ReadUInt32();
                header.dwPitchOrLinearSize = reader.ReadUInt32();
                header.dwDepth = reader.ReadUInt32();
                header.dwMipMapCount = reader.ReadUInt32();
                // The next 11 DWORDs are reserved and unused
                for (int i = 0; i < 11; ++i)
                    reader.ReadUInt32();
                // Read DDS_PIXELFORMAT
                header.ddspf.dwSize = reader.ReadUInt32();
                if (header.ddspf.dwSize != 32)
                    throw new ContentLoadException("Invalid DDS_PIXELFORMAT dwSize value");
                header.ddspf.dwFlags = (Ddpf)reader.ReadUInt32();
                header.ddspf.dwFourCC = (FourCC)reader.ReadUInt32();
                header.ddspf.dwRgbBitCount = reader.ReadUInt32();
                header.ddspf.dwRBitMask = reader.ReadUInt32();
                header.ddspf.dwGBitMask = reader.ReadUInt32();
                header.ddspf.dwBBitMask = reader.ReadUInt32();
                header.ddspf.dwABitMask = reader.ReadUInt32();
                // Continue reading DDS_HEADER
                header.dwCaps = (DdsCaps)reader.ReadUInt32();
                header.dwCaps2 = (DdsCaps2)reader.ReadUInt32();
                // dwCaps3 unused
                reader.ReadUInt32();
                // dwCaps4 unused
                reader.ReadUInt32();
                // dwReserved2 unused
                reader.ReadUInt32();

                // Check for the existence of the DDS_HEADER_DXT10 struct next
                if (header.ddspf.dwFlags == Ddpf.FourCC && header.ddspf.dwFourCC == FourCC.Dx10)
                {
                    throw new ContentLoadException("Unsupported DDS_HEADER_DXT10 struct found");
                }

                int faceCount = 1;
                int mipMapCount = (int)(header.dwCaps.HasFlag(DdsCaps.MipMap) ? header.dwMipMapCount : 1);
                if (header.dwCaps.HasFlag(DdsCaps.Complex))
                {
                    if (header.dwCaps2.HasFlag(DdsCaps2.Cubemap))
                    {
                        if (!header.dwCaps2.HasFlag(DdsCaps2.CubemapAllFaces))
                            throw new ContentLoadException("Incomplete cubemap in DDS file");
                        faceCount = 6;
                        output = new TextureCubeContent() { Identity = identity };
                    }
                    else
                    {
                        output = new Texture2DContent() { Identity = identity };
                    }
                }
                else
                {
                    output = new Texture2DContent() { Identity = identity };
                }

                bool rbSwap;
                var format = GetSurfaceFormat(ref header.ddspf, out rbSwap);

                for (int f = 0; f < faceCount; ++f)
                {
                    var w = (int)header.dwWidth;
                    var h = (int)header.dwHeight;
                    var mipMaps = new MipmapChain();
                    for (int m = 0; m < mipMapCount; ++m)
                    {
                        var content = CreateBitmapContent(format, w, h);
                        var byteCount = GetBitmapSize(format, w, h);
                        var bytes = reader.ReadBytes(byteCount);
                        content.SetPixelData(bytes);
                        mipMaps.Add(content);
                        w = MathHelper.Max(1, w / 2);
                        h = MathHelper.Max(1, h / 2);
                    }
                    output.Faces[f] = mipMaps;
                }
            }

            return output;
        }
Ejemplo n.º 15
0
        public void TextureCubeContent()
        {
            var content = new TextureCubeContent();

            Assert.NotNull(content.Faces);
            Assert.AreEqual(6, content.Faces.Count);
            Assert.NotNull(content.Faces[0]);
            Assert.NotNull(content.Faces[1]);
            Assert.NotNull(content.Faces[2]);
            Assert.NotNull(content.Faces[3]);
            Assert.NotNull(content.Faces[4]);
            Assert.NotNull(content.Faces[5]);
            Assert.AreEqual(0, content.Faces[0].Count);
            Assert.AreEqual(0, content.Faces[1].Count);
            Assert.AreEqual(0, content.Faces[2].Count);
            Assert.AreEqual(0, content.Faces[3].Count);
            Assert.AreEqual(0, content.Faces[4].Count);
            Assert.AreEqual(0, content.Faces[5].Count);

            var face0 = new MipmapChain(new PixelBitmapContent<Color>(2, 2));
            content.Faces[0] = face0;
            Assert.AreEqual(face0, content.Faces[0]);
            Assert.AreEqual(1, content.Faces[0].Count);

            content.Faces[0].Add(new PixelBitmapContent<Color>(1, 1));
            Assert.AreEqual(2, content.Faces[0].Count);

            content.Faces[1].Add(new PixelBitmapContent<Color>(2, 2));
            content.Faces[2].Add(new PixelBitmapContent<Color>(2, 2));
            content.Faces[3].Add(new PixelBitmapContent<Color>(2, 2));
            content.Faces[4].Add(new PixelBitmapContent<Color>(2, 2));
            content.Faces[5].Add(new PixelBitmapContent<Color>(2, 2));
            Assert.AreEqual(1, content.Faces[1].Count);
            Assert.AreEqual(1, content.Faces[2].Count);
            Assert.AreEqual(1, content.Faces[3].Count);
            Assert.AreEqual(1, content.Faces[4].Count);
            Assert.AreEqual(1, content.Faces[5].Count);

            Assert.Throws<NotSupportedException>(() => content.Faces.Clear());
            Assert.Throws<NotSupportedException>(() => content.Faces.RemoveAt(0));
            Assert.Throws<NotSupportedException>(() => content.Faces.Add(new MipmapChain()));
            Assert.Throws<NotSupportedException>(() => content.Faces.Insert(0, new MipmapChain()));
            Assert.Throws<NotSupportedException>(() => content.Faces.Remove(content.Faces[0]));
        }
Ejemplo n.º 16
0
        public override TextureContent Process(TextureContent input, ContentProcessorContext context)
        {
            //System.Diagnostics.Debugger.Launch();

            input.Validate();

            try
            {
                using (CompressorOptionsBundle compressor = new CompressorOptionsBundle())
                {
                    compressor.InputOptions.ResetTextureLayout();

                    TextureType textureType = FindTextureType(input);

                    /*
                     * Set options
                     */

                    compressor.InputOptions.SetTextureLayout(textureType, input.Faces[0][0].Width, input.Faces[0][0].Height, 1);
                    compressor.InputOptions.SetFormat(InputFormat.BGRA_8UB);
                    compressor.InputOptions.SetAlphaMode(AlphaMode);
                    compressor.InputOptions.SetMipmapFilter(MipMapFilter);
                    compressor.InputOptions.SetMipmapGeneration(GenerateMipmaps, MaxMipMapLevel);
                    compressor.InputOptions.SetRoundMode(TextureRoundingMode);
                    compressor.InputOptions.SetWrapMode(WrapMode);

                    compressor.InputOptions.SetGamma(InputGamma, OutputGamma);
                    compressor.InputOptions.SetNormalizeMipmaps(false);
                    compressor.InputOptions.SetNormalMap(false);
                    compressor.InputOptions.SetConvertToNormalMap(false);

                    compressor.CompressionOptions.SetQuality(Quality);

                    GeneralOutputHandler outputHandler;

                    switch (TextureFormat)
                    {
                    case TextureOutputFormat.Colour:
                        compressor.CompressionOptions.SetFormat(Format.RGBA);
                        outputHandler = new PixelOutputHandler <Color>(textureType);
                        break;

                    case TextureOutputFormat.Normals:
                        compressor.CompressionOptions.SetFormat(Format.RGBA);
                        outputHandler = new PixelOutputHandler <Color>(textureType);

                        compressor.InputOptions.SetNormalizeMipmaps(true);
                        compressor.InputOptions.SetNormalMap(true);
                        compressor.InputOptions.SetConvertToNormalMap(ConvertToNormalMap);
                        compressor.InputOptions.SetGamma(1.0f, 1.0f);
                        break;

                    case TextureOutputFormat.DXT1:
                        compressor.CompressionOptions.SetFormat(Format.DXT1);
                        outputHandler = new Dxt1OutputHandler(textureType);
                        break;

                    case TextureOutputFormat.DXT1a:
                        compressor.CompressionOptions.SetFormat(Format.DXT1a);
                        outputHandler = new Dxt1OutputHandler(textureType);
                        break;

                    case TextureOutputFormat.DXT3:
                        compressor.CompressionOptions.SetFormat(Format.DXT3);
                        outputHandler = new Dxt3OutputHandler(textureType);
                        break;

                    case TextureOutputFormat.DXT5:
                        compressor.CompressionOptions.SetFormat(Format.DXT5);
                        outputHandler = new Dxt5OutputHandler(textureType);
                        break;

                    case TextureOutputFormat.DXT5n:
                        //FIXME: We force fastest quality since the normal compression mode is _very_ slow.
                        compressor.CompressionOptions.SetQuality(Quality.Fastest);

                        compressor.CompressionOptions.SetFormat(Format.DXT5n);

                        compressor.InputOptions.SetNormalizeMipmaps(true);
                        compressor.InputOptions.SetNormalMap(true);
                        compressor.InputOptions.SetConvertToNormalMap(ConvertToNormalMap);
                        compressor.InputOptions.SetGamma(1.0f, 1.0f);

                        outputHandler = new Dxt5OutputHandler(textureType);
                        break;

                    default:
                        throw new NotSupportedException("Unknown texture output format: " + TextureFormat);
                    }

                    /*
                     * Set input data
                     */

                    //TODO: Use a float format when texture tools support it.
                    input.ConvertBitmapType(typeof(PixelBitmapContent <Color>));

                    for (int i = 0; i < input.Faces.Count; i++)
                    {
                        MipmapChain mipChain = input.Faces[i];

                        for (int j = 0; j < mipChain.Count; j++)
                        {
                            BitmapContent bitmap = mipChain[j];

                            byte[] bitmapData = bitmap.GetPixelData();

                            //FIXME: When we move to XNA 4 the layout of Color will change, hence we need to swizzle the input.
                            compressor.InputOptions.SetMipmapData(bitmapData, bitmap.Width, bitmap.Height, 1, i, j);
                        }
                    }

                    /*
                     * Setup output
                     */


                    compressor.OutputOptions.SetOutputHandler(outputHandler);
                    compressor.OutputOptions.SetOutputHeader(false);

                    /*
                     * Go!
                     */
                    compressor.Compressor.SetEnableCuda(EnableCuda);

                    compressor.Compress();

                    /*
                     * Check the output makes sense.
                     */

                    outputHandler.OutputTextureContent.Validate();

                    return(outputHandler.OutputTextureContent);
                }
            }
            catch (TextureToolsException ttexcept)
            {
                throw ConvertException(ttexcept);
            }
        }
Ejemplo n.º 17
0
 public Texture2DContent(MipmapChain mipmaps)
     : base(new MipmapChainCollection(1))
 {
     base.Faces[0] = mipmaps;
 }