Exemple #1
0
 // Use this for initialization
 void Start()
 {
     m_tMajor.position = m_cBeizerLine.GetPoint(0f);
     m_tMajor.forward  = m_cBeizerLine.GetDir(0f);
 }
        public unsafe static Gdi::Bitmap Transform(
            Gdi::Bitmap src, int width, int height,
            Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4,
            Vector2 q1, Vector2 q2, Vector2 q3, Vector2 q4
            )
        {
            Gdi::Bitmap    dst     = new Gdi::Bitmap(width, height);
            Gdi::Rectangle dstRect = new Gdi::Rectangle(Gdi::Point.Empty, dst.Size);

            Gdi::Imaging.BitmapData dstData = dst.LockBits(dstRect, Gdi::Imaging.ImageLockMode.WriteOnly, Gdi::Imaging.PixelFormat.Format32bppRgb);
            byte *pdst   = (byte *)dstData.Scan0;
            int   stride = dstData.Stride;

            double[] x_st_table1 = new double[width];
            double[] x_st_table2 = new double[width];
            double[] y_st_table1 = new double[height];
            double[] y_st_table2 = new double[height];
            new BezierArcMeasure(p1, p2, q1).InitializeTable(x_st_table1);
            new BezierArcMeasure(p4, p3, q3).InitializeTable(x_st_table2);
            new BezierArcMeasure(p1, p4, q4).InitializeTable(y_st_table1);
            new BezierArcMeasure(p2, p3, q2).InitializeTable(y_st_table2);

            BezierLine p12 = new BezierLine(p1, p2, q1);
            BezierLine p43 = new BezierLine(p4, p3, q3);
            BezierLine p14 = new BezierLine(p1, p4, q4);
            BezierLine p23 = new BezierLine(p2, p3, q2);

            using (BicubicInterpolatedImage srcImage = new BicubicInterpolatedImage(src)){
                for (int ix = 0; ix < width; ix++)
                {
                    double xt1 = x_st_table1[ix];
                    double xt2 = x_st_table2[ix];
                    double dxt = xt2 - xt1;
                    for (int iy = 0; iy < height; iy++)
                    {
                        double yt1 = y_st_table1[iy];
                        double yt2 = y_st_table2[iy];
                        double dyt = yt2 - yt1;
                        double xt  = (xt1 + yt1 * dxt) / (1 - dxt * dyt);
                        double yt  = (yt1 + xt1 * dyt) / (1 - dxt * dyt);

                        // linear interpolation
                        Vector2 pl
                            = (1 - xt1) * (1 - yt1) * p1
                              + xt1 * (1 - yt2) * p2
                              + xt2 * yt2 * p3
                              + (1 - xt2) * yt1 * p4;

                        // bezier interpolation
                        Vector2 pBx = p43.GetPoint(xt2) * yt + p12.GetPoint(xt1) * (1 - yt);
                        Vector2 pBy = p23.GetPoint(yt2) * xt + p14.GetPoint(yt1) * (1 - xt);

                        Vector2 p = pBx + pBy - pl;
                        *(int *)(pdst + 4 * ix + stride * iy) = srcImage.Get(p.x, p.y);
                    }
                }
            }

            dst.UnlockBits(dstData);
            return(dst);
        }