Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 1 | #include "imageviewer.h" |
| 2 | |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 3 | #include <QDebug> |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 4 | #include <QDesktopWidget> |
Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 5 | #include <QPainter> |
| 6 | #include <QPixmap> |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 7 | #include <QScrollBar> |
Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 8 | |
| 9 | ImageViewer::ImageViewer(QWidget *parent) |
| 10 | : QDialog(parent) |
| 11 | { |
| 12 | setupUi(this); |
| 13 | |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 14 | connect(lowerSpinBox, SIGNAL(valueChanged(double)), |
| 15 | SLOT(slotUpdate())); |
| 16 | connect(upperSpinBox, SIGNAL(valueChanged(double)), |
| 17 | SLOT(slotUpdate())); |
| 18 | connect(flipCheckBox, SIGNAL(stateChanged(int)), |
| 19 | SLOT(slotUpdate())); |
José Fonseca | fe13f77 | 2012-03-21 08:49:40 +0000 | [diff] [blame] | 20 | connect(opaqueCheckBox, SIGNAL(stateChanged(int)), |
| 21 | SLOT(slotUpdate())); |
gregory | 8f0aa38 | 2012-07-01 17:18:04 +0200 | [diff] [blame] | 22 | connect(alphaCheckBox, SIGNAL(stateChanged(int)), |
| 23 | SLOT(slotUpdate())); |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 24 | |
Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 25 | QPixmap px(32, 32); |
| 26 | QPainter p(&px); |
| 27 | p.fillRect(0, 0, 32, 32, Qt::white); |
| 28 | p.fillRect(0, 0, 16, 16, QColor(193, 193, 193)); |
| 29 | p.fillRect(16, 16, 16, 16, QColor(193, 193, 193)); |
| 30 | p.end(); |
| 31 | QPalette pal = scrollAreaWidgetContents->palette(); |
| 32 | pal.setBrush(QPalette::Background, |
| 33 | QBrush(px)); |
| 34 | pal.setBrush(QPalette::Base, |
| 35 | QBrush(px)); |
| 36 | scrollAreaWidgetContents->setPalette(pal); |
| 37 | } |
| 38 | |
| 39 | void ImageViewer::setImage(const QImage &image) |
| 40 | { |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 41 | m_image = image; |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 42 | m_temp = m_image; |
| 43 | QPixmap px = QPixmap::fromImage(m_temp); |
Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 44 | imageLabel->setPixmap(px); |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 45 | updateGeometry(); |
| 46 | } |
| 47 | |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 48 | static inline int clamp(int x) |
| 49 | { |
| 50 | if (x <= 0) { |
| 51 | return 0; |
| 52 | } |
| 53 | if (x > 255) { |
| 54 | return 255; |
| 55 | } |
| 56 | return x; |
| 57 | } |
| 58 | |
| 59 | void ImageViewer::slotUpdate() |
| 60 | { |
| 61 | m_temp = m_image.mirrored(false, flipCheckBox->isChecked()); |
| 62 | |
| 63 | double lowerValue = lowerSpinBox->value(); |
| 64 | double upperValue = upperSpinBox->value(); |
| 65 | |
José Fonseca | fe13f77 | 2012-03-21 08:49:40 +0000 | [diff] [blame] | 66 | bool opaque = opaqueCheckBox->isChecked(); |
gregory | 8f0aa38 | 2012-07-01 17:18:04 +0200 | [diff] [blame] | 67 | bool alpha = alphaCheckBox->isChecked(); |
José Fonseca | fe13f77 | 2012-03-21 08:49:40 +0000 | [diff] [blame] | 68 | |
gregory | 8f0aa38 | 2012-07-01 17:18:04 +0200 | [diff] [blame] | 69 | if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) { |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 70 | /* |
| 71 | * Rescale the image. |
| 72 | * |
| 73 | * XXX: This would be much more useful if done with the full precision |
| 74 | * of the original image |
| 75 | */ |
| 76 | |
| 77 | int offset = - lowerValue * 255; |
| 78 | int scale = 256 / (upperValue - lowerValue); |
| 79 | |
| 80 | m_temp = m_temp.convertToFormat(QImage::Format_ARGB32); |
| 81 | |
| 82 | if (0) { |
| 83 | qDebug() |
| 84 | << "offset = " << offset << "\n" |
| 85 | << "scale = " << scale << "\n"; |
| 86 | } |
| 87 | |
| 88 | int width = m_temp.width(); |
| 89 | int height = m_temp.height(); |
| 90 | |
José Fonseca | fe13f77 | 2012-03-21 08:49:40 +0000 | [diff] [blame] | 91 | int aMask = opaque ? 0xff : 0; |
| 92 | |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 93 | for (int y = 0; y < height; ++y) { |
| 94 | QRgb *scanline = (QRgb *)m_temp.scanLine(y); |
| 95 | for (int x = 0; x < width; ++x) { |
| 96 | QRgb pixel = scanline[x]; |
| 97 | int r = qRed(pixel); |
| 98 | int g = qGreen(pixel); |
| 99 | int b = qBlue(pixel); |
José Fonseca | fe13f77 | 2012-03-21 08:49:40 +0000 | [diff] [blame] | 100 | int a = qAlpha(pixel); |
gregory | 8f0aa38 | 2012-07-01 17:18:04 +0200 | [diff] [blame] | 101 | if (alpha) { |
| 102 | a = clamp(((a + offset) * scale) >> 8); |
| 103 | scanline[x] = qRgba(a, a, a, 0xff); |
| 104 | } else { |
| 105 | r = clamp(((r + offset) * scale) >> 8); |
| 106 | g = clamp(((g + offset) * scale) >> 8); |
| 107 | b = clamp(((b + offset) * scale) >> 8); |
| 108 | a |= aMask; |
| 109 | scanline[x] = qRgba(r, g, b, a); |
| 110 | } |
José Fonseca | d562b8e | 2011-11-25 15:51:09 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | QPixmap px = QPixmap::fromImage(m_temp); |
| 116 | imageLabel->setPixmap(px); |
| 117 | |
| 118 | updateGeometry(); |
| 119 | } |
| 120 | |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 121 | QSize ImageViewer::sizeHint() const |
| 122 | { |
| 123 | QSize size; |
| 124 | |
| 125 | int vScrollWidth = scrollArea->verticalScrollBar() ? |
| 126 | scrollArea->verticalScrollBar()->width() : 0; |
| 127 | int hScrollHeight = scrollArea->horizontalScrollBar() ? |
| 128 | scrollArea->horizontalScrollBar()->height() : 0; |
| 129 | QSize optimalWindowSize(m_image.width() + vScrollWidth, |
| 130 | m_image.height() + hScrollHeight); |
| 131 | |
| 132 | QRect screenRect = QApplication::desktop()->availableGeometry(); |
José Fonseca | 5d7fc05 | 2012-11-13 19:53:47 +0000 | [diff] [blame] | 133 | const float maxPercentOfDesktopSpace = 0.8f; |
Zack Rusin | 2ffe9f8 | 2011-09-23 20:25:47 -0400 | [diff] [blame] | 134 | QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(), |
| 135 | maxPercentOfDesktopSpace * screenRect.height()); |
| 136 | |
| 137 | return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()), |
| 138 | qMin(optimalWindowSize.height(), maxAvailableSize.height())); |
Zack Rusin | 5cb6b87 | 2011-04-10 02:19:59 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | #include "imageviewer.moc" |