Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMovieGenerator.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkMovieGenerator.h"
18 #include "mitkConfig.h"
19 #include "mitkGL.h"
20 #include <mitkRenderingManager.h>
21 
22 #if WIN32
23 #ifndef __GNUC__
24 //#if ! (_MSC_VER >= 1400)
26 //#endif
27 #else
28 #include "GL/glext.h"
29 #endif
30 #endif
31 #ifndef GL_BGR
32 #define GL_BGR GL_BGR_EXT
33 #endif
34 
36  : m_stepper(NULL), m_renderer(NULL), m_width(0), m_height(0), m_initialized(false), m_FrameRate(20)
37 {
38  m_fileName[0] = 0;
39 }
40 
42 {
43  Pointer smartPtr;
44  MovieGenerator *rawPtr = ::itk::ObjectFactory<MovieGenerator>::Create();
45  if (rawPtr == NULL)
46  {
47 #ifdef WIN32
48 #ifndef __GNUC__
49  //#if ! (_MSC_VER >= 1400)
51  return wp;
52 //#endif
53 #endif
54 #endif
55  }
56  smartPtr = rawPtr;
57  if (rawPtr != NULL)
58  rawPtr->UnRegister();
59  return smartPtr;
60 }
61 
63 {
64  bool ok = false;
65  if (m_stepper)
66  {
67  if (m_renderer)
68  m_renderer->GetRenderWindow()->MakeCurrent();
69  // m_stepper->First();
70  RenderingManager::GetInstance()->ForceImmediateUpdate(m_renderer->GetRenderWindow());
71 
72  ok = InitGenerator();
73  if (!ok)
74  {
75  TerminateGenerator();
76  return false;
77  }
78  int imgSize = 3 * m_width * m_height;
79  printf("Video size = %i x %i\n", m_width, m_height);
80  GLbyte *data = new GLbyte[imgSize];
81 
82  // duplicate steps if pingPong option is switched to on.
83  unsigned int numOfSteps = m_stepper->GetSteps();
84  if (m_stepper->GetPingPong())
85  numOfSteps *= 2;
86 
87  for (unsigned int i = 0; i < numOfSteps; i++)
88  {
89  if (m_renderer)
90  m_renderer->GetRenderWindow()->MakeCurrent();
91  RenderingManager::GetInstance()->ForceImmediateUpdate(m_renderer->GetRenderWindow());
92  glReadPixels(5, 5, m_width, m_height, GL_BGR, GL_UNSIGNED_BYTE, (void *)data);
93  AddFrame(data);
94  m_stepper->Next();
95  }
96  ok = TerminateGenerator();
97  delete[] data;
98  }
99  return ok;
100 }
101 
103 {
104  if (m_renderer)
105  {
106  m_renderer->GetRenderWindow()->MakeCurrent();
107 
108  if (!m_initialized)
109  {
110  RenderingManager::GetInstance()->ForceImmediateUpdate(m_renderer->GetRenderWindow());
111  m_initialized = InitGenerator();
112  }
113  if (!m_initialized)
114  {
115  TerminateGenerator();
116  return false;
117  }
118  int imgSize = 3 * m_width * m_height;
119  GLbyte *data = new GLbyte[imgSize];
120 
121  RenderingManager::GetInstance()->ForceImmediateUpdate(m_renderer->GetRenderWindow());
122  glReadPixels(5, 5, m_width, m_height, GL_BGR, GL_UNSIGNED_BYTE, (void *)data);
123  AddFrame(data);
124  delete[] data;
125  }
126  return true;
127 }
128 
130 {
131  TerminateGenerator();
132  m_initialized = false;
133 }
134 
135 void mitk::MovieGenerator::SetFrameRate(unsigned int rate)
136 {
137  m_FrameRate = rate;
138 }
139 
141 {
142  return m_FrameRate;
143 }
itk::SmartPointer< Self > Pointer
virtual void ReleaseMovieWriter()
releases a movie writer after usage of WriteCurrentFrameToMovie()
char m_fileName[1024]
stores the movie filename
virtual bool WriteMovie()
uses given stepper and filename to create a movie from the active OpenGL context
static Pointer New(void)
virtual void SetFrameRate(unsigned int rate)
MovieGenerator()
default constructor
static RenderingManager * GetInstance()
#define GL_BGR
void ForceImmediateUpdate(vtkRenderWindow *renderWindow)
static Pointer New()
virtual bool WriteCurrentFrameToMovie()
alternative way, which does not use a stepper;