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