Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxViewCollectionImageWriter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
33 
34 #include <QPixmap>
35 #include "cxPatientModelService.h"
36 #include <QtConcurrent>
37 #include <QDesktopWidget>
38 #include <QApplication>
39 #include "cxReporter.h"
40 #include "boost/bind.hpp"
41 #include <QScreen>
42 #include <QVBoxLayout>
43 #include "cxViewService.h"
44 //#include "cxSecondaryViewLayoutWindow.h"
45 #include "cxViewCollectionWidget.h"
46 #include "vtkRenderer.h"
47 #include "vtkWindowToImageFilter.h"
48 #include "vtkRenderWindow.h"
49 #include "vtkPNGWriter.h"
50 #include "vtkUnsignedCharArray.h"
51 #include <QPainter>
52 #include "cxVolumeHelpers.h"
53 #include "vtkImageImport.h"
54 #include <QTime>
55 
56 namespace cx
57 {
58 
59 
61 {
62 
63 }
64 
66 {
67  std::vector<ViewPtr> views = mWidget->getViews();
68  Eigen::Array3i target_size(mWidget->width(), mWidget->height(), 1);
69  vtkImageDataPtr target = generateVtkImageData(target_size, Vector3D(1,1,1), 150, 3);
70 
71  for (unsigned i=0; i<views.size(); ++i)
72  {
73 // CX_LOG_CHANNEL_DEBUG("CA") << "chec pos for view " << i;
74  vtkImageDataPtr vtkImage = this->view2vtkImageData(views[i]);
75  QPoint vtkpos = this->getVtkPositionOfView(views[i]);
76  this->drawImageAtPos(target, vtkImage, vtkpos);
77  }
78 
79  return target;
80 }
81 
82 QPoint ViewCollectionImageWriter::getVtkPositionOfView(ViewPtr view)
83 {
84  QPoint qpos_ul = mWidget->getPosition(view); // UL position in qt-space of mWidget
85 // CX_LOG_CHANNEL_DEBUG("CA") << " qpos_ul " << qpos_ul.x() << " " << qpos_ul.y();
86  Eigen::Array2i size_view(view->getRenderer()->GetSize());
87 // CX_LOG_CHANNEL_DEBUG("CA") << " size " << size;
88  QPoint qpos_ll = qpos_ul + QPoint(0, size_view[1]-1); // LL position in qt-space of mWidget
89 // CX_LOG_CHANNEL_DEBUG("CA") << " qpos_ll " << qpos_ll.x() << " " << qpos_ll.y();
90  QPoint vtkpos_ll = this->qt2vtk(qpos_ll);
91 // CX_LOG_CHANNEL_DEBUG("CA") << " vtkpos_ll " << vtkpos_ll.x() << " " << vtkpos_ll.y();
92  return vtkpos_ll;
93 }
94 
95 QPoint ViewCollectionImageWriter::qt2vtk(QPoint qpos)
96 {
97  int H = mWidget->height();
98  QPoint vtkpos(qpos.x(), H-1-qpos.y());
99  return vtkpos;
100 }
101 
102 
103 void ViewCollectionImageWriter::drawImageAtPos(vtkImageDataPtr target, vtkImageDataPtr image, QPoint pos)
104 {
105  unsigned char* src = reinterpret_cast<unsigned char*>(image->GetScalarPointer());
106  unsigned char* dst = reinterpret_cast<unsigned char*>(target->GetScalarPointer());
107  Eigen::Array3i dim_src(image->GetDimensions());
108  int depth = 3;
109  CX_ASSERT(target->GetNumberOfScalarComponents()==depth);
110  CX_ASSERT(image->GetNumberOfScalarComponents()==depth);
111 
112  for (int y=0; y<dim_src[1]; ++y)
113  {
114  unsigned char* src = reinterpret_cast<unsigned char*>(image->GetScalarPointer(0,y,0));
115  unsigned char* dst = reinterpret_cast<unsigned char*>(target->GetScalarPointer(pos.x(),pos.y()+y,0));
116  memcpy(dst, src, dim_src[0]*depth);
117  }
118 }
119 
120 // the vtk canonical way
121 //vtkImageDataPtr ViewCollectionImageWriter::view2vtkImageData(ViewPtr view)
122 //{
123 // Eigen::Array4d vp(view->getRenderer()->GetViewport());
124 // vtkWindowToImageFilterPtr w2i = vtkWindowToImageFilterPtr::New();
125 // w2i->ShouldRerenderOff();
126 // w2i->SetInput(view->getRenderWindow());
127 // w2i->SetViewport(vp.data());
128 // w2i->SetReadFrontBuffer(false);
129 // CX_LOG_CHANNEL_DEBUG("CA") << " - 2";
130 // w2i->Update();
131 // CX_LOG_CHANNEL_DEBUG("CA") << " - 3 -upd";
132 // vtkImageDataPtr image = w2i->GetOutput();
133 // return image;
134 //}
135 
136 // alternative to vtkWindowToImageFilter, effectively reimplementing parts of that filter (for debugging speed)
137 vtkImageDataPtr ViewCollectionImageWriter::view2vtkImageData(ViewPtr view)
138 {
139  vtkRenderWindowPtr renderWindow = view->getRenderWindow();
140  vtkRendererPtr renderer = view->getRenderer();
141  Eigen::Array4d vp(renderer->GetViewport()); // need the viewport in pixels
142 
143  Eigen::Array2i origin(renderer->GetOrigin());
144  Eigen::Array2i size(renderer->GetSize());
145  Eigen::Array<int,6,1> extent;
146  extent[0] = 0;
147  extent[1] = size[0]-1;
148  extent[2] = 0;
149  extent[3] = size[1]-1;
150  extent[4] = 0;
151  extent[5] = 0;
152  int frontBuffer = false;
153 
154  unsigned char *pixels;
155 // QTime qtimer = QTime::currentTime();
156 
157 // CX_LOG_CHANNEL_DEBUG("CA") << " renderWindow->GetPixelData0";
158  renderWindow->MakeCurrent();
159 // CX_LOG_CHANNEL_DEBUG("CA") << " after makecurrent";
160  pixels = renderWindow->GetPixelData(origin[0], origin[1], origin[0]+size[0]-1, origin[1]+size[1]-1, frontBuffer);
161 // CX_LOG_CHANNEL_DEBUG("CA") << " renderWindow->GetPixelData2";
162 
163 // std::cout << "ViewCollectionImageWriter::view2vtkImageData qtimer ms=" << qtimer.msecsTo(QTime::currentTime()) << std::endl;
164 
165  typedef vtkSmartPointer<vtkImageImport> vtkImageImportPtr;
166  vtkImageImportPtr import = vtkImageImportPtr::New();
167  import->SetDataScalarTypeToUnsignedChar();
168  import->SetNumberOfScalarComponents(3);
169  import->SetDataExtent(extent.data());
170  import->SetWholeExtent(extent.data());
171  bool takeOwnership = true;
172  import->SetImportVoidPointer(pixels, !takeOwnership);
173  import->Update();
174  return import->GetOutput();
175 }
176 
177 
179 {
180  CX_ASSERT(input->GetNumberOfScalarComponents()==3);
181  CX_ASSERT(input->GetScalarType() == VTK_UNSIGNED_CHAR);
182 
183  unsigned char* ptr = reinterpret_cast<unsigned char*>(input->GetScalarPointer());
184  Eigen::Array3i dim(input->GetDimensions());
185 
186  // important: input the line length. This will not copy the buffer.
187  QImage retval(ptr, dim[0], dim[1], dim[0]*3, QImage::Format_RGB888);
188  // copy contents into image, then flip according to conventions vtk<->qt.
189  return retval.copy().mirrored(false, true);
190 }
191 
192 
193 } // namespace cx
#define CX_ASSERT(statement)
Definition: cxLogger.h:131
virtual std::vector< ViewPtr > getViews()=0
ViewCollectionImageWriter(ViewCollectionWidget *widget)
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
boost::shared_ptr< class View > ViewPtr
static QImage vtkImageData2QImage(vtkImageDataPtr input)
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
vtkImageDataPtr generateVtkImageData(Eigen::Array3i dim, Vector3D spacing, const unsigned char initValue, int components)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
virtual QPoint getPosition(ViewPtr view)=0
vtkSmartPointer< class vtkImageData > vtkImageDataPtr