OTB  9.0.0
Orfeo Toolbox
otbImportVectorImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbImportVectorImageFilter_hxx
23 #define otbImportVectorImageFilter_hxx
24 
26 #include "itkObjectFactory.h"
27 
28 namespace otb
29 {
30 
34 template <class TOutputImage>
36 {
37  unsigned int idx;
38 
39  for (idx = 0; idx < TOutputImage::ImageDimension; ++idx)
40  {
41  m_Spacing[idx] = 1.0;
42  m_Origin[idx] = 0.0;
43  }
44  m_Direction.SetIdentity();
45 
46  m_ImportPointer = 0;
47  m_FilterManageMemory = false;
48  m_Size = 0;
49 }
50 
54 template <class TOutputImage>
56 {
57  if (m_ImportPointer && m_FilterManageMemory)
58  {
59  delete[] m_ImportPointer;
60  }
61 }
63 
67 template <class TOutputImage>
68 void ImportVectorImageFilter<TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
69 {
70  int i;
71 
72  Superclass::PrintSelf(os, indent);
73 
74  if (m_ImportPointer)
75  {
76  os << indent << "Imported pointer: (" << m_ImportPointer << ")" << std::endl;
77  }
78  else
79  {
80  os << indent << "Imported pointer: (None)" << std::endl;
81  }
82  os << indent << "Import buffer size: " << m_Size << std::endl;
83  os << indent << "Import buffer size: " << m_Size << std::endl;
84  os << indent << "Filter manages memory: " << (m_FilterManageMemory ? "true" : "false") << std::endl;
85 
86  os << indent << "Spacing: [";
87  for (i = 0; i < static_cast<int>(TOutputImage::ImageDimension) - 1; ++i)
88  {
89  os << m_Spacing[i] << ", ";
90  }
91  os << m_Spacing[i] << "]" << std::endl;
92 
93  os << indent << "Origin: [";
94  for (i = 0; i < static_cast<int>(TOutputImage::ImageDimension) - 1; ++i)
95  {
96  os << m_Origin[i] << ", ";
97  }
98  os << m_Origin[i] << "]" << std::endl;
99  os << indent << "Direction: " << std::endl << this->GetDirection() << std::endl;
100 }
101 
106 template <class TOutputImage>
107 void ImportVectorImageFilter<TOutputImage>::SetImportPointer(TPixel* ptr, unsigned long num, bool LetFilterManageMemory)
108 {
109  if (ptr != m_ImportPointer)
110  {
111  if (m_ImportPointer && m_FilterManageMemory)
112  {
113  delete[] m_ImportPointer;
114  }
115  m_ImportPointer = ptr;
116  this->Modified();
117  }
118  m_FilterManageMemory = LetFilterManageMemory;
119  m_Size = num;
120 }
121 
125 template <class TOutputImage>
127 {
128  return m_ImportPointer;
129 }
130 
134 template <class TOutputImage>
136 {
137  // call the superclass' implementation of this method
138  Superclass::EnlargeOutputRequestedRegion(output);
139 
140  // get pointer to the output
141  OutputImagePointer outputPtr = this->GetOutput();
142 
143  // set the requested region to the largest possible region (in this case
144  // the amount of data that we have)
145  outputPtr->SetRequestedRegion(outputPtr->GetLargestPossibleRegion());
146 }
147 
151 template <class TOutputImage>
153 {
154  // call the superclass' implementation of this method
155  Superclass::GenerateOutputInformation();
156 
157  // get pointer to the output
158  OutputImagePointer outputPtr = this->GetOutput();
159 
160  // we need to compute the output spacing, the output origin, the
161  // output image size, and the output image start index
162  outputPtr->SetSignedSpacing(m_Spacing);
163  outputPtr->SetOrigin(m_Origin);
164  outputPtr->SetDirection(m_Direction);
165  outputPtr->SetLargestPossibleRegion(m_Region);
166 
167  typename RegionType::SizeType size = m_Region.GetSize();
168 
169  int numberOfBands = m_Size / (size[0] * size[1]);
170 
171  if (numberOfBands != static_cast<int>(numberOfBands))
172  itkExceptionMacro(<< "Buffer size and image size are not compatible !");
173 
174  outputPtr->SetNumberOfComponentsPerPixel(numberOfBands);
175 }
176 
180 template <class TOutputImage>
182 {
183  // Normally, GenerateData() allocates memory. However, the application
184  // provides the memory for this filter via the SetImportPointer() method.
185  // Therefore, this filter does not call outputPtr->Allocate().
186 
187  // get pointer to the output
188  OutputImagePointer outputPtr = this->GetOutput();
189 
190  // the output buffer size is set to the size specified by the user via the
191  // SetRegion() method.
192  outputPtr->SetBufferedRegion(outputPtr->GetLargestPossibleRegion());
193 
194  // pass the pointer down to the container during each Update() since
195  // a call to Initialize() causes the container to forget the
196  // pointer. Note that we tell the container NOT to manage the
197  // memory itself. This filter will properly manage the memory (as
198  // opposed to the container) if the user wants it to.
199  outputPtr->GetPixelContainer()->SetImportPointer(m_ImportPointer, m_Size, false);
200 }
201 
205 template <class TOutputImage>
207 {
208  double dspacing[TOutputImage::ImageDimension];
209  for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
210  {
211  dspacing[i] = spacing[i];
212  }
213  this->SetSpacing(dspacing);
214 }
216 
220 template <class TOutputImage>
222 {
223  double dorigin[TOutputImage::ImageDimension];
224  for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
225  {
226  dorigin[i] = origin[i];
227  }
228  this->SetOrigin(dorigin);
229 }
231 
232 //----------------------------------------------------------------------------
233 template <class TOutputImage>
235 {
236  bool modified = false;
237  for (unsigned int r = 0; r < TOutputImage::ImageDimension; ++r)
238  {
239  for (unsigned int c = 0; c < TOutputImage::ImageDimension; ++c)
240  {
241  if (m_Direction[r][c] != direction[r][c])
242  {
243  m_Direction[r][c] = direction[r][c];
244  modified = true;
245  }
246  }
247  }
248  if (modified)
249  {
250  this->Modified();
251  }
252 }
253 
254 } // end namespace otb
255 
256 #endif
otb::ImportVectorImageFilter::SetImportPointer
virtual void SetImportPointer(TPixel *ptr, unsigned long num, bool LetFilterManageMemory)
Definition: otbImportVectorImageFilter.hxx:107
otb::ImportVectorImageFilter::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbImportVectorImageFilter.h:50
otb::ImportVectorImageFilter::SetDirection
virtual void SetDirection(const DirectionType direction)
Definition: otbImportVectorImageFilter.hxx:234
otb::ImportVectorImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbImportVectorImageFilter.hxx:152
otb::ImportVectorImageFilter::GenerateData
void GenerateData() override
Definition: otbImportVectorImageFilter.hxx:181
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImportVectorImageFilter::DirectionType
itk::Matrix< double, OutputImageType::ImageDimension, OutputImageType::ImageDimension > DirectionType
Definition: otbImportVectorImageFilter.h:139
otbImportVectorImageFilter.h
otb::ImportVectorImageFilter::SpacingType
OutputImageType::SpacingType SpacingType
Definition: otbImportVectorImageFilter.h:51
otb::ImportVectorImageFilter::OriginType
OutputImageType::PointType OriginType
Definition: otbImportVectorImageFilter.h:52
otb::ImportVectorImageFilter::ImportVectorImageFilter
ImportVectorImageFilter()
Definition: otbImportVectorImageFilter.hxx:35
otb::ImportVectorImageFilter::SetOrigin
virtual void SetOrigin(const double data[])
otb::ImportVectorImageFilter::GetImportPointer
TPixel * GetImportPointer()
Definition: otbImportVectorImageFilter.hxx:126
otb::ImportVectorImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbImportVectorImageFilter.hxx:68
otb::ImportVectorImageFilter::~ImportVectorImageFilter
virtual ~ImportVectorImageFilter()
Definition: otbImportVectorImageFilter.hxx:55
otb::ImportVectorImageFilter::EnlargeOutputRequestedRegion
void EnlargeOutputRequestedRegion(itk::DataObject *output) override
Definition: otbImportVectorImageFilter.hxx:135
otb::ImportVectorImageFilter::SetSpacing
virtual void SetSpacing(const double data[])
otb::ImportVectorImageFilter::TPixel
TOutputPixel::ValueType TPixel
Definition: otbImportVectorImageFilter.h:78