]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/imageUndoRedo.cxx
Support #1768 CREATIS Licence insertion
[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 //virtual
43 ImageUndoRedo::~ImageUndoRedo( )
44 {
45
46 }
47 // ----------------------------------------------------------------------------------
48 //virtual
49 void ImageUndoRedo::Undo( )
50 {
51         ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo( );
52         if( imageInfo != NULL)
53         {
54                 this->DrawUR( imageInfo, true );
55                 this->UpdateUndoImage( );
56         }
57 }
58 // ----------------------------------------------------------------------------------
59 //virtual
60 void ImageUndoRedo::Redo( )
61 {
62         ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo( );
63         if( imageInfo != NULL)
64         {
65                 this->DrawUR( imageInfo, false );
66                 this->UpdateUndoImage( );
67         }
68 }
69 // ----------------------------------------------------------------------------------
70 void ImageUndoRedo::SetImage( VTKImageDataPointerType image )
71 {
72         this->m_CurrentImage = image;
73         this->m_CurrentImage->Update( );
74         this->UpdateUndoImage( );
75 }
76 // ----------------------------------------------------------------------------------
77 //virtual
78 void ImageUndoRedo::SetURImages( ImageMManagerType* imMManager )
79 {
80         ImageMManagerType* newImageManager = new ImageMManagerType( imMManager );
81         if( newImageManager->ValidateRegion( ) )
82         {
83                 RegionSType region = newImageManager->GetModifiedRegion( );
84                 VTKImageDataPointerType imgUndo = this->GetImageRegion( region,
85                         this->m_UndoImage );
86                 VTKImageDataPointerType imgRedo = this->GetImageRegion( region,
87                         this->m_CurrentImage );
88                 this->m_ImagesDeque->AddImagesToURContainer( imgUndo, imgRedo,
89                         newImageManager );
90                 this->UpdateUndoImage( );
91         }
92         else
93         {
94                 std::cerr << "INVALID REGION" << std::endl;
95         }
96 }
97 // ----------------------------------------------------------------------------------
98 void ImageUndoRedo::UpdateUndoImage( )
99 {
100         this->m_CurrentImage->Update( );
101         this->m_UndoImage = VTKImageDataPointerType::New( );
102         this->m_UndoImage->DeepCopy( m_CurrentImage );
103         this->m_UndoImage->Update( );
104 }
105 // ----------------------------------------------------------------------------------
106 ImageUndoRedo::VTKImageDataPointerType ImageUndoRedo::GetImageRegion(
107         const RegionSType& region, VTKImageDataPointerType img )
108 {
109         VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New( );
110         extract->SetVOI( region.minX, region.maxX, region.minY, region.maxY,
111                 region.minZ, region.maxZ );
112         extract->SetSampleRate( 1, 1, 1 );
113         extract->SetInput( img );
114         VTKImageDataPointerType imgResult = extract->GetOutput( );
115         imgResult->Update( );
116         return ( imgResult );
117 }
118 // ----------------------------------------------------------------------------------
119 void ImageUndoRedo::SetCurrentImage( VTKImageDataPointerType img )
120 {
121         this->m_CurrentImage = img;
122 }
123 // ----------------------------------------------------------------------------------
124 //virtual
125 void ImageUndoRedo::DrawUR( ImageInfoUR* imageInfo, const bool& undo )
126 {
127         VTKImageDataPointerType img;
128         if( undo )
129         {
130                 img = imageInfo->GetUndoImage( );
131         } //fi
132         else
133         {
134                 img = imageInfo->GetRedoImage( );
135         } //else
136         RegionSType region = imageInfo->GetImageMManager( )->GetModifiedRegion( );
137         if( img != NULL)
138         {
139                 int *dim = img->GetDimensions( );
140                 int sizeXImageIn = dim[ 0 ];
141                 size_t linesize = sizeXImageIn * sizeof(unsigned short);
142                 for( int j = region.minY, y = 0; j <= region.maxY; j++, y++ )
143                 {
144                         for( int k = region.minZ, z = 0; k <= region.maxZ; k++, z++ )
145                         {
146                                 void* ptrsrc = img->GetScalarPointer( 0, y, z );
147                                 void* ptrdest = this->m_CurrentImage->GetScalarPointer(
148                                         region.minX, j, k );
149                                 memcpy( ptrdest, ptrsrc, linesize );
150                         }
151                 }
152                 this->m_CurrentImage->Modified( );
153         }
154         this->m_ImagesDeque->ManageMemory( );
155 }
156 // ----------------------------------------------------------------------------------