将不规则四边形图像插值成为一个矩形图像

下面是在qt中将不规则四边形图像插值成为一个矩形图像的示例代码:

static QImage generateInterpolatedPixmap(
const QImage& vi_img, 
const double vi_scaleFactor, 
const QPointF vi_ctrlPoint1, 
const QPointF vi_ctrlPoint2, 
const QPointF vi_ctrlPoint3,
 const QPointF vi_ctrlPoint4)
    {
        double x1=vi_ctrlPoint1.x()*vi_scaleFactor, 
            x2=vi_ctrlPoint2.x()*vi_scaleFactor,
            x3=vi_ctrlPoint3.x()*vi_scaleFactor,
            x4=vi_ctrlPoint4.x()*vi_scaleFactor;
        double y1=vi_ctrlPoint1.y()*vi_scaleFactor, 
            y2=vi_ctrlPoint2.y()*vi_scaleFactor,
            y3=vi_ctrlPoint3.y()*vi_scaleFactor,
            y4=vi_ctrlPoint4.y()*vi_scaleFactor;
        double inImgWidth = (x2+x3-x1-x4)/2.0;
        double inImgHeight = (y3+y4-y1-y2)/2.0;

        if (inImgWidth<=0||inImgHeight<=0)
        {
            QImage img;
            return img;
        }
        double imgWidth = int(inImgWidth);
        double imgHeight = int(inImgHeight);

        QSize imgSize(imgWidth, imgHeight);
        QImage img(imgSize, QImage::Format_ARGB32);

        for (double a = -1.0; a<1.0; a+= 2.0/imgWidth)
        {
            for (double b = -1.0; b<1.0; b+= 2.0/imgHeight)
            {
                int imgIndexX = (a+1)/2.0 * imgWidth;
                int imgIndexY = (b+1)/2.0 * imgHeight;

                double N1 = 1/4.0 *(1-a)*(1-b);
                double N2 = 1/4.0 *(1+a)*(1-b);
                double N3 = 1/4.0 *(1+a)*(1+b);
                double N4 = 1/4.0 *(1-a)*(1+b);

                int inImgIndexX = N1*x1 + N2*x2 + N3*x3 + N4*x4;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                int inImgIndexY = N1*y1 + N2*y2 + N3*y3 + N4*y4;

                if (inImgIndexX>=0
                    &&inImgIndexX=0
                    &&inImgIndexY=0
                    &&imgIndexY>=0
                    &&imgIndexXreturn img;
    }

你可能感兴趣的:(图形图像)