X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=gui%2Fimageviewer.cpp;h=a795993fb6c7b538128e477e3b625cbff9f0a4fc;hb=a69f0de6b922160bec029cfd18b5002b904afccf;hp=f4ba76640277f0edace423cc4707507fb475c31e;hpb=d562b8ea67082b09802d0b1818a975d4be680073;p=apitrace diff --git a/gui/imageviewer.cpp b/gui/imageviewer.cpp index f4ba766..a795993 100644 --- a/gui/imageviewer.cpp +++ b/gui/imageviewer.cpp @@ -1,4 +1,8 @@ #include "imageviewer.h" +#include "pixelwidget.h" +#include "apisurface.h" + +#include "image/image.hpp" #include #include @@ -7,7 +11,8 @@ #include ImageViewer::ImageViewer(QWidget *parent) - : QDialog(parent) + : QDialog(parent), + m_image(0) { setupUi(this); @@ -17,6 +22,10 @@ ImageViewer::ImageViewer(QWidget *parent) SLOT(slotUpdate())); connect(flipCheckBox, SIGNAL(stateChanged(int)), SLOT(slotUpdate())); + connect(opaqueCheckBox, SIGNAL(stateChanged(int)), + SLOT(slotUpdate())); + connect(alphaCheckBox, SIGNAL(stateChanged(int)), + SLOT(slotUpdate())); QPixmap px(32, 32); QPainter p(&px); @@ -30,14 +39,34 @@ ImageViewer::ImageViewer(QWidget *parent) pal.setBrush(QPalette::Base, QBrush(px)); scrollAreaWidgetContents->setPalette(pal); + + m_pixelWidget = new PixelWidget(scrollAreaWidgetContents); + verticalLayout_2->addWidget(m_pixelWidget); + + rectLabel->hide(); + pixelLabel->hide(); + + connect(m_pixelWidget, SIGNAL(zoomChanged(int)), + zoomSpinBox, SLOT(setValue(int))); + connect(zoomSpinBox, SIGNAL(valueChanged(int)), + m_pixelWidget, SLOT(setZoom(int))); + connect(m_pixelWidget, SIGNAL(mousePosition(int, int)), + this, SLOT(showPixel(int, int))); + connect(m_pixelWidget, SIGNAL(gridGeometry(const QRect &)), + this, SLOT(showGrid(const QRect &))); +} + +ImageViewer::~ImageViewer() +{ + delete m_image; } -void ImageViewer::setImage(const QImage &image) +void ImageViewer::setBase64Data(const QByteArray &base64) { - m_image = image; - m_temp = m_image; - QPixmap px = QPixmap::fromImage(m_temp); - imageLabel->setPixmap(px); + delete m_image; + m_image = ApiSurface::imageFromBase64(base64); + m_convertedImage = ApiSurface::qimageFromRawImage(m_image); + m_pixelWidget->setSurface(m_convertedImage); updateGeometry(); } @@ -54,12 +83,16 @@ static inline int clamp(int x) void ImageViewer::slotUpdate() { - m_temp = m_image.mirrored(false, flipCheckBox->isChecked()); + m_convertedImage = + m_convertedImage.mirrored(false, flipCheckBox->isChecked()); double lowerValue = lowerSpinBox->value(); double upperValue = upperSpinBox->value(); - if (lowerValue != 0.0 || upperValue != 1.0) { + bool opaque = opaqueCheckBox->isChecked(); + bool alpha = alphaCheckBox->isChecked(); + + if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) { /* * Rescale the image. * @@ -70,7 +103,8 @@ void ImageViewer::slotUpdate() int offset = - lowerValue * 255; int scale = 256 / (upperValue - lowerValue); - m_temp = m_temp.convertToFormat(QImage::Format_ARGB32); + m_convertedImage = + m_convertedImage.convertToFormat(QImage::Format_ARGB32); if (0) { qDebug() @@ -78,27 +112,34 @@ void ImageViewer::slotUpdate() << "scale = " << scale << "\n"; } - int width = m_temp.width(); - int height = m_temp.height(); + int width = m_convertedImage.width(); + int height = m_convertedImage.height(); + + int aMask = opaque ? 0xff : 0; for (int y = 0; y < height; ++y) { - QRgb *scanline = (QRgb *)m_temp.scanLine(y); + QRgb *scanline = (QRgb *)m_convertedImage.scanLine(y); for (int x = 0; x < width; ++x) { QRgb pixel = scanline[x]; int r = qRed(pixel); int g = qGreen(pixel); int b = qBlue(pixel); - r = clamp(((r + offset) * scale) >> 8); - g = clamp(((g + offset) * scale) >> 8); - b = clamp(((b + offset) * scale) >> 8); - int a = 255; - scanline[x] = qRgba(r, g, b, a); + int a = qAlpha(pixel); + if (alpha) { + a = clamp(((a + offset) * scale) >> 8); + scanline[x] = qRgba(a, a, a, 0xff); + } else { + r = clamp(((r + offset) * scale) >> 8); + g = clamp(((g + offset) * scale) >> 8); + b = clamp(((b + offset) * scale) >> 8); + a |= aMask; + scanline[x] = qRgba(r, g, b, a); + } } } } - QPixmap px = QPixmap::fromImage(m_temp); - imageLabel->setPixmap(px); + m_pixelWidget->setSurface(m_convertedImage); updateGeometry(); } @@ -111,11 +152,11 @@ QSize ImageViewer::sizeHint() const scrollArea->verticalScrollBar()->width() : 0; int hScrollHeight = scrollArea->horizontalScrollBar() ? scrollArea->horizontalScrollBar()->height() : 0; - QSize optimalWindowSize(m_image.width() + vScrollWidth, - m_image.height() + hScrollHeight); + QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth, + m_convertedImage.height() + hScrollHeight); QRect screenRect = QApplication::desktop()->availableGeometry(); - const float maxPercentOfDesktopSpace = 0.8; + const float maxPercentOfDesktopSpace = 0.8f; QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(), maxPercentOfDesktopSpace * screenRect.height()); @@ -123,4 +164,65 @@ QSize ImageViewer::sizeHint() const qMin(optimalWindowSize.height(), maxAvailableSize.height())); } +void ImageViewer::resizeEvent(QResizeEvent *e) +{ + QWidget::resizeEvent(e); +} + +template +QString createPixelLabel(image::Image *img, int x, int y) +{ + QString pixelLabel; + unsigned char *pixelLocation = 0; + T *pixel; + + pixelLocation = img->pixels + img->stride() * y; + pixelLocation += x * img->bytesPerPixel; + pixel = ((T*)pixelLocation); + + pixelLabel += QLatin1String("["); + pixelLabel += QString::fromLatin1("%1").arg(pixel[0]); + + for (int channel = 1; channel < img->channels; ++channel) { + pixelLabel += QString::fromLatin1(", %1").arg(pixel[channel]); + } + pixelLabel += QLatin1String("]"); + + return pixelLabel; +} + +void ImageViewer::showPixel(int x, int y) +{ + xSpinBox->setValue(x); + ySpinBox->setValue(y); + + if (!m_image) + return; + + QString label = tr("Pixel: "); + + if (m_image->channelType == image::TYPE_UNORM8) { + label += createPixelLabel(m_image, x, y); + } else { + label += createPixelLabel(m_image, x, y); + } + + pixelLabel->setText(label); + pixelLabel->show(); +} + +void ImageViewer::showGrid(const QRect &rect) +{ + if (rect.isEmpty()) { + rectLabel->hide(); + return; + } + rectLabel->setText(tr("Grid: [%1, %2, %3, %4]") + .arg(rect.x()) + .arg(rect.y()) + .arg(rect.width()) + .arg(rect.height())); + rectLabel->show(); +} + #include "imageviewer.moc"