00001 /*! @file Qt/ImageCanvas.cpp widget for deling with images */ 00002 00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt/ImageCanvas.cpp $ 00004 // $Id: ImageCanvas.cpp 12962 2010-03-06 02:13:53Z irock $ 00005 00006 00007 #include "ImageCanvas.h" 00008 #include "Raster/Raster.H" 00009 #include "Image/ShapeOps.H" 00010 #include "Image/ColorOps.H" 00011 #include "Image/MathOps.H" 00012 #include <qfiledialog.h> 00013 #include <qpainter.h> 00014 00015 ImageCanvas::ImageCanvas( QWidget *parent, const char *name ) 00016 : QWidget( parent, name ) 00017 { 00018 QHBoxLayout *layout = new QHBoxLayout( this ); 00019 layout->setMargin( 0 ); 00020 setBackgroundMode( QWidget::NoBackground ); 00021 setCursor( Qt::crossCursor ); 00022 setUpdatesEnabled(true); 00023 } 00024 00025 ImageCanvas::~ImageCanvas() 00026 { 00027 } 00028 00029 //! convert the image to QPixmap and display 00030 void ImageCanvas::setImage(Image<PixRGB<byte> > &img){ 00031 00032 //Store a copy of the new image locally 00033 itsImgByte = img; 00034 00035 //Call the update function, which will in turn call paintEvent 00036 update(); 00037 00038 } 00039 00040 void ImageCanvas::paintEvent(QPaintEvent *) 00041 { 00042 QPainter painter(this); 00043 00044 if(itsImgByte.initialized()) 00045 { 00046 painter.drawPixmap(0,0, convertToQPixmap( 00047 rescale(itsImgByte, Dims(this->width(), this->height())) 00048 ) 00049 ); 00050 } 00051 else 00052 { 00053 painter.drawPixmap(0,0, convertToQPixmap( 00054 Image<PixRGB<byte> >(this->width(), this->height(), ZEROS) 00055 ) 00056 ); 00057 } 00058 00059 } 00060 00061 //int ImageCanvas::getWidth() 00062 //{ 00063 // ret 00064 //} 00065 //int ImageCanvas::getHeight(); 00066 00067 void ImageCanvas::setImage(Image<float> &img){ 00068 itsImgFloat = img; 00069 00070 inplaceNormalize(img, 0.0F, 255.0F); 00071 itsImgByte = toRGB(img); 00072 00073 //Call the update function, which will in turn call paintEvent 00074 update(); 00075 } 00076 00077 void ImageCanvas::saveImg(){ 00078 static QString prevFilename = QString::null; 00079 static int imgNum = 0; 00080 00081 if (itsImgByte.initialized()) 00082 { 00083 QString file = QFileDialog::getSaveFileName( prevFilename, 00084 "PPM image (*.ppm)", 00085 this, "SaveImageDialog", 00086 "Save Image as..." ); 00087 prevFilename = file; 00088 file.replace("%d", QString("%L1").arg(imgNum++)); 00089 00090 if (!file.isEmpty()) 00091 Raster::WriteRGB(itsImgByte, file.ascii()); 00092 } 00093 else 00094 LINFO("Image not initialized"); 00095 } 00096 00097 //TODO: Check out the pixel info functionality, it doesn't seem to be working correctly 00098 void ImageCanvas::mousePressEvent( QMouseEvent *e ) 00099 { 00100 itsPointsClicked.push_back(Point2D<int>(e->x(),e->y())); 00101 LINFO("%d,%d",e->x(),e->y()); 00102 } 00103 /*void ImageCanvas::mousePressEvent( QMouseEvent *e ){ 00104 00105 //get the position in the img coordinates 00106 const int newx = int((float(e->x())) * 00107 float(itsImgByte.getWidth()) / float(this->width())); 00108 00109 const int newy = int((float(e->y())) * 00110 float(itsImgByte.getHeight()) / float(this->height())); 00111 00112 //Show the pixel value if the right button is clicked 00113 if (e->button() == Qt::RightButton){ 00114 //LINFO("Pix value at (%i,%i): (%i,%i,%i)", newx, newy, 00115 //pixVal[0], pixVal[1], pixVal[2]); 00116 //Raster::WriteRGB(itsImg, "out.ppm"); 00117 00118 00119 static QPopupMenu *popupMenu = NULL; 00120 00121 if (itsImgByte.initialized()) 00122 { 00123 if (popupMenu) delete popupMenu; 00124 popupMenu = new QPopupMenu(this); 00125 CHECK_PTR(popupMenu); 00126 popupMenu->insertItem("Pixel Info"); 00127 popupMenu->insertItem(QString(" Loc: (%L1,%L2)").arg(newx).arg(newy)); 00128 00129 if (itsImgFloat.initialized()) 00130 { 00131 float pixVal = itsImgFloat.getVal(newx, newy); 00132 popupMenu->insertItem(QString(" Val :(%L1)").arg(pixVal)); 00133 } 00134 else 00135 { 00136 PixRGB<byte> pixVal = itsImgByte.getVal(newx, newy); 00137 popupMenu->insertItem(QString(" Val :(%L1,%L2, %L3)"). 00138 arg(pixVal[0]).arg(pixVal[1]).arg(pixVal[2])); 00139 } 00140 00141 popupMenu->insertSeparator(); 00142 00143 00144 popupMenu->insertItem("Save Image", this, SLOT(saveImg())); 00145 popupMenu->popup(mapToGlobal(e->pos())); 00146 } 00147 00148 } else { 00149 // LINFO("Button is not a right mouse click"); 00150 emit mousePressed(newx, newy, e->button()); 00151 } 00152 }*/ 00153 00154 Image<PixRGB<byte> > ImageCanvas::getImage(){ 00155 return itsImgByte; 00156 }