qtDistortionDialog.h 2.96 KB
#ifndef DISTORTIONDIALOG_H
#define DISTORTIONDIALOG_H

#include <QtWidgets/QDialog>
#include <QDragEnterEvent>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <qfiledialog.h>
#include <qinputdialog.h>
#include "ui_distortiondialog.h"
#include "globals.h"

extern QGraphicsScene* distortionScene;
extern QGraphicsView* distortionWindow;
extern QGraphicsPixmapItem* pixmapItem;

class qtDistortionDialog : public QDialog
{
    Q_OBJECT

public:
    qtDistortionDialog(QWidget *parent = 0, Qt::WindowFlags flags = 0);
    ~qtDistortionDialog();
    bool updating;

private:
    Ui::DistortionDialogClass ui;

public slots:
    void on_btnComputeDistortionMap_clicked() {

        //get the filename
        QString filename = QFileDialog::getSaveFileName(NULL, "Distortion File");

        int steps = ui.spinSteps->value();

        //delete the pixmap if one previously existed
        if(pixmapItem != NULL)
            delete(pixmapItem);

        //create an image
        QImage image(steps, steps, QImage::Format_RGB888);

        QColor color(255, 255, 0);

        double minIn, maxIn, minOut, maxOut;

        minIn = ui.spinInStart->value();
        maxIn = ui.spinInEnd->value();
        minOut = ui.spinOutStart->value();
        maxOut = ui.spinOutEnd->value();

        //compute the distortion map (2D steps x steps image)
        float* distortionMap = (float*)malloc(sizeof(float) * steps * steps);
        DistortionMap(distortionMap, minIn, maxIn, minOut, maxOut, steps, filename.toUtf8().constData());

        //compute the extrema
        float minDistortion = 99999;
        float maxDistortion = distortionMap[0];
        for(int i=1; i<steps*steps; i++) {
            if(distortionMap[i] < minDistortion && distortionMap[i] > 0)
                minDistortion = distortionMap[i];
            if(distortionMap[i] > maxDistortion)
                maxDistortion = distortionMap[i];
        }
        cout<<minDistortion<<"----------"<<maxDistortion<<endl;
        FILE* outraw = fopen("out.raw", "wb");
        fwrite(distortionMap, sizeof(float), steps * steps, outraw);
        fclose(outraw);

        //compute the distortion map image
        int i, o;
        float intensity;
        float v;
        for(i=0; i<steps; i++)
            for(o=0; o<steps; o++) {
                v = distortionMap[o * steps + i];
                intensity = (v - minDistortion) / (maxDistortion - minDistortion) * 255;

                if(intensity < 0) {
                    //cout<<"low intensity"<<endl;
                    intensity = 0;
                }

                //QColor color(intensity, (float)o / (float)steps * 255, (float)i / (float)steps * 255);
                QColor color(intensity, intensity, intensity);
                image.setPixel(o, (steps-1-i), color.rgb());
            }

        pixmapItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
        distortionScene->addItem(pixmapItem);
        distortionWindow->show();


    }
};

#endif