cmtkQtRenderImageRGB.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2356 $
00026 //
00027 //  $LastChangedDate: 2010-09-24 11:54:25 -0700 (Fri, 24 Sep 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkQtRenderImageRGB.h"
00034 
00035 #include <iostream>
00036 #include <qpainter.h>
00037 #include <qpixmap.h>
00038 #include <qcursor.h>
00039 
00040 #include <QPaintEvent>
00041 #include <QMouseEvent>
00042 
00043 namespace
00044 cmtk
00045 {
00046 
00049 
00050 QtRenderImageRGB::QtRenderImageRGB
00051 ( QWidget *const parent, Qt::WFlags f )
00052   : QWidget( parent, f ),
00053     ZoomFactorPercent( 100 ),
00054     FlipX( false ), FlipY( false ),
00055     ImageAspectMode( AspectNone ),
00056     CrosshairMode( false ),
00057     Image()
00058 {
00059   CrosshairPosition[0] = CrosshairPosition[1] = 0;
00060 
00061   this->setBaseSize( 512, 512 );
00062   //  this->setMouseTracking( true );
00063   this->setCursor( QCursor( Qt::CrossCursor ) );
00064 }
00065 
00066 QtRenderImageRGB::~QtRenderImageRGB()
00067 {
00068 }
00069 
00070 void
00071 QtRenderImageRGB::paintEvent( QPaintEvent *const )
00072 {
00073   // update modified time to force rendering
00074   this->UpdateModifiedTime();
00075   this->Update();
00076   this->RenderTo( this );
00077   this->UpdateExecuteTime();
00078 }
00079 
00080 void
00081 QtRenderImageRGB::Execute()
00082 {
00083   if ( Input == NULL ) return;
00084 }
00085 
00086 void
00087 QtRenderImageRGB::RenderTo( QPaintDevice *pd )
00088 {
00089   if ( ! Input ) return;
00090 
00091   if ( Input->GetAlphaChannel() != IMAGE_RGBA ) 
00092     {
00093     return;
00094     }
00095   
00096   unsigned char* imageDataRGB = (unsigned char*) Input->GetDataPtr();
00097   if ( imageDataRGB == NULL ) return;
00098 
00099   unsigned int width, height;
00100   Input->GetDims( width, height );
00101 
00102   this->setFixedSize( ZoomFactorPercent * width / 100, ZoomFactorPercent * height / 100 );
00103   Image = QImage( width, height, QImage::Format_RGB32 );
00104   memcpy( Image.bits(), imageDataRGB, width * height * 4 );
00105 
00106   if ( FlipX || FlipY )
00107     Image = Image.mirrored( FlipX, FlipY );
00108 
00109   if ( ZoomFactorPercent != 100 ) 
00110     {
00111     Image = Image.scaled( width * ZoomFactorPercent / 100, height * ZoomFactorPercent / 100 );
00112     }
00113   
00114   QPainter painter( pd );
00115   painter.drawImage( 0, 0, Image );
00116   
00117   if ( CrosshairMode )
00118     this->DrawCrosshair( painter, width, height );
00119 }
00120 
00121 void
00122 QtRenderImageRGB::DrawCrosshair
00123 ( QPainter &painter, const unsigned int width, const unsigned int height ) 
00124   const
00125 {
00126   int crosshairX = ( FlipX ) ? width-1-CrosshairPosition[0] : CrosshairPosition[0];
00127   crosshairX = static_cast<int>( (0.5+crosshairX) * ZoomFactorPercent / 100 );
00128 
00129   int crosshairY = ( FlipY ) ? height-1-CrosshairPosition[1] : CrosshairPosition[1];
00130   crosshairY = static_cast<int>( (0.5+crosshairY) * ZoomFactorPercent / 100 );
00131 
00132   const int realWidth = static_cast<int>( 1.0 * width * ZoomFactorPercent / 100 );
00133   const int realHeight = static_cast<int>( 1.0 * height * ZoomFactorPercent / 100 );
00134   
00135   painter.setPen( CrosshairColors[0] );
00136   painter.drawLine( 0, crosshairY, realWidth-1, crosshairY );
00137   
00138   painter.setPen( CrosshairColors[1] );
00139   painter.drawLine( crosshairX, 0, crosshairX, realHeight-1 );
00140 }
00141 
00142 QPixmap
00143 QtRenderImageRGB::GetPixmap()
00144 {
00145   if ( Input == NULL ) return QPixmap();
00146   
00147   QPixmap capture( ZoomFactorPercent * Input->GetDims( AXIS_X ) / 100, ZoomFactorPercent * Input->GetDims( AXIS_Y ) / 100 );
00148   this->RenderTo( &capture );
00149   return capture;
00150 }
00151 
00153 void 
00154 QtRenderImageRGB
00155 ::mousePressEvent( QMouseEvent *e ) 
00156 {
00157   unsigned int scaledX = (e->x()-static_cast<int>(ZoomFactorPercent/200)) * 100 / ZoomFactorPercent;
00158   if ( Input && this->FlipX )
00159     {
00160     scaledX = Input->GetDims( AXIS_X ) - 1 - scaledX;
00161     }
00162 
00163   unsigned int scaledY = (e->y()-static_cast<int>(ZoomFactorPercent/200)) * 100 / ZoomFactorPercent;
00164   if ( Input && this->FlipY )
00165     {
00166     scaledY = Input->GetDims( AXIS_Y ) - 1 - scaledY;
00167     }
00168   
00169   emit signalMousePressed( e->button(), scaledX, scaledY );
00170   Vector3D v;
00171   Input->GetPixelLocation( v, scaledX, scaledY );
00172   emit signalMouse3D( e->button(), v );
00173   e->accept();
00174 }
00175 
00176 void
00177 QtRenderImageRGB
00178 ::mouseMoveEvent( QMouseEvent *e ) 
00179 {
00180   unsigned int scaledX = (e->x()-static_cast<int>(ZoomFactorPercent/200)) * 100 / ZoomFactorPercent;
00181   if ( Input && this->FlipX )
00182     {
00183     scaledX = Input->GetDims( AXIS_X ) - 1 - scaledX;
00184     }
00185 
00186   unsigned int scaledY = (e->y()-static_cast<int>(ZoomFactorPercent/200)) * 100 / ZoomFactorPercent;
00187   if ( Input && this->FlipY )
00188     {
00189     scaledY = Input->GetDims( AXIS_Y ) - 1 - scaledY;
00190     }  
00191   
00192   emit signalMousePressed( e->button(), scaledX, scaledY );
00193   Vector3D v;
00194   Input->GetPixelLocation( v, scaledX, scaledY );
00195 
00196   emit signalMouse3D( e->button(), v );
00197   e->accept();
00198 }
00199 
00200 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines