]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/imageUndoRedo.cxx
0d650a7df91ac84f465cedb3c855ef2d8182f0e2
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / imageUndoRedo / imageUndoRedo.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*!
27  * @file        imageUndoRedo.cxx
28  * @brief       This file contains the implementation of the ImageUndoRedo class.
29  * @author      Info-Dev
30  * @author      Diego CACERES (diego.caceres[AT]creatis.insa-lyon.fr)
31  * @date        2011-11-15
32  */
33
34 #include "imageUndoRedo.h"
35
36 // ----------------------------------------------------------------------------------
37 ImageUndoRedo::ImageUndoRedo()
38 {
39         this->m_ImagesDeque = new IDequeType( );
40 }
41
42 // ----------------------------------------------------------------------------------
43 //virtual
44 ImageUndoRedo::~ImageUndoRedo( )
45 {
46 }
47
48 // ----------------------------------------------------------------------------------
49 //virtual
50 void ImageUndoRedo::Undo( )
51 {
52         ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo( );
53         if( imageInfo != NULL)
54         {
55                 this->DrawUR( imageInfo, true );
56                 this->UpdateUndoImage( );
57         } //if  imageInfo
58 }
59 // ----------------------------------------------------------------------------------
60 //virtual
61 void ImageUndoRedo::Redo( )
62 {
63         ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo( );
64         if( imageInfo != NULL)
65         {
66                 this->DrawUR( imageInfo, false );
67                 this->UpdateUndoImage( );
68         }
69 }
70 // ----------------------------------------------------------------------------------
71 void ImageUndoRedo::SetImage( VTKImageDataPointerType image )
72 {
73         this->m_CurrentImage = image;
74 //EED 2017-01-01 Migration VTK7
75 #if VTK_MAJOR_VERSION <= 5
76         this->m_CurrentImage->Update( );
77 #else
78         this->m_CurrentImage->Modified( );
79 #endif
80
81         this->UpdateUndoImage( );
82 }
83
84 // ----------------------------------------------------------------------------------
85 //virtual
86 void ImageUndoRedo::SetURImages( ImageMManagerType* imMManager )
87 {
88         ImageMManagerType* newImageManager = new ImageMManagerType( imMManager );
89         if( newImageManager->ValidateRegion() )
90         {
91                 RegionSType             region  = newImageManager->GetModifiedRegion( );
92                 VTKImageDataPointerType imgUndo = this->GetImageRegion( region,this->m_UndoImage );
93                 VTKImageDataPointerType imgRedo = this->GetImageRegion( region,this->m_CurrentImage );
94                 this->m_ImagesDeque->AddImagesToURContainer( imgUndo, imgRedo,newImageManager );
95                 this->UpdateUndoImage( );
96         } else {
97                 std::cerr << "INVALID REGION" << std::endl;
98         }
99 }
100
101 // ----------------------------------------------------------------------------------
102 void ImageUndoRedo::UpdateUndoImage( )
103 {
104 //EED 2017-01-01 Migration VTK7
105 #if VTK_MAJOR_VERSION <= 5
106         this->m_CurrentImage->Update( );
107 #else
108         this->m_CurrentImage->Modified( );
109 #endif
110         this->m_UndoImage = VTKImageDataPointerType::New( );
111         this->m_UndoImage->DeepCopy( m_CurrentImage );
112 //EED 2017-01-01 Migration VTK7
113 #if VTK_MAJOR_VERSION <= 5
114         this->m_UndoImage->Update( );
115 #else
116         this->m_UndoImage->Modified( );
117 #endif
118 }
119
120 // ----------------------------------------------------------------------------------
121 ImageUndoRedo::VTKImageDataPointerType ImageUndoRedo::GetImageRegion( const RegionSType& region, VTKImageDataPointerType img )
122 {
123         VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New( );
124         extract->SetVOI( region.minX, region.maxX, region.minY, region.maxY,region.minZ, region.maxZ );
125         extract->SetSampleRate( 1, 1, 1 );
126 //EED 2017-01-01 Migration VTK7
127 #if VTK_MAJOR_VERSION <= 5
128         extract->SetInput( img );
129 #else
130         extract->SetInputData( img );
131         extract->Update();
132 #endif
133         VTKImageDataPointerType imgResult = extract->GetOutput( );
134 //EED 2017-01-01 Migration VTK7
135 #if VTK_MAJOR_VERSION <= 5
136         imgResult->Update( );
137 #else
138         imgResult->Modified( );
139 #endif
140         return ( imgResult );
141 }
142 // ----------------------------------------------------------------------------------
143 void ImageUndoRedo::SetCurrentImage( VTKImageDataPointerType img )
144 {
145         this->m_CurrentImage = img;
146 }
147 // ----------------------------------------------------------------------------------
148 //virtual
149 void ImageUndoRedo::DrawUR( ImageInfoUR* imageInfo, const bool& undo )
150 {
151         VTKImageDataPointerType img;
152         if( undo )
153         {
154                 img = imageInfo->GetUndoImage( );
155         } else {
156                 img = imageInfo->GetRedoImage( );
157         } //else
158         RegionSType region                      = imageInfo->GetImageMManager()->GetModifiedRegion();
159         m_RestorBaseInitialPointX       = imageInfo->GetImageMManager()->GetBaseInitialPointX();
160         m_RestorBaseInitialPointY       = imageInfo->GetImageMManager()->GetBaseInitialPointY();
161         m_RestorBaseInitialPointZ       = imageInfo->GetImageMManager()->GetBaseInitialPointZ();
162
163         if( img != NULL)
164         {
165                 int *dim                        = img->GetDimensions( );        
166                 int sizeXImageIn        = dim[ 0 ];
167 //EED         size_t linesize     = sizeXImageIn * sizeof(unsigned short);
168         
169         size_t linesize     = sizeXImageIn * img->GetScalarSize();
170                 for( int j = region.minY, y = 0; j <= region.maxY; j++, y++ )
171                 {
172                         for( int k = region.minZ, z = 0; k <= region.maxZ; k++, z++ )
173                         {
174                                 void* ptrsrc    = img->GetScalarPointer( 0, y, z );
175                                 void* ptrdest   = this->m_CurrentImage->GetScalarPointer( region.minX, j, k );
176                                 memcpy( ptrdest, ptrsrc, linesize );
177                         } // for k
178                 } // for j
179                 this->m_CurrentImage->Modified( );
180         } // if img
181         this->m_ImagesDeque->ManageMemory( );
182 }
183 // ----------------------------------------------------------------------------------
184
185 int ImageUndoRedo::GetRestorBaseInitialPointX() 
186 {
187         return  m_RestorBaseInitialPointX;
188 }
189
190 int ImageUndoRedo::GetRestorBaseInitialPointY() 
191 {
192         return  m_RestorBaseInitialPointY;
193 }
194
195 int ImageUndoRedo::GetRestorBaseInitialPointZ() 
196 {
197         return  m_RestorBaseInitialPointZ;
198 }
199