Beispiel #1
0
        public static string ToCode(this CropAnchor anchor)
        {
            switch (anchor)
            {
            case Bottom: return("b");

            case Bottom | Left: return("bl");

            case Bottom | Right: return("br");

            case Center: return("c");

            case Left: return("l");

            case Right: return("r");

            case Top: return("t");

            case Top | Left: return("tl");

            case Top | Right: return("tr");

            default: throw new ArgumentException($"Invalid anchor. Was '{anchor}'");
            }
        }
Beispiel #2
0
        public static PaddedBox Pad(
            Size source,
            Size bounds,
            CropAnchor anchor = CropAnchor.Center,
            bool upscale      = false)
        {
            var size = Fit(source, bounds, upscale); // Fit if it doesn't fit or needs upscaling...

            // Distribute the padding
            var extraWidth  = bounds.Width - size.Width;
            var extraHeight = bounds.Height - size.Height;

            int top    = extraHeight / 2,
                right  = extraWidth / 2,
                bottom = extraHeight / 2,
                left   = extraWidth / 2;

            if (anchor.HasFlag(CropAnchor.Left))
            {
                right = extraWidth;
                left  = 0;
            }
            else if (anchor.HasFlag(CropAnchor.Right))
            {
                left  = extraWidth;
                right = 0;
            }
            else
            {
                // If we split on a pixel, add 1px to the left
                if (extraWidth % 2 == 1)
                {
                    left += 1;
                }
            }

            if (anchor.HasFlag(CropAnchor.Top))
            {
                bottom = extraHeight;
                top    = 0;
            }
            else if (anchor.HasFlag(CropAnchor.Bottom))
            {
                top    = extraHeight;
                bottom = 0;
            }
            else
            {
                // If we split on a pixel, add 1px to the top
                if (extraHeight % 2 == 1)
                {
                    top += 1;
                }
            }

            var padding = new Padding(top, right, bottom, left);

            return(new PaddedBox(size, padding));
        }
Beispiel #3
0
        public static void Crop(Texture2D texture, int width, int height, CropAnchor anchor = CropAnchor.CENTER)
        {
            int resultPosX   = 0;
            int resultPosY   = 0;
            int resultWidth  = 0;
            int resultHeight = 0;

            float ratioWidth  = (float)width / (float)texture.width;
            float ratioHeight = (float)height / (float)texture.height;

            if (ratioHeight < ratioWidth)
            {
                resultWidth  = width;
                resultHeight = (int)(texture.height * ratioWidth);

                switch (anchor)
                {
                case CropAnchor.TOP:
                    resultPosY = (int)(resultHeight - height);
                    break;

                case CropAnchor.BOTTOM:
                    resultPosY = 0;
                    break;

                default:
                    resultPosY = (int)((resultHeight - height) * 0.5f);
                    break;
                }
            }
            else
            {
                resultWidth  = (int)(texture.width * ratioHeight);
                resultHeight = height;

                switch (anchor)
                {
                case CropAnchor.LEFT:
                    resultPosX = 0;
                    break;

                case CropAnchor.RIGHT:
                    resultPosX = (int)(resultWidth - width);
                    break;

                default:
                    resultPosX = (int)((resultWidth - width) * 0.5f);
                    break;
                }
            }

            Resize(texture, resultWidth, resultHeight);
            Color[] pixelArray = texture.GetPixels(resultPosX, resultPosY, width, height);

            texture.Resize(width, height);
            texture.SetPixels(pixelArray, 0);
            texture.Apply();
        }
Beispiel #4
0
		public static void Crop(Texture2D texture, int width, int height, CropAnchor anchor = CropAnchor.CENTER)
		{
			int resultPosX = 0;
			int resultPosY = 0;
			int resultWidth = 0;
			int resultHeight = 0;
			
			float ratioWidth = (float)width/(float)texture.width;
			float ratioHeight = (float)height/(float)texture.height;
			
			if (ratioHeight < ratioWidth)
			{
				resultWidth = width;
				resultHeight = (int)(texture.height * ratioWidth);
				
				switch(anchor)
				{
				case CropAnchor.TOP:
					resultPosY = (int)(resultHeight - height);
					break;
				case CropAnchor.BOTTOM:
					resultPosY = 0;
					break;
				default:
					resultPosY = (int)((resultHeight - height) * 0.5f);
					break;
				}
			}
			else
			{
				resultWidth  = (int)(texture.width * ratioHeight);
				resultHeight = height;
				
				switch(anchor)
				{
				case CropAnchor.LEFT:
					resultPosX = 0;
					break;
				case CropAnchor.RIGHT:
					resultPosX = (int)(resultWidth - width);
					break;
				default:
					resultPosX = (int)((resultWidth - width) * 0.5f);
					break;
				}
			}
			
			Resize(texture, resultWidth, resultHeight);
			Color[] pixelArray = texture.GetPixels(resultPosX, resultPosY, width, height);
			
			texture.Resize(width, height);
			texture.SetPixels(pixelArray, 0); 
			texture.Apply();
		}
 public SizingOptions(
     int width, int height, int padding,
     Color backgroundColor,
     CropAnchor anchor,
     long sprocketFileID)
 {
     this.padding         = padding;
     this.height          = height;
     this.width           = width;
     this.backgroundColor = backgroundColor;
     this.displayType     = Display.Crop;
     this.anchor          = anchor;
     this.sprocketFileID  = sprocketFileID;
     SetFilename();
 }
 public SizingOptions(
     int width, int height, int padding,
     Color backgroundColor,
     Color borderColor, int borderSize,
     CropAnchor anchor,
     long sprocketFileID)
 {
     this.padding = padding;
     this.height = height;
     this.width = width;
     this.backgroundColor = backgroundColor;
     this.borderColor = borderColor;
     this.borderSize = borderSize;
     this.displayType = Display.Crop;
     this.anchor = anchor;
     this.sprocketFileID = sprocketFileID;
     SetFilename();
 }
Beispiel #7
0
        // Align the box within the bounds

        public static void Align(ref Rectangle box, Size bounds, CropAnchor anchor)
        {
            double x = 0d,
                   y = 0d;

            // There's horizontal space
            if (box.Width < bounds.Width)
            {
                if (anchor.HasFlag(CropAnchor.Left))
                {
                    x = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Right))
                {
                    x = bounds.Width - box.Width;
                }
                else // center
                {
                    x = (bounds.Width - box.Width) / 2d;
                }
            }

            // There's vertical space
            if (box.Height < bounds.Height)
            {
                if (anchor.HasFlag(CropAnchor.Top))
                {
                    y = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Bottom))
                {
                    y = bounds.Height - box.Height;
                }
                else // Center
                {
                    y = (bounds.Height - box.Height) / 2d;
                }
            }

            box.X = (int)x;
            box.Y = (int)y;
        }
Beispiel #8
0
        // source is ALWAYS bigger than bounds
        private static Rectangle CalculateCropRectangle(Size source, Size target, CropAnchor anchor)
        {
            int x = 0,
                y = 0;

            // There's veritical space to distribute
            if (source.Height > target.Height)
            {
                if (anchor.HasFlag(CropAnchor.Top))
                {
                    y = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Bottom))
                {
                    y = source.Height - target.Height;
                }
                else // Center
                {
                    y = (int)((source.Height - target.Height) / 2d);
                }
            }

            // There's horizontal space to distribute
            if (source.Width > target.Width)
            {
                if (anchor.HasFlag(CropAnchor.Left))
                {
                    x = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Right))
                {
                    x = source.Width - target.Width;
                }
                else // center
                {
                    x = (int)((source.Width - target.Width) / 2d);
                }
            }

            return(new Rectangle(x, y, target.Width, target.Height));
        }
Beispiel #9
0
        public MediaTransformation Resize(int width, int height, CropAnchor anchor)
        {
            Apply(new ResizeTransform(new Size(width, height), anchor));

            return(this);
        }
Beispiel #10
0
        public static Rectangle CalculateCropRectangle(Size source, Rational aspect, CropAnchor anchor)
        {
            var targetSize = Max(source, aspect);

            return(CalculateCropRectangle(source, targetSize, anchor));
        }
Beispiel #11
0
		public static Texture2D LoadCroppedFromFile(string filePath, int width, int height, CropAnchor anchor)
		{
			Texture2D thumbnail = TextureUtils.LoadFixedHeightFromFile(filePath, height, TextureFormat.R16);
			TextureUtils.Crop(thumbnail, width, height, anchor);
			return thumbnail;
		}
Beispiel #12
0
        public static Texture2D LoadCroppedFromFile(string filePath, int width, int height, CropAnchor anchor)
        {
            Texture2D thumbnail = TextureUtils.LoadFixedHeightFromFile(filePath, height, TextureFormat.R16);

            TextureUtils.Crop(thumbnail, width, height, anchor);
            return(thumbnail);
        }