QGCVideoMainWindow.cc 9.89 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
  QByteArray imageRecBuffer3 = QByteArray(376*240,255);
  QByteArray imageRecBuffer4 = QByteArray(376*240,255);
42
  static int part = 0;
43
  unsigned char last_id = 0;
44

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

    // 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();
LM's avatar
LM committed
63 64 65 66 67 68 69 70 71 72 73 74

    // Show flow // FIXME
    int xCount = 16;
    int yCount = 5;

    unsigned char flowX[xCount][yCount];
    unsigned char flowY[xCount][yCount];

    flowX[3][3] = 10;
    flowY[3][3] = 5;

    ui->video4Widget->copyFlow((const unsigned char*)flowX, (const unsigned char*)flowY, xCount, yCount);
75 76 77 78 79 80
}

QGCVideoMainWindow::~QGCVideoMainWindow()
{
    delete ui;
}
81 82 83 84 85 86 87 88 89 90 91

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;
92
    QString index;
93
    QString imageid;
94
    QString ascii;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110



    // 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);

111
    unsigned char i0 = data[0];
112 113 114 115 116
    unsigned char id = data[1];

    index.append(QString().sprintf("%02x", i0));
    imageid.append(QString().sprintf("%02x", id));
     qDebug() << "Received" << data.size() << "bytes"<< " part: " <<index<< " imageid: " <<imageid;
117 118

    switch (i0)
119
    {
120
    case 0x01:
121
        {
122 123 124 125 126 127 128 129
            for (int i=0; i<data.size()/4-1; i++)
            {
                imageRecBuffer1[i] = data[i*4+4];
                imageRecBuffer2[i] = data[i*4+5]+127;
                imageRecBuffer3[i] = data[i*4+6]+127;
                imageRecBuffer4[i] = data[i*4+7];
            }
            if(id != last_id)
130
            {
131
                part = 0;
132
            }
133 134
            part = part | 1;
            break;
135
        }
136
    case 0x02:
137
        {
138
            for (int i=0; i<data.size()/4-1; i++)
139
            {
140 141 142 143 144 145 146 147
                imageRecBuffer1[i+45120/4] = data[i*4+4];
                imageRecBuffer2[i+45120/4] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4] = data[i*4+7];
            }
            if(id != last_id)
            {
                part = 0;
148
            }
149 150
            part = part | 2;
            break;
151
        }
152 153
    case 0x03:
        {
154 155 156 157 158 159 160 161
            for (int i=0; i<data.size()/4-1; i++)
            {
                imageRecBuffer1[i+45120/4*2] = data[i*4+4];
                imageRecBuffer2[i+45120/4*2] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*2] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*2] = data[i*4+7];
            }
            if(id != last_id)
162
            {
163
                part = 0;
164 165 166 167 168 169
            }
            part = part | 4;
            break;
        }
    case 0x04:
        {
170
            for (int i=0; i<data.size()/4-1; i++)
171
            {
172 173 174 175 176 177 178 179
                imageRecBuffer1[i+45120/4*3] = data[i*4+4];
                imageRecBuffer2[i+45120/4*3] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*3] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*3] = data[i*4+7];
            }
            if(id != last_id)
            {
                part = 0;
180 181 182 183 184
            }
            part = part | 8;
            break;
        }
    case 0x05:
185
        {
186 187 188 189 190 191 192 193
            for (int i=0; i<data.size()/4-1; i++)
            {
                imageRecBuffer1[i+45120/4*4] = data[i*4+4];
                imageRecBuffer2[i+45120/4*4] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*4] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*4] = data[i*4+7];
            }
            if(id != last_id)
194
            {
195
                part = 0;
196 197 198 199 200 201
            }
            part = part | 16;
            break;
        }
    case 0x06:
        {
202
            for (int i=0; i<data.size()/4-1; i++)
203
            {
204 205 206 207 208 209 210 211
                imageRecBuffer1[i+45120/4*5] = data[i*4+4];
                imageRecBuffer2[i+45120/4*5] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*5] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*5] = data[i*4+7];
            }
            if(id != last_id)
            {
                part = 0;
212 213 214
            }
            part = part | 32;
            break;
215
        }
216 217
    case 0x07:
        {
218
            for (int i=0; i<data.size()/4-1; i++)
219
            {
220 221 222 223 224 225 226 227
                imageRecBuffer1[i+45120/4*6] = data[i*4+4];
                imageRecBuffer2[i+45120/4*6] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*6] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*6] = data[i*4+7];
            }
            if(id != last_id)
            {
                part = 0;
228 229 230 231 232 233
            }
            part = part | 64;
            break;
        }
    case 0x08:
        {
234 235 236 237 238 239 240 241
            for (int i=0; i<data.size()/4-1; i++)
            {
                imageRecBuffer1[i+45120/4*7] = data[i*4+4];
                imageRecBuffer2[i+45120/4*7] = data[i*4+5]+127;
                imageRecBuffer3[i+45120/4*7] = data[i*4+6]+127;
                imageRecBuffer4[i+45120/4*7] = data[i*4+7];
            }
            if(id != last_id)
242
            {
243
                part = 0;
244 245 246 247 248
            }
            part = part | 128;
            break;
        }
    }
249 250 251 252

    last_id = id;


253 254
    if(part==255)
    {
255

256 257 258 259
        QByteArray tmpImage1(header.toStdString().c_str(), header.toStdString().size());
        tmpImage1.append(imageRecBuffer1);
        QByteArray tmpImage2(header.toStdString().c_str(), header.toStdString().size());
        tmpImage2.append(imageRecBuffer2);
260 261 262 263
        QByteArray tmpImage3(header.toStdString().c_str(), header.toStdString().size());
        tmpImage3.append(imageRecBuffer3);
        QByteArray tmpImage4(header.toStdString().c_str(), header.toStdString().size());
        tmpImage4.append(imageRecBuffer4);
264 265

        // Load image into window
266 267 268
        //QImage test(":images/patterns/lenna.jpg");
        QImage image1;
        QImage image2;
269 270
        QImage image3;
        QImage image4;
271 272


273
        if (imageRecBuffer1.isNull())
274 275
        {
            qDebug()<< "could not convertToPGM()";
276

277
        }
278

279 280 281
        if (!image1.loadFromData(tmpImage1, "PGM"))
        {
            qDebug()<< "could not create extracted image1";
282

283 284 285 286
        }
        if (imageRecBuffer2.isNull())
        {
            qDebug()<< "could not convertToPGM()";
287

288
        }
289

290 291 292
        if (!image2.loadFromData(tmpImage2, "PGM"))
        {
            qDebug()<< "could not create extracted image2";
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        }
        if (imageRecBuffer3.isNull())
        {
            qDebug()<< "could not convertToPGM()";

        }

        if (!image3.loadFromData(tmpImage3, "PGM"))
        {
            qDebug()<< "could not create extracted image3";

        }
        if (imageRecBuffer4.isNull())
        {
            qDebug()<< "could not convertToPGM()";

        }

        if (!image4.loadFromData(tmpImage4, "PGM"))
        {
            qDebug()<< "could not create extracted image3";

        }
317 318
        tmpImage1.clear();
        tmpImage2.clear();
319 320
        tmpImage3.clear();
        tmpImage4.clear();
321
        //ui->video1Widget->copyImage(test);
322 323 324 325
        ui->video1Widget->copyImage(image1);
        ui->video2Widget->copyImage(image2);
        ui->video3Widget->copyImage(image3);
        ui->video4Widget->copyImage(image4);
326
        part = 0;
327 328
        imageRecBuffer1.clear();
        imageRecBuffer2.clear();
329 330
        imageRecBuffer3.clear();
        imageRecBuffer4.clear();
LM's avatar
LM committed
331 332 333 334 335 336 337 338 339 340

        ui->video4Widget->enableFlow(true);

        int xCount = 16;
        int yCount = 5;

        unsigned char flowX[xCount][yCount];
        unsigned char flowY[xCount][yCount];

        ui->video4Widget->copyFlow((const unsigned char*)flowX, (const unsigned char*)flowY, xCount, yCount);
341 342 343
    }


344

345

346 347

   /* for (int j=0; j<data.size(); j++) {
348
        unsigned char v = data[j];
349
        bytes.append(QString().sprintf("%02x ", v));
350
        if (data.at(j) > 31 && data.at(j) < 127)
351
        {
352
            ascii.append(data.at(j));
353 354 355 356 357
        }
        else
        {
            ascii.append(219);
        }
358

359 360
    }*/

361 362 363
    //qDebug() << bytes;
    //qDebug() << "ASCII:" << ascii;

364 365 366 367



}