00001
00022 #include "DGVImage.h"
00023
00024 DGVImage::DGVImage(QWidget *parent) : QLabel(parent)
00025 {
00026 loadComplete = false;
00027
00028 createActions();
00029 createConnections();
00030 }
00031
00032 DGVImage::DGVImage(Array<imageType,2> &data, QWidget *parent) : QLabel(parent)
00033 {
00034 loadComplete = false;
00035
00036 createActions();
00037 createConnections();
00038
00039 setPixmap(data);
00040 }
00041
00042 DGVImage::~DGVImage()
00043 {
00044
00045 }
00046
00047 void DGVImage::setName(QString filename)
00048 {
00049 name = filename;
00050 setWindowTitle(strippedNamePrefix());
00051 }
00052
00053 bool DGVImage::openImage(QString filename)
00054 {
00055 if(!filename.isEmpty())
00056 {
00057 QPixmap pix;
00058 name = filename;
00059
00061 pix.load(name);
00062 DGVImage::setPixmap(pix);
00063 QLabel::setWindowTitle(strippedName());
00064 QLabel::update();
00065
00066 return true;
00067 }
00068 else
00069 return false;
00070 }
00071
00072 void DGVImage::linkResult(DGVImage *result)
00073 {
00074 resultImage = result;
00075 }
00076
00077 void DGVImage::setPixmap(const QPixmap &pix)
00078 {
00079 QImage image;
00080
00081 image = pix.toImage();
00082 Blitz.QImageToArray(&image,imageData);
00083 cerr << "Warning: Data Overwriten in Memory!" << endl;
00084
00085 refreshImage();
00086 }
00087
00088 void DGVImage::setPixmap(const Array<imageType,2> &bins)
00089 {
00090 QImage *image;
00091 QPixmap pix;
00092
00093 imageData.resize(bins.rows(),bins.cols());
00094 imageData = bins;
00095
00096 image = Blitz.arrayToQImage(bins);
00097
00098 pix = pix.fromImage(*image);
00099 cerr << "Warning: Data Overwriten!" << endl;
00100
00101 QLabel::setPixmap(pix);
00102 QLabel::adjustSize();
00103 QLabel::setScaledContents(true);
00104 QLabel::update();
00105 loadComplete = true;
00106 }
00107
00108 void DGVImage::setInterpolation(bool interpolation)
00109 {
00110 if(interpolation)
00111 cerr << "Turning Interpolation/Anti-Aliasing ON" << endl;
00112 else
00113 cerr << "Turning Interpolation/Anti-Aliasing OFF" << endl;
00114
00115 QLabel::update();
00116 }
00117
00118 QString DGVImage::strippedName()
00119 {
00120 return QFileInfo(name).fileName();
00121 }
00122
00123 QString DGVImage::strippedNamePrefix()
00124 {
00125 return "Image: " + QFileInfo(name).fileName();
00126 }
00127
00128
00129 void DGVImage::table()
00130 {
00131 dataTable = new DGVTable(this);
00132 QString tmp;
00133
00134 dataTable->setData(imageData);
00135
00136 connectTable(dataTable);
00137
00138 emit tableAvailable(dataTable);
00139 }
00140
00141 void DGVImage::updateData(int row, int col)
00142 {
00143 imageData(row,col) = dataTable->item(row,col)->text().toDouble();
00144 }
00145
00146 void DGVImage::refreshImage()
00147 {
00148 setPixmap(imageData);
00149 QLabel::adjustSize();
00150 QLabel::setScaledContents(true);
00151 QLabel::update();
00152 loadComplete = true;
00153 }
00154
00155 void DGVImage::normGreyscale()
00156 {
00157 QImage image(imageData.cols(),imageData.rows(),QImage::Format_Indexed8);
00158 QPixmap pix;
00159 imageType tmp = 0, max = 0, min = 0;
00160
00161 if(loadComplete)
00162 {
00163 image = pixmap()->toImage();
00164
00166 if(imageData.cols() > 0 && imageData.rows() > 0)
00167 min = max = qGray(image.pixel(0,0));
00168
00169 for(int j = 0; j < imageData.rows(); j ++)
00170 for(int k = 0; k < imageData.cols(); k ++)
00171 {
00172 if(max < qGray(image.pixel(k,j)))
00173 max = qGray(image.pixel(k,j));
00174 if(min > qGray(image.pixel(k,j)))
00175 min = qGray(image.pixel(k,j));
00176 }
00177 qDebug() << "\nNorm - Max Pixel: " << max << ", Min Pixel: " << min;
00178 max -= min;
00179
00181 image.setNumColors(256);
00182 for(int j = 0; j < 256; j ++)
00183 image.setColor(j,qRgb(j, j, j));
00184
00186 for(int j = 0; j < imageData.rows(); j ++)
00187 for(int k = 0; k < imageData.cols(); k ++)
00188 {
00189 tmp = static_cast<imageType>( (qGray(image.pixel(k,j))-min)/static_cast<double>(max)*255.0 );
00190
00191 image.setPixel(k,j,qRgb(static_cast<int>(tmp),static_cast<int>(tmp),static_cast<int>(tmp)));
00192 }
00193
00194 pix = pix.fromImage(image);
00195
00196 QLabel::setPixmap(pix);
00197 QLabel::adjustSize();
00198 QLabel::setScaledContents(true);
00199 QLabel::update();
00200 }
00201 }
00202
00203 void DGVImage::save()
00204 {
00205 QFileDialog *fileSaver = new QFileDialog(this);
00206 QString filename = fileSaver->getSaveFileName(this,
00207 "Select File Name to Save",
00208 ".",
00209 "Images (*.png *.ppm *.jpeg *.jpg *.bmp)");
00210
00211 if (!filename.isEmpty())
00212 pixmap()->save(filename);
00213 }
00214
00215 void DGVImage::createActions()
00216 {
00217 normGreyAct = new QAction(tr("&Normalise Greyscale"), this);
00218 normGreyAct->setShortcut(tr("Ctrl+n"));
00219 interpolateAct = new QAction(tr("&Interpolation"), this);
00220 interpolateAct->setCheckable(true);
00221 interpolateAct->setChecked(true);
00222 interpolateAct->setShortcut(tr("Ctrl+i"));
00223 }
00224
00225 void DGVImage::createConnections()
00226 {
00228 connect(normGreyAct, SIGNAL(triggered()), this, SLOT(normGreyscale()));
00229 connect(interpolateAct, SIGNAL(triggered(bool)), this, SLOT(setInterpolation(bool)));
00230 }
00231
00232 void DGVImage::connectTable(DGVTable *child)
00233 {
00234 QObject::connect(child,SIGNAL(cellChanged(int,int)), this, SLOT(updateData(int,int)));
00235 }
00236
00237 void DGVImage::contextMenuEvent(QContextMenuEvent *event)
00238 {
00239 QMenu menu(this);
00240
00242 imageMenu = menu.addMenu("Image");
00243 imageMenu->addAction(normGreyAct);
00244 imageMenu->addAction(interpolateAct);
00246 tableAct = menu.addAction(tr("&Data"));
00247 tableAct->setShortcut(tr("Ctrl+d"));
00248 connect(tableAct, SIGNAL(triggered()), this, SLOT(table()));
00249 menu.addSeparator();
00251 refreshAct = menu.addAction(tr("&Refresh"));
00252 refreshAct->setShortcut(tr("Ctrl+r"));
00253 connect(refreshAct, SIGNAL(triggered()), this, SLOT(refreshImage()));
00255 saveAct = menu.addAction(tr("&Save"));
00256 saveAct->setShortcut(tr("Ctrl+S"));
00257 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
00259 closeAct = menu.addAction(tr("&Close"));
00260 closeAct->setShortcut(tr("Ctrl+c"));
00261 connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
00262
00263 menu.exec(event->globalPos());
00264 }
00265
00266 void DGVImage::paintEvent(QPaintEvent *event)
00267 {
00268 QLabel::paintEvent(event);
00269 }