public void TestTooLargeRectangle() {
      ArevaloRectanglePacker packer = new ArevaloRectanglePacker(128, 128);
      Point placement;

      bool result = packer.TryPack(129, 10, out placement);
      Assert.IsFalse(result);

      result = packer.TryPack(10, 129, out placement);
      Assert.IsFalse(result);
    }
Example #2
0
        private bool TestPackingImages(int testWidth, int testHeight, RectangleF[] testImagePlacement,
                                       out int packedWidth, out int packedHeight)
        {
            packedWidth = packedHeight = 0;
            // create the rectangle packer
            var rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            foreach (var image in ItemArray)
            {
                // get the bitmap for this file
                var size = ImageSizes[image.Key];

                // pack the image
                if (!rectanglePacker.TryPack((int)Math.Ceiling(size.Width + padding), (int)Math.Ceiling(size.Height + padding), out Point origin))
                {
                    return(false);
                }

                // add the destination rectangle to our dictionary
                testImagePlacement[image.Key] = new RectangleF(origin.X, origin.Y, size.Width, size.Height);
            }
            packedWidth  = rectanglePacker.ActualPackingAreaWidth;
            packedHeight = rectanglePacker.ActualPackingAreaHeight;
            return(true);
        }
Example #3
0
        private bool TestPackingRectangles(int testWidth, int testHeight, IDictionary <string, Rectangle> placementStorage)
        {
            RectanglePacker rectanglePacker;
            bool            result;

            result = true;

            rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            // reset the results
            placementStorage.Clear();

            foreach (string image in _files)
            {
                Size  size;
                Point origin;

                // get the bitmap for this file
                size = _imageSizes[image];

                // pack the image
                result = rectanglePacker.TryPack(size.Width, size.Height, out origin);

                if (result)
                {
                    placementStorage.Add(image, new Rectangle(origin.X, origin.Y, size.Width, size.Height));
                }
                else
                {
                    break;
                }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Determines whether the atlas' images can fit on a texture of the specified size.
        /// </summary>
        private static Boolean TestPackingImages(TextureAtlasDescription atlasDesc, IEnumerable <TextureAtlasImage> atlasImages,
                                                 Int32 testWidth, Int32 testHeight, Dictionary <String, Rectangle> testImagePlacement)
        {
            var rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            foreach (var image in atlasImages)
            {
                var size    = image.Size;
                var origin  = Vector2.Zero;
                var padding = atlasDesc.Metadata.Padding;
                if (!rectanglePacker.TryPack(size.Width + padding, size.Height + padding, out origin))
                {
                    return(false);
                }
                testImagePlacement.Add(image.Name, new Rectangle((int)origin.X, (int)origin.Y,
                                                                 size.Width + padding, size.Height + padding));
            }
            return(true);
        }
Example #5
0
        private bool TestPackingImages(int testWidth, int testHeight, Dictionary <string, Rectangle> testImagePlacement)
        {
            // create the rectangle packer
            ArevaloRectanglePacker rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            foreach (var image in files)
            {
                // get the bitmap for this file
                Size size = imageSizes[image.Name];

                // pack the image
                if (!rectanglePacker.TryPack(size.Width + padding, size.Height + padding, out var origin))
                {
                    return(false);
                }

                // add the destination rectangle to our dictionary
                testImagePlacement.Add(image.Name, new Rectangle(origin.X, origin.Y, size.Width + padding, size.Height + padding));
            }

            return(true);
        }
Example #6
0
        private bool TestPackingImages(int testWidth, int testHeight, Dictionary<string, Microsoft.Xna.Framework.Rectangle> testImagePlacement)
        {
            // create the rectangle packer
            ArevaloRectanglePacker rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            foreach (var image in files)
            {
                // get the bitmap for this file
                Size size = imageSizes[System.IO.Path.GetFileNameWithoutExtension(image)];

                // pack the image
                Point origin;
                if (!rectanglePacker.TryPack(size.Width + padding, size.Height + padding, out origin))
                {
                    return false;
                }

                // add the destination rectangle to our dictionary
                testImagePlacement.Add(System.IO.Path.GetFileNameWithoutExtension(image), new Microsoft.Xna.Framework.Rectangle(origin.X, origin.Y, size.Width + padding, size.Height + padding));
            }

            return true;
        }
Example #7
0
        private bool TestPackingImages(int testWidth, int testHeight, Lexicon<string, Rectangle> testImagePlacement)
        {
            // create the rectangle packer
            ArevaloRectanglePacker rectanglePacker = new ArevaloRectanglePacker(testWidth, testHeight);

            foreach (var image in files)
            {
                // get the bitmap for this file
                Size size = imageSizes[image.Key][0];

                // pack the image
                Point origin;
                if (!rectanglePacker.TryPack(size.Width + padding, size.Height + padding, out origin))
                {
                    return false;
                }

                // add the destination rectangle to our dictionary
                testImagePlacement.Add(image.Key, new Rectangle(origin.X, origin.Y, size.Width + padding, size.Height + padding));
            }

            return true;
        }
Example #8
0
        //贴图装箱算法
        public static IList<TexturePacket> PacketTexture(GraphicsDevice gdevice,IDictionary<string,Texture2D> sources)
        {
            List<TexturePacket> list = new List<TexturePacket>();

            int packetsize = 512;
            RectanglePacker packer = new ArevaloRectanglePacker(packetsize, packetsize);
            Texture2D tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color);
            Dictionary<string, RectangleF> rects = new Dictionary<string, RectangleF>();
            foreach (var i in sources)
            {
               UInt32[] data=new UInt32[i.Value.Width*i.Value.Height];
               i.Value.GetData<UInt32>(data);
               int twidth = i.Value.Width;
               int theight = i.Value.Height;
                bool bfixx=false;
                bool bfixy = false;
                if((twidth== packetsize/2||twidth==packetsize/4))
                {
                }
                else
                {
                    bfixx=true;
                    twidth+=2;
                }
                if (theight==packetsize/2||theight==packetsize/4)
                {

                }
                else
                {
                    theight += 2;
                    bfixy = true;
                }
                Point pos;

                if (packer.TryPack(twidth, theight, out pos))
                {
                    //right
                }
                else
                {
                    TexturePacket p = new TexturePacket(tex, rects);
                    list.Add(p);
                    packer = new ArevaloRectanglePacker(packetsize, packetsize);
                    tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color);
                    rects = new Dictionary<string, RectangleF>();

                    packer.TryPack(i.Value.Width, i.Value.Height, out pos);
                }
                if (bfixx) pos.X += 1;
                if(bfixy)    pos.Y+=1;

                tex.SetData<UInt32>(0, new Rectangle(pos.X, pos.Y, i.Value.Width, i.Value.Height), data, 0, data.Length);
                rects.Add(i.Key, new RectangleF(
                    (float)(pos.X) / (float)packer.PackingAreaWidth,
                    (float)(pos.Y) / (float)packer.PackingAreaHeight,
                    (float)i.Value.Width / (float)packer.PackingAreaWidth,
                    (float)i.Value.Height / (float)packer.PackingAreaHeight
                    ));
            }
            {
                TexturePacket p = new TexturePacket(tex, rects);
                list.Add(p);
            }
            return list;
        }
Example #9
0
        //贴图装箱算法
        public static IList <TexturePacket> PacketTexture(GraphicsDevice gdevice, IDictionary <string, Texture2D> sources)
        {
            List <TexturePacket> list = new List <TexturePacket>();

            int             packetsize            = 512;
            RectanglePacker packer                = new ArevaloRectanglePacker(packetsize, packetsize);
            Texture2D       tex                   = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color);
            Dictionary <string, RectangleF> rects = new Dictionary <string, RectangleF>();

            foreach (var i in sources)
            {
                UInt32[] data = new UInt32[i.Value.Width * i.Value.Height];
                i.Value.GetData <UInt32>(data);
                int  twidth  = i.Value.Width;
                int  theight = i.Value.Height;
                bool bfixx   = false;
                bool bfixy   = false;
                if ((twidth == packetsize / 2 || twidth == packetsize / 4))
                {
                }
                else
                {
                    bfixx   = true;
                    twidth += 2;
                }
                if (theight == packetsize / 2 || theight == packetsize / 4)
                {
                }
                else
                {
                    theight += 2;
                    bfixy    = true;
                }
                Point pos;

                if (packer.TryPack(twidth, theight, out pos))
                {
                    //right
                }
                else
                {
                    TexturePacket p = new TexturePacket(tex, rects);
                    list.Add(p);
                    packer = new ArevaloRectanglePacker(packetsize, packetsize);
                    tex    = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color);
                    rects  = new Dictionary <string, RectangleF>();

                    packer.TryPack(i.Value.Width, i.Value.Height, out pos);
                }
                if (bfixx)
                {
                    pos.X += 1;
                }
                if (bfixy)
                {
                    pos.Y += 1;
                }

                tex.SetData <UInt32>(0, new Rectangle(pos.X, pos.Y, i.Value.Width, i.Value.Height), data, 0, data.Length);
                rects.Add(i.Key, new RectangleF(
                              (float)(pos.X) / (float)packer.PackingAreaWidth,
                              (float)(pos.Y) / (float)packer.PackingAreaHeight,
                              (float)i.Value.Width / (float)packer.PackingAreaWidth,
                              (float)i.Value.Height / (float)packer.PackingAreaHeight
                              ));
            }
            {
                TexturePacket p = new TexturePacket(tex, rects);
                list.Add(p);
            }
            return(list);
        }