QGCVideoMainWindow.cc 6.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

 ======================================================================*/

/**
 * @file
 *   @brief Implementation of main window
 *
 *   @author Dominik Honegger
 *
 */

#include "QGCVideoMainWindow.h"
#include "ui_QGCVideoMainWindow.h"

35 36 37
#include "UDPLink.h"
#include <QDebug>

38 39
  QByteArray imageRecBuffer1 = QByteArray(376*240,255);
  QByteArray imageRecBuffer2 = QByteArray(376*240,255);
40 41
  static int part = 0;

42 43
QGCVideoMainWindow::QGCVideoMainWindow(QWidget *parent) :
    QMainWindow(parent),
44
    link(QHostAddress::Any, 5555),
45 46 47
    ui(new Ui::QGCVideoMainWindow)
{
    ui->setupUi(this);
48 49 50 51 52 53 54 55 56 57 58 59

    // Set widgets in video mode
    ui->video1Widget->enableVideo(true);
    ui->video2Widget->enableVideo(true);
    ui->video3Widget->enableVideo(true);
    ui->video4Widget->enableVideo(true);

    // Connect link to this widget, receive all bytes
    connect(&link, SIGNAL(bytesReceived(LinkInterface*,QByteArray)), this, SLOT(receiveBytes(LinkInterface*,QByteArray)));

    // Open port
    link.connect();
60 61 62 63 64 65
}

QGCVideoMainWindow::~QGCVideoMainWindow()
{
    delete ui;
}
66 67 68 69 70 71 72 73 74 75 76

void QGCVideoMainWindow::receiveBytes(LinkInterface* link, QByteArray data)
{
    // There is no need to differentiate between links
    // for this use case here
    Q_UNUSED(link);

    // Image data is stored in QByteArray
    // Output bytes and load Lenna!

    QString bytes;
77
    QString index;
78
    QString ascii;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94



    // TODO FIXME Fabian
    // RAW hardcoded to 22x22
    int imgWidth = 376;
    int imgHeight = 240;
    int imgColors = 255;


    //const int headerSize = 15;

    // Construct PGM header
    QString header("P5\n%1 %2\n%3\n");
    header = header.arg(imgWidth).arg(imgHeight).arg(imgColors);

95 96 97
    unsigned char i0 = data[0];

    switch (i0)
98
    {
99
    case 0x01:
100 101 102
        {
            for (int i=4; i<data.size()/4; i++)
            {
103 104
                imageRecBuffer1[i] = data[i*4];
                imageRecBuffer2[i] = data[i*4+1];
105
            }
106 107
            part = part | 1;
            break;
108
        }
109
    case 0x02:
110 111 112
        {
            for (int i=4; i<data.size()/4; i++)
            {
113 114
                imageRecBuffer1[i+45124/4] = data[i*4];
                imageRecBuffer2[i+45124/4] = data[i*4+1];
115
            }
116 117
            part = part | 2;
            break;
118
        }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    case 0x03:
        {
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*2] = data[i*4];
                imageRecBuffer2[i+45124/4*2] = data[i*4+1];
            }
            part = part | 4;
            break;
        }
    case 0x04:
        {
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*3] = data[i*4];
                imageRecBuffer2[i+45124/4*3] = data[i*4+1];
            }
            part = part | 8;
            break;
        }
    case 0x05:
140
        {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*4] = data[i*4];
                imageRecBuffer2[i+45124/4*4] = data[i*4+1];
            }
            part = part | 16;
            break;
        }
    case 0x06:
        {
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*5] = data[i*4];
                imageRecBuffer2[i+45124/4*5] = data[i*4+1];
            }
            part = part | 32;
            break;
158
        }
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    case 0x07:
        {
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*6] = data[i*4];
                imageRecBuffer2[i+45124/4*6] = data[i*4+1];
            }
            part = part | 64;
            break;
        }
    case 0x08:
        {
            for (int i=4; i<data.size()/4; i++)
            {
                imageRecBuffer1[i+45124/4*7] = data[i*4];
                imageRecBuffer2[i+45124/4*7] = data[i*4+1];
            }
            part = part | 128;
            break;
        }
    }
    if(part==255)
    {
182

183 184 185 186
        QByteArray tmpImage1(header.toStdString().c_str(), header.toStdString().size());
        tmpImage1.append(imageRecBuffer1);
        QByteArray tmpImage2(header.toStdString().c_str(), header.toStdString().size());
        tmpImage2.append(imageRecBuffer2);
187 188

        // Load image into window
189 190 191
        //QImage test(":images/patterns/lenna.jpg");
        QImage image1;
        QImage image2;
192 193


194
        if (imageRecBuffer1.isNull())
195 196 197 198 199
            {
                qDebug()<< "could not convertToPGM()";

            }

200
            if (!image1.loadFromData(tmpImage1, "PGM"))
201
            {
202 203 204 205 206 207
                qDebug()<< "could not create extracted image1";

            }
         if (imageRecBuffer2.isNull())
            {
                qDebug()<< "could not convertToPGM()";
208 209 210

            }

211 212 213 214 215 216 217 218 219 220
            if (!image2.loadFromData(tmpImage2, "PGM"))
            {
                 qDebug()<< "could not create extracted image2";

            }
        tmpImage1.clear();
        tmpImage2.clear();
        //ui->video1Widget->copyImage(test);
        ui->video2Widget->copyImage(image1);
        ui->video3Widget->copyImage(image2);
221 222
        //ui->video4Widget->copyImage(test);
        part = 0;
223 224
        imageRecBuffer1.clear();
        imageRecBuffer2.clear();
225 226 227
    }


228

229 230 231 232
    index.append(QString().sprintf("%02x ", i0));

    for (int j=0; j<data.size(); j++) {
        unsigned char v = data[j];
233
        bytes.append(QString().sprintf("%02x ", v));
234
        if (data.at(j) > 31 && data.at(j) < 127)
235
        {
236
            ascii.append(data.at(j));
237 238 239 240 241
        }
        else
        {
            ascii.append(219);
        }
242

243
    }
244 245 246 247 248
     qDebug() << "Received" << data.size() << "bytes";
     qDebug() << "index: " <<index;
    //qDebug() << bytes;
    //qDebug() << "ASCII:" << ascii;

249 250 251 252



}