コード例 #1
0
        /// <summary>
        /// Flip/Rotate Filter constructor
        /// </summary>
        public FlipRotateFilter()
        {
            _innerFilter = new SpatialTransformFilter();

            FilterParameter flipVertical   = new FilterParameter(FLIPVERTICAL, "Verical flip of the image", (bool)false);
            FilterParameter flipHorizontal = new FilterParameter(FLIPHORIZONTAL, "Horizontal Flip of the image", (bool)false);
            FilterParameter rotation       = new FilterParameter(ROTATION, "Rotate the image", (RotationValue)RotationValue.NoRotation);

            AddParameter(flipVertical);
            AddParameter(flipHorizontal);
            AddParameter(rotation);
        }
コード例 #2
0
        /// <summary>
        /// Filter Implementation
        /// </summary>
        protected override IImageAdapter ProcessFilter(IImageAdapter source)
        {
            IImageAdapter retVal = null;

            if (source == null)
            {
                throw new ArgumentNullException("source", "IImageAdapter must be set to a valid instance (null passed in)");
            }

            if (MorphToShape != Shape.Square && MorphToShape != Shape.Circle)
            {
                throw new NotImplementedException("Will be implemented if requested");
            }

            int width  = source.Width;
            int height = source.Height;
            int side   = 0;

            if (ExtendToMaxSide == false)
            {
                side = (int)Math.Min(width, height);
            }
            else
            {
                side = (int)Math.Max(width, height);
            }

            retVal = new ImageAdapter(side, side);

            if (MorphToShape == Shape.Square || MorphToShape == Shape.Circle)
            {
                // Shrink/Expand image to square
                double   ratio  = (double)width / (double)height;
                double[] matrix = new double[] { 1, 0, 0, 1, 0, 0 };
                if (ratio >= 1.0)
                {
                    if (ExtendToMaxSide)
                    {
                        matrix[3] *= ratio;
                    }
                    else
                    {
                        matrix[0] /= ratio;
                    }
                }
                else
                {
                    if (ExtendToMaxSide)
                    {
                        matrix[0] /= ratio;
                    }
                    else
                    {
                        matrix[3] *= ratio;
                    }
                }
                SpatialTransformFilter filter = new SpatialTransformFilter();
                filter.Matrix = matrix;
                retVal        = filter.Process(source);
            }

            // Step 2 : Compute every pixel in resulting image
            if (MorphToShape == Shape.Circle)
            {
                int          x       = 0;
                int          y       = 0;
                double       center  = side / 2.0;
                double       Y       = double.NaN;
                double       X       = double.NaN;
                double       theta   = double.NaN;
                double       ratio   = double.NaN;
                double       xSource = double.NaN;
                double       ySource = double.NaN;
                ImageAdapter temp    = new ImageAdapter(side, side);

                for (y = 0; y < side; y++)
                {
                    for (x = 0; x < side; x++)
                    {
                        X       = x - center;
                        Y       = center - y;
                        theta   = Math.Atan(Y / X);
                        ratio   = Math.Max(Math.Abs(Math.Cos(theta)), Math.Abs(Math.Sin(theta)));
                        xSource = X / ratio + center;
                        ySource = -(Y / ratio - center);
                        if (xSource >= 0 && xSource < side && ySource >= 0 && ySource < side)
                        {
                            temp[x, y] = (IColor)retVal[(int)xSource, (int)ySource].Clone();
                        }
                        else
                        {
                            temp[x, y] = (IColor)retVal[0, 0].Clone(); temp[x, y].IsEmpty = true;
                        }
                    }
                }
                retVal = temp;
            }

            return(retVal);
        }