GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SnapshotHelper.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  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  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #include "SnapshotHelper.h"
36 
37 #include "vtkRenderWindow.h"
38 #include "vtkRendererCollection.h"
39 #include "vtkImageData.h"
40 
41 #include "vtkImageClip.h"
42 #include "vtkImagePermute.h"
43 #include "vtkImageResample.h"
44 #include "vtkWindowToImageFilter.h"
45 #include "vtkSmartPointer.h"
46 
47 #include "vtkBMPWriter.h"
48 #include "vtkPostScriptWriter.h"
49 #include "vtkJPEGWriter.h"
50 #include "vtkPNGWriter.h"
51 #include "vtkTIFFWriter.h"
52 
53 #include "QVTKWidget.h"
54 
55 #include "vtkImageWriterHelper.h"
56 #include "vtkViewImage2D.h"
57 
58 //-------------------------------------------------------------------------
59 bool BuildScreenshotFromImage(vtkImageData *image,
60  vtkImageData *screenshot,
61  int tsize)
62 {
63  if ( !image || !screenshot )
64  {
65  return false;
66  }
67 
68  // Empty image, remove thumbnail/screenshot
69 
70  int image_dims[3];
71  image->GetDimensions(image_dims);
72  if ( image_dims[0] == 0
73  || image_dims[1] == 0
74  || image_dims[2] == 0 )
75  {
76  return false;
77  }
78 
79  vtkImageData *resample_input, *resample_output;
80 
81  // First, let's make sure we are processing the image as it
82  // is by clipping its UpdateExtent. By doing so, we prevent our resample
83  // and permute filter the process the image's *whole* extent.
84 
85  vtkSmartPointer< vtkImageClip > clip =
86  vtkSmartPointer< vtkImageClip >::New();
87  clip->SetInput(image);
88  clip->SetOutputWholeExtent( image->GetUpdateExtent() );
89  clip->Update();
90 
91  // Permute, as a convenience
92 
93  int clip_dims[3];
94  clip->GetOutput()->GetDimensions(clip_dims);
95 
96  vtkSmartPointer< vtkImagePermute > permute =
97  vtkSmartPointer< vtkImagePermute >::New();
98  if ( clip_dims[2] != 1 )
99  {
100  permute->SetInput( clip->GetOutput() );
101  if ( clip_dims[0] == 1 )
102  {
103  permute->SetFilteredAxes(1, 2, 0);
104  }
105  else
106  {
107  permute->SetFilteredAxes(0, 2, 1);
108  }
109  resample_input = permute->GetOutput();
110  }
111  else
112  {
113  resample_input = clip->GetOutput();
114  }
115 
116  resample_input->Update();
117  int resample_input_dims[3]; //, resample_output_dims[3];
118 
119  resample_input->GetDimensions(resample_input_dims);
120  double *resample_input_spacing = resample_input->GetSpacing();
121 
122  int large_dim = 0, small_dim = 1;
123  if ( resample_input_dims[0] < resample_input_dims[1] )
124  {
125  large_dim = 1;
126  small_dim = 0;
127  }
128 
129  if ( tsize != 0 )
130  {
131  vtkSmartPointer< vtkImageResample > resample =
132  vtkSmartPointer< vtkImageResample >::New();
133  resample->SetInput(resample_input);
134  resample->SetInterpolationModeToCubic();
135  resample->SetDimensionality(2);
136 
137  // Create the screenshot
138 
139  double factor = static_cast< double >( tsize )
140  / static_cast< double >( resample_input_dims[large_dim] );
141 
142  resample->SetAxisMagnificationFactor(large_dim, factor);
143  resample->SetAxisMagnificationFactor(
144  small_dim, factor * ( resample_input_spacing[small_dim]
145  / resample_input_spacing[large_dim] ) );
146  resample->Update();
147  resample_output = resample->GetOutput();
148  // resample_output->GetDimensions(resample_output_dims);
149 
150  screenshot->ShallowCopy(resample_output);
151  }
152  else
153  {
154  screenshot->ShallowCopy(resample_input);
155  }
156 
157  return true;
158 }
159 
160 //-------------------------------------------------------------------------
161 
162 //-------------------------------------------------------------------------
164  vtkRenderWindow *win,
165  vtkImageData *screenshot,
166  int tsize)
167 {
168  if ( win && screenshot )
169  {
170  vtkSmartPointer< vtkWindowToImageFilter > filter =
171  vtkSmartPointer< vtkWindowToImageFilter >::New();
172  filter->ShouldRerenderOff();
173  filter->SetInput(win);
174  filter->Update();
175  bool res = BuildScreenshotFromImage(filter->GetOutput(),
176  screenshot, tsize);
177  return res;
178  }
179  return false;
180 }
181 
182 //-------------------------------------------------------------------------
183 
184 //-------------------------------------------------------------------------
185 QString SnapshotView(QVTKWidget *iWidget,
186  const GoFigure::FileType & iType,
187  const QString & iBaseName,
188  const unsigned int & iSnapshotId)
189 {
190  vtkSmartPointer< vtkImageData > image =
191  vtkSmartPointer< vtkImageData >::New();
192  BuildScreenshotFromRenderWindow(iWidget->GetRenderWindow(),
193  image);
194  QString filename = iBaseName;
195  filename.append( QString("%1").arg(iSnapshotId) );
196 
197  switch ( iType )
198  {
199  case GoFigure::BMP:
200  {
201  filename.append(".bmp");
202  vtkWriteImage< vtkBMPWriter >(image, filename);
203  break;
204  }
205  case GoFigure::EPS:
206  {
207  filename.append(".eps");
208  vtkWriteImage< vtkPostScriptWriter >(image, filename);
209  break;
210  }
211  case GoFigure::JPEG:
212  {
213  filename.append(".jpeg");
214  vtkWriteImage< vtkJPEGWriter >(image, filename);
215  break;
216  }
217  case GoFigure::PNG:
218  {
219  filename.append(".png");
220  vtkWriteImage< vtkPNGWriter >(image, filename);
221  break;
222  }
223  case GoFigure::TIFF:
224  {
225  filename.append(".tiff");
226  vtkWriteImage< vtkTIFFWriter >(image, filename);
227  break;
228  }
229  default:
230  {
231  std::cerr << "FileType is not supported for Snapshot" << std::endl;
232  return QString();
233  }
234  }
235  return filename;
236 }
237 
238 //-------------------------------------------------------------------------
239 
240 void SetupViewGivenQVTKWidget(vtkViewImage2D *iView, QVTKWidget *iWidget)
241 {
242  vtkRenderWindow *renwin = iWidget->GetRenderWindow();
243 
244  iView->SetRenderWindow(renwin);
245  iView->SetRenderer( renwin->GetRenderers()->GetFirstRenderer() );
246  iView->SetupInteractor( iWidget->GetInteractor() );
247 }
bool BuildScreenshotFromRenderWindow(vtkRenderWindow *win, vtkImageData *screenshot, int tsize)
QString & append(QChar ch)
void SetupViewGivenQVTKWidget(vtkViewImage2D *iView, QVTKWidget *iWidget)
bool BuildScreenshotFromImage(vtkImageData *image, vtkImageData *screenshot, int tsize)
QString SnapshotView(QVTKWidget *iWidget, const GoFigure::FileType &iType, const QString &iBaseName, const unsigned int &iSnapshotId)
Basic class to handle 2D/3D items such as images and polydatas visualization in 2D.