#include "outline.h"

OutLine::OutLine(QObject *parent) : QObject(parent)
{
    m_maxX = 0;
    m_minX = 0;
    m_maxY = 0;
    m_minY = 0;
}

void OutLine::setRangeXY(int maxX,int minX,int maxY,int minY)
{
    m_maxX = maxX;
    m_minX = minX;
    m_maxY = maxY;
    m_minY = minY;
}

//获取花样轮廓
QList<DsrStep> OutLine::getOutLine(QImage *img)
{
    int distance_lunkuo = 10;

    unsigned int *data = (unsigned int *)img->bits();

    int x = img->width();
    //int y = img->height();

    //记录x，y最大最小范围
    int x_max = 0;
    int x_min = 1000000;
    int y_max = 0;
    int y_min = 1000000;

    //当前点x，y坐标
    int x_now = 0;
    int y_now = 0;

    int red;
    //int green;
    //int blue;

    int size = img->width() * (img->height() - 12);

    for (int i = 1; i < size; i++)
    {
        red = qRed(data[i]);
        //green= qGreen(data[i]);
        //blue= qBlue(data[i]);

        if (red == 255 && qRed(data[i-1]) == 0)
        {
            y_now = i / x;
            x_now = i % x;

            if (y_now > y_max)
            {
                y_max = y_now;
            }

            if (y_now < y_min)
            {
                y_min = y_now;
            }

            if(x_now < x_min)
            {
                x_min = x_now;
            }
        }

        if (red == 0 && qRed(data[i - 1]) == 255)
        {
            x_now = i % x;

            if(x_now > x_max)
            {
                x_max = x_now;
            }
        }
    }

    qDebug()<<"x_max:"<<x_max;
    qDebug()<<"x_min:"<<x_min;
    qDebug()<<"y_max:"<<y_max;
    qDebug()<<"y_min:"<<y_min;

    //画4条线用于检测轮廓
    QStringList list_x_up;//最上面的线
    QStringList list_y_up;
    QStringList list_x_down;//最下面的线
    QStringList list_y_down;
    QStringList list_x_right;//最右面的线
    QStringList list_y_right;
    QStringList list_x_left;//最左面的线
    QStringList list_y_left;

    QStringList list_x_contour;//轮廓坐标
    QStringList list_y_contour;
    QStringList list_point_true;//判断点是否有效

    QStringList list_drawwhit_x;
    QStringList list_drawwhit_y;

    //上面的线
    for (int i = x_min ; i <= x_max ; i++ )
    {
        list_x_up.append(QString("%1").arg(i));
        list_y_up.append(QString("%1").arg(y_min - 6));
    }

    //右边的线
    for (int i = y_min ; i <= y_max ; i++)
    {
        list_x_right.append(QString("%1").arg(x_max + 6));
        list_y_right.append(QString("%1").arg(i));
    }

    //下边的线
    for (int i = x_max ; i >= x_min ; i--)
    {
        list_x_down.append(QString("%1").arg(i));
        list_y_down.append(QString("%1").arg(y_max + 6));
    }

    //左边的线
    for (int i = y_max ; i >= y_min ; i--)
    {
        list_x_left.append(QString("%1").arg(x_min - 6));
        list_y_left.append(QString("%1").arg(i));
    }

    QStringList list_X_temp1;
    QStringList list_Y_temp1;

    list_X_temp1.clear();
    list_Y_temp1.clear();

    // 最上面的点向下索引
    for (int i = 0; i < list_x_up.size(); i++)
    {
        QString str_x = list_x_up.at(i);
        QString str_y = list_y_up.at(i);
        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int location = point_y * x + point_x;
        red = qRed(data[location]);
        if (red == 255)
        {

        }
        else
        {
            //黑点，向下搜索
            for (int j = 1; j < (y_max - point_y + 1) / 2; j++)
            {
                //循环向下搜索，找到分界点
                if (qRed(data[(point_y + j) * x + point_x]) == 255)
                {
                    list_y_up[i] = QString("%1").arg(point_y + j - 1);
                    list_X_temp1.append(list_x_up.at(i));
                    list_Y_temp1.append(list_y_up.at(i));
                    break;
                }
            }
        }
    }
    list_x_up.clear();
    list_x_up = list_X_temp1;
    list_y_up.clear();
    list_y_up = list_Y_temp1;

    list_X_temp1.clear();
    list_Y_temp1.clear();

    //最右面的线向左索引
    for (int i = 0; i < list_y_right.size(); i++)
    {
        QString str_x = list_x_right.at(i);
        QString str_y = list_y_right.at(i);
        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int location = point_y * x + point_x;
        red = qRed(data[location]);

        if (red == 255)
        {

        }
        else
        {
            //黑色，向左搜索
            for (int j = 1; j < (point_x) / 2; j++)
            {
                //循环向左搜索，找到分界点
                if (qRed(data[point_y * x + point_x - j]) == 255)
                {
                    list_x_right[i] = QString("%1").arg(point_x - j + 1);
                    list_X_temp1.append(list_x_right.at(i));
                    list_Y_temp1.append(list_y_right.at(i));
                    break;
                }
            }
        }
    }
    list_x_right.clear();
    list_x_right = list_X_temp1;
    list_y_right.clear();
    list_y_right = list_Y_temp1;

    list_X_temp1.clear();
    list_Y_temp1.clear();

    //最下面的线向上索引
    for (int i = 0; i < list_x_down.size(); i++)
    {
        QString str_x = list_x_down.at(i);
        QString str_y = list_y_down.at(i);
        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int location = point_y * x + point_x;
        red = qRed(data[location]);

        if (red == 255)
        {

        }
        else
        {
            //黑点，向上搜索
            for (int j = 1; j < (point_y ) / 2; j++)
            {
                //循环向上搜索，找到分界点
                if (qRed(data[(point_y - j) * x + point_x]) == 255)
                {
                    list_y_down[i] = QString("%1").arg(point_y - j + 1);
                    list_X_temp1.append(list_x_down.at(i));
                    list_Y_temp1.append(list_y_down.at(i));
                    break;
                }
            }
        }
    }
    list_x_down.clear();
    list_x_down = list_X_temp1;
    list_y_down.clear();
    list_y_down = list_Y_temp1;

    list_X_temp1.clear();
    list_Y_temp1.clear();

    //最左面的线向右索引
    for (int i = 0; i < list_x_left.size(); i++)
    {
        QString str_x = list_x_left.at(i);
        QString str_y = list_y_left.at(i);
        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int location = point_y * x + point_x;
        red = qRed(data[location]);

        if (red == 255)
        {

        }
        else
        {
            //黑色，向右搜索
            for (int j = 1; j < (y_max) / 2; j++)
            {
                //循环向右搜索，找到分界点
                if (qRed(data[point_y * x + point_x + j]) == 255)
                {
                    list_x_left[i] = QString("%1").arg(point_x + j - 1);
                    list_X_temp1.append(list_x_left.at(i));
                    list_Y_temp1.append(list_y_left.at(i));
                    break;
                }
            }
        }
    }
    list_x_left.clear();
    list_x_left = list_X_temp1;
    list_y_left.clear();
    list_y_left = list_Y_temp1;


    QList <int> list_int_loaction;
    int int_location = 0;
    QStringList listXTemp;
    QStringList listYTemp;
    list_int_loaction.clear();
    for (int i = 0; i < list_x_up.size(); i++)
    {
        for (int j = 0; j < list_x_right.size(); j++)
        {
            if (list_x_up.at(i) == list_x_right.at(j) && list_y_up.at(i) == list_y_right.at(j))
            {
                list_int_loaction.append(i);
                break;
            }
        }
    }
    int_location = list_x_up.size() - 1;
    if (list_int_loaction.size() > 0)
    {
        int_location = list_int_loaction.at(list_int_loaction.size()-1);
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = 0; i <= int_location; i++)
    {
        listXTemp.append(list_x_up.at(i));
        listYTemp.append(list_y_up.at(i));
    }
    list_x_up.clear();
    list_x_up = listXTemp;
    list_y_up.clear();
    list_y_up = listYTemp;
    int_location = 0;
    for (int i = 0; i < list_x_right.size(); i++)
    {
        if (list_x_right.at(i) == list_x_up.at(list_x_up.size()-1) && list_y_right.at(i) == list_y_up.at(list_y_up.size()-1))
        {
            int_location = i;
        }
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = int_location; i < list_x_right.size(); i++)
    {
        listXTemp.append(list_x_right.at(i));
        listYTemp.append(list_y_right.at(i));
    }
    list_x_right.clear();
    list_x_right = listXTemp;
    list_y_right.clear();
    list_y_right = listYTemp;

    list_int_loaction.clear();
    for (int i = 0; i < list_x_right.size(); i++)
    {
        for (int j = 0; j < list_x_down.size(); j++)
        {
            if (list_x_right.at(i) == list_x_down.at(j) && list_y_right.at(i) == list_y_down.at(j))
            {
                list_int_loaction.append(i);
                break;
            }
        }
    }
    int_location = list_x_right.size() - 1;
    if (list_int_loaction.size() > 0)
    {
        int_location = list_int_loaction.at(list_int_loaction.size()-1);
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = 0; i <= int_location; i++)
    {
        listXTemp.append(list_x_right.at(i));
        listYTemp.append(list_y_right.at(i));
    }
    list_x_right.clear();
    list_x_right = listXTemp;
    list_y_right.clear();
    list_y_right = listYTemp;
    int_location = 0;
    for (int i = 0; i < list_x_down.size(); i++)
    {
        if (list_x_down.at(i) == list_x_right.at(list_x_right.size()-1) && list_y_down.at(i) == list_y_right.at(list_y_right.size()-1))
        {
            int_location = i;
        }
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = int_location; i < list_x_down.size(); i++)
    {
        listXTemp.append(list_x_down.at(i));
        listYTemp.append(list_y_down.at(i));
    }
    list_x_down.clear();
    list_x_down = listXTemp;
    list_y_down.clear();
    list_y_down = listYTemp;

    list_int_loaction.clear();
    for (int i = 0; i < list_x_down.size(); i++)
    {
        for (int j = 0; j < list_x_left.size(); j++)
        {
            if (list_x_down.at(i) == list_x_left.at(j) && list_y_down.at(i) == list_y_left.at(j))
            {
                list_int_loaction.append(i);
                break;
            }
        }
    }
    int_location = list_x_down.size() - 1;
    if (list_int_loaction.size() > 0)
    {
        int_location = list_int_loaction.at(list_int_loaction.size()-1);
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = 0; i <= int_location; i++)
    {
        listXTemp.append(list_x_down.at(i));
        listYTemp.append(list_y_down.at(i));
    }
    list_x_down.clear();
    list_x_down = listXTemp;
    list_y_down.clear();
    list_y_down = listYTemp;
    int_location = 0;
    for (int i = 0; i < list_x_left.size(); i++)
    {
        if (list_x_left.at(i) == list_x_down.at(list_x_down.size()-1) && list_y_left.at(i) == list_y_down.at(list_y_down.size()-1))
        {
            int_location = i;
        }
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = int_location; i < list_x_left.size(); i++)
    {
        listXTemp.append(list_x_left.at(i));
        listYTemp.append(list_y_left.at(i));
    }
    list_x_left.clear();
    list_x_left = listXTemp;
    list_y_left.clear();
    list_y_left = listYTemp;
#if 1
    list_int_loaction.clear();
    for (int i = 0; i < list_x_left.size(); i++)
    {
        for (int j = 0; j < list_x_up.size(); j++)
        {
            if (list_x_left.at(i) == list_x_up.at(j) && list_y_left.at(i) == list_y_up.at(j))
            {
                list_int_loaction.append(i);
                break;
            }
        }
    }
    int_location = list_x_left.size() - 1;
    if (list_int_loaction.size() > 0)
    {
        int_location = list_int_loaction.at(list_int_loaction.size()-1);
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = 0; i <= int_location; i++)
    {
        listXTemp.append(list_x_left.at(i));
        listYTemp.append(list_y_left.at(i));
    }
    list_x_left.clear();
    list_x_left = listXTemp;
    list_y_left.clear();
    list_y_left = listYTemp;
    int_location = 0;
    for (int i = 0; i < list_x_up.size(); i++)
    {
        if (list_x_up.at(i) == list_x_left.at(list_x_left.size()-1) && list_y_up.at(i) == list_y_left.at(list_y_left.size()-1))
        {
            int_location = i;
        }
    }
    listXTemp.clear();
    listYTemp.clear();
    for (int i = int_location; i < list_x_up.size(); i++)
    {
        listXTemp.append(list_x_up.at(i));
        listYTemp.append(list_y_up.at(i));
    }
    list_x_up.clear();
    list_x_up = listXTemp;
    list_y_up.clear();
    list_y_up = listYTemp;
#endif

#if 0

    //最上面的线向下索引
    for (int i = 0; i < list_x_up.size(); i++)
    {
        QString str_x = list_x_up.at(i);
        QString str_y = list_y_up.at(i);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();

        int location = point_y * x + point_x;

        red = qRed(data[location]);
        //green= qGreen(data[location]);
        //blue= qBlue(data[location]);

        if (red == 255)
        {
            //白点，为分界线
            list_x_contour.append(QString("%1").arg(point_x));
            list_y_contour.append(QString("%1").arg(point_y - distance_lunkuo));
            list_point_true.append(QString("%1").arg(1));
        }
        else
        {
            //黑点，向下搜索
            for (int j = 1; j < (y_max - point_y + 1); j++)
            {
                //循环向下搜索，找到分界点
                if (qRed(data[(point_y + j) * x + point_x]) == 255)
                {
                    list_drawwhit_x.append(QString("%1").arg(point_x));
                    list_drawwhit_y.append(QString("%1").arg(point_y + j));

                    if (list_x_contour.size() > 0)
                    {
                        //如果不是起始点，判断与上一个点的位置关系
                        //QString last_x_contour = list_x_contour.at(list_x_contour.size()-1);
                        QString last_y_contour = list_y_contour.at(list_y_contour.size()-1);

                        //int point_last_x_contour = last_x_contour.toInt();
                        int point_last_y_contour = last_y_contour.toInt() + distance_lunkuo;

                        if (0)
                            //if ((point_last_y_contour - (point_y + j)) > (10) || (point_last_y_contour - (point_y + j)) < -10)
                        {
                            list_x_contour.append(QString("%1").arg(point_x));
                            list_y_contour.append(QString("%1").arg(point_last_y_contour - distance_lunkuo));
                        }
                        else
                        {
                            list_x_contour.append(QString("%1").arg(point_x));
                            list_y_contour.append(QString("%1").arg(point_y + j - distance_lunkuo));
                        }

                        QString str_check_x = list_x_contour.at(list_x_contour.size()-1);
                        QString str_check_y = list_y_contour.at(list_y_contour.size()-1);

                        int point_check_x = str_check_x.toInt();
                        int point_check_y = str_check_y.toInt();

                        for (int check = 0; check <= 5; check++)
                        {
                            //判断当前点是否可用，是否需要移动
                            if (qRed(data[point_check_y * x + point_check_x + check]) == 255 )
                            {
                                //需要向右移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo * 2; check_1++)
                                {
                                    if (qRed(data[point_check_y * x + point_check_x + check - check_1]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_x_contour.replace(list_x_contour.size()-1,QString("%1").arg(point_check_x + check - distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else if (qRed(data[point_check_y * x + point_check_x - check]) == 255)
                            {
                                //需要向左移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo * 2; check_1++)
                                {
                                    if (qRed(data[point_check_y * x + point_check_x - check + check_1]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_x_contour.replace(list_x_contour.size()-1,QString("%1").arg(point_check_x - check + distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x - check + distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else
                            {
                                //不需要移动
                                if (check == 5)
                                {
                                    list_point_true.append(QString("%1").arg(1));
                                    break;
                                }
                            }
                        }
                        break;
                    }
                    else
                    {
                        list_x_contour.append(QString("%1").arg(point_x));
                        list_y_contour.append(QString("%1").arg(point_y + j - distance_lunkuo));
                        list_point_true.append(QString("%1").arg(1));
                        break;
                    }
                }
            }
            //----------------------------------
            if (0)
            {
                QString str_y_finally = list_y_contour.at(list_y_contour.size() - 1);
                QString str_y_finally_last;

                for (int j = list_y_contour.size() - 2; j > 0; j--)
                {
                    if (list_point_true.at(j) == "1")
                    {
                        str_y_finally_last = list_y_contour.at(j);
                        break;
                    }
                }

                int str_y_finally_int = str_y_finally.toInt();
                int str_y_finally_last_int = str_y_finally_last.toInt();

                if ((str_y_finally_int - str_y_finally_last_int) > 20 || (str_y_finally_last_int - str_y_finally_int) > 20)
                {
                    list_point_true.replace(list_point_true.size() - 1, QString("%1").arg(0));
                }
            }
            //---------------------------------------
        }
    }

    QStringList list_get_up_x;
    QStringList list_get_up_y;
    QStringList list_get_up_true;

    for (int i = 0; i < list_x_contour.size(); i++)
    {
        if (list_point_true.at(i) == "1")
        {
            list_get_up_x.append(list_x_contour.at(i));
            list_get_up_y.append(list_y_contour.at(i));
            list_get_up_true.append(QString("%1").arg(1));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();
    list_x_contour = list_get_up_x;
    list_y_contour = list_get_up_y;
    list_point_true = list_get_up_true;

    int next_line_star = 0;
    for (int i = 0; i < list_y_right.size(); i++)
    {
        QString str = list_y_right.at(i);
        QString str_1 = list_y_contour.at(list_y_contour.size()-1);

        if (str.toInt() == str_1.toInt())
        {
            next_line_star = i;
            break;
        }
    }

    //最右面的线向左索引
    for (int i = next_line_star; i < list_y_right.size(); i++)
    {
        QString str_x = list_x_right.at(i);
        QString str_y = list_y_right.at(i);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();

        int location = point_y * x + point_x;

        red = qRed(data[location]);
        //green= qGreen(data[location]);
        //blue= qBlue(data[location]);

        if (red == 255)
        {
            //白点，为分界线
            list_x_contour.append(QString("%1").arg(point_x + distance_lunkuo));
            list_y_contour.append(QString("%1").arg(point_y));
            list_point_true.append(QString("%1").arg(1));
        }
        else
        {
            //黑色，向左搜索
            for (int j = 1; j < (point_x); j++)
            {
                //循环向左搜索，找到分界点
                if (qRed(data[point_y * x + point_x - j]) == 255)
                {
                    list_drawwhit_x.append(QString("%1").arg(point_x - j));
                    list_drawwhit_y.append(QString("%1").arg(point_y));

                    if (list_x_contour.size() > 0)
                    {
                        //如果不是起始点，判断与上一个点的位置关系
                        list_x_contour.append(QString("%1").arg(point_x - j + distance_lunkuo));
                        list_y_contour.append(QString("%1").arg(point_y));

                        QString str_check_x = list_x_contour.at(list_x_contour.size()-1);
                        QString str_check_y = list_y_contour.at(list_y_contour.size()-1);

                        int point_check_x = str_check_x.toInt();
                        int point_check_y = str_check_y.toInt();

                        for (int check = 0; check <= 5; check++)
                        {
                            //判断当前点是否可用，是否需要移动
                            if (qRed(data[(point_check_y + check) * x + point_check_x]) == 255)
                            {
                                //需要向上移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo; check_1++)
                                {
                                    if (qRed(data[(point_check_y + check - check_1) * x + point_check_x]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_y_contour.replace(list_y_contour.size()-1,QString("%1").arg(point_check_y + check - distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else if (qRed(data[(point_check_y - check) * x + point_check_x]) == 255)
                            {
                                //需要向下移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo; check_1++)
                                {
                                    if (qRed(data[(point_check_y - check + check_1) * x + point_check_x]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_y_contour.replace(list_y_contour.size()-1,QString("%1").arg(point_check_y - check + distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else
                            {
                                //不需要移动
                                if (check == 5)
                                {
                                    list_point_true.append(QString("%1").arg(1));
                                    break;
                                }
                            }
                        }

                        break;
                    }
                    else
                    {
                        list_x_contour.append(QString("%1").arg(point_x));
                        list_y_contour.append(QString("%1").arg(point_y + j - distance_lunkuo));
                        list_point_true.append(QString("%1").arg(1));
                        break;
                    }
                }
            }
        }

        //----------------------------------
        QString str_x_finally = list_x_contour.at(list_x_contour.size() - 1);
        QString str_x_finally_last;

        for (int j = list_x_contour.size() - 2; j > 0; j--)
        {
            if (list_point_true.at(j) == "1")
            {
                str_x_finally_last = list_x_contour.at(j);
                break;
            }
        }

        int str_x_finally_int = str_x_finally.toInt();
        int str_x_finally_last_int = str_x_finally_last.toInt();

        if ((str_x_finally_int - str_x_finally_last_int) > 20 || (str_x_finally_last_int - str_x_finally_int) > 20)
        {
            list_point_true.replace(list_point_true.size() - 1, QString("%1").arg(0));
        }
        //---------------------------------------
    }

    QStringList list_get_right_x;
    QStringList list_get_right_y;
    QStringList list_get_right_true;

    for (int i = 0; i < list_x_contour.size(); i++)
    {
        if (list_point_true.at(i) == "1")
        {
            list_get_right_x.append(list_x_contour.at(i));
            list_get_right_y.append(list_y_contour.at(i));
            list_get_right_true.append(QString("%1").arg(1));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();
    list_x_contour = list_get_right_x;
    list_y_contour = list_get_right_y;
    list_point_true = list_get_right_true;

    for (int i = 0; i < list_x_down.size(); i++)
    {
        QString str = list_x_down.at(i);
        QString str_1 = list_x_contour.at(list_y_contour.size()-1);

        if (str.toInt() == str_1.toInt())
        {
            next_line_star = i;
            break;
        }
    }

    //最下面的线向上索引
    for (int i = next_line_star; i < list_x_down.size(); i++)
    {
        QString str_x = list_x_down.at(i);
        QString str_y = list_y_down.at(i);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();

        int location = point_y * x + point_x;

        red = qRed(data[location]);
        //green= qGreen(data[location]);
        //blue= qBlue(data[location]);

        if (red == 255)
        {
            //白点，为分界线
            list_x_contour.append(QString("%1").arg(point_x));
            list_y_contour.append(QString("%1").arg(point_y + distance_lunkuo));
            list_point_true.append(QString("%1").arg(1));
        }
        else
        {
            //黑点，向上搜索
            for (int j = 1; j < (point_y ); j++)
            {
                //循环向上搜索，找到分界点
                list_drawwhit_x.append(QString("%1").arg(point_x));
                list_drawwhit_y.append(QString("%1").arg(point_y - j));

                if (qRed(data[(point_y - j) * x + point_x]) == 255)
                {
                    if (list_x_contour.size() > 0)
                    {
                        //如果不是起始点，判断与上一个点的位置关系
                        //QString last_x_contour = list_x_contour.at(list_x_contour.size()-1);
                        QString last_y_contour = list_y_contour.at(list_y_contour.size()-1);

                        //int point_last_x_contour = last_x_contour.toInt();
                        int point_last_y_contour = last_y_contour.toInt() + distance_lunkuo;

                        if (0)
                            //if ((point_last_y_contour - (point_y + j)) > (10) || (point_last_y_contour - (point_y + j)) < -10)
                        {
                            list_x_contour.append(QString("%1").arg(point_x));
                            list_y_contour.append(QString("%1").arg(point_last_y_contour - distance_lunkuo));
                        }
                        else
                        {
                            list_x_contour.append(QString("%1").arg(point_x));
                            list_y_contour.append(QString("%1").arg(point_y - j + distance_lunkuo));
                        }

                        QString str_check_x = list_x_contour.at(list_x_contour.size()-1);
                        QString str_check_y = list_y_contour.at(list_y_contour.size()-1);

                        int point_check_x = str_check_x.toInt();
                        int point_check_y = str_check_y.toInt();

                        for (int check = 0; check <= 5; check++)
                        {
                            //判断当前点是否可用，是否需要移动
                            if (qRed(data[point_check_y * x + point_check_x + check]) == 255 )
                            {
                                //需要向右移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo * 2; check_1++)
                                {
                                    if (qRed(data[point_check_y * x + point_check_x + check - check_1]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_x_contour.replace(list_x_contour.size()-1,QString("%1").arg(point_check_x + check - distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else if (qRed(data[point_check_y * x + point_check_x - check]) == 255)
                            {
                                //需要向左移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo * 2; check_1++)
                                {
                                    if (qRed(data[point_check_y * x + point_check_x - check + check_1]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_x_contour.replace(list_x_contour.size()-1,QString("%1").arg(point_check_x - check + distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x - check + distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else
                            {
                                //不需要移动
                                if (check == 5)
                                {
                                    list_point_true.append(QString("%1").arg(1));
                                    break;
                                }
                            }
                        }
                        break;
                    }
                    else
                    {
                        list_x_contour.append(QString("%1").arg(point_x));
                        list_y_contour.append(QString("%1").arg(point_y - j + distance_lunkuo));
                        list_point_true.append(QString("%1").arg(1));
                        break;
                    }
                }
            }
        }

        //----------------------------------
        QString str_y_finally = list_y_contour.at(list_y_contour.size() - 1);
        QString str_y_finally_last;

        for (int j = list_y_contour.size() - 2; j > 0; j--)
        {
            if (list_point_true.at(j) == "1")
            {
                str_y_finally_last = list_y_contour.at(j);
                break;
            }
        }

        int str_y_finally_int = str_y_finally.toInt();
        int str_y_finally_last_int = str_y_finally_last.toInt();

        if ((str_y_finally_int - str_y_finally_last_int) > 20 || (str_y_finally_last_int - str_y_finally_int) > 20)
        {
            list_point_true.replace(list_point_true.size() - 1, QString("%1").arg(0));
        }
        //---------------------------------------
    }

    QStringList list_get_down_x;
    QStringList list_get_down_y;
    QStringList list_get_down_true;

    for (int i = 0; i < list_x_contour.size(); i++)
    {
        if (list_point_true.at(i) == "1")
        {
            list_get_down_x.append(list_x_contour.at(i));
            list_get_down_y.append(list_y_contour.at(i));
            list_get_down_true.append(QString("%1").arg(1));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();
    list_x_contour = list_get_down_x;
    list_y_contour = list_get_down_y;
    list_point_true = list_get_down_true;

    for (int i = 0; i < list_y_left.size(); i++)
    {
        QString str = list_y_left.at(i);
        QString str_1 = list_y_contour.at(list_y_contour.size()-1);
        if (str.toInt() == str_1.toInt())
        {
            next_line_star = i;
            break;
        }
    }

    int next_line_end = 0;

    for (int i = 0; i < list_y_left.size(); i++)
    {
        QString str = list_y_left.at(i);
        QString str_1 = list_y_contour.at(0);
        if (str.toInt() == str_1.toInt())
        {
            next_line_end = i;
            break;
        }
    }

    qDebug()<<"next_line_star:"<<next_line_star;
    qDebug()<<"next_line_end:"<<next_line_end;

    //最左面的线向右索引
    for (int i = next_line_star; i < next_line_end; i++)
    {
        QString str_x = list_x_left.at(i);
        QString str_y = list_y_left.at(i);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();

        int location = point_y * x + point_x;

        red = qRed(data[location]);
        //green= qGreen(data[location]);
        //blue= qBlue(data[location]);

        if (red == 255)
        {
            //白点，为分界线
            list_x_contour.append(QString("%1").arg(point_x - distance_lunkuo));
            list_y_contour.append(QString("%1").arg(point_y));
            list_point_true.append(QString("%1").arg(1));
        }
        else
        {
            //黑色，向右搜索
            for (int j = 1; j < (y_max); j++)
            {
                //循环向右搜索，找到分界点
                if (qRed(data[point_y * x + point_x + j]) == 255)
                {
                    list_drawwhit_x.append(QString("%1").arg(point_x + j));
                    list_drawwhit_y.append(QString("%1").arg(point_y));

                    if (list_x_contour.size() > 0)
                    {
                        //如果不是起始点，判断与上一个点的位置关系
                        list_x_contour.append(QString("%1").arg(point_x + j - distance_lunkuo));
                        list_y_contour.append(QString("%1").arg(point_y));

                        QString str_check_x = list_x_contour.at(list_x_contour.size()-1);
                        QString str_check_y = list_y_contour.at(list_y_contour.size()-1);

                        int point_check_x = str_check_x.toInt();
                        int point_check_y = str_check_y.toInt();

                        for (int check = 0; check <= 5; check++)
                        {
                            //判断当前点是否可用，是否需要移动
                            if (qRed(data[(point_check_y + check) * x + point_check_x]) == 255)
                            {
                                //需要向上移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo; check_1++)
                                {
                                    if (qRed(data[(point_check_y + check - check_1) * x + point_check_x]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_y_contour.replace(list_y_contour.size()-1,QString("%1").arg(point_check_y + check - distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else if (qRed(data[(point_check_y - check) * x + point_check_x]) == 255)
                            {
                                //需要向下移动
                                for (int check_1 = 1; check_1 <= distance_lunkuo; check_1++)
                                {
                                    if (qRed(data[(point_check_y - check + check_1) * x + point_check_x]) == 255 )
                                    {
                                        //无法移动，需要舍弃
                                        list_point_true.append(QString("%1").arg(0));
                                        break;
                                    }
                                    else
                                    {
                                        //可以移动
                                        if (check_1 == distance_lunkuo)
                                        {
                                            //list_y_contour.replace(list_y_contour.size()-1,QString("%1").arg(point_check_y - check + distance_lunkuo));
                                            //list_x_contour[list_x_contour.size()-1] = QString("%1").arg(point_check_y * x + point_check_x + check - distance_lunkuo);
                                            list_point_true.append(QString("%1").arg(1));
                                            //list_point_true.append(QString("%1").arg(0));
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                            else
                            {
                                //不需要移动
                                if (check == 5)
                                {
                                    list_point_true.append(QString("%1").arg(1));
                                    break;
                                }
                            }
                        }

                        break;
                    }
                    else
                    {
                        list_x_contour.append(QString("%1").arg(point_x));
                        list_y_contour.append(QString("%1").arg(point_y + j - distance_lunkuo));
                        list_point_true.append(QString("%1").arg(1));
                        break;
                    }
                }
            }
        }

        //----------------------------------
        QString str_x_finally = list_x_contour.at(list_x_contour.size() - 1);
        QString str_x_finally_last;

        for (int j = list_x_contour.size() - 2; j > 0; j--)
        {
            if (list_point_true.at(j) == "1")
            {
                str_x_finally_last = list_x_contour.at(j);
                break;
            }
        }

        int str_x_finally_int = str_x_finally.toInt();
        int str_x_finally_last_int = str_x_finally_last.toInt();

        if ((str_x_finally_int - str_x_finally_last_int) > 20 || (str_x_finally_last_int - str_x_finally_int) > 20)
        {
            list_point_true.replace(list_point_true.size() - 1, QString("%1").arg(0));
        }
        //---------------------------------------
    }

    QStringList list_get_left_x;
    QStringList list_get_left_y;
    QStringList list_get_left_true;

    for (int i = 0; i < list_x_contour.size(); i++)
    {
        if (list_point_true.at(i) == "1")
        {
            list_get_left_x.append(list_x_contour.at(i));
            list_get_left_y.append(list_y_contour.at(i));
            list_get_left_true.append(QString("%1").arg(1));
        }
    }
    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();
    list_x_contour = list_get_left_x;
    list_y_contour = list_get_left_y;
    list_point_true = list_get_left_true;

    for (int i = 0; i < list_x_down.size(); i++)
    {
        QString str = list_x_down.at(i);
        QString str_1 = list_x_contour.at(list_y_contour.size()-1);

        if (str.toInt() == str_1.toInt())
        {
            next_line_star = i;
            break;
        }
    }

    qDebug()<<"list_x:"<<list_x_contour.size();
    qDebug()<<"list_y:"<<list_y_contour.size();
    qDebug()<<"list_point:"<<list_point_true.size();

    QStringList list_x_cache_1;
    QStringList list_y_cache_1;
    QStringList list_point_true_cache_1;

#endif

#if 0
    //删除距离图像外边界距离小于轮廓的点
    for (int i = 0; i< list_x_contour.size(); i++)
    {
        QString str_point_x = list_x_contour.at(i);
        QString str_point_y = list_y_contour.at(i);

        int point_x = str_point_x.toInt();
        int point_y = str_point_y.toInt();

        if (point_x > distance_lunkuo && point_x < (x - distance_lunkuo) && point_y > distance_lunkuo && point_y < (y - distance_lunkuo))
        {
            for (int j = 0; j < distance_lunkuo; j++)
            {
                if((qRed(data[point_y * x + point_x + j]) == 255) //向右搜索
                        || (qRed(data[point_y * x + point_x - j]) == 255)  //向左搜索
                        || (qRed(data[(point_y + j)* x + point_x]) == 255) //向下搜索
                        || (qRed(data[(point_y - j)* x + point_x]) == 255) //向下搜索
                        || (qRed(data[(point_y - j / 2)* x + point_x + j / 2]) == 255) //向右上搜索
                        || (qRed(data[(point_y - j / 2)* x + point_x - j / 2]) == 255) //向左上搜索
                        || (qRed(data[(point_y + j / 2)* x + point_x + j / 2]) == 255) //向右下搜索
                        || (qRed(data[(point_y + j / 2)* x + point_x - j / 2]) == 255) //向左下搜索
                        )
                {
                    list_point_true.replace(i,QString("%1").arg(0));
                }
            }
        }
    }

    list_x_cache_1.clear();
    list_y_cache_1.clear();
    list_point_true_cache_1.clear();

    for(int i = 0; i < list_point_true.size(); i++)
    {
        if(list_point_true.at(i) == "1")
        {
            QString str_1 = list_x_contour.at(i);
            QString str_2 = list_y_contour.at(i);

            list_x_cache_1.append(str_1);
            list_y_cache_1.append(str_2);
            list_point_true_cache_1.append(QString("%1").arg(1));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();

    list_point_true = list_point_true_cache_1;
    list_x_contour = list_x_cache_1;
    list_y_contour = list_y_cache_1;

#endif

#if 0
    //删除距离太远的点
    for (int i = 1; i < list_x_contour.size(); i++)
    {
        QString str_x = list_x_contour.at(i);
        QString str_y = list_y_contour.at(i);
        QString str_x_lasr = list_x_contour.at(i - 1);
        QString str_y_lasr = list_y_contour.at(i - 1);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int point_x_last = str_x_lasr.toInt();
        int point_y_last = str_y_lasr.toInt();

        if ( ((point_x - point_x_last) > 100) || ((point_x_last - point_x) > 100)
             || ((point_y - point_y_last) > 100) || ((point_y_last - point_y) > 100)
             )
        {
            list_point_true.replace(i,QString("%1").arg(0));
        }
    }

    list_x_cache_1.clear();
    list_y_cache_1.clear();
    list_point_true_cache_1.clear();

    for(int i = 0; i < list_point_true.size(); i++)
    {
        if(list_point_true.at(i) == "1")
        {
            QString str_1 = list_x_contour.at(i);
            QString str_2 = list_y_contour.at(i);

            list_x_cache_1.append(str_1);
            list_y_cache_1.append(str_2);
            list_point_true_cache_1.append(QString("%1").arg(1));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();

    list_point_true = list_point_true_cache_1;
    list_x_contour = list_x_cache_1;
    list_y_contour = list_y_cache_1;

#endif

#if 0
    //对获取的点重新排序
    QStringList list_x_reset;
    QStringList list_y_reset;
    QStringList list_point_true_reset;
    QString str_reset_x_now;
    QString str_reset_x_last;
    QString str_reset_y_now;
    QString str_reset_y_last;
    int reset_x_now = 0;
    int reset_x_last = 0;
    int reset_y_now = 0;
    int reset_y_last = 0;
    double distance_point_min = 100000.0;
    int jilupointposition = 0;

    for (int i = 0; i < list_x_contour.size(); i++)
    {
        jilupointposition = 0;
        if(i == 0)
        {
            list_x_reset.append(list_x_contour.at(0));
            list_y_reset.append(list_y_contour.at(0));
            list_point_true_reset.append(list_point_true.at(0));

            list_x_contour.replace(0,QString("%1").arg(100000));
            list_y_contour.replace(0,QString("%1").arg(100000));
        }
        else
        {
            double distance = 0.0;
            distance_point_min = 100000.0;
            for (int j = 1; j < list_x_contour.size(); j++)
            {
                str_reset_x_now = list_x_contour.at(j);
                str_reset_x_last = list_x_reset.at(list_x_reset.size()-1);
                str_reset_y_now = list_y_contour.at(j);
                str_reset_y_last = list_y_reset.at(list_y_reset.size()-1);

                reset_x_now = str_reset_x_now.toInt();
                reset_x_last = str_reset_x_last.toInt();
                reset_y_now = str_reset_y_now.toInt();
                reset_y_last = str_reset_y_last.toInt();

                if (reset_x_now != 100000 || reset_y_now != 100000)
                {
                    distance = sqrt((reset_x_now - reset_x_last) * (reset_x_now - reset_x_last) * 1.0 + (reset_y_now - reset_y_last) * (reset_y_now - reset_y_last) * 1.0);
                    if(distance < distance_point_min)
                    {
                        distance_point_min = distance;
                        jilupointposition = j;
                    }
                }
            }

            list_x_reset.append(list_x_contour.at(jilupointposition));
            list_y_reset.append(list_y_contour.at(jilupointposition));
            list_point_true_reset.append(list_point_true.at(jilupointposition));

            list_x_contour.replace(jilupointposition,QString("%1").arg(100000));
            list_y_contour.replace(jilupointposition,QString("%1").arg(100000));
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_point_true.clear();
    list_x_contour = list_x_reset;
    list_y_contour = list_y_reset;
    list_point_true = list_point_true_reset;

#endif

    //输出的x，y坐标
    QStringList list_output_x;
    QStringList list_output_y;

    //画图
    //按点画图
#if 0
    for (int i = 0 ; i < list_x_contour.size(); i++)
    {
        if (list_point_true.at(i) == "1")
        {
            QString str_x = list_x_contour.at(i);
            QString str_y = list_y_contour.at(i);

            int point_x = str_x.toInt();
            int point_y = str_y.toInt();

            int weizhi = point_y * x + point_x;
            int rgb = 255;

            data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
        }
    }
#endif

    //按线画图
#if 0

    QString str_x_first = list_x_contour.at(0);
    QString str_y_first = list_y_contour.at(0);
    list_x_contour.append(str_x_first);
    list_y_contour.append(str_y_first);

    for (int i = 0 ; i < list_x_contour.size() - 1; i++)
    {
        QStringList list_Cache_x;
        QStringList list_Cache_y;

        QString str_x = list_x_contour.at(i);
        QString str_y = list_y_contour.at(i);
        QString str_x_lasr = list_x_contour.at(i + 1);
        QString str_y_lasr = list_y_contour.at(i + 1);

        int point_x = str_x.toInt();
        int point_y = str_y.toInt();
        int point_x_last = str_x_lasr.toInt();
        int point_y_last = str_y_lasr.toInt();

        if (point_x_last >= point_x)
        {
            if (point_y_last >= point_y)
            {
                for (int x_huatu = 1; x_huatu <= (point_x_last - point_x); x_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x + x_huatu));
                    list_Cache_y.append(QString("%1").arg(point_y));

                    //                    int weizhi = point_y * x + point_x + x_huatu;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
                for (int y_huatu = 1; y_huatu <= (point_y_last - point_y); y_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x_last));
                    list_Cache_y.append(QString("%1").arg(point_y + y_huatu));

                    //                    int weizhi = (point_y + y_huatu) * x + point_x_last;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
            }
            else
            {
                for (int y_huatu = 1; y_huatu <= (point_y - point_y_last); y_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x));
                    list_Cache_y.append(QString("%1").arg(point_y - y_huatu));

                    //                    int weizhi = (point_y - y_huatu) * x + point_x;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
                for (int x_huatu = 1; x_huatu <= (point_x_last - point_x); x_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x + x_huatu));
                    list_Cache_y.append(QString("%1").arg(point_y_last));

                    //                    int weizhi = point_y_last * x + point_x + x_huatu;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
            }
        }
        else
        {
            if (point_y_last >= point_y)
            {
                for (int y_huatu = 1; y_huatu <= (point_y_last - point_y); y_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x));
                    list_Cache_y.append(QString("%1").arg(point_y + y_huatu));

                    //                    int weizhi = (point_y + y_huatu) * x + point_x;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
                for (int x_huatu = 1; x_huatu <= (point_x - point_x_last); x_huatu++)
                {

                    list_Cache_x.append(QString("%1").arg(point_x - x_huatu));
                    list_Cache_y.append(QString("%1").arg(point_y_last));
                    //                    int weizhi = point_y_last * x + point_x - x_huatu;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
            }
            else
            {
                for (int x_huatu = 1; x_huatu <= (point_x - point_x_last); x_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x - x_huatu));
                    list_Cache_y.append(QString("%1").arg(point_y));

                    //                    int weizhi = point_y * x + point_x - x_huatu;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
                for (int y_huatu = 1; y_huatu <= (point_y - point_y_last); y_huatu++)
                {
                    list_Cache_x.append(QString("%1").arg(point_x_last));
                    list_Cache_y.append(QString("%1").arg(point_y - y_huatu));

                    //                    int weizhi = (point_y - y_huatu) * x + point_x_last;
                    //                    int rgb = 255;
                    //                    data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
                }
            }
        }

        //        int weizhi = point_y * x + point_x;
        //        int rgb = 255;
        //        data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));

        for (int check_num = 0; check_num < list_Cache_x.size(); check_num++)
        {
            QString str_x_check = list_Cache_x.at(check_num);
            QString str_y_check = list_Cache_y.at(check_num);
            int point_x_check = str_x_check.toInt();
            int point_y_check = str_y_check.toInt();

            if(qRed(data[point_y_check * x + point_x_check]) == 255)
            {//判断画的线是否经过图像
                list_Cache_x.clear();
                list_Cache_y.clear();
            }
        }

        if(list_Cache_x.size()>0)
        {
            for (int size = 0; size < list_Cache_x.size(); size++)
            {
                QString str_x_save = list_Cache_x.at(size);
                QString str_y_save = list_Cache_y.at(size);
                int point_x_save = str_x_save.toInt();
                int point_y_save = str_y_save.toInt();

                list_output_x.append(QString("%1").arg(point_x_save));
                list_output_y.append(QString("%1").arg(point_y_save));
            }
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    list_x_contour = list_output_x;
    list_y_contour = list_output_y;

    //画图
    for (int i = 0 ; i < list_x_contour.size(); i++)
    {
        //if (list_point_true.at(i) == "1")
        {
            QString str_x = list_x_contour.at(i);
            QString str_y = list_y_contour.at(i);

            int point_x = str_x.toInt();
            int point_y = str_y.toInt();

            int weizhi = point_y * x + point_x;
            int rgb = 255;

            data[weizhi] = qRgba(rgb, 0, 0, qAlpha(data[weizhi]));
        }
    }

#endif

#if 1

    for (int i = 0; i < list_x_up.size(); i++)
    {
        if (i == 0)
        {
            QString str_y = list_y_up.at(i);
            int point_y = str_y.toInt();
            list_y_up[i] = QString("%1").arg(point_y - distance_lunkuo);
            QString str_x = list_x_up.at(i);
            int point_x = str_x.toInt();
            list_x_up[i] = QString("%1").arg(point_x - distance_lunkuo);
        }
        else if (i == list_x_up.size()-1)
        {
            QString str_y = list_y_up.at(i);
            int point_y = str_y.toInt();
            list_y_up[i] = QString("%1").arg(point_y - distance_lunkuo);
            QString str_x = list_x_up.at(i);
            int point_x = str_x.toInt();
            list_x_up[i] = QString("%1").arg(point_x + distance_lunkuo);
        }
        else
        {
            QString str_y = list_y_up.at(i);
            int point_y = str_y.toInt();
            list_y_up[i] = QString("%1").arg(point_y - distance_lunkuo);
        }
    }

    for (int i = 0; i < list_x_right.size(); i++)
    {
        if ( i == 0)
        {
            QString str_x = list_x_right.at(i);
            int point_x = str_x.toInt();
            list_x_right[i] = QString("%1").arg(point_x + distance_lunkuo);
            QString str_y = list_y_right.at(i);
            int point_y = str_y.toInt();
            list_y_right[i] = QString("%1").arg(point_y - distance_lunkuo);
        }
        else if (i == list_x_right.size() - 1)
        {
            QString str_x = list_x_right.at(i);
            int point_x = str_x.toInt();
            list_x_right[i] = QString("%1").arg(point_x + distance_lunkuo);
            QString str_y = list_y_right.at(i);
            int point_y = str_y.toInt();
            list_y_right[i] = QString("%1").arg(point_y + distance_lunkuo);
        }
        else
        {
            QString str_x = list_x_right.at(i);
            int point_x = str_x.toInt();
            list_x_right[i] = QString("%1").arg(point_x + distance_lunkuo);
        }
    }

    for (int i = 0; i < list_x_down.size(); i++)
    {
        if (i == 0)
        {
            QString str_y = list_y_down.at(i);
            int point_y = str_y.toInt();
            list_y_down[i] = QString("%1").arg(point_y + distance_lunkuo);
            QString str_x = list_x_down.at(i);
            int point_x = str_x.toInt();
            list_x_down[i] = QString("%1").arg(point_x + distance_lunkuo);
        }
        else if (i == list_x_down.size() - 1)
        {
            QString str_y = list_y_down.at(i);
            int point_y = str_y.toInt();
            list_y_down[i] = QString("%1").arg(point_y + distance_lunkuo);
            QString str_x = list_x_down.at(i);
            int point_x = str_x.toInt();
            list_x_down[i] = QString("%1").arg(point_x - distance_lunkuo);
        }
        else
        {
            QString str_y = list_y_down.at(i);
            int point_y = str_y.toInt();
            list_y_down[i] = QString("%1").arg(point_y + distance_lunkuo);
        }
    }

    for (int i = 0; i < list_x_left.size(); i++)
    {
        if ( i == 0)
        {
            QString str_x = list_x_left.at(i);
            int point_x = str_x.toInt();
            list_x_left[i] = QString("%1").arg(point_x - distance_lunkuo);
            QString str_y = list_y_left.at(i);
            int point_y = str_y.toInt();
            list_y_left[i] = QString("%1").arg(point_y + distance_lunkuo);
        }
        else if (i == list_x_left.size() - 1)
        {
            QString str_x = list_x_left.at(i);
            int point_x = str_x.toInt();
            list_x_left[i] = QString("%1").arg(point_x - distance_lunkuo);
            QString str_y = list_y_left.at(i);
            int point_y = str_y.toInt();
            list_y_left[i] = QString("%1").arg(point_y - distance_lunkuo);
        }
        else
        {
            QString str_x = list_x_left.at(i);
            int point_x = str_x.toInt();
            list_x_left[i] = QString("%1").arg(point_x - distance_lunkuo);
        }
    }

    list_x_contour.clear();
    list_y_contour.clear();
    for (int i = 0; i < list_x_up.size(); i++)
    {
        list_x_contour.append(list_x_up.at(i));
        list_y_contour.append(list_y_up.at(i));
    }
    for (int i = 0; i < list_x_right.size(); i++)
    {
        list_x_contour.append(list_x_right.at(i));
        list_y_contour.append(list_y_right.at(i));
    }
    for (int i = 0; i < list_x_down.size(); i++)
    {
        list_x_contour.append(list_x_down.at(i));
        list_y_contour.append(list_y_down.at(i));
    }
    for (int i = 0; i < list_x_left.size(); i++)
    {
        list_x_contour.append(list_x_left.at(i));
        list_y_contour.append(list_y_left.at(i));
    }
#endif

    int dsr_x_max = m_maxX;
    int dsr_x_min = m_minX;
    int dsr_y_max = m_maxY;
    int dsr_y_min = m_minY;

    int point_x_max = x_max;
    int point_x_min = x_min;
    int point_y_max = y_max;
    int point_y_min = y_min;

    int Stitch_length = 4; //输入的针步长度参数,单位mm
    int Stitch_length_calculate = Stitch_length / 0.01;
    int Stitch_length_calculate_min = (Stitch_length - 1) / 0.01;
    int Stitch_length_calculate_max = (Stitch_length + 1) / 0.01;

    qDebug()<<"dsr_x_max:"<<dsr_x_max;
    qDebug()<<"dsr_x_min:"<<dsr_x_min;
    qDebug()<<"dsr_y_max:"<<dsr_y_max;
    qDebug()<<"dsr_y_min:"<<dsr_y_min;



    QStringList list_dsr_point_x = getDsrPointX(list_x_contour, dsr_x_max, dsr_x_min, point_x_max, point_x_min);
    QStringList list_dsr_point_y = getDsrPointY(list_y_contour, dsr_y_max, dsr_y_min, point_y_max, point_y_min);

//    if (list_dsr_point_x.size() > 2)
//    {
//        list_dsr_point_x.append(list_dsr_point_x.at(0));
//        list_dsr_point_y.append(list_dsr_point_y.at(0));
//    }

    QStringList list_x_1;
    QStringList list_y_1;

    int cont_x = 0;

    int x_check = (dsr_x_max - dsr_x_min) * 3 /4 + dsr_x_min;
    int y_check = (dsr_y_max - dsr_y_min) / 2 + dsr_y_min;
    int x_use_check = 10000000;

    for (int i = 0 ; i < list_dsr_point_x.size(); i++)
    {
        QString str_get_x = list_dsr_point_x.at(i);
        QString str_get_y = list_dsr_point_y.at(i);

        int str_get_x_int = str_get_x.toInt();
        int str_get_y_int = str_get_y.toInt();

        if ((str_get_x_int - x_check) <= x_use_check && str_get_y_int <= y_check && str_get_x_int > x_check )
        {
            x_use_check = str_get_x_int - x_check;
            cont_x = i;
        }
    }

    qDebug()<<"list_dsr_point_x.size="<<list_dsr_point_x.size();
    qDebug()<<"cont_x="<<cont_x;

    list_x_1.append(list_dsr_point_x.at(cont_x));
    list_x_1.append(QString("%1").arg((dsr_x_max + dsr_x_min) / 2));

    list_y_1.append(list_dsr_point_y.at(cont_x));
    list_y_1.append(QString("%1").arg((dsr_y_max + dsr_y_min) / 2));

    for(int i = 0; i < list_dsr_point_x.size(); i++)
    {
        list_x_1.append(list_dsr_point_x.at(i));
        list_y_1.append(list_dsr_point_y.at(i));
    }

    list_x_1.append(list_dsr_point_x.at(0));
    list_y_1.append(list_dsr_point_y.at(0));

    list_dsr_point_x.clear();
    list_dsr_point_y.clear();

    list_dsr_point_x = list_x_1;
    list_dsr_point_y = list_y_1;

    QStringList list_dsr_x_save;
    QStringList list_dsr_y_save;

    list_dsr_x_save.append(list_dsr_point_x.at(0));
    list_dsr_y_save.append(list_dsr_point_y.at(0));

    for (int i = 0; i < list_dsr_point_x.size() - 2;)
    {
        QString str_dsr_x_now = list_dsr_point_x.at(i);
        QString str_dsr_y_now = list_dsr_point_y.at(i);
        int int_dsr_x_now = str_dsr_x_now.toInt();
        int int_dsr_y_now = str_dsr_y_now.toInt();

        for (int j = i + 1; j < list_dsr_point_x.size(); j++)
        {
            QString str_dsr_x_nest = list_dsr_point_x.at(j);
            QString str_dsr_y_nest = list_dsr_point_y.at(j);
            int int_dsr_x_next = str_dsr_x_nest.toInt();
            int int_dsr_y_next = str_dsr_y_nest.toInt();

            double point_length = getPointLength(int_dsr_x_now, int_dsr_y_now, int_dsr_x_next, int_dsr_y_next);

            if (point_length < Stitch_length_calculate_min)
            {
                if (j == list_dsr_point_x.size() - 1)
                {
                    list_dsr_x_save.append(list_dsr_point_x.at(list_dsr_point_x.size() - 1));
                    list_dsr_y_save.append(list_dsr_point_y.at(list_dsr_point_y.size() - 1));
                    i = j;
                    break;
                }
            }
            else if (point_length >= Stitch_length_calculate_min && point_length <= Stitch_length_calculate_max)
            {
                list_dsr_x_save.append(list_dsr_point_x.at(j));
                list_dsr_y_save.append(list_dsr_point_y.at(j));
                i = j;
                break;
            }
            else if (point_length > Stitch_length_calculate_max)
            {
                int point_length_int = point_length;

                int num = point_length_int / Stitch_length_calculate + 1;
                for (int k = 1; k <= num; k++)
                {
                    int point_x_now = ((int_dsr_x_next - int_dsr_x_now) * (k * Stitch_length_calculate) / point_length_int ) + int_dsr_x_now;
                    int point_y_now = ((int_dsr_y_next - int_dsr_y_now) * (k * Stitch_length_calculate) / point_length_int ) + int_dsr_y_now;

                    list_dsr_x_save.append(QString("%1").arg(point_x_now));
                    list_dsr_y_save.append(QString("%1").arg(point_y_now));
                }
                i = j;
                break;
            }
            else
            {

            }

            if (i == list_dsr_point_x.size() - 1)
            {
                break;
            }
        }
    }

    //list_dsr_x_save.append(list_dsr_point_x.at(0));
    //list_dsr_y_save.append(list_dsr_point_y.at(0));

    DsrStep point_dsr_item;
    QList<DsrStep> dsr_item_list;//记录DSR文件内所有点信息

    //第一针插入0
//    memset(&point_dsr_item, 0, sizeof(DsrStep));
//    point_dsr_item.ctrl = DSR_EMB;
//    point_dsr_item.attr = 0;
//    point_dsr_item.action = 0;
//    point_dsr_item.dx =0;
//    point_dsr_item.dy = 0;
//    point_dsr_item.dr = 0;
//    dsr_item_list.append(point_dsr_item);

    int point_move_x;
    int point_move_y;

    //----生成数据点
    for (int i = 0; i< list_dsr_x_save.size(); i++)
    {
        QString str_point_x = list_dsr_x_save.at(i);
        QString str_point_y = list_dsr_y_save.at(i);
        int point_x = str_point_x.toInt();
        int point_y = str_point_y.toInt();

        if (i == 0)
        {
            memset(&point_dsr_item, 0, sizeof(DsrStep));
            point_dsr_item.ctrl = DSR_EMB;
            point_dsr_item.attr = 0;
            point_dsr_item.action = 0;
            point_dsr_item.dx = 0;
            point_move_x = point_x * (-1);
            point_dsr_item.dy = 0;
            point_move_y = point_y;
            point_dsr_item.dr = 0;

            dsr_item_list.append(point_dsr_item);
        }
        else
        {
            memset(&point_dsr_item, 0, sizeof(DsrStep));
            point_dsr_item.ctrl = DSR_EMB;
            point_dsr_item.attr = 0;
            point_dsr_item.action = 0;
            point_dsr_item.dx = point_x * (-1) - point_move_x;
            point_dsr_item.dy = point_y - point_move_y;
            point_dsr_item.dr = getDsrDr(point_dsr_item.dx, point_dsr_item.dy);

            dsr_item_list.append(point_dsr_item);
        }


        if (i == list_dsr_x_save.size() - 1)
        {
            memset(&point_dsr_item, 0, sizeof(DsrStep));
            point_dsr_item.ctrl = DSR_NULL;
            point_dsr_item.attr = 0;
            point_dsr_item.action = 0;
            point_dsr_item.dx = point_x * (-1) - point_move_x;
            point_dsr_item.dy = point_y - point_move_y;
            point_dsr_item.dr = getDsrDr(point_dsr_item.dx, point_dsr_item.dy);

            dsr_item_list.append(point_dsr_item);
        }
    }

    qDebug()<<dsr_item_list.size();
    return dsr_item_list;
}

int OutLine::getDsrDr(int dx, int dy)
{
    float angle = atan(dy *1.0 / dx *1.0)*180/M_PI;

    int angle_return = angle;
    return angle_return;
}

QStringList OutLine::getDsrPointX(QStringList list_x_contour, int dsr_x_max, int dsr_x_min, int point_x_max, int point_x_min)
{
    QStringList list_reture;
    for (int i = 0; i< list_x_contour.size(); i++)
    {
        QString str_x = list_x_contour.at(i);
        int point_x = str_x.toInt();
        int point_x_dsr = dsr_x_min + ((dsr_x_max - dsr_x_min) * (point_x - point_x_min)) / (point_x_max - point_x_min);
        list_reture.append(QString("%1").arg(point_x_dsr));
    }
    return list_reture;
}

QStringList OutLine::getDsrPointY(QStringList list_y_contour, int dsr_y_max, int dsr_y_min, int point_y_max, int point_y_min)
{
    QStringList list_reture;
    for (int i = 0; i< list_y_contour.size(); i++)
    {
        QString str_y = list_y_contour.at(i);
        int point_y = str_y.toInt();
        int point_y_dsr = dsr_y_min + ((dsr_y_max - dsr_y_min) * (point_y - point_y_min)) / (point_y_max - point_y_min);
        list_reture.append(QString("%1").arg(point_y_dsr));
    }
    return list_reture;
}

double OutLine::getPointLength(int x1, int y1, int x2, int y2)
{
    return sqrt(((x1 - x2) * (x1 - x2) * 1.0) + ((y1 - y2) * (y1 - y2) * 1.0));
}

