From 72935e928835080d055fa68e154000e6a4940d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Sun, 17 May 2020 17:59:39 +0200 Subject: [PATCH] Save image into file --- image.frag | 5 ----- imagescrollareagl.cpp | 45 ++++++++++++++++++++++++++++++++----------- imagescrollareagl.h | 2 ++ mainwindow.cpp | 12 ++++++++++++ mainwindow.h | 1 + rawimage.cpp | 2 ++ stretchpanel.cpp | 3 +++ 7 files changed, 54 insertions(+), 16 deletions(-) diff --git a/image.frag b/image.frag index e7fa6fa..6e5916a 100644 --- a/image.frag +++ b/image.frag @@ -6,11 +6,6 @@ uniform vec2 scale; uniform float a; uniform int stretch; -float asinh_(float x) -{ - return log(x + sqrt(x*x + 1.0f)); -} - void main(void) { vec4 color = texture2D(qt_Texture0, qt_TexCoord0); diff --git a/imagescrollareagl.cpp b/imagescrollareagl.cpp index 65f7d67..023746f 100644 --- a/imagescrollareagl.cpp +++ b/imagescrollareagl.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include void setScrollRange(QScrollBar *scrollBar, int newRange) @@ -17,14 +18,6 @@ void setScrollRange(QScrollBar *scrollBar, int newRange) scrollBar->setValue(relPos*newRange - page/2); } -void setRelativePos(QScrollBar *scrollBar, float relPos) -{ - int page = scrollBar->pageStep(); - int pos = scrollBar->value()+page/2; - int range = scrollBar->maximum()+page; - scrollBar->setValue(pos); -} - ImageWidget::ImageWidget(QWidget *parent) : QOpenGLWidget(parent) { setFocusPolicy(Qt::ClickFocus); @@ -34,6 +27,8 @@ ImageWidget::ImageWidget(QWidget *parent) : QOpenGLWidget(parent) m_dx = m_dy = 0; m_scale = 1.0f; m_blockRepaint = false; + m_range = UINT16_MAX; + m_imgWidth = m_imgHeight = -1; } ImageWidget::~ImageWidget() @@ -45,6 +40,9 @@ void ImageWidget::setImage(RawImage *image) { if(image == nullptr)return; + m_imgWidth = image->width(); + m_imgHeight = image->height(); + m_image->destroy(); m_image->setFormat(QOpenGLTexture::R16_UNorm); m_image->setSize(image->width(), image->height()); @@ -56,13 +54,14 @@ void ImageWidget::setImage(RawImage *image) { case RawImage::UINT8: m_image->setData(0, QOpenGLTexture::Red, QOpenGLTexture::UInt8, image->data(), m_transferOptions.get()); - m_range = UINT8_MAX; break; case RawImage::UINT16: m_image->setData(0, QOpenGLTexture::Red, QOpenGLTexture::UInt16, image->data(), m_transferOptions.get()); - m_range = UINT16_MAX; break; case RawImage::FLOAT32: + m_image->setData(0, QOpenGLTexture::Red, QOpenGLTexture::Float32, image->data(), m_transferOptions.get()); + break; + default: break; } m_image->generateMipMaps(); @@ -73,6 +72,9 @@ void ImageWidget::setImage(const QPixmap &pixmap) { QImage img = pixmap.toImage(); + m_imgWidth = pixmap.width(); + m_imgHeight = pixmap.height(); + m_image->destroy(); m_image->setData(img); m_image->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::Linear); @@ -125,6 +127,27 @@ void ImageWidget::setOffset(int dx, int dy) update(); } +QImage ImageWidget::renderToImage() +{ + if(m_imgWidth < 0)return QImage(); + makeCurrent(); + QOpenGLFramebufferObject fbo(m_imgWidth, m_imgHeight); + fbo.bind(); + + f->glViewport(0, 0, m_imgWidth, m_imgHeight); + + m_program->bind(); + m_program->setUniformValue("viewport", (float)m_imgWidth, (float)m_imgHeight); + m_program->setUniformValue("offset", 0.0f, 0.0f); + m_program->setUniformValue("zoom", 1.0f); + + m_image->bind(0); + f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + fbo.bindDefault(); + return fbo.toImage(true); +} + void ImageWidget::paintGL() { if(m_blockRepaint)return; @@ -218,7 +241,7 @@ void ImageWidget::initializeGL() m_transferOptions->setAlignment(1); } -ImageScrollAreaGL::ImageScrollAreaGL(QWidget *parent) +ImageScrollAreaGL::ImageScrollAreaGL(QWidget *parent) : QWidget(parent) { QGridLayout *layout = new QGridLayout(this); setLayout(layout); diff --git a/imagescrollareagl.h b/imagescrollareagl.h index cda253f..5a48d9d 100644 --- a/imagescrollareagl.h +++ b/imagescrollareagl.h @@ -31,6 +31,7 @@ class ImageWidget : public QOpenGLWidget std::unique_ptr m_vao; std::unique_ptr m_transferOptions; int m_width, m_height; + int m_imgWidth, m_imgHeight; float m_low; float m_high; int m_stretch; @@ -52,6 +53,7 @@ public slots: void setStrech(int stretch); void setStretchParam(float param); void setOffset(int dx, int dy); + QImage renderToImage(); protected: void paintGL(); void resizeGL(int w, int h); diff --git a/mainwindow.cpp b/mainwindow.cpp index 69b7db3..055b28c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -69,6 +69,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) QMenu *fileMenu = new QMenu(tr("File"), this); fileMenu->addAction(tr("Open"), this, SLOT(openFile()), QKeySequence("Ctrl+O")); fileMenu->addAction(tr("Copy marked files"), this, SLOT(copyMarked())); + fileMenu->addAction(tr("Save as"), this, SLOT(saveAs()), QKeySequence("Ctrl+S")); QAction *liveModeAction = fileMenu->addAction(tr("Live mode"), this, SLOT(liveMode(bool))); liveModeAction->setCheckable(true); fileMenu->addAction(tr("Exit"), this, SLOT(close())); @@ -230,6 +231,17 @@ void MainWindow::openFile() } } +void MainWindow::saveAs() +{ + QString file = QFileDialog::getSaveFileName(this, tr("Save as"), _lastDir, tr("Images (*.jpg *.png *.JPG *.PNG)")); + if(!file.isEmpty()) + { + QImage img = m_imageGL->imageWidget()->renderToImage(); + if(!img.isNull()) + img.save(file); + } +} + void MainWindow::markImage() { ImagePtr ptr = m_ringList->currentImage(); diff --git a/mainwindow.h b/mainwindow.h index ece8e06..f7207f0 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -34,6 +34,7 @@ protected slots: void updateWindowTitle(); void pixmapLoaded(Image *image); void openFile(); + void saveAs(); void markImage(); void unmarkImage(); void markAndNext(); diff --git a/rawimage.cpp b/rawimage.cpp index a23558f..e4e0898 100644 --- a/rawimage.cpp +++ b/rawimage.cpp @@ -29,6 +29,8 @@ int Type2CV(RawImage::ImgType type) return CV_32F; case RawImage::UNKNOWN: return CV_8S; + default: + return CV_8U; } } diff --git a/stretchpanel.cpp b/stretchpanel.cpp index 6a83550..3132ef0 100644 --- a/stretchpanel.cpp +++ b/stretchpanel.cpp @@ -11,9 +11,12 @@ StretchPanel::StretchPanel(QWidget *parent) : QWidget(parent) m_paramSlider = new QSlider(Qt::Horizontal, this); m_lowSlider->setRange(0, UINT16_MAX); + m_lowSlider->setPageStep(512); m_highSlider->setRange(0, UINT16_MAX); + m_highSlider->setPageStep(512); m_highSlider->setValue(UINT16_MAX); m_paramSlider->setRange(0, UINT16_MAX); + m_paramSlider->setPageStep(512); m_stretchSelect = new QComboBox(this); m_stretchSelect->addItems({tr("Linear"), tr("Square root"), tr("Power"), tr("Logarithm"), tr("Asinh")});