1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    PrmMagnify.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10   Copyright 2005 Sandia Corporation. Under the terms of Contract
11   DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
12   or on behalf of the U.S. Government. Redistribution and use in source and
13   binary forms, with or without modification, are permitted provided that this
14   Notice and any statement of authorship are reproduced on all copies.
15 
16      This software is distributed WITHOUT ANY WARRANTY; without even
17      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18      PURPOSE.  See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
22 #include "vtkActor.h"
23 #include "vtkCellData.h"
24 #include "vtkDataArray.h"
25 #include "vtkDummyController.h"
26 #include "vtkIdFilter.h"
27 #include "vtkObjectFactory.h"
28 #include "vtkParallelRenderManager.h"
29 #include "vtkPolyDataMapper.h"
30 #include "vtkRegressionTestImage.h"
31 #include "vtkRenderer.h"
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderWindowInteractor.h"
34 #include "vtkSphereSource.h"
35 
36 #include "vtkImageActor.h"
37 #include "vtkImageMapper3D.h"
38 #include "vtkImageData.h"
39 #include "vtkImageMandelbrotSource.h"
40 #include "vtkImageShiftScale.h"
41 #include "vtkPointData.h"
42 #include "vtkUnsignedCharArray.h"
43 
44 #include "vtkSmartPointer.h"
45 
46 #define VTK_CREATE(type, var)   \
47   vtkSmartPointer<type> var = vtkSmartPointer<type>::New()
48 
49 //-----------------------------------------------------------------------------
50 
51 class vtkTestMagnifyRenderManager : public vtkParallelRenderManager
52 {
53 public:
54   vtkTypeMacro(vtkTestMagnifyRenderManager, vtkParallelRenderManager);
55   static vtkTestMagnifyRenderManager *New();
56 
57 protected:
58   vtkTestMagnifyRenderManager();
59   ~vtkTestMagnifyRenderManager();
60 
61   virtual void PreRenderProcessing();
62   virtual void PostRenderProcessing();
63 
64   virtual void ReadReducedImage();
65 
66   vtkImageMandelbrotSource *Mandelbrot;
67 
68 private:
69   vtkTestMagnifyRenderManager(const vtkTestMagnifyRenderManager &);  // Not implemented.
70   void operator=(const vtkTestMagnifyRenderManager &);  // Not implemented.
71 };
72 
73 vtkStandardNewMacro(vtkTestMagnifyRenderManager);
74 
vtkTestMagnifyRenderManager()75 vtkTestMagnifyRenderManager::vtkTestMagnifyRenderManager()
76 {
77   this->Mandelbrot = vtkImageMandelbrotSource::New();
78 }
79 
~vtkTestMagnifyRenderManager()80 vtkTestMagnifyRenderManager::~vtkTestMagnifyRenderManager()
81 {
82   this->Mandelbrot->Delete();
83 }
84 
PreRenderProcessing()85 void vtkTestMagnifyRenderManager::PreRenderProcessing()
86 {
87   this->RenderWindowImageUpToDate = 0;
88   this->RenderWindow->SwapBuffersOff();
89 }
90 
PostRenderProcessing()91 void vtkTestMagnifyRenderManager::PostRenderProcessing()
92 {
93   this->FullImage->SetNumberOfComponents(4);
94   this->FullImage->SetNumberOfTuples(this->FullImageSize[0]*this->FullImageSize[1]);
95 
96   int fullImageViewport[4], reducedImageViewport[4];
97 
98   // Read in image as RGBA.
99   this->UseRGBA = 1;
100   this->ReducedImageUpToDate = 0;
101   this->ReadReducedImage();
102 
103   fullImageViewport[0] = 0;  fullImageViewport[1] = 0;
104   fullImageViewport[2] = this->FullImageSize[0]/2;
105   fullImageViewport[3] = this->FullImageSize[1]/2;
106   reducedImageViewport[0] = 0;  reducedImageViewport[1] = 0;
107   reducedImageViewport[2] = this->ReducedImageSize[0]/2;
108   reducedImageViewport[3] = this->ReducedImageSize[1]/2;
109   this->MagnifyImageNearest(this->FullImage, this->FullImageSize,
110                             this->ReducedImage, this->ReducedImageSize,
111                             fullImageViewport, reducedImageViewport);
112 
113   fullImageViewport[0] = this->FullImageSize[0]/2;
114   fullImageViewport[1] = 0;
115   fullImageViewport[2] = this->FullImageSize[0];
116   fullImageViewport[3] = this->FullImageSize[1]/2;
117   reducedImageViewport[0] = this->ReducedImageSize[0]/2;
118   reducedImageViewport[1] = 0;
119   reducedImageViewport[2] = this->ReducedImageSize[0];
120   reducedImageViewport[3] = this->ReducedImageSize[1]/2;
121   this->MagnifyImageLinear(this->FullImage, this->FullImageSize,
122                            this->ReducedImage, this->ReducedImageSize,
123                            fullImageViewport, reducedImageViewport);
124 
125   // Read in image as RGB.
126   this->UseRGBA = 0;
127   this->ReducedImageUpToDate = 0;
128   this->ReadReducedImage();
129 
130   fullImageViewport[0] = 0;
131   fullImageViewport[1] = this->FullImageSize[1]/2;
132   fullImageViewport[2] = this->FullImageSize[0]/2;
133   fullImageViewport[3] = this->FullImageSize[1];
134   reducedImageViewport[0] = 0;
135   reducedImageViewport[1] = this->ReducedImageSize[1]/2;
136   reducedImageViewport[2] = this->ReducedImageSize[0]/2;
137   reducedImageViewport[3] = this->ReducedImageSize[1];
138   this->MagnifyImageNearest(this->FullImage, this->FullImageSize,
139                             this->ReducedImage, this->ReducedImageSize,
140                             fullImageViewport, reducedImageViewport);
141 
142   fullImageViewport[0] = this->FullImageSize[0]/2;
143   fullImageViewport[1] = this->FullImageSize[1]/2;
144   fullImageViewport[2] = this->FullImageSize[0];
145   fullImageViewport[3] = this->FullImageSize[1];
146   reducedImageViewport[0] = this->ReducedImageSize[0]/2;
147   reducedImageViewport[1] = this->ReducedImageSize[1]/2;
148   reducedImageViewport[2] = this->ReducedImageSize[0];
149   reducedImageViewport[3] = this->ReducedImageSize[1];
150   this->MagnifyImageLinear(this->FullImage, this->FullImageSize,
151                            this->ReducedImage, this->ReducedImageSize,
152                            fullImageViewport, reducedImageViewport);
153 
154   this->FullImageUpToDate = 1;
155 
156   this->WriteFullImage();
157 
158   this->RenderWindow->SwapBuffersOn();
159   this->RenderWindow->Frame();
160 }
161 
ReadReducedImage()162 void vtkTestMagnifyRenderManager::ReadReducedImage()
163 {
164   if (this->ReducedImageUpToDate) return;
165 
166   this->Mandelbrot->SetWholeExtent(0, this->ReducedImageSize[0]-1,
167                                    0, this->ReducedImageSize[1]-1, 0, 0);
168   this->Mandelbrot->SetMaximumNumberOfIterations(255);
169   this->Mandelbrot->Update();
170 
171   vtkIdType numpixels = this->ReducedImageSize[0]*this->ReducedImageSize[1];
172 
173   vtkDataArray *src
174     = this->Mandelbrot->GetOutput()->GetPointData()->GetScalars();
175   if (src->GetNumberOfTuples() != numpixels)
176     {
177     vtkErrorMacro("Image is wrong size!");
178     return;
179     }
180 
181   if (this->UseRGBA)
182     {
183     this->ReducedImage->SetNumberOfComponents(4);
184     }
185   else
186     {
187     this->ReducedImage->SetNumberOfComponents(3);
188     }
189   this->ReducedImage->SetNumberOfTuples(numpixels);
190 
191   double color[4];
192   color[3] = 255;
193 
194   for (vtkIdType i = 0; i < numpixels; i++)
195     {
196     double value = src->GetComponent(i, 0);
197     color[0] = value;
198     color[1] = (value < 128) ? value : (255 - value);
199     color[2] = 255 - value;
200     this->ReducedImage->SetTuple(i, color);
201     }
202 }
203 
204 
205 //-----------------------------------------------------------------------------
206 
PrmMagnify(int argc,char * argv[])207 int PrmMagnify(int argc, char *argv[])
208 {
209   VTK_CREATE(vtkDummyController, controller);
210   controller->Initialize(&argc, &argv);
211 
212   VTK_CREATE(vtkTestMagnifyRenderManager, prm);
213   prm->SetController(controller);
214 
215 //   VTK_CREATE(vtkSphereSource, sphere);
216 //   sphere->SetEndPhi(90.0);
217 //   sphere->SetPhiResolution(4);
218 
219 //   VTK_CREATE(vtkIdFilter, colors);
220 //   colors->SetInputConnection(sphere->GetOutputPort());
221 //   colors->PointIdsOff();
222 //   colors->CellIdsOn();
223 //   colors->FieldDataOff();
224 //   colors->Update();
225 
226 //   VTK_CREATE(vtkPolyDataMapper, mapper);
227 //   mapper->SetInputConnection(colors->GetOutputPort());
228 //   mapper->UseLookupTableScalarRangeOff();
229 //   mapper->SetScalarRange(colors->GetOutput()->GetCellData()
230 //                          ->GetScalars()->GetRange());
231 
232 //   VTK_CREATE(vtkActor, actor);
233 //   actor->SetMapper(mapper);
234 
235   VTK_CREATE(vtkImageMandelbrotSource, mandelbrot);
236   mandelbrot->SetWholeExtent(0, 73, 0, 73, 0, 0);
237   mandelbrot->SetMaximumNumberOfIterations(255);
238 
239   VTK_CREATE(vtkImageShiftScale, charImage);
240   charImage->SetInputConnection(mandelbrot->GetOutputPort());
241   charImage->SetShift(0);
242   charImage->SetScale(1);
243   charImage->SetOutputScalarTypeToUnsignedChar();
244 
245   VTK_CREATE(vtkImageActor, actor);
246   actor->GetMapper()->SetInputConnection(charImage->GetOutputPort());
247   actor->InterpolateOff();
248 
249   vtkSmartPointer<vtkRenderer> renderer = prm->MakeRenderer();
250   renderer->Delete();   // Remove duplicate reference.
251   renderer->AddActor(actor);
252   renderer->SetBackground(1, 0, 0);
253 
254   vtkSmartPointer<vtkRenderWindow> renwin = prm->MakeRenderWindow();
255   renwin->Delete();     // Remove duplicate reference.
256   renwin->SetSize(256, 256);
257   renwin->AddRenderer(renderer);
258   prm->SetRenderWindow(renwin);
259 
260   prm->ResetAllCameras();
261   prm->SetImageReductionFactor(8);
262 
263   // Run the regression test.
264   renwin->Render();
265   int retVal = vtkRegressionTestImage(renwin);
266   if (retVal == vtkRegressionTester::DO_INTERACTOR)
267     {
268     VTK_CREATE(vtkRenderWindowInteractor, iren);
269     iren->SetRenderWindow(renwin);
270     renwin->Render();
271     iren->Start();
272     retVal = vtkRegressionTester::PASSED;
273     }
274 
275   controller->Finalize();
276 
277   return !retVal;
278 }
279