]> Creatis software - creaMaracasVisu.git/commitdiff
DFCH: undo/redo image paint algorithm optimized =)
authorDiego Caceres <Diego.Caceres@creatis.insa-lyon.fr>
Tue, 29 Nov 2011 15:27:48 +0000 (15:27 +0000)
committerDiego Caceres <Diego.Caceres@creatis.insa-lyon.fr>
Tue, 29 Nov 2011 15:27:48 +0000 (15:27 +0000)
lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/imageUndoRedo.cxx

index 7672f4efe942636d4d60b7d65a5c459b32ed7926..d49b1b21d2e980660400992d2b84cd30d8182835 100755 (executable)
 #include "imageUndoRedo.h"
 
 // ----------------------------------------------------------------------------------
-ImageUndoRedo::ImageUndoRedo() {
-       this->m_ImagesDeque = new IDequeType();
+ImageUndoRedo::ImageUndoRedo( )
+{
+       this->m_ImagesDeque = new IDequeType( );
 }
 // ----------------------------------------------------------------------------------
 //virtual
-ImageUndoRedo::~ImageUndoRedo() {
+ImageUndoRedo::~ImageUndoRedo( )
+{
 
 }
 // ----------------------------------------------------------------------------------
 //virtual
-void ImageUndoRedo::Undo() {
-       ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo();
-       if (imageInfo != NULL) {
-               this->DrawUR(imageInfo, true);
-               this->UpdateUndoImage();
+void ImageUndoRedo::Undo( )
+{
+       ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo( );
+       if( imageInfo != NULL)
+       {
+               this->DrawUR( imageInfo, true );
+               this->UpdateUndoImage( );
        }
 }
 // ----------------------------------------------------------------------------------
 //virtual
-void ImageUndoRedo::Redo() {
-       ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo();
-       if (imageInfo != NULL) {
-               this->DrawUR(imageInfo, false);
-               this->UpdateUndoImage();
+void ImageUndoRedo::Redo( )
+{
+       ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo( );
+       if( imageInfo != NULL)
+       {
+               this->DrawUR( imageInfo, false );
+               this->UpdateUndoImage( );
        }
 }
 // ----------------------------------------------------------------------------------
-void ImageUndoRedo::SetImage(VTKImageDataPointerType image) {
+void ImageUndoRedo::SetImage( VTKImageDataPointerType image )
+{
        this->m_CurrentImage = image;
-       this->m_CurrentImage->Update();
-       this->UpdateUndoImage();
+       this->m_CurrentImage->Update( );
+       this->UpdateUndoImage( );
 }
 // ----------------------------------------------------------------------------------
 //virtual
-void ImageUndoRedo::SetURImages(ImageMManagerType* imMManager) {
-       ImageMManagerType* newImageManager = new ImageMManagerType(imMManager);
-       if (newImageManager->ValidateRegion()) {
-               RegionSType region = newImageManager->GetModifiedRegion();
-               VTKImageDataPointerType imgUndo = this->GetImageRegion(region,
-                               this->m_UndoImage);
-               VTKImageDataPointerType imgRedo = this->GetImageRegion(region,
-                               this->m_CurrentImage);
-               this->m_ImagesDeque->AddImagesToURContainer(imgUndo, imgRedo,
-                               newImageManager);
-               this->UpdateUndoImage();
-       } else {
+void ImageUndoRedo::SetURImages( ImageMManagerType* imMManager )
+{
+       ImageMManagerType* newImageManager = new ImageMManagerType( imMManager );
+       if( newImageManager->ValidateRegion( ) )
+       {
+               RegionSType region = newImageManager->GetModifiedRegion( );
+               VTKImageDataPointerType imgUndo = this->GetImageRegion( region,
+                       this->m_UndoImage );
+               VTKImageDataPointerType imgRedo = this->GetImageRegion( region,
+                       this->m_CurrentImage );
+               this->m_ImagesDeque->AddImagesToURContainer( imgUndo, imgRedo,
+                       newImageManager );
+               this->UpdateUndoImage( );
+       }
+       else
+       {
                std::cerr << "INVALID REGION" << std::endl;
        }
 }
 // ----------------------------------------------------------------------------------
-void ImageUndoRedo::UpdateUndoImage() {
-       this->m_CurrentImage->Update();
-       this->m_UndoImage = VTKImageDataPointerType::New();
-       this->m_UndoImage->DeepCopy(m_CurrentImage);
-       this->m_UndoImage->Update();
+void ImageUndoRedo::UpdateUndoImage( )
+{
+       this->m_CurrentImage->Update( );
+       this->m_UndoImage = VTKImageDataPointerType::New( );
+       this->m_UndoImage->DeepCopy( m_CurrentImage );
+       this->m_UndoImage->Update( );
 }
 // ----------------------------------------------------------------------------------
 ImageUndoRedo::VTKImageDataPointerType ImageUndoRedo::GetImageRegion(
-               const RegionSType& region, VTKImageDataPointerType img) {
-       VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New();
-       extract->SetVOI(region.minX, region.maxX, region.minY, region.maxY,
-                       region.minZ, region.maxZ);
-       extract->SetSampleRate(1, 1, 1);
-       extract->SetInput(img);
-       VTKImageDataPointerType imgResult = extract->GetOutput();
-       imgResult->Update();
-       return (imgResult);
+       const RegionSType& region, VTKImageDataPointerType img )
+{
+       VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New( );
+       extract->SetVOI( region.minX, region.maxX, region.minY, region.maxY,
+               region.minZ, region.maxZ );
+       extract->SetSampleRate( 1, 1, 1 );
+       extract->SetInput( img );
+       VTKImageDataPointerType imgResult = extract->GetOutput( );
+       imgResult->Update( );
+       return ( imgResult );
 }
 // ----------------------------------------------------------------------------------
-void ImageUndoRedo::SetCurrentImage(VTKImageDataPointerType img) {
+void ImageUndoRedo::SetCurrentImage( VTKImageDataPointerType img )
+{
        this->m_CurrentImage = img;
 }
 // ----------------------------------------------------------------------------------
 //virtual
-void ImageUndoRedo::DrawUR(ImageInfoUR* imageInfo, const bool& undo) {
+void ImageUndoRedo::DrawUR( ImageInfoUR* imageInfo, const bool& undo )
+{
        VTKImageDataPointerType img;
-       if (undo) {
-               img = imageInfo->GetUndoImage();
+       if( undo )
+       {
+               img = imageInfo->GetUndoImage( );
        } //fi
-       else {
-               img = imageInfo->GetRedoImage();
+       else
+       {
+               img = imageInfo->GetRedoImage( );
        } //else
-       RegionSType region = imageInfo->GetImageMManager()->GetModifiedRegion();
-       if (img != NULL) {
-               for (int i = region.minX, x = 0; i <= region.maxX; i++, x++) {
-                       for (int j = region.minY, y = 0; j <= region.maxY; j++, y++) {
-                               for (int k = region.minZ, z = 0; k <= region.maxZ; k++, z++) {
-                                       float value = img->GetScalarComponentAsFloat(x, y, z, 0);
-                                       this->m_CurrentImage->SetScalarComponentFromFloat(i, j, k,
-                                                       0, value);
-                               } //rof
-                       } //rof
-               } //rof
-               this->m_CurrentImage->Modified();
+       RegionSType region = imageInfo->GetImageMManager( )->GetModifiedRegion( );
+       if( img != NULL)
+       {
+               int *dim = img->GetDimensions( );
+               int sizeXImageIn = dim[ 0 ];
+               size_t linesize = sizeXImageIn * sizeof(unsigned short);
+               for( int j = region.minY, y = 0; j <= region.maxY; j++, y++ )
+               {
+                       for( int k = region.minZ, z = 0; k <= region.maxZ; k++, z++ )
+                       {
+                               void* ptrsrc = img->GetScalarPointer( 0, y, z );
+                               void* ptrdest = this->m_CurrentImage->GetScalarPointer(
+                                       region.minX, j, k );
+                               memcpy( ptrdest, ptrsrc, linesize );
+                       }
+               }
+               this->m_CurrentImage->Modified( );
        }
-       this->m_ImagesDeque->ManageMemory();
+       this->m_ImagesDeque->ManageMemory( );
 }
 // ----------------------------------------------------------------------------------