public Rectangle Insert(Frame frame)
        {
            if (this.filled)
            {
                // I am not a leaf
                Rectangle output = this.Right.Insert(frame);
                if (output != Rectangle.Empty)
                {
                    return(output);
                }
                else
                {
                    return(this.Bottom.Insert(frame));
                }
            }
            //so this is leaf
            if (this.rect.Width < frame.avatar.Label.Image.Width)
            {
                OTNode.wFlag     = true;
                OTNode.widthNeed = frame.avatar.Label.Image.Width - this.rect.Width;
                return(Rectangle.Empty);
            }
            OTNode.widthNeed = 0;
            if (this.rect.Height < frame.avatar.Label.Image.Height)
            {
                OTNode.wFlag      = false;
                OTNode.heightNeed = frame.avatar.Label.Image.Height - this.rect.Height;
                if (target.Description.Width < target.Description.Height)
                {
                    OTNode.wFlag     = true;
                    OTNode.widthNeed = frame.avatar.Label.Image.Width;
                }
                return(Rectangle.Empty);
            }
            OTNode.heightNeed = 0;
            // frame is fitt for this
            //if((this.rect.Width == frame.avatar.Label.Image.Width) && (this.rect.Height == frame.avatar.Label.Image.Height))
            //{
            //    this.Insert(frame, this.rect);
            //    return this.rect;
            //}
            //frame is smaller than this.rect
            Rectangle destRect = new Rectangle(this.rect.Left, this.rect.Top, frame.avatar.Label.Image.Width, frame.avatar.Label.Image.Height);

            this.Insert(frame, destRect);
            //int dw = this.rect.Width - frame.avatar.Label.Image.Width;
            //int dh = this.rect.Height - frame.avatar.Label.Image.Height;


            Rectangle BRect = new Rectangle(this.rect.Left
                                            , this.rect.Top + frame.avatar.Label.Image.Height
                                            , this.rect.Width
                                            , this.rect.Height - frame.avatar.Label.Image.Height);
            Rectangle RRect = new Rectangle(this.rect.Left + frame.avatar.Label.Image.Width
                                            , this.rect.Top
                                            , this.rect.Width - frame.avatar.Label.Image.Width
                                            , frame.avatar.Label.Image.Height);

            //if (dw > dh)
            //{
            this.Bottom = new OTNode(BRect);
            this.Right  = new OTNode(RRect);
            //}
            //else
            //{
            //    this.Bottom = new OTNode(RRect);
            //    this.Right = new OTNode(BRect);
            //}
            return(destRect);
        }
        void SaveTexture(string filename)
        {
            //this.frames.Sort(FrameCompare);
            int     tWidth        = 1; // frames[0].avatar.Label.Image.Width;
            int     tHeight       = 1; // frames[0].avatar.Label.Image.Height;
            bool    flag          = true;
            Texture outputTexture = null;

            CustomVertex.PositionColoredTextured[] vertexes = new CustomVertex.PositionColoredTextured[4 * frames.Count];
            short[] indexes = new short[frames.Count * 6];
            for (int j = 0; j < frames.Count * 4; j++)
            {
                vertexes[j].Color    = Color.White.ToArgb();
                vertexes[j].Position = new Vector3(0, 0, 0);
            }
            //int maxWidth, maxHeight;
            if (frames.Count > 1)
            {
                while (flag)
                {
                    if (OTNode.wFlag)
                    {
                        tWidth += OTNode.widthNeed;
                    }
                    else
                    {
                        tHeight += OTNode.heightNeed;
                    }
                    //tWidth *= 2;
                    // tHeight *= 2;
                    if (outputTexture != null)
                    {
                        outputTexture.Dispose();
                    }
                    outputTexture = new Texture(d3dDevice, tWidth, tHeight, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
                    Surface outputTextureSurface = outputTexture.GetSurfaceLevel(0);
                    d3dDevice.ColorFill(outputTextureSurface, new Rectangle(0, 0, tWidth, tHeight), Color.Magenta);
                    OTNode.target = outputTextureSurface;
                    OTNode node = new OTNode(new Rectangle(0, 0, tWidth, tHeight));

                    //maxHeight = maxWidth = 0;
                    for (int i = 0; i < frames.Count; i++)
                    {
                        toolStripProgressBar1.Value = (int)((float)(i + 1) / frames.Count * 100);
                        this.Update();
                        Rectangle rect = node.Insert(frames[i]);
                        if (rect == Rectangle.Empty)
                        {
                            flag = true;
                            break;
                        }
                        else
                        {
                            flag = false;
                        }
                        //if (rect.Right > maxWidth)
                        //    maxWidth = rect.Right;
                        //if (rect.Bottom > maxHeight)
                        //    maxHeight = rect.Bottom;
                        vertexes[i * 4].X = -frames[i].centerPoint.X;
                        vertexes[i * 4].Y = frames[i].centerPoint.Y - frames[i].avatar.Label.Image.Height;

                        vertexes[i * 4 + 1].X = vertexes[i * 4].X;
                        vertexes[i * 4 + 1].Y = frames[i].centerPoint.Y;

                        vertexes[i * 4 + 2].X = frames[i].avatar.Label.Image.Width - frames[i].centerPoint.X;;
                        vertexes[i * 4 + 2].Y = vertexes[i * 4 + 1].Y;

                        vertexes[i * 4 + 3].X = vertexes[i * 4 + 2].X;
                        vertexes[i * 4 + 3].Y = vertexes[i * 4].Y;

                        vertexes[i * 4].Tu     = (rect.Left + 1) / (float)tWidth;
                        vertexes[i * 4].Tv     = (rect.Bottom - 1) / (float)tHeight;
                        vertexes[i * 4 + 1].Tu = vertexes[i * 4].Tu;
                        vertexes[i * 4 + 1].Tv = (rect.Top + 1) / (float)tHeight;
                        vertexes[i * 4 + 2].Tu = (rect.Right - 1) / (float)tWidth;
                        vertexes[i * 4 + 2].Tv = vertexes[i * 4 + 1].Tv;
                        vertexes[i * 4 + 3].Tu = vertexes[i * 4 + 2].Tu;
                        vertexes[i * 4 + 3].Tv = vertexes[i * 4].Tv;


                        indexes[i * 6 + 0] = (short)(i * 4 + 0); indexes[i * 6 + 1] = (short)(i * 4 + 1); indexes[i * 6 + 2] = (short)(i * 4 + 2);
                        indexes[i * 6 + 3] = (short)(i * 4 + 3); indexes[i * 6 + 4] = (short)(i * 4 + 0); indexes[i * 6 + 5] = (short)(i * 4 + 2);
                    }
                }
            }
            else
            {
                int i = 0;
                tWidth  = frames[0].avatar.Label.Image.Width;
                tHeight = frames[0].avatar.Label.Image.Height;
                Rectangle rect = new Rectangle(0, 0, frames[0].avatar.Label.Image.Width, frames[0].avatar.Label.Image.Height);
                vertexes[i * 4].X = -frames[i].centerPoint.X;
                vertexes[i * 4].Y = frames[i].centerPoint.Y - frames[i].avatar.Label.Image.Height;

                vertexes[i * 4 + 1].X = vertexes[i * 4].X;
                vertexes[i * 4 + 1].Y = frames[i].centerPoint.Y;

                vertexes[i * 4 + 2].X = frames[i].avatar.Label.Image.Width - frames[i].centerPoint.X;;
                vertexes[i * 4 + 2].Y = vertexes[i * 4 + 1].Y;

                vertexes[i * 4 + 3].X = vertexes[i * 4 + 2].X;
                vertexes[i * 4 + 3].Y = vertexes[i * 4].Y;

                vertexes[i * 4].Tu     = (rect.Left + 1) / (float)tWidth;
                vertexes[i * 4].Tv     = (rect.Bottom - 1) / (float)tHeight;
                vertexes[i * 4 + 1].Tu = vertexes[i * 4].Tu;
                vertexes[i * 4 + 1].Tv = (rect.Top + 1) / (float)tHeight;
                vertexes[i * 4 + 2].Tu = (rect.Right - 1) / (float)tWidth;
                vertexes[i * 4 + 2].Tv = vertexes[i * 4 + 1].Tv;
                vertexes[i * 4 + 3].Tu = vertexes[i * 4 + 2].Tu;
                vertexes[i * 4 + 3].Tv = vertexes[i * 4].Tv;


                indexes[i * 6 + 0] = (short)(i * 4 + 0); indexes[i * 6 + 1] = (short)(i * 4 + 1); indexes[i * 6 + 2] = (short)(i * 4 + 2);
                indexes[i * 6 + 3] = (short)(i * 4 + 3); indexes[i * 6 + 4] = (short)(i * 4 + 0); indexes[i * 6 + 5] = (short)(i * 4 + 2);

                outputTexture = frames[0].texture;
            }
            Mesh mesh = new Mesh(2 * frames.Count, 4 * frames.Count, MeshFlags.Managed, CustomVertex.PositionColoredTextured.Format, d3dDevice);

            mesh.SetVertexBufferData(vertexes, LockFlags.None);
            mesh.SetIndexBufferData(indexes, LockFlags.None);
            //---------------------------
            //Setting one of these does NOT work.  You MUST set both.
            AttributeRange[] attributeTable = new AttributeRange[frames.Count];
            int[]            attributes     = mesh.LockAttributeBufferArray(LockFlags.None);
            for (int i = 0; i < frames.Count; i++)
            {
                attributes[i * 2]             = i;
                attributes[i * 2 + 1]         = i;
                attributeTable[i].AttributeId = i;  //ID is the value passed into the DrawSubset function of Mesh.
                attributeTable[i].FaceCount   = 2;  //Our subsets only have 1 face.
                attributeTable[i].FaceStart   = i * 2;
                attributeTable[i].VertexCount = 4;  //We need 3 vertices to make a face.
                attributeTable[i].VertexStart = i * 4;
            }
            mesh.UnlockAttributeBuffer(attributes);
            mesh.SetAttributeTable(attributeTable);
            //---------------------------
            ExtendedMaterial[] em = new ExtendedMaterial[frames.Count];
            em[0].TextureFilename = System.IO.Path.GetFileNameWithoutExtension(filename) + ".dds";
            for (int i = 0; i < frames.Count; i++)
            {
                Material mat = new Material();
                mat.Diffuse      = Color.White;
                mat.Ambient      = mat.Diffuse;
                mat.Specular     = mat.Diffuse;
                mat.Emissive     = Color.Black;
                em[i].Material3D = mat;
            }
            mesh.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + ".sht", (int[])null, em, null, XFileFormat.Binary);
            TextureLoader.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + ".dds", ImageFileFormat.Dds, outputTexture);
        }