Orfeo Toolbox  3.16
otbImageWidget.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbImageWidget_txx
19 #define __otbImageWidget_txx
20 
21 #include "otbImageWidget.h"
23 
24 namespace otb
25 {
26 template <class TInputImage>
28 ::ImageWidget() : m_IsotropicZoom(1.0), m_InterpolationMethod(GL_LINEAR),
29  m_OpenGlBuffer(NULL), m_OpenGlBufferedRegion(),
30  m_Extent(), m_SubsamplingRate(1), m_ImageToScreenTransform(),
31  m_ScreenToImageTransform(), m_GlComponents()
32 {
33  // Initialize space to screen transform and inverse
34  m_ImageToScreenTransform = AffineTransformType::New();
35  m_ScreenToImageTransform = AffineTransformType::New();
36 
37  // Initialize the components list
38  m_GlComponents = GlComponentListType::New();
39 }
40 
41 template <class TInputImage>
44 {
45  this->ClearBuffer();
46 }
47 
48 template <class TInputImage>
49 void
51 ::PrintSelf(std::ostream& os, itk::Indent indent) const
52 {
53  // Call the superclass implementation
54  Superclass::PrintSelf(os, indent);
55 
56  if (m_OpenGlBuffer == NULL)
57  {
58  os << indent << indent << "OpenGl buffer is not allocated." << std::endl;
59  }
60  else
61  {
62  os << indent << indent << "OpenGl buffer is allocated with size " << m_OpenGlBufferedRegion.GetSize() << "." <<
63  std::endl;
64  }
65  os << indent << indent << "OpenGl isotropic zoom is " << m_IsotropicZoom << "." << std::endl;
66 }
67 
68 template <class TInputImage>
69 void
71 ::ReadBuffer(const InputImageType * image, const RegionType& region)
72 {
73  // Before doing anything, check if region is inside the buffered
74  // region of image
75  if (!image->GetBufferedRegion().IsInside(region))
76  {
77  itkExceptionMacro(<< "Region to read is oustside of the buffered region.");
78  }
79  // Delete previous buffer if needed
80  this->ClearBuffer();
81 
82  // Allocate new memory
83  m_OpenGlBuffer = new unsigned char[3 * region.GetNumberOfPixels()];
84 
85  // Declare the iterator
87 
88  // Go to begin
89  it.GoToBegin();
90 
91  while (!it.IsAtEnd())
92  {
93  // Fill the buffer
94  unsigned int index = 0;
95  if (!this->GetUseGlAcceleration())
96  {
97  // compute the linear index (buffer is flipped around X axis
98  // when gl acceleration is disabled
99  index = ComputeXAxisFlippedBufferIndex(it.GetIndex(), region);
100  }
101  else
102  {
103  // Conpute the linear index
104  index = ComputeBufferIndex(it.GetIndex(), region);
105  }
106 
107  // Fill the buffer
108  m_OpenGlBuffer[index] = it.Get()[0];
109  m_OpenGlBuffer[index + 1] = it.Get()[1];
110  m_OpenGlBuffer[index + 2] = it.Get()[2];
111  ++it;
112  }
113  // Last, updating buffer size
114  m_OpenGlBufferedRegion = region;
115 }
116 
117 template <class TInputImage>
118 void
121 {
122  // Delete previous buffer if needed
123  if (m_OpenGlBuffer != NULL)
124  {
125  delete[] m_OpenGlBuffer;
126  m_OpenGlBuffer = NULL;
127  }
128 
129  RegionType region;
130  typename RegionType::IndexType index;
131  typename RegionType::SizeType size;
132  size.Fill(0);
133  index.Fill(0);
134  region.SetIndex(index);
135  region.SetSize(size);
136 
137  // Last, updating buffer size
138  m_OpenGlBufferedRegion = region;
139 
140 }
141 
142 template <class TInputImage>
143 void
146 {
147  if (m_IsotropicZoom <= 0)
148  {
149  itkExceptionMacro(<< "Internal error: Isotropic zoom should be non null positive.");
150  }
151 
152  typename RegionType::IndexType index;
153  typename RegionType::SizeType size;
154  // Update image extent
155  size[0] = static_cast<unsigned int>(m_IsotropicZoom * static_cast<double>(m_OpenGlBufferedRegion.GetSize()[0]));
156  size[1] = static_cast<unsigned int>(m_IsotropicZoom * static_cast<double>(m_OpenGlBufferedRegion.GetSize()[1]));
157  index[0] = (this->w() - size[0]) / 2;
158  index[1] = (this->h() - size[1]) / 2;
159  m_Extent.SetIndex(index);
160  m_Extent.SetSize(size);
161 
162  // Image to screen matrix
163  typename AffineTransformType::MatrixType s2iMatrix;
164  s2iMatrix.Fill(0);
165  const double s2iSpacing = (m_SubsamplingRate) / m_IsotropicZoom;
166  s2iMatrix(0, 0) = s2iSpacing;
167  s2iMatrix(1, 1) = -s2iSpacing;
168  m_ScreenToImageTransform->SetMatrix(s2iMatrix);
169 
170  // Image to screen translation
171  typename AffineTransformType::OutputVectorType translation;
172  translation[0] = m_SubsamplingRate * (m_OpenGlBufferedRegion.GetIndex()[0] - m_Extent.GetIndex()[0] / m_IsotropicZoom);
173  translation[1] = m_SubsamplingRate *
174  (((m_Extent.GetIndex()[1] +
175  m_Extent.GetSize()[1]) / m_IsotropicZoom) + m_OpenGlBufferedRegion.GetIndex()[1]);
176  m_ScreenToImageTransform->SetTranslation(translation);
177 
178  // Compute the inverse transform
179  bool couldInvert = m_ScreenToImageTransform->GetInverse(m_ImageToScreenTransform);
180  if (couldInvert == false)
181  {
182  itkExceptionMacro(<< "Internal error: Could not invert ScreenToImageTransform.");
183  }
184 }
185 
186 template <class TInputImage>
187 void
190 {
191  // perform checks from superclass and gl initialization
192  Superclass::draw();
193 
194  glDisable(GL_BLEND);
195  // Check if there is somthing to draw
196  if (m_OpenGlBuffer == NULL)
197  {
198  return;
199  }
200 
201  // Update transforms
202  this->UpdateTransforms();
203 
204  if (!this->GetUseGlAcceleration())
205  {
206  // Set the pixel Zoom
207  glRasterPos2f(m_Extent.GetIndex()[0], m_Extent.GetIndex()[1]);
208  glPixelZoom(m_IsotropicZoom, m_IsotropicZoom);
209 
210  // display the image
211  glDrawPixels(m_OpenGlBufferedRegion.GetSize()[0],
212  m_OpenGlBufferedRegion.GetSize()[1],
213  GL_RGB,
214  GL_UNSIGNED_BYTE,
215  m_OpenGlBuffer);
216  }
217  else
218  {
219  glEnable(GL_TEXTURE_2D);
220  glColor4f(1.0, 1.0, 1.0, 0.0);
221  GLuint texture;
222  glGenTextures(1, &texture);
223  glBindTexture(GL_TEXTURE_2D, texture);
224  glTexImage2D(GL_TEXTURE_2D, 0, 3, m_OpenGlBufferedRegion.GetSize()[0],
225  m_OpenGlBufferedRegion.GetSize()[1], 0, GL_RGB, GL_UNSIGNED_BYTE, m_OpenGlBuffer);
226  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_InterpolationMethod);
227  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_InterpolationMethod);
228  glBindTexture (GL_TEXTURE_2D, texture);
229  glBegin (GL_QUADS);
230  glTexCoord2f (0.0, 1.0);
231  glVertex3f (m_Extent.GetIndex()[0], m_Extent.GetIndex()[1], 0.0);
232  glTexCoord2f (1.0, 1.0);
233  glVertex3f (m_Extent.GetIndex()[0] + m_Extent.GetSize()[0], m_Extent.GetIndex()[1], 0.0);
234  glTexCoord2f (1.0, 0.0);
235  glVertex3f (m_Extent.GetIndex()[0] + m_Extent.GetSize()[0], m_Extent.GetIndex()[1] + m_Extent.GetSize()[1], 0.0);
236  glTexCoord2f (0.0, 0.0);
237  glVertex3f (m_Extent.GetIndex()[0], m_Extent.GetIndex()[1] + m_Extent.GetSize()[1], 0.0);
238  glEnd ();
239  glDeleteTextures(1, &texture);
240  glDisable(GL_TEXTURE_2D);
241  }
242 
243  // Render additionnal GlComponents
244  for (GlComponentIteratorType it = m_GlComponents->ReverseBegin();
245  it != m_GlComponents->ReverseEnd(); ++it)
246  {
247  if (it.Get()->GetVisible())
248  {
249  it.Get()->Render(m_Extent, m_ImageToScreenTransform);
250  }
251  }
252 }
253 }
254 #endif

Generated at Sun Feb 3 2013 00:31:20 for Orfeo Toolbox with doxygen 1.8.1.1