00001 #include "WidgetImage.hpp"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <QScrollArea>
00026 #include <QPaintEvent>
00027 #include <QPainter>
00028
00029 #include <PixmapWidget.hpp>
00030
00031 #include <cmath>
00032
00033 #ifndef QT_NO_DEBUG
00034 #include <iostream>
00035 #endif
00036
00037 WidgetImage :: WidgetImage(const unsigned char* const img, const QVector<QRgb>& table, const Params params, QWidget* parent )
00038 :QScrollArea(parent), params(params),gamma(1),saveGamma(1)
00039 {
00040 this->img = QImage(params.iWidth, params.iHeight, QImage::Format_Indexed8);
00041
00042
00043 {
00044 unsigned char* pPixels = this->img.bits();
00045
00046 for ( unsigned int i = 0 ; i < params.iWidth * params.iHeight ; ++i )
00047 {
00048 pPixels[i] = img[i];
00049 }
00050 }
00051 this->img.setColorTable(table);
00052
00053 this->saveImg = QImage(this->img);
00054
00055
00056
00057 pw = new PixmapWidget(&this->img, this);
00058
00059 this->setAlignment(Qt::AlignCenter);
00060
00061 this->setWidget(pw);
00062
00063 connect(pw, SIGNAL(zoom(QPoint, QPoint)), this, SLOT(pushSignalZoom(QPoint, QPoint)));
00064 connect(pw, SIGNAL(unzoom(void)), this, SLOT(pushSignalUnZoom(void)));
00065
00066 gamma = 0;
00067
00068 #ifndef QT_NO_DEBUG
00069 std::cout << "WidgetImage created" << std::endl;
00070 #endif
00071 }
00072
00073 WidgetImage :: WidgetImage(const unsigned char* const img, const QVector<QRgb>& table, const ParamsCurve params, QWidget* parent )
00074 :QScrollArea(parent), paramsCurve(params),gamma(1),saveGamma(1)
00075 {
00076 this->img = QImage(params.iWidth, params.iHeight, QImage::Format_Indexed8);
00077
00078
00079 {
00080 unsigned char* pPixels = this->img.bits();
00081
00082 for ( unsigned int i = 0 ; i < params.iWidth * params.iHeight ; ++i )
00083 {
00084 pPixels[i] = img[i];
00085 }
00086 }
00087 this->img.setColorTable(table);
00088
00089 this->saveImg = QImage(this->img);
00090
00091
00092
00093 pw = new PixmapWidget(&this->img, this);
00094
00095 this->setAlignment(Qt::AlignCenter);
00096
00097 this->setWidget(pw);
00098
00099 connect(pw, SIGNAL(zoom(QPoint, QPoint)), this, SLOT(pushSignalZoomCurve(QPoint, QPoint)));
00100 connect(pw, SIGNAL(unzoom(void)), this, SLOT(pushSignalUnZoomCurve(void)));
00101
00102 gamma = 0;
00103
00104 #ifndef QT_NO_DEBUG
00105 std::cout << "WidgetImage created (curved)" << std::endl;
00106 #endif
00107 }
00108
00109 WidgetImage :: ~WidgetImage(void)
00110 {
00111 disconnect(pw, SIGNAL(zoom(QPoint, QPoint)), this, SLOT(pushSignalZoom(QPoint, QPoint)));
00112 disconnect(pw, SIGNAL(unzoom()), this, SLOT(pushSignalUnZoom()));
00113 disconnect(pw, SIGNAL(zoom(QPoint, QPoint)), this, SLOT(pushSignalZoomCurve(QPoint, QPoint)));
00114 disconnect(pw, SIGNAL(unzoom()), this, SLOT(pushSignalUnZoomCurve()));
00115
00116 delete pw;
00117
00118 #ifndef QT_NO_DEBUG
00119 std::cout << "WidgetImage deleted" << std::endl;
00120 #endif
00121 }
00122
00123 void WidgetImage :: pushSignalZoom(QPoint start, QPoint end)
00124 {
00125 emit zoom(start, end, params, img.colorTable());
00126 }
00127
00128 void WidgetImage :: pushSignalZoomCurve(QPoint start, QPoint end)
00129 {
00130 emit zoom(start, end, paramsCurve, img.colorTable());
00131 }
00132
00133 void WidgetImage :: pushSignalUnZoom()
00134 {
00135 emit unzoom(params, img.colorTable());
00136 }
00137
00138 void WidgetImage :: pushSignalUnZoomCurve(void)
00139 {
00140 emit unzoom(paramsCurve, img.colorTable());
00141 }
00142
00143 void WidgetImage :: gammaChanged(double value)
00144 {
00145 #ifndef QT_NO_DEBUG
00146 std::cout << "\tSLOT: WidgetImage::valueChanged:" << value << std::endl;
00147 #endif
00148
00149
00150 {
00151
00152 unsigned char* data = img.bits();
00153 unsigned char max = 0;
00154
00155
00156 for ( unsigned int i = 0 ; i < (unsigned)img.width() * img.height() ; i++ )
00157 {
00158 if ( max < data[i] )
00159 {
00160 max = data[i];
00161 }
00162 }
00163
00164 {
00165 unsigned int tmpColor = 0;
00166
00167 for ( unsigned int i = 0 ; i < (unsigned)img.width() * img.height() ; i++ )
00168 {
00169 tmpColor = 255.0 * std::pow(static_cast<double>(data[i]) / max, value) + 0.4;
00170
00171 if ( tmpColor > 255 )
00172 {
00173 data[i] = 255;
00174 }
00175 else
00176 {
00177 data[i] = static_cast<unsigned char>(tmpColor);
00178 }
00179 }
00180 }
00181
00182 }
00183
00184
00185
00186 pw->repaint();
00187
00188
00189
00190 this->gamma = value;
00191 }
00192
00193 void WidgetImage :: needRestore(void)
00194 {
00195 #ifndef QT_NO_DEBUG
00196 std::cout << "\tSLOT: WidgetImage::needRestore" << std::endl;
00197 #endif
00198
00199 img = saveImg;
00200 gamma = saveGamma;
00201
00202
00203 pw->repaint();
00204 }
00205
00206 void WidgetImage :: needSave(void)
00207 {
00208 #ifndef QT_NO_DEBUG
00209 std::cout << "\tSLOT: WidgetImage::needSave(" << std::endl;
00210 #endif
00211
00212 saveImg = img;
00213 saveGamma = gamma;
00214
00215
00216 pw->repaint();
00217 }
00218
00219 void WidgetImage :: setColourTable(QVector<QRgb> newTable)
00220 {
00221 #ifndef QT_NO_DEBUG
00222 std::cout << "\tWidgetImage :: setColourTable" << std::endl;
00223 #endif
00224
00225 img.setColorTable(newTable);
00226
00227
00228 pw->repaint();
00229
00230 saveImg = img;
00231 }