public static SegaSaturnTexture ConvertFrom(Bitmap image, string name, SegaSaturnTextureCoordinates p1, SegaSaturnTextureCoordinates p2, SegaSaturnTextureCoordinates p3, SegaSaturnTextureCoordinates p4, bool isTriangleMapping)
        {
            SegaSaturnTexture toReturn = new SegaSaturnTexture
            {
                Name = name,
                Hash = String.Format("{0}¤{1}¤{2}¤{3}", p1.Hash, p2.Hash, p3.Hash, p4.Hash)
            };
            Rectangle rect = SegaSaturnTexture.GetRectangleFromTextureCoordinates(p1, p2, p3, p4);

            if (!isTriangleMapping)
            {
                toReturn.Image = JoMapEditorTools.CropBitmap(image, rect);
            }
            else
            {
                List <IntPoint> corners = new List <IntPoint>();
                corners.Add(new IntPoint(p1.X, p1.Y));
                corners.Add(new IntPoint(p2.X, p2.Y));
                corners.Add(new IntPoint(p3.X, p3.Y));
                corners.Add(new IntPoint(p4.X, p4.Y));
                SimpleQuadrilateralTransformation filter = new SimpleQuadrilateralTransformation(corners, rect.Width, rect.Height);
                toReturn.Image = filter.Apply(image);
            }
            if ((toReturn.Image.Width % 8) == 0)
            {
                toReturn.Image = toReturn.Image;
            }
            else
            {
                int width = (toReturn.Image.Width / 8) * 8;
                if (width <= 0)
                {
                    width = 8;
                }
                toReturn.Image = JoMapEditorTools.ResizeImage(toReturn.Image, width, image.Height);
            }
            return(toReturn);
        }
        private static Rectangle GetRectangleFromTextureCoordinates(SegaSaturnTextureCoordinates p1, SegaSaturnTextureCoordinates p2, SegaSaturnTextureCoordinates p3, SegaSaturnTextureCoordinates p4)
        {
            int x = p1.X;

            if (p2.X < x)
            {
                x = p2.X;
            }
            if (p3.X < x)
            {
                x = p3.X;
            }
            if (p4.X < x)
            {
                x = p4.X;
            }

            int x2 = p1.X;

            if (p2.X > x2)
            {
                x2 = p2.X;
            }
            if (p3.X > x2)
            {
                x2 = p3.X;
            }
            if (p4.X > x2)
            {
                x2 = p4.X;
            }

            int y = p1.Y;

            if (p2.Y < y)
            {
                y = p2.Y;
            }
            if (p3.Y < y)
            {
                y = p3.Y;
            }
            if (p4.Y < y)
            {
                y = p4.Y;
            }

            int y2 = p1.Y;

            if (p2.Y > y2)
            {
                y2 = p2.Y;
            }
            if (p3.Y > y2)
            {
                y2 = p3.Y;
            }
            if (p4.Y > y2)
            {
                y2 = p4.Y;
            }
            Rectangle rect = new Rectangle(x, y, x2 - x, y2 - y);

            if (rect.Width <= 0)
            {
                rect.Width = rect.Height;
            }
            if (rect.Height <= 0)
            {
                rect.Height = rect.Width;
            }
            return(rect);
        }