Exemplo n.º 1
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            // get source image size
            int width  = sourceData.Width;
            int height = sourceData.Height;

            int    pixelSize = (sourceData.PixelFormat == PixelFormat.Format8bppIndexed) ? 1 : 3;
            int    srcStride = sourceData.Stride;
            int    dstOffset = destinationData.Stride - pixelSize * newWidth;
            double xFactor   = (double)width / newWidth;
            double yFactor   = (double)height / newHeight;

            // do the job
            byte *src = (byte *)sourceData.ImageData.ToPointer( );
            byte *dst = (byte *)destinationData.ImageData.ToPointer( );

            // coordinates of source points and cooefficiens
            double ox, oy, dx, dy, k1, k2;
            int    ox1, oy1, ox2, oy2;
            // destination pixel values
            double r, g, b;
            // width and height decreased by 1
            int ymax = height - 1;
            int xmax = width - 1;
            // temporary pointer
            byte *p;

            // check pixel format
            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // grayscale
                for (int y = 0; y < newHeight; y++)
                {
                    // Y coordinates
                    oy  = (double)y * yFactor - 0.5;
                    oy1 = (int)oy;
                    dy  = oy - (double)oy1;

                    for (int x = 0; x < newWidth; x++, dst++)
                    {
                        // X coordinates
                        ox  = (double)x * xFactor - 0.5f;
                        ox1 = (int)ox;
                        dx  = ox - (double)ox1;

                        // initial pixel value
                        g = 0;

                        for (int n = -1; n < 3; n++)
                        {
                            // get Y cooefficient
                            k1 = Interpolation.BiCubicKernel(dy - (double)n);

                            oy2 = oy1 + n;
                            if (oy2 < 0)
                            {
                                oy2 = 0;
                            }
                            if (oy2 > ymax)
                            {
                                oy2 = ymax;
                            }

                            for (int m = -1; m < 3; m++)
                            {
                                // get X cooefficient
                                k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                ox2 = ox1 + m;
                                if (ox2 < 0)
                                {
                                    ox2 = 0;
                                }
                                if (ox2 > xmax)
                                {
                                    ox2 = xmax;
                                }

                                g += k2 * src[oy2 * srcStride + ox2];
                            }
                        }
                        *dst = (byte)Math.Max(0, Math.Min(255, g));
                    }
                    dst += dstOffset;
                }
            }
            else
            {
                // RGB
                for (int y = 0; y < newHeight; y++)
                {
                    // Y coordinates
                    oy  = (double)y * yFactor - 0.5f;
                    oy1 = (int)oy;
                    dy  = oy - (double)oy1;

                    for (int x = 0; x < newWidth; x++, dst += 3)
                    {
                        // X coordinates
                        ox  = (double)x * xFactor - 0.5f;
                        ox1 = (int)ox;
                        dx  = ox - (double)ox1;

                        // initial pixel value
                        r = g = b = 0;

                        for (int n = -1; n < 3; n++)
                        {
                            // get Y cooefficient
                            k1 = Interpolation.BiCubicKernel(dy - (double)n);

                            oy2 = oy1 + n;
                            if (oy2 < 0)
                            {
                                oy2 = 0;
                            }
                            if (oy2 > ymax)
                            {
                                oy2 = ymax;
                            }

                            for (int m = -1; m < 3; m++)
                            {
                                // get X cooefficient
                                k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                ox2 = ox1 + m;
                                if (ox2 < 0)
                                {
                                    ox2 = 0;
                                }
                                if (ox2 > xmax)
                                {
                                    ox2 = xmax;
                                }

                                // get pixel of original image
                                p = src + oy2 * srcStride + ox2 * 3;

                                r += k2 * p[RGB.R];
                                g += k2 * p[RGB.G];
                                b += k2 * p[RGB.B];
                            }
                        }

                        dst[RGB.R] = (byte)Math.Max(0, Math.Min(255, r));
                        dst[RGB.G] = (byte)Math.Max(0, Math.Min(255, g));
                        dst[RGB.B] = (byte)Math.Max(0, Math.Min(255, b));
                    }
                    dst += dstOffset;
                }
            }
        }
Exemplo n.º 2
0
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            if (angle == 0)
            {
                // just clone the image
                return(AForge.Imaging.Image.Clone(srcImg));
            }

            double angleRad = -angle * Math.PI / 180;
            double angleCos = Math.Cos(angleRad);
            double angleSin = Math.Sin(angleRad);

            double halfWidth  = (double)width / 2;
            double halfHeight = (double)height / 2;

            double halfNewWidth, halfNewHeight;
            int    newWidth, newHeight;

            if (keepSize)
            {
                halfNewWidth  = halfWidth;
                halfNewHeight = halfHeight;

                newWidth  = width;
                newHeight = height;
            }
            else
            {
                // rotate corners
                double cx1 = halfWidth * angleCos;
                double cy1 = halfWidth * angleSin;

                double cx2 = halfWidth * angleCos - halfHeight * angleSin;
                double cy2 = halfWidth * angleSin + halfHeight * angleCos;

                double cx3 = -halfHeight * angleSin;
                double cy3 = halfHeight * angleCos;

                double cx4 = 0;
                double cy4 = 0;

                halfNewWidth  = Math.Max(Math.Max(cx1, cx2), Math.Max(cx3, cx4)) - Math.Min(Math.Min(cx1, cx2), Math.Min(cx3, cx4));
                halfNewHeight = Math.Max(Math.Max(cy1, cy2), Math.Max(cy3, cy4)) - Math.Min(Math.Min(cy1, cy2), Math.Min(cy3, cy4));

                newWidth  = (int)(halfNewWidth * 2 + 0.5);
                newHeight = (int)(halfNewHeight * 2 + 0.5);
            }

            // pixel format
            PixelFormat fmt = (srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ?
                              PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, fmt);

            // create new image
            Bitmap dstImg = (fmt == PixelFormat.Format8bppIndexed) ?
                            AForge.Imaging.Image.CreateGrayscaleImage(newWidth, newHeight) :
                            new Bitmap(newWidth, newHeight, fmt);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, newWidth, newHeight),
                ImageLockMode.ReadWrite, fmt);

            int srcStride = srcData.Stride;
            int dstOffset = dstData.Stride - ((fmt == PixelFormat.Format8bppIndexed) ? newWidth : newWidth * 3);

            byte fillR = fillColor.R;
            byte fillG = fillColor.G;
            byte fillB = fillColor.B;

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer();
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                switch (method)
                {
                case InterpolationMethod.NearestNeighbor:
                {
                    // -------------------------------------------
                    // rotate using nearest neighbor interpolation
                    // -------------------------------------------

                    double cx, cy;
                    int    ox, oy;
                    byte * p;

                    if (fmt == PixelFormat.Format8bppIndexed)
                    {
                        // grayscale
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst++)
                            {
                                // coordinate of the nearest point
                                ox = (int)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (int)(-angleSin * cx + angleCos * cy + halfHeight);

                                if ((ox < 0) || (oy < 0) || (ox >= width) || (oy >= height))
                                {
                                    *dst = fillG;
                                }
                                else
                                {
                                    *dst = src[oy * srcStride + ox];
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    else
                    {
                        // RGB
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst += 3)
                            {
                                // coordinate of the nearest point
                                ox = (int)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (int)(-angleSin * cx + angleCos * cy + halfHeight);

                                if ((ox < 0) || (oy < 0) || (ox >= width) || (oy >= height))
                                {
                                    dst[RGB.R] = fillR;
                                    dst[RGB.G] = fillG;
                                    dst[RGB.B] = fillB;
                                }
                                else
                                {
                                    p = src + oy * srcStride + ox * 3;

                                    dst[RGB.R] = p[RGB.R];
                                    dst[RGB.G] = p[RGB.G];
                                    dst[RGB.B] = p[RGB.B];
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    break;
                }

                case InterpolationMethod.Bilinear:
                {
                    // ------------------------------------
                    // rotate using  bilinear interpolation
                    // ------------------------------------

                    double cx, cy;
                    float  ox, oy, dx1, dy1, dx2, dy2;
                    int    ox1, oy1, ox2, oy2;
                    int    ymax = height - 1;
                    int    xmax = width - 1;
                    byte   v1, v2;
                    byte * p1, p2, p3, p4;

                    if (fmt == PixelFormat.Format8bppIndexed)
                    {
                        // grayscale
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst++)
                            {
                                ox = (float)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (float)(-angleSin * cx + angleCos * cy + halfHeight);

                                // top-left coordinate
                                ox1 = (int)ox;
                                oy1 = (int)oy;

                                if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                                {
                                    *dst = fillG;
                                }
                                else
                                {
                                    // bottom-right coordinate
                                    ox2 = (ox1 == xmax) ? ox1 : ox1 + 1;
                                    oy2 = (oy1 == ymax) ? oy1 : oy1 + 1;

                                    if ((dx1 = ox - (float)ox1) < 0)
                                    {
                                        dx1 = 0;
                                    }
                                    dx2 = 1.0f - dx1;

                                    if ((dy1 = oy - (float)oy1) < 0)
                                    {
                                        dy1 = 0;
                                    }
                                    dy2 = 1.0f - dy1;

                                    p1 = src + oy1 * srcStride;
                                    p2 = src + oy2 * srcStride;

                                    // interpolate using 4 points
                                    v1 = (byte)(dx2 * p1[ox1] + dx1 * p1[ox2]);
                                    v2 = (byte)(dx2 * p2[ox1] + dx1 * p2[ox2]);
                                    *dst = (byte)(dy2 * v1 + dy1 * v2);
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    else
                    {
                        // RGB
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst += 3)
                            {
                                ox = (float)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (float)(-angleSin * cx + angleCos * cy + halfHeight);

                                // top-left coordinate
                                ox1 = (int)ox;
                                oy1 = (int)oy;

                                if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                                {
                                    dst[RGB.R] = fillR;
                                    dst[RGB.G] = fillG;
                                    dst[RGB.B] = fillB;
                                }
                                else
                                {
                                    // bottom-right coordinate
                                    ox2 = (ox1 == xmax) ? ox1 : ox1 + 1;
                                    oy2 = (oy1 == ymax) ? oy1 : oy1 + 1;

                                    if ((dx1 = ox - (float)ox1) < 0)
                                    {
                                        dx1 = 0;
                                    }
                                    dx2 = 1.0f - dx1;

                                    if ((dy1 = oy - (float)oy1) < 0)
                                    {
                                        dy1 = 0;
                                    }
                                    dy2 = 1.0f - dy1;

                                    // get four points
                                    p1  = p2 = src + oy1 * srcStride;
                                    p1 += ox1 * 3;
                                    p2 += ox2 * 3;

                                    p3  = p4 = src + oy2 * srcStride;
                                    p3 += ox1 * 3;
                                    p4 += ox2 * 3;

                                    // interpolate using 4 points

                                    // red
                                    v1         = (byte)(dx2 * p1[RGB.R] + dx1 * p2[RGB.R]);
                                    v2         = (byte)(dx2 * p3[RGB.R] + dx1 * p4[RGB.R]);
                                    dst[RGB.R] = (byte)(dy2 * v1 + dy1 * v2);

                                    // green
                                    v1         = (byte)(dx2 * p1[RGB.G] + dx1 * p2[RGB.G]);
                                    v2         = (byte)(dx2 * p3[RGB.G] + dx1 * p4[RGB.G]);
                                    dst[RGB.G] = (byte)(dy2 * v1 + dy1 * v2);

                                    // blue
                                    v1         = (byte)(dx2 * p1[RGB.B] + dx1 * p2[RGB.B]);
                                    v2         = (byte)(dx2 * p3[RGB.B] + dx1 * p4[RGB.B]);
                                    dst[RGB.B] = (byte)(dy2 * v1 + dy1 * v2);
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    break;
                }

                case InterpolationMethod.Bicubic:
                {
                    // ----------------------------------
                    // rotate using bicubic interpolation
                    // ----------------------------------

                    double cx, cy;
                    float  ox, oy, dx, dy, k1, k2;
                    float  r, g, b;
                    int    ox1, oy1, ox2, oy2;
                    int    ymax = height - 1;
                    int    xmax = width - 1;
                    byte * p;

                    if (fmt == PixelFormat.Format8bppIndexed)
                    {
                        // grayscale
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst++)
                            {
                                ox = (float)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (float)(-angleSin * cx + angleCos * cy + halfHeight);

                                ox1 = (int)ox;
                                oy1 = (int)oy;

                                if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                                {
                                    *dst = fillG;
                                }
                                else
                                {
                                    dx = ox - (float)ox1;
                                    dy = oy - (float)oy1;

                                    g = 0;

                                    for (int n = -1; n < 3; n++)
                                    {
                                        k1 = Interpolation.BiCubicKernel(dy - (float)n);

                                        oy2 = oy1 + n;
                                        if (oy2 < 0)
                                        {
                                            oy2 = 0;
                                        }
                                        if (oy2 > ymax)
                                        {
                                            oy2 = ymax;
                                        }

                                        for (int m = -1; m < 3; m++)
                                        {
                                            k2 = k1 * Interpolation.BiCubicKernel((float)m - dx);

                                            ox2 = ox1 + m;
                                            if (ox2 < 0)
                                            {
                                                ox2 = 0;
                                            }
                                            if (ox2 > xmax)
                                            {
                                                ox2 = xmax;
                                            }

                                            g += k2 * src[oy2 * srcStride + ox2];
                                        }
                                    }
                                    *dst = (byte)g;
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    else
                    {
                        // RGB
                        cy = -halfNewHeight;
                        for (int y = 0; y < newHeight; y++)
                        {
                            cx = -halfNewWidth;
                            for (int x = 0; x < newWidth; x++, dst += 3)
                            {
                                ox = (float)(angleCos * cx + angleSin * cy + halfWidth);
                                oy = (float)(-angleSin * cx + angleCos * cy + halfHeight);

                                ox1 = (int)ox;
                                oy1 = (int)oy;

                                if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                                {
                                    dst[RGB.R] = fillR;
                                    dst[RGB.G] = fillG;
                                    dst[RGB.B] = fillB;
                                }
                                else
                                {
                                    dx = ox - (float)ox1;
                                    dy = oy - (float)oy1;

                                    r = g = b = 0;

                                    for (int n = -1; n < 3; n++)
                                    {
                                        k1 = Interpolation.BiCubicKernel(dy - (float)n);

                                        oy2 = oy1 + n;
                                        if (oy2 < 0)
                                        {
                                            oy2 = 0;
                                        }
                                        if (oy2 > ymax)
                                        {
                                            oy2 = ymax;
                                        }

                                        for (int m = -1; m < 3; m++)
                                        {
                                            k2 = k1 * Interpolation.BiCubicKernel((float)m - dx);

                                            ox2 = ox1 + m;
                                            if (ox2 < 0)
                                            {
                                                ox2 = 0;
                                            }
                                            if (ox2 > xmax)
                                            {
                                                ox2 = xmax;
                                            }

                                            // get pixel of original image
                                            p = src + oy2 * srcStride + ox2 * 3;

                                            r += k2 * p[RGB.R];
                                            g += k2 * p[RGB.G];
                                            b += k2 * p[RGB.B];
                                        }
                                    }
                                    dst[RGB.R] = (byte)r;
                                    dst[RGB.G] = (byte)g;
                                    dst[RGB.B] = (byte)b;
                                }
                                cx++;
                            }
                            cy++;
                            dst += dstOffset;
                        }
                    }
                    break;
                }
                }
            }

            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            return(dstImg);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            // get source image size
            int    width      = sourceData.Width;
            int    height     = sourceData.Height;
            double oldXradius = (double)(width - 1) / 2;
            double oldYradius = (double)(height - 1) / 2;

            // get destination image size
            int    newWidth   = destinationData.Width;
            int    newHeight  = destinationData.Height;
            double newXradius = (double)(newWidth - 1) / 2;
            double newYradius = (double)(newHeight - 1) / 2;

            // angle's sine and cosine
            double angleRad = -angle * Math.PI / 180;
            double angleCos = Math.Cos(angleRad);
            double angleSin = Math.Sin(angleRad);

            int srcStride = sourceData.Stride;
            int dstOffset = destinationData.Stride -
                            ((destinationData.PixelFormat == PixelFormat.Format8bppIndexed) ? newWidth : newWidth * 3);

            // fill values
            byte fillR = fillColor.R;
            byte fillG = fillColor.G;
            byte fillB = fillColor.B;

            // do the job
            byte *src = (byte *)sourceData.ImageData.ToPointer( );
            byte *dst = (byte *)destinationData.ImageData.ToPointer( );

            // destination pixel's coordinate relative to image center
            double cx, cy;
            // coordinates of source points and cooefficiens
            double ox, oy, dx, dy, k1, k2;
            int    ox1, oy1, ox2, oy2;
            // destination pixel values
            double r, g, b;
            // width and height decreased by 1
            int ymax = height - 1;
            int xmax = width - 1;
            // temporary pointer
            byte *p;

            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // grayscale
                cy = -newYradius;
                for (int y = 0; y < newHeight; y++)
                {
                    cx = -newXradius;
                    for (int x = 0; x < newWidth; x++, dst++)
                    {
                        // coordinates of source point
                        ox = angleCos * cx + angleSin * cy + oldXradius;
                        oy = -angleSin * cx + angleCos * cy + oldYradius;

                        ox1 = (int)ox;
                        oy1 = (int)oy;

                        // validate source pixel's coordinates
                        if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                        {
                            // fill destination image with filler
                            *dst = fillG;
                        }
                        else
                        {
                            dx = ox - (double)ox1;
                            dy = oy - (double)oy1;

                            // initial pixel value
                            g = 0;

                            for (int n = -1; n < 3; n++)
                            {
                                // get Y cooefficient
                                k1 = Interpolation.BiCubicKernel(dy - (double)n);

                                oy2 = oy1 + n;
                                if (oy2 < 0)
                                {
                                    oy2 = 0;
                                }
                                if (oy2 > ymax)
                                {
                                    oy2 = ymax;
                                }

                                for (int m = -1; m < 3; m++)
                                {
                                    // get X cooefficient
                                    k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                    ox2 = ox1 + m;
                                    if (ox2 < 0)
                                    {
                                        ox2 = 0;
                                    }
                                    if (ox2 > xmax)
                                    {
                                        ox2 = xmax;
                                    }

                                    g += k2 * src[oy2 * srcStride + ox2];
                                }
                            }
                            *dst = (byte)Math.Max(0, Math.Min(255, g));
                        }
                        cx++;
                    }
                    cy++;
                    dst += dstOffset;
                }
            }
            else
            {
                // RGB
                cy = -newYradius;
                for (int y = 0; y < newHeight; y++)
                {
                    cx = -newXradius;
                    for (int x = 0; x < newWidth; x++, dst += 3)
                    {
                        // coordinates of source point
                        ox = angleCos * cx + angleSin * cy + oldXradius;
                        oy = -angleSin * cx + angleCos * cy + oldYradius;

                        ox1 = (int)ox;
                        oy1 = (int)oy;

                        // validate source pixel's coordinates
                        if ((ox1 < 0) || (oy1 < 0) || (ox1 >= width) || (oy1 >= height))
                        {
                            // fill destination image with filler
                            dst[RGB.R] = fillR;
                            dst[RGB.G] = fillG;
                            dst[RGB.B] = fillB;
                        }
                        else
                        {
                            dx = ox - (float)ox1;
                            dy = oy - (float)oy1;

                            // initial pixel value
                            r = g = b = 0;

                            for (int n = -1; n < 3; n++)
                            {
                                // get Y cooefficient
                                k1 = Interpolation.BiCubicKernel(dy - (float)n);

                                oy2 = oy1 + n;
                                if (oy2 < 0)
                                {
                                    oy2 = 0;
                                }
                                if (oy2 > ymax)
                                {
                                    oy2 = ymax;
                                }

                                for (int m = -1; m < 3; m++)
                                {
                                    // get X cooefficient
                                    k2 = k1 * Interpolation.BiCubicKernel((float)m - dx);

                                    ox2 = ox1 + m;
                                    if (ox2 < 0)
                                    {
                                        ox2 = 0;
                                    }
                                    if (ox2 > xmax)
                                    {
                                        ox2 = xmax;
                                    }

                                    // get pixel of original image
                                    p = src + oy2 * srcStride + ox2 * 3;

                                    r += k2 * p[RGB.R];
                                    g += k2 * p[RGB.G];
                                    b += k2 * p[RGB.B];
                                }
                            }
                            dst[RGB.R] = (byte)Math.Max(0, Math.Min(255, r));
                            dst[RGB.G] = (byte)Math.Max(0, Math.Min(255, g));
                            dst[RGB.B] = (byte)Math.Max(0, Math.Min(255, b));
                        }
                        cx++;
                    }
                    cy++;
                    dst += dstOffset;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            // get source image size
            int width  = sourceData.Width;
            int height = sourceData.Height;

            int    pixelSize = sourceData.PixelSize;
            int    srcStride = sourceData.Stride;
            int    dstOffset = destinationData.Offset;
            double xFactor   = (double)width / newWidth;
            double yFactor   = (double)height / newHeight;

            // do the job
            byte *src = (byte *)sourceData.ImageData.ToPointer();
            byte *dst = (byte *)destinationData.ImageData.ToPointer();

            // width and height decreased by 1
            int ymax = height - 1;
            int xmax = width - 1;

            // check pixel format
            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // grayscale
                for (int y = 0; y < newHeight; y++)
                {
                    // Y coordinates
                    double oy  = (double)y * yFactor - 0.5;
                    int    oy1 = (int)oy;
                    double dy  = oy - (double)oy1;

                    for (int x = 0; x < newWidth; x++, dst++)
                    {
                        // X coordinates
                        double ox  = (double)x * xFactor - 0.5f;
                        int    ox1 = (int)ox;
                        double dx  = ox - (double)ox1;

                        // initial pixel value
                        double g = 0;

                        for (int n = -1; n < 3; n++)
                        {
                            // get Y cooefficient
                            double k1 = Interpolation.BiCubicKernel(dy - (double)n);

                            int oy2 = oy1 + n;
                            if (oy2 < 0)
                            {
                                oy2 = 0;
                            }
                            if (oy2 > ymax)
                            {
                                oy2 = ymax;
                            }

                            for (int m = -1; m < 3; m++)
                            {
                                // get X cooefficient
                                double k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                int ox2 = ox1 + m;
                                if (ox2 < 0)
                                {
                                    ox2 = 0;
                                }
                                if (ox2 > xmax)
                                {
                                    ox2 = xmax;
                                }

                                g += k2 * src[oy2 * srcStride + ox2];
                            }
                        }
                        *dst = (byte)Math.Max(0, Math.Min(255, g));
                    }
                    dst += dstOffset;
                }
            }
            else if (pixelSize == 3)
            {
                // RGB
                for (int y = 0; y < newHeight; y++)
                {
                    // Y coordinates
                    double oy  = (double)y * yFactor - 0.5f;
                    int    oy1 = (int)oy;
                    double dy  = oy - (double)oy1;

                    for (int x = 0; x < newWidth; x++, dst += 3)
                    {
                        // X coordinates
                        double ox  = (double)x * xFactor - 0.5f;
                        int    ox1 = (int)ox;
                        double dx  = ox - (double)ox1;

                        // initial pixel value
                        double r = 0;
                        double g = 0;
                        double b = 0;

                        for (int n = -1; n < 3; n++)
                        {
                            // get Y cooefficient
                            double k1 = Interpolation.BiCubicKernel(dy - (double)n);

                            int oy2 = oy1 + n;
                            if (oy2 < 0)
                            {
                                oy2 = 0;
                            }
                            if (oy2 > ymax)
                            {
                                oy2 = ymax;
                            }

                            for (int m = -1; m < 3; m++)
                            {
                                // get X cooefficient
                                double k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                int ox2 = ox1 + m;
                                if (ox2 < 0)
                                {
                                    ox2 = 0;
                                }
                                if (ox2 > xmax)
                                {
                                    ox2 = xmax;
                                }

                                // get pixel of original image
                                byte *p = src + oy2 * srcStride + ox2 * 3;

                                r += k2 * p[RGB.R];
                                g += k2 * p[RGB.G];
                                b += k2 * p[RGB.B];
                            }
                        }

                        dst[RGB.R] = (byte)Math.Max(0, Math.Min(255, r));
                        dst[RGB.G] = (byte)Math.Max(0, Math.Min(255, g));
                        dst[RGB.B] = (byte)Math.Max(0, Math.Min(255, b));
                    }
                    dst += dstOffset;
                }
            }
            else if (pixelSize == 4)
            {
                // ARGB
                for (int y = 0; y < newHeight; y++)
                {
                    // Y coordinates
                    double oy  = (double)y * yFactor - 0.5f;
                    int    oy1 = (int)oy;
                    double dy  = oy - (double)oy1;

                    for (int x = 0; x < newWidth; x++, dst += 3)
                    {
                        // X coordinates
                        double ox  = (double)x * xFactor - 0.5f;
                        int    ox1 = (int)ox;
                        double dx  = ox - (double)ox1;

                        // initial pixel value
                        double a = 0;
                        double r = 0;
                        double g = 0;
                        double b = 0;

                        for (int n = -1; n < 3; n++)
                        {
                            // get Y cooefficient
                            double k1 = Interpolation.BiCubicKernel(dy - (double)n);

                            int oy2 = oy1 + n;
                            if (oy2 < 0)
                            {
                                oy2 = 0;
                            }
                            if (oy2 > ymax)
                            {
                                oy2 = ymax;
                            }

                            for (int m = -1; m < 3; m++)
                            {
                                // get X cooefficient
                                double k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                                int ox2 = ox1 + m;
                                if (ox2 < 0)
                                {
                                    ox2 = 0;
                                }
                                if (ox2 > xmax)
                                {
                                    ox2 = xmax;
                                }

                                // get pixel of original image
                                byte *p = src + oy2 * srcStride + ox2 * 3;

                                a += k2 * p[RGB.A];
                                r += k2 * p[RGB.R];
                                g += k2 * p[RGB.G];
                                b += k2 * p[RGB.B];
                            }
                        }

                        dst[RGB.A] = (byte)Math.Max(0, Math.Min(255, a));
                        dst[RGB.R] = (byte)Math.Max(0, Math.Min(255, r));
                        dst[RGB.G] = (byte)Math.Max(0, Math.Min(255, g));
                        dst[RGB.B] = (byte)Math.Max(0, Math.Min(255, b));
                    }
                    dst += dstOffset;
                }
            }
            else
            {
                throw new InvalidOperationException("Execution should never reach here.");
            }
        }
Exemplo n.º 5
0
        public unsafe Bitmap Apply(Bitmap srcImg)
        {
            int width  = srcImg.Width;
            int height = srcImg.Height;

            if ((this.newWidth == width) && (this.newHeight == height))
            {
                return(GodLesZ.Library.Imaging.Image.Clone(srcImg));
            }
            PixelFormat format     = (srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;
            BitmapData  bitmapdata = srcImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, format);
            Bitmap      bitmap     = (format == PixelFormat.Format8bppIndexed) ? GodLesZ.Library.Imaging.Image.CreateGrayscaleImage(this.newWidth, this.newHeight) : new Bitmap(this.newWidth, this.newHeight, format);
            BitmapData  data2      = bitmap.LockBits(new Rectangle(0, 0, this.newWidth, this.newHeight), ImageLockMode.ReadWrite, format);
            int         num3       = (format == PixelFormat.Format8bppIndexed) ? 1 : 3;
            int         stride     = bitmapdata.Stride;
            int         num5       = data2.Stride - (num3 * this.newWidth);
            float       num6       = ((float)width) / ((float)this.newWidth);
            float       num7       = ((float)height) / ((float)this.newHeight);
            byte *      numPtr     = (byte *)bitmapdata.Scan0.ToPointer();
            byte *      numPtr2    = (byte *)data2.Scan0.ToPointer();

            switch (this.method)
            {
            case InterpolationMethod.NearestNeighbor:
                for (int i = 0; i < this.newHeight; i++)
                {
                    int num9 = (int)(i * num7);
                    for (int j = 0; j < this.newWidth; j++)
                    {
                        int   num8    = (int)(j * num6);
                        byte *numPtr3 = (numPtr + (num9 * stride)) + (num8 * num3);
                        int   num12   = 0;
                        while (num12 < num3)
                        {
                            numPtr2[0] = numPtr3[0];
                            num12++;
                            numPtr2++;
                            numPtr3++;
                        }
                    }
                    numPtr2 += num5;
                }
                break;

            case InterpolationMethod.Bilinear:
            {
                int num23 = height - 1;
                int num24 = width - 1;
                for (int k = 0; k < this.newHeight; k++)
                {
                    float num14   = k * num7;
                    int   num20   = (int)num14;
                    int   num22   = (num20 == num23) ? num20 : (num20 + 1);
                    float num16   = num14 - num20;
                    float num18   = 1f - num16;
                    byte *numPtr4 = numPtr + (num20 * stride);
                    byte *numPtr5 = numPtr + (num22 * stride);
                    for (int m = 0; m < this.newWidth; m++)
                    {
                        float num13   = m * num6;
                        int   num19   = (int)num13;
                        int   num21   = (num19 == num24) ? num19 : (num19 + 1);
                        float num15   = num13 - num19;
                        float num17   = 1f - num15;
                        byte *numPtr6 = numPtr4 + (num19 * num3);
                        byte *numPtr7 = numPtr4 + (num21 * num3);
                        byte *numPtr8 = numPtr5 + (num19 * num3);
                        byte *numPtr9 = numPtr5 + (num21 * num3);
                        int   num29   = 0;
                        while (num29 < num3)
                        {
                            byte num25 = (byte)((num17 * numPtr6[0]) + (num15 * numPtr7[0]));
                            byte num26 = (byte)((num17 * numPtr8[0]) + (num15 * numPtr9[0]));
                            numPtr2[0] = (byte)((num18 * num25) + (num16 * num26));
                            num29++;
                            numPtr2++;
                            numPtr6++;
                            numPtr7++;
                            numPtr8++;
                            numPtr9++;
                        }
                    }
                    numPtr2 += num5;
                }
                break;
            }

            case InterpolationMethod.Bicubic:
            {
                float num30;
                float num31;
                float num32;
                float num33;
                float num34;
                float num35;
                float num37;
                int   num39;
                int   num40;
                int   num41;
                int   num42;
                int   num43 = height - 1;
                int   num44 = width - 1;
                if (format != PixelFormat.Format8bppIndexed)
                {
                    for (int num49 = 0; num49 < this.newHeight; num49++)
                    {
                        num31 = (num49 * num7) - 0.5f;
                        num40 = (int)num31;
                        num33 = num31 - num40;
                        int num50 = 0;
                        while (num50 < this.newWidth)
                        {
                            float num38;
                            num30 = (num50 * num6) - 0.5f;
                            num39 = (int)num30;
                            num32 = num30 - num39;
                            float num36 = num37 = num38 = 0f;
                            for (int num51 = -1; num51 < 3; num51++)
                            {
                                num34 = Interpolation.BiCubicKernel(num33 - num51);
                                num42 = num40 + num51;
                                if (num42 < 0)
                                {
                                    num42 = 0;
                                }
                                if (num42 > num43)
                                {
                                    num42 = num43;
                                }
                                for (int num52 = -1; num52 < 3; num52++)
                                {
                                    num35 = num34 * Interpolation.BiCubicKernel(num52 - num32);
                                    num41 = num39 + num52;
                                    if (num41 < 0)
                                    {
                                        num41 = 0;
                                    }
                                    if (num41 > num44)
                                    {
                                        num41 = num44;
                                    }
                                    byte *numPtr10 = (numPtr + (num42 * stride)) + (num41 * 3);
                                    num36 += num35 * numPtr10[2];
                                    num37 += num35 * numPtr10[1];
                                    num38 += num35 * numPtr10[0];
                                }
                            }
                            numPtr2[2] = (byte)num36;
                            numPtr2[1] = (byte)num37;
                            numPtr2[0] = (byte)num38;
                            num50++;
                            numPtr2 += 3;
                        }
                        numPtr2 += num5;
                    }
                    break;
                }
                for (int n = 0; n < this.newHeight; n++)
                {
                    num31 = (n * num7) - 0.5f;
                    num40 = (int)num31;
                    num33 = num31 - num40;
                    int num46 = 0;
                    while (num46 < this.newWidth)
                    {
                        num30 = (num46 * num6) - 0.5f;
                        num39 = (int)num30;
                        num32 = num30 - num39;
                        num37 = 0f;
                        for (int num47 = -1; num47 < 3; num47++)
                        {
                            num34 = Interpolation.BiCubicKernel(num33 - num47);
                            num42 = num40 + num47;
                            if (num42 < 0)
                            {
                                num42 = 0;
                            }
                            if (num42 > num43)
                            {
                                num42 = num43;
                            }
                            for (int num48 = -1; num48 < 3; num48++)
                            {
                                num35 = num34 * Interpolation.BiCubicKernel(num48 - num32);
                                num41 = num39 + num48;
                                if (num41 < 0)
                                {
                                    num41 = 0;
                                }
                                if (num41 > num44)
                                {
                                    num41 = num44;
                                }
                                num37 += num35 * numPtr[(num42 * stride) + num41];
                            }
                        }
                        numPtr2[0] = (byte)num37;
                        num46++;
                        numPtr2++;
                    }
                    numPtr2 += num5;
                }
                break;
            }
            }
            bitmap.UnlockBits(data2);
            srcImg.UnlockBits(bitmapdata);
            return(bitmap);
        }
Exemplo n.º 6
0
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int    width  = sourceData.Width;
            int    height = sourceData.Height;
            int    num    = (sourceData.PixelFormat == PixelFormat.Format8bppIndexed) ? 1 : 3;
            int    stride = sourceData.Stride;
            int    num2   = destinationData.Stride - num * newWidth;
            double num3   = (double)width / (double)newWidth;
            double num4   = (double)height / (double)newHeight;
            byte * ptr    = (byte *)sourceData.ImageData.ToPointer();
            byte * ptr2   = (byte *)destinationData.ImageData.ToPointer();
            int    num5   = height - 1;
            int    num6   = width - 1;

            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                for (int i = 0; i < newHeight; i++)
                {
                    double num7  = (double)i * num4 - 0.5;
                    int    num8  = (int)num7;
                    double num9  = num7 - (double)num8;
                    int    num10 = 0;
                    while (num10 < newWidth)
                    {
                        double num11 = (double)num10 * num3 - 0.5;
                        int    num12 = (int)num11;
                        double num13 = num11 - (double)num12;
                        double num14 = 0.0;
                        for (int j = -1; j < 3; j++)
                        {
                            double num15 = Interpolation.BiCubicKernel(num9 - (double)j);
                            int    num16 = num8 + j;
                            if (num16 < 0)
                            {
                                num16 = 0;
                            }
                            if (num16 > num5)
                            {
                                num16 = num5;
                            }
                            for (int k = -1; k < 3; k++)
                            {
                                double num17 = num15 * Interpolation.BiCubicKernel((double)k - num13);
                                int    num18 = num12 + k;
                                if (num18 < 0)
                                {
                                    num18 = 0;
                                }
                                if (num18 > num6)
                                {
                                    num18 = num6;
                                }
                                num14 += num17 * (double)(int)ptr[num16 * stride + num18];
                            }
                        }
                        *ptr2 = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num14));
                        num10++;
                        ptr2++;
                    }
                    ptr2 += num2;
                }
                return;
            }
            for (int l = 0; l < newHeight; l++)
            {
                double num7  = (double)l * num4 - 0.5;
                int    num8  = (int)num7;
                double num9  = num7 - (double)num8;
                int    num19 = 0;
                while (num19 < newWidth)
                {
                    double num11 = (double)num19 * num3 - 0.5;
                    int    num12 = (int)num11;
                    double num13 = num11 - (double)num12;
                    double num14;
                    double num20;
                    double num21 = num14 = (num20 = 0.0);
                    for (int m = -1; m < 3; m++)
                    {
                        double num15 = Interpolation.BiCubicKernel(num9 - (double)m);
                        int    num16 = num8 + m;
                        if (num16 < 0)
                        {
                            num16 = 0;
                        }
                        if (num16 > num5)
                        {
                            num16 = num5;
                        }
                        for (int n = -1; n < 3; n++)
                        {
                            double num17 = num15 * Interpolation.BiCubicKernel((double)n - num13);
                            int    num18 = num12 + n;
                            if (num18 < 0)
                            {
                                num18 = 0;
                            }
                            if (num18 > num6)
                            {
                                num18 = num6;
                            }
                            byte *ptr3 = ptr + (long)num16 * (long)stride + (long)num18 * 3L;
                            num21 += num17 * (double)(int)ptr3[2];
                            num14 += num17 * (double)(int)ptr3[1];
                            num20 += num17 * (double)(int)(*ptr3);
                        }
                    }
                    ptr2[2] = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num21));
                    ptr2[1] = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num14));
                    *ptr2 = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num20));
                    num19++;
                    ptr2 += 3;
                }
                ptr2 += num2;
            }
        }
Exemplo n.º 7
0
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int    width   = sourceData.Width;
            int    height  = sourceData.Height;
            double num     = (double)(width - 1) / 2.0;
            double num2    = (double)(height - 1) / 2.0;
            int    width2  = destinationData.Width;
            int    height2 = destinationData.Height;
            double num3    = (double)(width2 - 1) / 2.0;
            double num4    = (double)(height2 - 1) / 2.0;
            double num5    = (0.0 - angle) * System.Math.PI / 180.0;
            double num6    = System.Math.Cos(num5);
            double num7    = System.Math.Sin(num5);
            int    stride  = sourceData.Stride;
            int    num8    = destinationData.Stride - ((destinationData.PixelFormat == PixelFormat.Format8bppIndexed) ? width2 : (width2 * 3));
            byte   r       = fillColor.R;
            byte   g       = fillColor.G;
            byte   b       = fillColor.B;
            byte * ptr     = (byte *)sourceData.ImageData.ToPointer();
            byte * ptr2    = (byte *)destinationData.ImageData.ToPointer();
            int    num9    = height - 1;
            int    num10   = width - 1;
            double num11;

            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                num11 = 0.0 - num4;
                for (int i = 0; i < height2; i++)
                {
                    double num12 = 0.0 - num3;
                    int    num13 = 0;
                    while (num13 < width2)
                    {
                        double num14 = num6 * num12 + num7 * num11 + num;
                        double num15 = (0.0 - num7) * num12 + num6 * num11 + num2;
                        int    num16 = (int)num14;
                        int    num17 = (int)num15;
                        if (num16 < 0 || num17 < 0 || num16 >= width || num17 >= height)
                        {
                            *ptr2 = g;
                        }
                        else
                        {
                            double num18 = num14 - (double)num16;
                            double num19 = num15 - (double)num17;
                            double num20 = 0.0;
                            for (int j = -1; j < 3; j++)
                            {
                                double num21 = Interpolation.BiCubicKernel(num19 - (double)j);
                                int    num22 = num17 + j;
                                if (num22 < 0)
                                {
                                    num22 = 0;
                                }
                                if (num22 > num9)
                                {
                                    num22 = num9;
                                }
                                for (int k = -1; k < 3; k++)
                                {
                                    double num23 = num21 * Interpolation.BiCubicKernel((double)k - num18);
                                    int    num24 = num16 + k;
                                    if (num24 < 0)
                                    {
                                        num24 = 0;
                                    }
                                    if (num24 > num10)
                                    {
                                        num24 = num10;
                                    }
                                    num20 += num23 * (double)(int)ptr[num22 * stride + num24];
                                }
                            }
                            *ptr2 = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num20));
                        }
                        num12 += 1.0;
                        num13++;
                        ptr2++;
                    }
                    num11 += 1.0;
                    ptr2  += num8;
                }
                return;
            }
            num11 = 0.0 - num4;
            for (int l = 0; l < height2; l++)
            {
                double num12 = 0.0 - num3;
                int    num25 = 0;
                while (num25 < width2)
                {
                    double num14 = num6 * num12 + num7 * num11 + num;
                    double num15 = (0.0 - num7) * num12 + num6 * num11 + num2;
                    int    num16 = (int)num14;
                    int    num17 = (int)num15;
                    if (num16 < 0 || num17 < 0 || num16 >= width || num17 >= height)
                    {
                        ptr2[2] = r;
                        ptr2[1] = g;
                        *ptr2 = b;
                    }
                    else
                    {
                        double num18 = num14 - (double)(float)num16;
                        double num19 = num15 - (double)(float)num17;
                        double num20;
                        double num26;
                        double num27 = num20 = (num26 = 0.0);
                        for (int m = -1; m < 3; m++)
                        {
                            double num21 = Interpolation.BiCubicKernel(num19 - (double)(float)m);
                            int    num22 = num17 + m;
                            if (num22 < 0)
                            {
                                num22 = 0;
                            }
                            if (num22 > num9)
                            {
                                num22 = num9;
                            }
                            for (int n = -1; n < 3; n++)
                            {
                                double num23 = num21 * Interpolation.BiCubicKernel((double)(float)n - num18);
                                int    num24 = num16 + n;
                                if (num24 < 0)
                                {
                                    num24 = 0;
                                }
                                if (num24 > num10)
                                {
                                    num24 = num10;
                                }
                                byte *ptr3 = ptr + (long)num22 * (long)stride + (long)num24 * 3L;
                                num27 += num23 * (double)(int)ptr3[2];
                                num20 += num23 * (double)(int)ptr3[1];
                                num26 += num23 * (double)(int)(*ptr3);
                            }
                        }
                        ptr2[2] = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num27));
                        ptr2[1] = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num20));
                        *ptr2 = (byte)System.Math.Max(0.0, System.Math.Min(255.0, num26));
                    }
                    num12 += 1.0;
                    num25++;
                    ptr2 += 3;
                }
                num11 += 1.0;
                ptr2  += num8;
            }
        }
Exemplo n.º 8
0
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            if ((newWidth == width) && (newHeight == height))
            {
                // just clone the image
                return(AForge.Imaging.Image.Clone(srcImg));
            }

            PixelFormat fmt = (srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ?
                              PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, fmt);

            // create new image
            Bitmap dstImg = (fmt == PixelFormat.Format8bppIndexed) ?
                            AForge.Imaging.Image.CreateGrayscaleImage(newWidth, newHeight) :
                            new Bitmap(newWidth, newHeight, fmt);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, newWidth, newHeight),
                ImageLockMode.ReadWrite, fmt);

            int   pixelSize = (fmt == PixelFormat.Format8bppIndexed) ? 1 : 3;
            int   srcStride = srcData.Stride;
            int   dstOffset = dstData.Stride - pixelSize * newWidth;
            float xFactor   = (float)width / newWidth;
            float yFactor   = (float)height / newHeight;

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer();
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                switch (method)
                {
                case InterpolationMethod.NearestNeighbor:
                {
                    // -------------------------------------------
                    // resize using nearest neighbor interpolation
                    // -------------------------------------------

                    int   ox, oy;
                    byte *p;

                    // for each line
                    for (int y = 0; y < newHeight; y++)
                    {
                        // Y coordinate of the nearest point
                        oy = (int)(y * yFactor);

                        // for each pixel
                        for (int x = 0; x < newWidth; x++)
                        {
                            // X coordinate of the nearest point
                            ox = (int)(x * xFactor);

                            p = src + oy * srcStride + ox * pixelSize;

                            for (int i = 0; i < pixelSize; i++, dst++, p++)
                            {
                                *dst = *p;
                            }
                        }
                        dst += dstOffset;
                    }
                    break;
                }

                case InterpolationMethod.Bilinear:
                {
                    // ------------------------------------
                    // resize using  bilinear interpolation
                    // ------------------------------------

                    float ox, oy, dx1, dy1, dx2, dy2;
                    int   ox1, oy1, ox2, oy2;
                    int   ymax = height - 1;
                    int   xmax = width - 1;
                    byte  v1, v2;
                    byte *tp1, tp2;

                    byte *p1, p2, p3, p4;

                    // for each line
                    for (int y = 0; y < newHeight; y++)
                    {
                        // Y coordinates
                        oy  = (float)y * yFactor;
                        oy1 = (int)oy;
                        oy2 = (oy1 == ymax) ? oy1 : oy1 + 1;
                        dy1 = oy - (float)oy1;
                        dy2 = 1.0f - dy1;

                        // get temp pointers
                        tp1 = src + oy1 * srcStride;
                        tp2 = src + oy2 * srcStride;

                        // for each pixel
                        for (int x = 0; x < newWidth; x++)
                        {
                            // X coordinates
                            ox  = (float)x * xFactor;
                            ox1 = (int)ox;
                            ox2 = (ox1 == xmax) ? ox1 : ox1 + 1;
                            dx1 = ox - (float)ox1;
                            dx2 = 1.0f - dx1;

                            // get four points
                            p1 = tp1 + ox1 * pixelSize;
                            p2 = tp1 + ox2 * pixelSize;
                            p3 = tp2 + ox1 * pixelSize;
                            p4 = tp2 + ox2 * pixelSize;

                            // interpolate using 4 points
                            for (int i = 0; i < pixelSize; i++, dst++, p1++, p2++, p3++, p4++)
                            {
                                v1 = (byte)(dx2 * (*p1) + dx1 * (*p2));
                                v2 = (byte)(dx2 * (*p3) + dx1 * (*p4));
                                *dst = (byte)(dy2 * v1 + dy1 * v2);
                            }
                        }
                        dst += dstOffset;
                    }
                    break;
                }

                case InterpolationMethod.Bicubic:
                {
                    // ----------------------------------
                    // resize using bicubic interpolation
                    // ----------------------------------

                    float ox, oy, dx, dy, k1, k2;
                    float r, g, b;
                    int   ox1, oy1, ox2, oy2;
                    int   ymax = height - 1;
                    int   xmax = width - 1;
                    byte *p;

                    if (fmt == PixelFormat.Format8bppIndexed)
                    {
                        // grayscale
                        for (int y = 0; y < newHeight; y++)
                        {
                            // Y coordinates
                            oy  = (float)y * yFactor - 0.5f;
                            oy1 = (int)oy;
                            dy  = oy - (float)oy1;

                            for (int x = 0; x < newWidth; x++, dst++)
                            {
                                // X coordinates
                                ox  = (float)x * xFactor - 0.5f;
                                ox1 = (int)ox;
                                dx  = ox - (float)ox1;

                                g = 0;

                                for (int n = -1; n < 3; n++)
                                {
                                    k1 = Interpolation.BiCubicKernel(dy - (float)n);

                                    oy2 = oy1 + n;
                                    if (oy2 < 0)
                                    {
                                        oy2 = 0;
                                    }
                                    if (oy2 > ymax)
                                    {
                                        oy2 = ymax;
                                    }

                                    for (int m = -1; m < 3; m++)
                                    {
                                        k2 = k1 * Interpolation.BiCubicKernel((float)m - dx);

                                        ox2 = ox1 + m;
                                        if (ox2 < 0)
                                        {
                                            ox2 = 0;
                                        }
                                        if (ox2 > xmax)
                                        {
                                            ox2 = xmax;
                                        }

                                        g += k2 * src[oy2 * srcStride + ox2];
                                    }
                                }
                                *dst = (byte)g;
                            }
                            dst += dstOffset;
                        }
                    }
                    else
                    {
                        // RGB
                        for (int y = 0; y < newHeight; y++)
                        {
                            // Y coordinates
                            oy  = (float)y * yFactor - 0.5f;
                            oy1 = (int)oy;
                            dy  = oy - (float)oy1;

                            for (int x = 0; x < newWidth; x++, dst += 3)
                            {
                                // X coordinates
                                ox  = (float)x * xFactor - 0.5f;
                                ox1 = (int)ox;
                                dx  = ox - (float)ox1;

                                r = g = b = 0;

                                for (int n = -1; n < 3; n++)
                                {
                                    k1 = Interpolation.BiCubicKernel(dy - (float)n);

                                    oy2 = oy1 + n;
                                    if (oy2 < 0)
                                    {
                                        oy2 = 0;
                                    }
                                    if (oy2 > ymax)
                                    {
                                        oy2 = ymax;
                                    }

                                    for (int m = -1; m < 3; m++)
                                    {
                                        k2 = k1 * Interpolation.BiCubicKernel((float)m - dx);

                                        ox2 = ox1 + m;
                                        if (ox2 < 0)
                                        {
                                            ox2 = 0;
                                        }
                                        if (ox2 > xmax)
                                        {
                                            ox2 = xmax;
                                        }

                                        // get pixel of original image
                                        p = src + oy2 * srcStride + ox2 * 3;

                                        r += k2 * p[RGB.R];
                                        g += k2 * p[RGB.G];
                                        b += k2 * p[RGB.B];
                                    }
                                }

                                dst[RGB.R] = (byte)r;
                                dst[RGB.G] = (byte)g;
                                dst[RGB.B] = (byte)b;
                            }
                            dst += dstOffset;
                        }
                    }
                    break;
                }
                }
            }

            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            return(dstImg);
        }
Exemplo n.º 9
0
        public override void OnProcess()
        {
            _outputSize = new Size()
            {
                Width  = (int)(Source.Width * Ratio),
                Height = (int)(Source.Height * Ratio),
            };

            // get source image size
            int width  = Source.Width;
            int height = Source.Height;

            double xFactor = (double)width / _outputSize.Width;
            double yFactor = (double)height / _outputSize.Height;

            // coordinates of source points and cooefficiens
            double ox, oy, dx, dy, k1, k2;
            int    ox1, oy1, ox2, oy2;
            // destination pixel values
            double r, g, b;
            // width and height decreased by 1
            int ymax = height - 1;
            int xmax = width - 1;

            for (int y = 0; y < _outputSize.Height; y++)
            {
                // Y coordinates
                oy  = (double)y * yFactor - 0.5f;
                oy1 = (int)oy;
                dy  = oy - (double)oy1;

                for (int x = 0; x < _outputSize.Width; x++)
                {
                    // X coordinates
                    ox  = (double)x * xFactor - 0.5f;
                    ox1 = (int)ox;
                    dx  = ox - (double)ox1;

                    // initial pixel value
                    r = g = b = 0;

                    for (int n = -1; n < 3; n++)
                    {
                        // get Y cooefficient
                        k1 = Interpolation.BiCubicKernel(dy - (double)n);

                        oy2 = oy1 + n;
                        if (oy2 < 0)
                        {
                            oy2 = 0;
                        }
                        if (oy2 > ymax)
                        {
                            oy2 = ymax;
                        }

                        for (int m = -1; m < 3; m++)
                        {
                            // get X cooefficient
                            k2 = k1 * Interpolation.BiCubicKernel((double)m - dx);

                            ox2 = ox1 + m;
                            if (ox2 < 0)
                            {
                                ox2 = 0;
                            }
                            if (ox2 > xmax)
                            {
                                ox2 = xmax;
                            }

                            r += k2 * Source.Map[oy2][ox2].R;
                            g += k2 * Source.Map[oy2][ox2].G;
                            b += k2 * Source.Map[oy2][ox2].B;
                        }
                    }

                    Source.Map[y][x] = new Pixel(255, (byte)Math.Max(0, Math.Min(255, r)), (byte)Math.Max(0, Math.Min(255, g)), (byte)Math.Max(0, Math.Min(255, b)));
                }
            }

            Source.Width  = _outputSize.Width;
            Source.Height = _outputSize.Height;
        }
Exemplo n.º 10
0
        public unsafe Bitmap Apply(Bitmap srcImg)
        {
            double num8;
            double num9;
            int    num10;
            int    num11;
            int    width  = srcImg.Width;
            int    height = srcImg.Height;

            if (this.angle == 0f)
            {
                return(GodLesZ.Library.Imaging.Image.Clone(srcImg));
            }
            double d    = (-this.angle * 3.1415926535897931) / 180.0;
            double num4 = Math.Cos(d);
            double num5 = Math.Sin(d);
            double num6 = ((double)width) / 2.0;
            double num7 = ((double)height) / 2.0;

            if (this.keepSize)
            {
                num8  = num6;
                num9  = num7;
                num10 = width;
                num11 = height;
            }
            else
            {
                double num12 = num6 * num4;
                double num13 = num6 * num5;
                double num14 = (num6 * num4) - (num7 * num5);
                double num15 = (num6 * num5) + (num7 * num4);
                double num16 = -num7 * num5;
                double num17 = num7 * num4;
                double num18 = 0.0;
                double num19 = 0.0;
                num8  = Math.Max(Math.Max(num12, num14), Math.Max(num16, num18)) - Math.Min(Math.Min(num12, num14), Math.Min(num16, num18));
                num9  = Math.Max(Math.Max(num13, num15), Math.Max(num17, num19)) - Math.Min(Math.Min(num13, num15), Math.Min(num17, num19));
                num10 = (int)((num8 * 2.0) + 0.5);
                num11 = (int)((num9 * 2.0) + 0.5);
            }
            PixelFormat format     = (srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;
            BitmapData  bitmapdata = srcImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, format);
            Bitmap      bitmap     = (format == PixelFormat.Format8bppIndexed) ? GodLesZ.Library.Imaging.Image.CreateGrayscaleImage(num10, num11) : new Bitmap(num10, num11, format);
            BitmapData  data2      = bitmap.LockBits(new Rectangle(0, 0, num10, num11), ImageLockMode.ReadWrite, format);
            int         stride     = bitmapdata.Stride;
            int         num21      = data2.Stride - ((format == PixelFormat.Format8bppIndexed) ? num10 : (num10 * 3));
            byte        r          = this.fillColor.R;
            byte        g          = this.fillColor.G;
            byte        b          = this.fillColor.B;
            byte *      numPtr     = (byte *)bitmapdata.Scan0.ToPointer();
            byte *      numPtr2    = (byte *)data2.Scan0.ToPointer();

            switch (this.method)
            {
            case InterpolationMethod.NearestNeighbor:
                double num25;
                double num26;
                int    num27;
                int    num28;
                if (format != PixelFormat.Format8bppIndexed)
                {
                    num26 = -num9;
                    for (int j = 0; j < num11; j++)
                    {
                        num25 = -num8;
                        int num32 = 0;
                        while (num32 < num10)
                        {
                            num27 = (int)(((num4 * num25) + (num5 * num26)) + num6);
                            num28 = (int)(((-num5 * num25) + (num4 * num26)) + num7);
                            if (((num27 < 0) || (num28 < 0)) || ((num27 >= width) || (num28 >= height)))
                            {
                                numPtr2[2] = r;
                                numPtr2[1] = g;
                                numPtr2[0] = b;
                            }
                            else
                            {
                                byte *numPtr3 = (numPtr + (num28 * stride)) + (num27 * 3);
                                numPtr2[2] = numPtr3[2];
                                numPtr2[1] = numPtr3[1];
                                numPtr2[0] = numPtr3[0];
                            }
                            num25++;
                            num32++;
                            numPtr2 += 3;
                        }
                        num26++;
                        numPtr2 += num21;
                    }
                    break;
                }
                num26 = -num9;
                for (int i = 0; i < num11; i++)
                {
                    num25 = -num8;
                    int num30 = 0;
                    while (num30 < num10)
                    {
                        num27 = (int)(((num4 * num25) + (num5 * num26)) + num6);
                        num28 = (int)(((-num5 * num25) + (num4 * num26)) + num7);
                        if (((num27 < 0) || (num28 < 0)) || ((num27 >= width) || (num28 >= height)))
                        {
                            numPtr2[0] = g;
                        }
                        else
                        {
                            numPtr2[0] = numPtr[(num28 * stride) + num27];
                        }
                        num25++;
                        num30++;
                        numPtr2++;
                    }
                    num26++;
                    numPtr2 += num21;
                }
                break;

            case InterpolationMethod.Bilinear:
            {
                double num33;
                double num34;
                float  num35;
                float  num36;
                float  num37;
                float  num38;
                float  num39;
                float  num40;
                int    num41;
                int    num42;
                int    num43;
                int    num44;
                byte   num47;
                byte   num48;
                byte * numPtr4;
                byte * numPtr5;
                int    num45 = height - 1;
                int    num46 = width - 1;
                if (format != PixelFormat.Format8bppIndexed)
                {
                    num34 = -num9;
                    for (int m = 0; m < num11; m++)
                    {
                        num33 = -num8;
                        int num52 = 0;
                        while (num52 < num10)
                        {
                            num35 = (float)(((num4 * num33) + (num5 * num34)) + num6);
                            num36 = (float)(((-num5 * num33) + (num4 * num34)) + num7);
                            num41 = (int)num35;
                            num42 = (int)num36;
                            if (((num41 < 0) || (num42 < 0)) || ((num41 >= width) || (num42 >= height)))
                            {
                                numPtr2[2] = r;
                                numPtr2[1] = g;
                                numPtr2[0] = b;
                            }
                            else
                            {
                                byte *numPtr7;
                                num43 = (num41 == num46) ? num41 : (num41 + 1);
                                num44 = (num42 == num45) ? num42 : (num42 + 1);
                                num37 = num35 - num41;
                                if (num37 < 0f)
                                {
                                    num37 = 0f;
                                }
                                num39 = 1f - num37;
                                num38 = num36 - num42;
                                if (num38 < 0f)
                                {
                                    num38 = 0f;
                                }
                                num40    = 1f - num38;
                                numPtr4  = numPtr5 = numPtr + (num42 * stride);
                                numPtr4 += num41 * 3;
                                numPtr5 += num43 * 3;
                                byte *numPtr6 = numPtr7 = numPtr + (num44 * stride);
                                numPtr6   += num41 * 3;
                                numPtr7   += num43 * 3;
                                num47      = (byte)((num39 * numPtr4[2]) + (num37 * numPtr5[2]));
                                num48      = (byte)((num39 * numPtr6[2]) + (num37 * numPtr7[2]));
                                numPtr2[2] = (byte)((num40 * num47) + (num38 * num48));
                                num47      = (byte)((num39 * numPtr4[1]) + (num37 * numPtr5[1]));
                                num48      = (byte)((num39 * numPtr6[1]) + (num37 * numPtr7[1]));
                                numPtr2[1] = (byte)((num40 * num47) + (num38 * num48));
                                num47      = (byte)((num39 * numPtr4[0]) + (num37 * numPtr5[0]));
                                num48      = (byte)((num39 * numPtr6[0]) + (num37 * numPtr7[0]));
                                numPtr2[0] = (byte)((num40 * num47) + (num38 * num48));
                            }
                            num33++;
                            num52++;
                            numPtr2 += 3;
                        }
                        num34++;
                        numPtr2 += num21;
                    }
                    break;
                }
                num34 = -num9;
                for (int k = 0; k < num11; k++)
                {
                    num33 = -num8;
                    int num50 = 0;
                    while (num50 < num10)
                    {
                        num35 = (float)(((num4 * num33) + (num5 * num34)) + num6);
                        num36 = (float)(((-num5 * num33) + (num4 * num34)) + num7);
                        num41 = (int)num35;
                        num42 = (int)num36;
                        if (((num41 < 0) || (num42 < 0)) || ((num41 >= width) || (num42 >= height)))
                        {
                            numPtr2[0] = g;
                        }
                        else
                        {
                            num43 = (num41 == num46) ? num41 : (num41 + 1);
                            num44 = (num42 == num45) ? num42 : (num42 + 1);
                            num37 = num35 - num41;
                            if (num37 < 0f)
                            {
                                num37 = 0f;
                            }
                            num39 = 1f - num37;
                            num38 = num36 - num42;
                            if (num38 < 0f)
                            {
                                num38 = 0f;
                            }
                            num40      = 1f - num38;
                            numPtr4    = numPtr + (num42 * stride);
                            numPtr5    = numPtr + (num44 * stride);
                            num47      = (byte)((num39 * numPtr4[num41]) + (num37 * numPtr4[num43]));
                            num48      = (byte)((num39 * numPtr5[num41]) + (num37 * numPtr5[num43]));
                            numPtr2[0] = (byte)((num40 * num47) + (num38 * num48));
                        }
                        num33++;
                        num50++;
                        numPtr2++;
                    }
                    num34++;
                    numPtr2 += num21;
                }
                break;
            }

            case InterpolationMethod.Bicubic:
            {
                double num53;
                double num54;
                float  num55;
                float  num56;
                float  num57;
                float  num58;
                float  num59;
                float  num60;
                float  num62;
                int    num64;
                int    num65;
                int    num66;
                int    num67;
                int    num68 = height - 1;
                int    num69 = width - 1;
                if (format != PixelFormat.Format8bppIndexed)
                {
                    num54 = -num9;
                    for (int num74 = 0; num74 < num11; num74++)
                    {
                        num53 = -num8;
                        int num75 = 0;
                        while (num75 < num10)
                        {
                            num55 = (float)(((num4 * num53) + (num5 * num54)) + num6);
                            num56 = (float)(((-num5 * num53) + (num4 * num54)) + num7);
                            num64 = (int)num55;
                            num65 = (int)num56;
                            if (((num64 < 0) || (num65 < 0)) || ((num64 >= width) || (num65 >= height)))
                            {
                                numPtr2[2] = r;
                                numPtr2[1] = g;
                                numPtr2[0] = b;
                            }
                            else
                            {
                                float num63;
                                num57 = num55 - num64;
                                num58 = num56 - num65;
                                float num61 = num62 = num63 = 0f;
                                for (int num76 = -1; num76 < 3; num76++)
                                {
                                    num59 = Interpolation.BiCubicKernel(num58 - num76);
                                    num67 = num65 + num76;
                                    if (num67 < 0)
                                    {
                                        num67 = 0;
                                    }
                                    if (num67 > num68)
                                    {
                                        num67 = num68;
                                    }
                                    for (int num77 = -1; num77 < 3; num77++)
                                    {
                                        num60 = num59 * Interpolation.BiCubicKernel(num77 - num57);
                                        num66 = num64 + num77;
                                        if (num66 < 0)
                                        {
                                            num66 = 0;
                                        }
                                        if (num66 > num69)
                                        {
                                            num66 = num69;
                                        }
                                        byte *numPtr8 = (numPtr + (num67 * stride)) + (num66 * 3);
                                        num61 += num60 * numPtr8[2];
                                        num62 += num60 * numPtr8[1];
                                        num63 += num60 * numPtr8[0];
                                    }
                                }
                                numPtr2[2] = (byte)num61;
                                numPtr2[1] = (byte)num62;
                                numPtr2[0] = (byte)num63;
                            }
                            num53++;
                            num75++;
                            numPtr2 += 3;
                        }
                        num54++;
                        numPtr2 += num21;
                    }
                    break;
                }
                num54 = -num9;
                for (int n = 0; n < num11; n++)
                {
                    num53 = -num8;
                    int num71 = 0;
                    while (num71 < num10)
                    {
                        num55 = (float)(((num4 * num53) + (num5 * num54)) + num6);
                        num56 = (float)(((-num5 * num53) + (num4 * num54)) + num7);
                        num64 = (int)num55;
                        num65 = (int)num56;
                        if (((num64 < 0) || (num65 < 0)) || ((num64 >= width) || (num65 >= height)))
                        {
                            numPtr2[0] = g;
                        }
                        else
                        {
                            num57 = num55 - num64;
                            num58 = num56 - num65;
                            num62 = 0f;
                            for (int num72 = -1; num72 < 3; num72++)
                            {
                                num59 = Interpolation.BiCubicKernel(num58 - num72);
                                num67 = num65 + num72;
                                if (num67 < 0)
                                {
                                    num67 = 0;
                                }
                                if (num67 > num68)
                                {
                                    num67 = num68;
                                }
                                for (int num73 = -1; num73 < 3; num73++)
                                {
                                    num60 = num59 * Interpolation.BiCubicKernel(num73 - num57);
                                    num66 = num64 + num73;
                                    if (num66 < 0)
                                    {
                                        num66 = 0;
                                    }
                                    if (num66 > num69)
                                    {
                                        num66 = num69;
                                    }
                                    num62 += num60 * numPtr[(num67 * stride) + num66];
                                }
                            }
                            numPtr2[0] = (byte)num62;
                        }
                        num53++;
                        num71++;
                        numPtr2++;
                    }
                    num54++;
                    numPtr2 += num21;
                }
                break;
            }
            }
            bitmap.UnlockBits(data2);
            srcImg.UnlockBits(bitmapdata);
            return(bitmap);
        }