QGCVideoMainWindow.cc 9.31 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();
63 64 65 66 67 68
}

QGCVideoMainWindow::~QGCVideoMainWindow()
{
    delete ui;
}
69 70 71 72 73 74 75 76 77 78 79

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;
80
    QString index;
81
    QString imageid;
82
    QString ascii;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98



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

99
    unsigned char i0 = data[0];
100 101 102 103 104
    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;
105 106

    switch (i0)
107
    {
108
    case 0x01:
109
        {
110 111 112 113 114 115 116 117
            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)
118
            {
119
                part = 0;
120
            }
121 122
            part = part | 1;
            break;
123
        }
124
    case 0x02:
125
        {
126
            for (int i=0; i<data.size()/4-1; i++)
127
            {
128 129 130 131 132 133 134 135
                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;
136
            }
137 138
            part = part | 2;
            break;
139
        }
140 141
    case 0x03:
        {
142 143 144 145 146 147 148 149
            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)
150
            {
151
                part = 0;
152 153 154 155 156 157
            }
            part = part | 4;
            break;
        }
    case 0x04:
        {
158
            for (int i=0; i<data.size()/4-1; i++)
159
            {
160 161 162 163 164 165 166 167
                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;
168 169 170 171 172
            }
            part = part | 8;
            break;
        }
    case 0x05:
173
        {
174 175 176 177 178 179 180 181
            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)
182
            {
183
                part = 0;
184 185 186 187 188 189
            }
            part = part | 16;
            break;
        }
    case 0x06:
        {
190
            for (int i=0; i<data.size()/4-1; i++)
191
            {
192 193 194 195 196 197 198 199
                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;
200 201 202
            }
            part = part | 32;
            break;
203
        }
204 205
    case 0x07:
        {
206
            for (int i=0; i<data.size()/4-1; i++)
207
            {
208 209 210 211 212 213 214 215
                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;
216 217 218 219 220 221
            }
            part = part | 64;
            break;
        }
    case 0x08:
        {
222 223 224 225 226 227 228 229
            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)
230
            {
231
                part = 0;
232 233 234 235 236
            }
            part = part | 128;
            break;
        }
    }
237 238 239 240

    last_id = id;


241 242
    if(part==255)
    {
243

244 245 246 247
        QByteArray tmpImage1(header.toStdString().c_str(), header.toStdString().size());
        tmpImage1.append(imageRecBuffer1);
        QByteArray tmpImage2(header.toStdString().c_str(), header.toStdString().size());
        tmpImage2.append(imageRecBuffer2);
248 249 250 251
        QByteArray tmpImage3(header.toStdString().c_str(), header.toStdString().size());
        tmpImage3.append(imageRecBuffer3);
        QByteArray tmpImage4(header.toStdString().c_str(), header.toStdString().size());
        tmpImage4.append(imageRecBuffer4);
252 253

        // Load image into window
254 255 256
        //QImage test(":images/patterns/lenna.jpg");
        QImage image1;
        QImage image2;
257 258
        QImage image3;
        QImage image4;
259 260


261
        if (imageRecBuffer1.isNull())
262 263
        {
            qDebug()<< "could not convertToPGM()";
264

265
        }
266

267 268 269
        if (!image1.loadFromData(tmpImage1, "PGM"))
        {
            qDebug()<< "could not create extracted image1";
270

271 272 273 274
        }
        if (imageRecBuffer2.isNull())
        {
            qDebug()<< "could not convertToPGM()";
275

276
        }
277

278 279 280
        if (!image2.loadFromData(tmpImage2, "PGM"))
        {
            qDebug()<< "could not create extracted image2";
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        }
        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";

        }
305 306
        tmpImage1.clear();
        tmpImage2.clear();
307 308
        tmpImage3.clear();
        tmpImage4.clear();
309
        //ui->video1Widget->copyImage(test);
310 311 312 313
        ui->video1Widget->copyImage(image1);
        ui->video2Widget->copyImage(image2);
        ui->video3Widget->copyImage(image3);
        ui->video4Widget->copyImage(image4);
314
        part = 0;
315 316
        imageRecBuffer1.clear();
        imageRecBuffer2.clear();
317 318
        imageRecBuffer3.clear();
        imageRecBuffer4.clear();
319 320 321
    }


322

323

324 325

   /* for (int j=0; j<data.size(); j++) {
326
        unsigned char v = data[j];
327
        bytes.append(QString().sprintf("%02x ", v));
328
        if (data.at(j) > 31 && data.at(j) < 127)
329
        {
330
            ascii.append(data.at(j));
331 332 333 334 335
        }
        else
        {
            ascii.append(219);
        }
336

337 338
    }*/

339 340 341
    //qDebug() << bytes;
    //qDebug() << "ASCII:" << ascii;

342 343 344 345



}