Beispiel #1
0
        void CompressDefault <T>(TargetPlatform platform, Color color, int width = 16, int height = 16)
        {
            var context = new TestProcessorContext(platform, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyEnabled    = false,
                GenerateMipmaps    = true,
                PremultiplyAlpha   = false,
                ResizeToPowerOfTwo = false,
                TextureFormat      = TextureProcessorOutputFormat.Compressed
            };

            var face = new PixelBitmapContent <Color>(width, height);

            Fill(face, color);
            var input = new Texture2DContent();

            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count, "Expected number of faces");
            Assert.AreEqual(5, output.Faces[0].Count, "Expected number of mipmaps");

            Assert.IsAssignableFrom <T>(output.Faces[0][0], "Incorrect pixel format");
        }
Beispiel #2
0
        void CompressDefault <T>(TargetPlatform platform, Color color)
        {
            var context = new TestProcessorContext(platform, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyEnabled    = false,
                GenerateMipmaps    = false,
                PremultiplyAlpha   = false,
                ResizeToPowerOfTwo = false,
                TextureFormat      = TextureProcessorOutputFormat.Compressed
            };

            var face = new PixelBitmapContent <Color>(16, 16);

            Fill(face, color);
            var input = new Texture2DContent();

            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count);
            Assert.AreEqual(1, output.Faces[0].Count);

            Assert.IsAssignableFrom <T>(output.Faces[0][0]);
        }
        public void ColorKey()
        {
            var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyColor = Color.Red,
                ColorKeyEnabled = true,
                GenerateMipmaps = false,
                PremultiplyAlpha = false,
                ResizeToPowerOfTwo = false,
                TextureFormat = TextureProcessorOutputFormat.Color
            };

            var face = new PixelBitmapContent<Color>(8, 8);
            Fill(face, Color.Red);
            var input = new Texture2DContent();
            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count);
            Assert.AreEqual(1, output.Faces[0].Count);

            Assert.IsAssignableFrom<PixelBitmapContent<Color>>(output.Faces[0][0]);
            var outFace = (PixelBitmapContent<Color>)output.Faces[0][0];
            Assert.AreEqual(8, outFace.Width);
            Assert.AreEqual(8, outFace.Height);

            for (var y=0; y < outFace.Height; y++)
                for (var x = 0; x < outFace.Width; x++)
                    Assert.AreEqual(Color.Transparent, outFace.GetPixel(x, y));
        }
Beispiel #4
0
        public override ParticleDesignerProcessorResult Process(ParticleDesignerContent input, ContentProcessorContext context)
        {
            logger = context.Logger;
            var result = new ParticleDesignerProcessorResult();

            // check for an embedded tiff texture
            if (input.emitterConfig.texture.data != null)
            {
                context.Logger.LogMessage("pex file has an embedded tiff. Extracting now.");
                using (var memoryStream = new MemoryStream(Convert.FromBase64String(input.emitterConfig.texture.data), writable: false))
                {
                    using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                    {
                        const int size   = 4096;
                        byte[]    buffer = new byte[size];
                        using (var memory = new MemoryStream())
                        {
                            int count = 0;
                            do
                            {
                                count = stream.Read(buffer, 0, size);
                                if (count > 0)
                                {
                                    memory.Write(buffer, 0, count);
                                }
                            } while(count > 0);

                            result.textureTiffData = memory.ToArray();
                        }
                    }
                }

                var tempFile = Path.Combine(Path.GetTempPath(), "tempParticleTexture.tif");
                File.WriteAllBytes(tempFile, result.textureTiffData);
                context.Logger.LogMessage("writing tiff to temp file: {0}", tempFile);

                context.Logger.LogMessage("running TextureImportor on tiff");
                var textureImporter = new TextureImporter();
                result.texture      = textureImporter.Import(tempFile, input.context) as Texture2DContent;
                result.texture.Name = input.emitterConfig.texture.name;

                context.Logger.LogMessage("deleting temp file");
                File.Delete(tempFile);

                // process
                context.Logger.LogMessage("processing TextureContent");
                var textureProcessor = new TextureProcessor {
                    GenerateMipmaps = false,
                    TextureFormat   = TextureProcessorOutputFormat.Color
                };
                result.texture = (Texture2DContent)textureProcessor.Process(result.texture, context);
                context.Logger.LogMessage("TextureContent processed");
            }

            result.particleEmitterConfig = input.emitterConfig;

            return(result);
        }
Beispiel #5
0
        public void MipmapNonSquareNonPowerOfTwo()
        {
            var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyEnabled    = false,
                GenerateMipmaps    = true,
                PremultiplyAlpha   = false,
                ResizeToPowerOfTwo = false,
                TextureFormat      = TextureProcessorOutputFormat.Color
            };

            var width  = 23;
            var height = 5;

            var face = new PixelBitmapContent <Color>(width, height);

            Fill(face, Color.Red);
            var input = new Texture2DContent();

            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count);

            var outChain = output.Faces[0];

            Assert.AreEqual(5, outChain.Count);

            foreach (var outFace in outChain)
            {
                Assert.AreEqual(width, outFace.Width);
                Assert.AreEqual(height, outFace.Height);

                var bitmap = (PixelBitmapContent <Color>)outFace;
                for (var y = 0; y < height; y++)
                {
                    for (var x = 0; x < width; x++)
                    {
                        Assert.AreEqual(Color.Red, bitmap.GetPixel(x, y));
                    }
                }

                if (width > 1)
                {
                    width /= 2;
                }
                if (height > 1)
                {
                    height /= 2;
                }
            }
        }
Beispiel #6
0
        public void ColorKey()
        {
            var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyColor      = Color.Red,
                ColorKeyEnabled    = true,
                GenerateMipmaps    = false,
                PremultiplyAlpha   = false,
                ResizeToPowerOfTwo = false,
                TextureFormat      = TextureProcessorOutputFormat.Color
            };

            var face = new PixelBitmapContent <Color>(8, 8);

            Fill(face, Color.Red);
            var input = new Texture2DContent();

            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count);
            Assert.AreEqual(1, output.Faces[0].Count);

            Assert.IsAssignableFrom <PixelBitmapContent <Color> >(output.Faces[0][0]);
            var outFace = (PixelBitmapContent <Color>)output.Faces[0][0];

            Assert.AreEqual(8, outFace.Width);
            Assert.AreEqual(8, outFace.Height);

            for (var y = 0; y < outFace.Height; y++)
            {
                for (var x = 0; x < outFace.Width; x++)
                {
                    Assert.AreEqual(Color.Transparent, outFace.GetPixel(x, y));
                }
            }
        }
Beispiel #7
0
        private TextureAtlasContent ProcessSingleTexture(Texture2DContent input, ContentProcessorContext context, TextureProcessor processor)
        {
            Texture2DContent texture = (Texture2DContent)processor.Process(input, context);

            TextureAtlasContent result = new TextureAtlasContent();

            result.Textures.Add(texture);

            int width  = input.Mipmaps[0].Width;
            int height = input.Mipmaps[0].Height;

            for (int y = 0; y < RowCount; ++y)
            {
                for (int x = 0; x < ColumnCount; ++x)
                {
                    result.SpriteTextures.Add(0);
                    result.SpriteRectangles.Add(new Rectangle(
                                                    x * width / ColumnCount, y * height / RowCount,
                                                    width / ColumnCount, height / RowCount));
                }
            }

            return(result);
        }
        public override ParticleDesignerProcessorResult Process(ParticleDesignerContent input, ContentProcessorContext context)
        {
            logger = context.Logger;
            var result = new ParticleDesignerProcessorResult();

            // check for an embedded tiff texture
            if (input.emitterConfig.texture.data != null)
            {
                context.Logger.LogMessage("pex file has an embedded tiff. Extracting now.");
                using (var memoryStream = new MemoryStream(Convert.FromBase64String(input.emitterConfig.texture.data), writable: false))
                {
                    using (var stream = new GZipStream(memoryStream, CompressionMode.Decompress))
                    {
                        const int size   = 4096;
                        byte[]    buffer = new byte[size];
                        using (var memory = new MemoryStream())
                        {
                            int count = 0;
                            do
                            {
                                count = stream.Read(buffer, 0, size);
                                if (count > 0)
                                {
                                    memory.Write(buffer, 0, count);
                                }
                            } while(count > 0);

                            result.textureTiffData = memory.ToArray();
                        }
                    }
                }

                var tempFile = Path.Combine(Path.GetTempPath(), "tempParticleTexture.tif");
                File.WriteAllBytes(tempFile, result.textureTiffData);
                context.Logger.LogMessage("writing tiff to temp file: {0}", tempFile);

                context.Logger.LogMessage("running TextureImportor on tiff");
                var textureImporter = new TextureImporter();
                result.texture      = textureImporter.Import(tempFile, input.context) as Texture2DContent;
                result.texture.Name = input.emitterConfig.texture.name;

                context.Logger.LogMessage("deleting temp file");
                File.Delete(tempFile);

                // process
                context.Logger.LogMessage("processing TextureContent");
                var textureProcessor = new TextureProcessor
                {
                    GenerateMipmaps = false,
                    TextureFormat   = TextureProcessorOutputFormat.Color
                };
                result.texture = (Texture2DContent)textureProcessor.Process(result.texture, context);
                context.Logger.LogMessage("TextureContent processed");
            }
            else             // no tiff data, so let's try loading the texture with the texture name, from the same directory as the particle file
            {
                string fileDirectory = Path.GetDirectoryName(input.path);
                string fullPath      = Path.Combine(fileDirectory, input.emitterConfig.texture.name);
                context.Logger.LogMessage("Looking for texture file at {0}", fullPath);
                result.texture = context.BuildAndLoadAsset <string, Texture2DContent>(new ExternalReference <string>(fullPath), "TextureProcessor");
                context.Logger.LogMessage("Texture file loaded.");
            }

            result.particleEmitterConfig = input.emitterConfig;
            context.Logger.LogMessage("Emitter configuration loaded.");

            return(result);
        }
Beispiel #9
0
        public override TextureAtlasContent Process(Texture2DContent input, ContentProcessorContext context)
        {
            TextureProcessor processor = new TextureProcessor();

            processor.ColorKeyColor      = ColorKey;
            processor.ColorKeyEnabled    = ColorKeyEnabled;
            processor.GenerateMipmaps    = GenerateMipmaps;
            processor.PremultiplyAlpha   = PremultiplyAlpha;
            processor.ResizeToPowerOfTwo = ResizeToPowerOfTwo;
            processor.TextureFormat      = TextureFormat;


            TextureAtlasContent  result        = new TextureAtlasContent();
            List <BitmapContent> sourceSprites = new List <BitmapContent>();

            string[] inputFiles = Import(input.Identity.SourceFilename);

            if (inputFiles.Length <= 0)
            {
                throw new InvalidOperationException();
            }

            if (inputFiles.Length == 1)
            {
                return(ProcessSingleTexture(input, context, processor));
            }

            // Loop over each input sprite filename.
            foreach (string inputFilename in inputFiles)
            {
                // Store the name of this sprite.
                string spriteName = Path.GetFileNameWithoutExtension(inputFilename);

                result.SpriteNames.Add(spriteName, sourceSprites.Count);

                // Load the sprite texture into memory.
                ExternalReference <Texture2DContent> textureReference =
                    new ExternalReference <Texture2DContent>(inputFilename);

                Texture2DContent texture =
                    context.BuildAndLoadAsset <Texture2DContent,
                                               Texture2DContent>(textureReference, null);

                if (Pack)
                {
                    result.SpriteTextures.Add(0);
                    sourceSprites.Add(texture.Faces[0][0]);
                }
                else
                {
                    texture = (Texture2DContent)processor.Process(texture, context);

                    result.SpriteTextures.Add(result.Textures.Count);
                    result.Textures.Add(texture);
                    result.SpriteRectangles.Add(new Rectangle(0, 0, texture.Mipmaps[0].Width,
                                                              texture.Mipmaps[0].Height));
                }
            }

            // Pack all the sprites into a single large texture.
            if (Pack)
            {
                BitmapContent packedSprites = SpritePacker.PackSprites(sourceSprites,
                                                                       result.SpriteRectangles, context);

                Texture2DContent content = new Texture2DContent();

                content.Mipmaps.Add(packedSprites);

                content = (Texture2DContent)processor.Process(content, context);

                result.Textures.Add(content);
            }

            return(result);
        }
        public void Mipmap()
        {
            var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");

            var processor = new TextureProcessor
            {
                ColorKeyEnabled = false,
                GenerateMipmaps = true,
                PremultiplyAlpha = false,
                ResizeToPowerOfTwo = false,
                TextureFormat = TextureProcessorOutputFormat.Color
            };

            var face = new PixelBitmapContent<Color>(8, 8);
            Fill(face, Color.Red);
            var input = new Texture2DContent();
            input.Faces[0] = face;

            var output = processor.Process(input, context);

            Assert.NotNull(output);
            Assert.AreEqual(1, output.Faces.Count);
            //Assert.AreNotEqual(face, output.Faces[0][0]);

            var outChain = output.Faces[0];
            Assert.AreEqual(4, outChain.Count);

            var width = 8;
            var height = 8;

            foreach (var outFace in outChain)
            {
                Assert.AreEqual(width, outFace.Width);
                Assert.AreEqual(height, outFace.Height);

                var bitmap = (PixelBitmapContent<Color>)outFace;
                for (var y = 0; y < height; y++)
                    for (var x = 0; x < width; x++)
                        Assert.AreEqual(Color.Red, bitmap.GetPixel(x, y));

                width = width >> 1;
                height = height >> 1;
            }
        }