protected override BitmapHolder Transform(BitmapHolder bitmapSource, string path, Work.ImageSource source, bool isPlaceholder, string key)
 {
     return(ToRounded(bitmapSource, (int)Radius, CropWidthRatio, CropHeightRatio, BorderSize, BorderHexColor));
 }
 protected override BitmapHolder Transform(BitmapHolder source)
 {
     return(ToRounded(source, (int)Radius, CropWidthRatio, CropHeightRatio, BorderSize, BorderHexColor));
 }
        public static BitmapHolder ToRounded(BitmapHolder source, int rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor)
        {
            double sourceWidth  = source.Width;
            double sourceHeight = source.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            double cropX = ((sourceWidth - desiredWidth) / 2);
            double cropY = ((sourceHeight - desiredHeight) / 2);

            BitmapHolder bitmap = null;

            if (cropX != 0 || cropY != 0)
            {
                bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight));
            }
            else
            {
                bitmap = new BitmapHolder(source.Pixels, source.Width, source.Height);
            }

            if (rad == 0)
            {
                rad = (int)(Math.Min(desiredWidth, desiredHeight) / 2);
            }
            else
            {
                rad = (int)(rad * (desiredWidth + desiredHeight) / 2 / 500);
            }

            int w = (int)desiredWidth;
            int h = (int)desiredHeight;

            int transparentColor = Colors.Transparent.ToInt();

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (x <= rad && y <= rad)
                    { //top left corner
                        if (!CheckRoundedCorner(rad, rad, rad, Corner.TopLeftCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x >= w - rad && y <= rad)
                    { // top right corner
                        if (!CheckRoundedCorner(w - rad, rad, rad, Corner.TopRightCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x >= w - rad && y >= h - rad)
                    { // bottom right corner
                        if (!CheckRoundedCorner(w - rad, h - rad, rad, Corner.BottomRightCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x <= rad && y >= h - rad)
                    { // bottom left corner
                        if (!CheckRoundedCorner(rad, h - rad, rad, Corner.BottomLeftCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                }
            }

            //TODO draws a border - we should optimize that and add some anti-aliasing
            if (borderSize > 0d)
            {
                borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 500d);
                int borderColor = Colors.Transparent.ToInt();

                try
                {
                    if (!borderHexColor.StartsWith("#", StringComparison.Ordinal))
                    {
                        borderHexColor.Insert(0, "#");
                    }
                    borderColor = borderHexColor.ToColorFromHex().ToInt();
                }
                catch (Exception)
                {
                }

                int intBorderSize = (int)Math.Ceiling(borderSize);

                for (int i = 2; i < intBorderSize; i++)
                {
                    CircleAA(bitmap, i, borderColor);
                }
            }

            return(bitmap);
        }
Exemplo n.º 4
0
        public static BitmapHolder ToCropped(BitmapHolder source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.Width;
            double sourceHeight = source.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            xOffset = xOffset * desiredWidth;
            yOffset = yOffset * desiredHeight;

            desiredWidth  = desiredWidth / zoomFactor;
            desiredHeight = desiredHeight / zoomFactor;

            float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset);
            float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset);

            if (cropX < 0)
            {
                cropX = 0;
            }

            if (cropY < 0)
            {
                cropY = 0;
            }

            if (cropX + desiredWidth > sourceWidth)
            {
                cropX = (float)(sourceWidth - desiredWidth);
            }

            if (cropY + desiredHeight > sourceHeight)
            {
                cropY = (float)(sourceHeight - desiredHeight);
            }

            int width  = (int)desiredWidth;
            int height = (int)desiredHeight;

            // Copy the pixels line by line using fast BlockCopy
            var result = new int[width * height];

            for (var line = 0; line < height; line++)
            {
                var srcOff = (((int)cropY + line) * source.Width + (int)cropX) * Helpers.SizeOfArgb;
                var dstOff = line * width * Helpers.SizeOfArgb;
                Helpers.BlockCopy(source.Pixels, srcOff, result, dstOff, width * Helpers.SizeOfArgb);
            }

            return(new BitmapHolder(result, width, height));
        }
        protected override BitmapHolder Transform(BitmapHolder source)
        {
            ToRounded(source, (int)_radius, _cropWidthRatio, _cropHeightRatio, _borderSize, _borderHexColor);

            return(source);
        }
        public static BitmapHolder ToTransformedCorners(BitmapHolder source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize,
                                                        CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.Width;
            double sourceHeight = source.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            double cropX = ((sourceWidth - desiredWidth) / 2);
            double cropY = ((sourceHeight - desiredHeight) / 2);

            BitmapHolder bitmap = null;

            if (cropX != 0 || cropY != 0)
            {
                bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight));
            }
            else
            {
                bitmap = new BitmapHolder(source.Pixels, source.Width, source.Height);
            }

            topLeftCornerSize     = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            topRightCornerSize    = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomLeftCornerSize  = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;

            int topLeftSize     = (int)topLeftCornerSize;
            int topRightSize    = (int)topRightCornerSize;
            int bottomLeftSize  = (int)bottomLeftCornerSize;
            int bottomRightSize = (int)bottomRightCornerSize;

            int w = bitmap.Width;
            int h = bitmap.Height;

            int transparentColor = Colors.Transparent.ToInt();

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (x <= topLeftSize && y <= topLeftSize)
                    { //top left corner
                        if (!CheckCorner(topLeftSize, topLeftSize, topLeftSize, cornersTransformType, Corner.TopLeftCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x >= w - topRightSize && y <= topRightSize && topRightSize > 0)
                    { // top right corner
                        if (!CheckCorner(w - topRightSize, topRightSize, topRightSize, cornersTransformType, Corner.TopRightCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x >= w - bottomRightSize && y >= h - bottomRightSize && bottomRightSize > 0)
                    { // bottom right corner
                        if (!CheckCorner(w - bottomRightSize, h - bottomRightSize, bottomRightSize, cornersTransformType, Corner.BottomRightCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                    else if (x <= bottomLeftSize && y >= h - bottomLeftSize && bottomLeftSize > 0)
                    { // bottom left corner
                        if (!CheckCorner(bottomLeftSize, h - bottomLeftSize, bottomLeftSize, cornersTransformType, Corner.BottomLeftCorner, x, y))
                        {
                            bitmap.Pixels[y * w + x] = transparentColor;
                        }
                    }
                }
            }

            return(bitmap);
        }
Exemplo n.º 7
0
 protected override BitmapHolder Transform(BitmapHolder source)
 {
     return(ToCropped(source, ZoomFactor, XOffset, YOffset, CropWidthRatio, CropHeightRatio));
 }
 protected override BitmapHolder Transform(BitmapHolder source)
 {
     return(ToTransformedCorners(source, TopLeftCornerSize, TopRightCornerSize, BottomLeftCornerSize, BottomRightCornerSize,
                                 CornersTransformType, CropWidthRatio, CropHeightRatio));
 }
Exemplo n.º 9
0
        public static void ToColorSpace(BitmapHolder bmp, float[][] rgbawMatrix)
        {
            var r0 = rgbawMatrix[0][0];
            var r1 = rgbawMatrix[0][1];
            var r2 = rgbawMatrix[0][2];
            var r3 = rgbawMatrix[0][3];

            var g0 = rgbawMatrix[1][0];
            var g1 = rgbawMatrix[1][1];
            var g2 = rgbawMatrix[1][2];
            var g3 = rgbawMatrix[1][3];

            var b0 = rgbawMatrix[2][0];
            var b1 = rgbawMatrix[2][1];
            var b2 = rgbawMatrix[2][2];
            var b3 = rgbawMatrix[2][3];

            var a0 = rgbawMatrix[3][0];
            var a1 = rgbawMatrix[3][1];
            var a2 = rgbawMatrix[3][2];
            var a3 = rgbawMatrix[3][3];

            var rOffset = rgbawMatrix[4][0];
            var gOffset = rgbawMatrix[4][1];
            var bOffset = rgbawMatrix[4][2];
            var aOffset = rgbawMatrix[4][3];

            var nWidth  = bmp.Width;
            var nHeight = bmp.Height;
            var len     = bmp.PixelCount;

            for (var i = 0; i < len; i++)
            {
                var c = bmp.GetPixelAsInt(i);
                var a = (c >> 24) & 0x000000FF;
                var r = (c >> 16) & 0x000000FF;
                var g = (c >> 8) & 0x000000FF;
                var b = (c) & 0x000000FF;

                var rNew = (int)(r * r0 + g * g0 + b * b0 + a * a0 + rOffset);
                var gNew = (int)(r * r1 + g * g1 + b * b1 + a * a1 + gOffset);
                var bNew = (int)(r * r2 + g * g2 + b * b2 + a * a2 + bOffset);
                var aNew = (int)(r * r3 + g * g3 + b * b3 + a * a3 + aOffset);

                if (rNew > 255)
                {
                    rNew = 255;
                }

                if (gNew > 255)
                {
                    gNew = 255;
                }

                if (bNew > 255)
                {
                    bNew = 255;
                }

                if (aNew > 255)
                {
                    aNew = 255;
                }

                if (rNew < 0)
                {
                    rNew = 0;
                }

                if (gNew < 0)
                {
                    gNew = 0;
                }

                if (bNew < 0)
                {
                    bNew = 0;
                }

                if (aNew < 0)
                {
                    aNew = 0;
                }

                bmp.SetPixel(i, (aNew << 24) | (rNew << 16) | (gNew << 8) | bNew);
            }
        }
Exemplo n.º 10
0
        protected override BitmapHolder Transform(BitmapHolder bitmapSource, string path, Work.ImageSource source, bool isPlaceholder, string key)
        {
            ToColorSpace(bitmapSource, _rgbawMatrix);

            return(bitmapSource);
        }
        protected override BitmapHolder Transform(BitmapHolder source)
        {
            ToGrayscale(source);

            return source;
        }
 protected override BitmapHolder Transform(BitmapHolder bitmapSource, string path, ImageSource source, bool isPlaceholder, string key)
 {
     return(RoundedTransformation.ToRounded(bitmapSource, 0, 1.0, 1.0, BorderSize, BorderHexColor));
 }