KWStyle - itkImportImageContainer.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImportImageContainer.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:40 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12   Portions of this code are covered under the VTK copyright.
13   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14
15      This software is distributed WITHOUT ANY WARRANTY; without even 
16 IND *****the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
17 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 DEF #ifndef _itkImportImageContainer_txx
21 DEF #define _itkImportImageContainer_txx
22
23 namespace itk
24 {
25
26 template <typename TElementIdentifier, typename TElement>
27 ImportImageContainer<TElementIdentifier , TElement>
28 ::ImportImageContainer()
29 {
30   m_ImportPointer = 0;
31   m_ContainerManageMemory = true;
32   m_Capacity = 0;
33   m_Size = 0;
34 }
35
36   
37 template <typename TElementIdentifier, typename TElement>
38 ImportImageContainer< TElementIdentifier , TElement >
39 ::~ImportImageContainer()
40 {
41   if (m_ImportPointer && m_ContainerManageMemory)
42     {
43     delete [] m_ImportPointer;
44     }
45 }
46
47
48 /**
49  * Tell the container to allocate enough memory to allow at least
50  * as many elements as the size given to be stored.  
51  */
52 template <typename TElementIdentifier, typename TElement>
53 void
54 ImportImageContainer< TElementIdentifier , TElement >
55 ::Reserve(ElementIdentifier size)
56 {
57   if (m_ImportPointer)
58     {
59     if (size > m_Capacity)
60       {
61       TElement* temp = this->AllocateElements(size);
62       // only copy the portion of the data used in the old buffer
63       memcpy(temp, m_ImportPointer, m_Size*sizeof(TElement));
64       if (m_ImportPointer && m_ContainerManageMemory)
65         {
66         delete [] m_ImportPointer;
67         }
68       m_ImportPointer = temp;
69       m_ContainerManageMemory = true;
70       m_Capacity = size;
71       m_Size = size;
72       this->Modified();
73       }
74     }
75   else
76     {
77     m_ImportPointer = this->AllocateElements(size);
78     m_Capacity = size;
79     m_Size = size;
80     m_ContainerManageMemory = true;
81     this->Modified();
82     }
83 }
84
85
86 /**
87  * Tell the container to try to minimize its memory usage for storage of
88  * the current number of elements.  
89  */
90 template <typename TElementIdentifier, typename TElement>
91 void
92 ImportImageContainer< TElementIdentifier , TElement >
93 ::Squeeze(void)
94 {
95   if (m_ImportPointer)
96     {
97     if (m_Size < m_Capacity)
98       {
99       TElement* temp = this->AllocateElements(m_Size);
100       memcpy(temp, m_ImportPointer, m_Size*sizeof(TElement));
101       if (m_ContainerManageMemory)
102         {
103         delete [] m_ImportPointer;
104         }
105       m_ImportPointer = temp;
106       m_ContainerManageMemory = true;
107       m_Capacity = m_Size;
108
109       this->Modified();
110       }
111     }
112 }
113
114
115 /**
116  * Tell the container to try to minimize its memory usage for storage of
117  * the current number of elements.  
118  */
119 template <typename TElementIdentifier, typename TElement>
120 void
121 ImportImageContainer< TElementIdentifier , TElement >
122 ::Initialize(void)
123 {
124   if (m_ImportPointer)
125     {
126     if (m_ContainerManageMemory)
127       {
128       delete [] m_ImportPointer;
129       }
130     m_ImportPointer = 0;
131     m_ContainerManageMemory = true;
132     m_Capacity = 0;
133     m_Size = 0;
134     
135     this->Modified();
136     }
137 }
138
139
140 /**
141  * Set the pointer from which the image data is imported.  "num" is
142  * the number of pixels in the block of memory. If
143  * "LetContainerManageMemory" is false, then the application retains
144  * the responsibility of freeing the memory for this image data.  If
145  * "LetContainerManageMemory" is true, then this class will free the
146  * memory when this object is destroyed.
147  */
148 template <typename TElementIdentifier, typename TElement>
149 void
150 ImportImageContainer< TElementIdentifier , TElement >
151 ::SetImportPointer(TElement *ptr, TElementIdentifier num,
152                    bool LetContainerManageMemory)
153 {
154   if (m_ImportPointer && m_ContainerManageMemory)
155     {
156     delete [] m_ImportPointer;
157     }
158   m_ImportPointer = ptr;
159   m_ContainerManageMemory = LetContainerManageMemory;
160   m_Capacity = num;
161   m_Size = num;
162
163   this->Modified();
164 }
165
166 template <typename TElementIdentifier, typename TElement>
167 TElement* ImportImageContainer< TElementIdentifier , TElement >
168 ::AllocateElements(ElementIdentifier size) const
169 {
170   // Encapsulate all image memory allocation here to throw an
171   // exception when memory allocation fails even when the compiler
172   // does not do this by default.
173   TElement* data;
174   try
175     {
176     data = new TElement[size];
177     }
178   catch(...)
179     {
180     data = 0;
181     }
182   if(!data)
183     {
184     // We cannot construct an error string here because we may be out
185     // of memory.  Do not use the exception macro.
186     throw MemoryAllocationError(__FILE__, __LINE__,
187                                 "Failed to allocate memory for image.",
188                                 ITK_LOCATION);
189     }
190   return data;
191 }
192
193 template <typename TElementIdentifier, typename TElement>
194 void
195 ImportImageContainer< TElementIdentifier , TElement >
196 ::PrintSelf(std::ostream& os, Indent indent) const
197 {
198   Superclass::PrintSelf(os,indent);
199
200   os << indent << "Pointer: " << &m_ImportPointer << std::endl;
201   os << indent << "Container manages memory: "
202 IND *****<< (m_ContainerManageMemory ? "true" : "false") << std::endl;
203   os << indent << "Size: " << m_Size << std::endl;
204   os << indent << "Capacity: " << m_Capacity << std::endl;
205 }
206
207 // end namespace itk
208
209 #endif
210

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:17PM
© Kitware Inc.