From 2959aae640147468d99615d51d244b424ab6d58f Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Jun 2015 21:43:25 -0500 Subject: [PATCH] added support for large images --- mainwindow.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- mainwindow.h | 8 ++++++-- mainwindow.ui | 12 ++++++------ spectrumplot.cpp | 0 spectrumplot.h | 43 ------------------------------------------- spectrumwindow.cpp | 46 +++++++++++++++++++++++++++++++++------------- spectrumwindow.h | 5 ++++- win32/Qt5Core.dll | Bin 4107264 -> 0 bytes win32/Qt5PrintSupport.dll | Bin 268288 -> 0 bytes win32/Qt5QuickWidgets.dll | Bin 0 -> 51712 bytes win32/hsiview.exe | Bin 509952 -> 0 bytes win32/platforms/qwindows.dll | Bin 906752 -> 0 bytes 12 files changed, 104 insertions(+), 78 deletions(-) delete mode 100644 spectrumplot.cpp delete mode 100644 spectrumplot.h create mode 100644 win32/Qt5QuickWidgets.dll diff --git a/mainwindow.cpp b/mainwindow.cpp index 22c5018..74cfc50 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,7 +20,7 @@ void MainWindow::loadImage(QString fileName){ hsi.open(file, file + ".hdr"); //initialize the size of the main window - resize(hsi.header.samples+2, hsi.header.lines + ui->menuBar->size().height()); + //resize(hsi.header.samples+2, hsi.header.lines + ui->menuBar->size().height()); //fill the x axis with wavelength numbers from the hsi x = QVector::fromStdVector(hsi.header.wavelength); @@ -29,7 +29,7 @@ void MainWindow::loadImage(QString fileName){ load_band(x.size()/2); //set the pick position to the center of the image - p = QPoint((int)(image.width()/2), (int)(image.height()/2)); + px = py = 0.5; //create a graphics scene to store the image and any drawn UI elements scene = new QGraphicsScene(this); @@ -45,6 +45,7 @@ void MainWindow::loadImage(QString fileName){ draw_viewport(); } + /// Update the image viewing widget so that it always fills the main window void MainWindow::resizeEvent(QResizeEvent* event){ @@ -53,14 +54,32 @@ void MainWindow::resizeEvent(QResizeEvent* event){ //get the current position of the QGraphicsView object QPoint imagepos = ui->graphicsView->pos(); - + + //calculate the maximum allowable image width and height given the user's selected window size + unsigned int maxwidth = winsize.width() - imagepos.x(); + unsigned int maxheight = winsize.height() - imagepos.y(); + + //get the size of the image + QSize imagesize = image.size(); + + //calculate the aspect ratio + //float aspect = imagesize.width() / imagesize.height(); + float width_ratio = imagesize.width() / maxwidth; + float height_ratio = imagesize.height() / maxheight; + + //if the width requires the largest scaling + if(width_ratio >= height_ratio) + main_image = image.scaledToWidth(maxwidth); + else + main_image = image.scaledToHeight(maxheight); + //update the size of the QGraphicsView object // the QGraphicsView will be either the size of the image or // the size of the window (whichever is smaller) - QSize viewsize = QSize( std::min(winsize.width() - imagepos.x(), image.width()+2), - std::min(winsize.height() - imagepos.y(), image.height()+2) ); + QSize viewsize = QSize( main_image.width()+2, main_image.height()+2 ); ui->graphicsView->setFixedSize(viewsize.width(), viewsize.height()); + draw_viewport(); } /// Update everything associated with when the image is clicked @@ -70,13 +89,20 @@ void MainWindow::mousePressEvent(QMouseEvent * event){ QPoint winp = event->pos(); //get the position of the QGraphicsView object (upper left corner of the image) - QPoint imagep = ui->graphicsView->pos(); + QPoint viewp = ui->graphicsView->pos(); //get the size of the menu bar int menu_height = ui->menuBar->size().height(); - //calculate the position of the click in the image - p = winp - imagep - QPoint(0, menu_height); + //calculate the position of the click in the graphics view + QPoint imagep = winp - viewp - QPoint(0, menu_height); + + //get the size of the graphics view object + QSize viewsize = ui->graphicsView->size(); + + //calculate the position of the point in the image space [0, 1] + px = (float)imagep.x() / (float) viewsize.width(); + py = (float)imagep.y() / (float) viewsize.height(); //load the spectrum associated with p load_spectrum(); @@ -129,6 +155,16 @@ void MainWindow::load_band(unsigned int i){ QImage::Format_RGB888 ) ); + + //get the main image size + QSize main_size = main_image.size(); + + //scale the band image + main_image = image.scaledToHeight(main_size.height()); + + //free the buffer and band memory + free(buffer); + free(band); } void MainWindow::draw_viewport(){ @@ -136,10 +172,10 @@ void MainWindow::draw_viewport(){ scene->clear(); //add the image to the scene - scene->addPixmap(image); + scene->addPixmap(main_image); //I'm not sure what this does... - scene->setSceneRect(image.rect()); + scene->setSceneRect(main_image.rect()); //draw the overlay indicating the selected pixel draw_overlay(); @@ -154,8 +190,11 @@ void MainWindow::draw_overlay(){ //get the size of the QGraphicsView object QSize image_size = ui->graphicsView->size(); - scene->addLine(0, p.y(), image_size.width(), p.y()); - scene->addLine(p.x(), 0, p.x(), image_size.height()); + //calculate the pixel position (index) into the main image + QPoint pi = QPoint(px * image_size.width(), py * image_size.height()); + + scene->addLine(0, pi.y(), image_size.width(), pi.y()); + scene->addLine(pi.x(), 0, pi.x(), image_size.height()); } /// Loads the spectrum from the picked point of the HSI and stores it in the member variable y @@ -164,8 +203,11 @@ void MainWindow::load_spectrum(){ //allocate the appropriate amount of memory for the spectrum float* temp_y = (float*) malloc(x.size() * sizeof(float)); + //pixel position in the band image + QPoint pi = QPoint(px * image.width(), py * image.height()); + //load the spectrum from the HSI - hsi.spectrum(temp_y, p.x(), p.y()); + hsi.spectrum(temp_y, pi.x(), pi.y()); //clear the previous spectrum y.clear(); diff --git a/mainwindow.h b/mainwindow.h index 96b390a..368aa50 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,8 +33,12 @@ private slots: private: Ui::MainWindow *ui; QGraphicsScene* scene; //scene containing the image and any drawn UI elements - QPixmap image; //stores the pixels for the displayed image - QPoint p; //stores the picked position of the image + QPixmap image; //stores the pixels for the current band + QPixmap main_image; //stores the pixels for the image displayed in the main view (may be scaled) + + //coordinates of the picked position in image space [0, 1] + double px; + double py; SpectrumWindow* s; //window displaying the spectrum of the selected pixel diff --git a/mainwindow.ui b/mainwindow.ui index 2c70553..f425e2e 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 361 - 377 + 780 + 733 @@ -19,8 +19,8 @@ 0 0 - 361 - 331 + 781 + 701 @@ -33,8 +33,8 @@ 0 0 - 361 - 21 + 780 + 31 diff --git a/spectrumplot.cpp b/spectrumplot.cpp deleted file mode 100644 index e69de29..0000000 --- a/spectrumplot.cpp +++ /dev/null diff --git a/spectrumplot.h b/spectrumplot.h deleted file mode 100644 index 1616233..0000000 --- a/spectrumplot.h +++ /dev/null @@ -1,43 +0,0 @@ -/*#ifndef SPECTRUMPLOT_H -#define SPECTRUMPLOT_H - -#include "qcustomplot.h" - -//stim libraries -#include "stim/envi/envi.h" - -namespace Ui { -class MainWindow; -} - -class SpectrumPlot : public QCustomPlot -{ - Q_OBJECT - -public: - //explicit MainWindow(QWidget *parent = 0); - //~MainWindow(); - - //void mousePressEvent(QMouseEvent* event); //handles when the image is clicked - //void resizeEvent(QResizeEvent* event); //handles when the window is resized - -private: - //Ui::MainWindow *ui; - //QGraphicsScene* scene; //scene containing the image and any drawn UI elements - //QPixmap image; //stores the pixels for the displayed image - //QPoint p; //stores the picked position of the image - - //SpectrumWindow* s; //window displaying the spectrum of the selected pixel - - //stim::envi hsi; //data structure storing the current hyperspectral image - //QVector x; //x values for the spectrum - //QVector y; //y values for the spectrum - - //void draw_overlay(); //draws an overlay showing the picked position - //void draw_viewport(); //draws the viewport, including the image and overlay - //void load_spectrum(); //loads a spectrum from the HSI and stores it in the global variable y - //void draw_spectrum(); //sends a spectrum to the SpectrumWindow for plotting - -}; - -#endif // SPECTRUMPLOT_H*/ diff --git a/spectrumwindow.cpp b/spectrumwindow.cpp index 0de1fee..37e21f7 100644 --- a/spectrumwindow.cpp +++ b/spectrumwindow.cpp @@ -8,7 +8,16 @@ SpectrumWindow::SpectrumWindow(QWidget *parent) : ui(new Ui::SpectrumWindow) { ui->setupUi(this); + + //install an event filter to detect user clicks ui->spectrumPlot->installEventFilter(this); + + //create a line for the current band + bandline = new QCPItemStraightLine(ui->spectrumPlot); + ui->spectrumPlot->addItem(bandline); + + + } SpectrumWindow::~SpectrumWindow() @@ -25,10 +34,15 @@ void SpectrumWindow::plotSpectrum(QVector x, QVector y){ if(y[i] < y_min) y_min = y[i]; if(y[i] > y_max) y_max = y[i]; } + + //set the range of the y axis to the minimum and maximum spectrum values ui->spectrumPlot->yAxis->setRange(y_min, y_max); + //add the graph to the plot ui->spectrumPlot->addGraph(); ui->spectrumPlot->graph(0)->setData(x, y); + + //redraw the plot ui->spectrumPlot->replot(); } @@ -39,6 +53,15 @@ void SpectrumWindow::initPlot(double x_min, double x_max, double y_min, double y // set axes ranges, so we see all data: ui->spectrumPlot->xAxis->setRange(x_min, x_max); ui->spectrumPlot->yAxis->setRange(y_min, y_max); + + //set the initial position of the band line + double xmid = (x_max + x_min) / 2; + std::cout<<"xmid: "<point1->setCoords(xmid, 0); + bandline->point2->setCoords(xmid, 1); + bandline->setPen(QPen(Qt::red)); + + ui->spectrumPlot->replot(); } @@ -54,6 +77,7 @@ void SpectrumWindow::resizeEvent(QResizeEvent* event){ bool SpectrumWindow::eventFilter(QObject *target, QEvent *event) { + if(target == ui->spectrumPlot && event->type() == QEvent::MouseMove) { QMouseEvent *_mouseEvent = static_cast(event); @@ -65,6 +89,10 @@ bool SpectrumWindow::eventFilter(QObject *target, QEvent *event) //calculate the position of the click in the image double x = ui->spectrumPlot->xAxis->pixelToCoord(pixel.x()); double y = ui->spectrumPlot->yAxis->pixelToCoord(pixel.y()); + + //set the position of the band line + bandline->point1->setCoords(x, 0); + bandline->point2->setCoords(x, 1); } else if(target == ui->spectrumPlot && event->type() == QEvent::MouseButtonPress) { @@ -80,21 +108,13 @@ bool SpectrumWindow::eventFilter(QObject *target, QEvent *event) //tell the parent MainWindow that a point on the spectrum was selected ((MainWindow*)parent())->mousePressSpectrum(x, y); + + //set the position of the band line + bandline->point1->setCoords(x, 0); + bandline->point2->setCoords(x, 1); } - return false; -} -/// Update everything associated with when the image is clicked -void SpectrumWindow::mousePressEvent(QMouseEvent * event){ - //get the window position of the click - QPoint winp = event->pos(); - - - //calculate the position of the click in the image - QPoint p = winp; - - //output the image position - std::cout< #include #include +#include "qcustomplot.h" namespace Ui { class SpectrumWindow; @@ -22,10 +23,12 @@ public: void resizeEvent(QResizeEvent* event); //handles when the window is resized bool SpectrumWindow::eventFilter(QObject *target, QEvent *event); - void mousePressEvent(QMouseEvent* event); //handles when the image is clicked private: Ui::SpectrumWindow *ui; + + //line that displays the currently selected band + QCPItemStraightLine* bandline; }; #endif // SPECTRUMWINDOW_H diff --git a/win32/Qt5Core.dll b/win32/Qt5Core.dll index 3309b20..8a82275 100644 Binary files a/win32/Qt5Core.dll and b/win32/Qt5Core.dll differ diff --git a/win32/Qt5PrintSupport.dll b/win32/Qt5PrintSupport.dll index 651a213..e35b92d 100644 Binary files a/win32/Qt5PrintSupport.dll and b/win32/Qt5PrintSupport.dll differ diff --git a/win32/Qt5QuickWidgets.dll b/win32/Qt5QuickWidgets.dll new file mode 100644 index 0000000..9384caf Binary files /dev/null and b/win32/Qt5QuickWidgets.dll differ diff --git a/win32/hsiview.exe b/win32/hsiview.exe index e5dd7bd..9433efd 100644 Binary files a/win32/hsiview.exe and b/win32/hsiview.exe differ diff --git a/win32/platforms/qwindows.dll b/win32/platforms/qwindows.dll index 0a4faa7..e1e669c 100644 Binary files a/win32/platforms/qwindows.dll and b/win32/platforms/qwindows.dll differ -- libgit2 0.21.4