61{
62 if (argc != 2 && argc != 4)
63 {
64 cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
65 exit(-1);
66 }
67
68 string vInput = argv[1];
69 string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
70
71
72 vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
73 vtkSmartPointer<vtkGraphicsFactory>::New();
74 graphics_factory->SetOffScreenOnlyMode(1);
75 graphics_factory->SetUseMesaClasses(1);
76
77
78 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
79 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
80 reader->SetFileName(vInput.c_str());
81 reader->Update();
82
83 vtkSmartPointer<vtkDataSet> data =
84 vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
85 data->GetPointData()->SetActiveScalars("u");
86
87 double scalar_range[2];
88 data->GetScalarRange(scalar_range);
89 if (argc == 4)
90 {
91 scalar_range[0] = atof(argv[2]);
92 scalar_range[1] = atof(argv[3]);
93 }
94
95
96 vtkSmartPointer<vtkLookupTable> lookup =
97 vtkSmartPointer<vtkLookupTable>::New();
98 lookup->SetHueRange(0.0, 1.0);
99 lookup->SetSaturationRange(1, 1);
100 lookup->SetTableRange(scalar_range);
101 lookup->SetValueRange(1, 1);
102 lookup->Build();
103
104
105 vtkSmartPointer<vtkDataSetMapper> mapper =
106 vtkSmartPointer<vtkDataSetMapper>::New();
107#if VTK_MAJOR_VERSION <= 5
108 mapper->SetInput(data);
109#else
110 mapper->SetInputData(data);
111#endif
112
113#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
114
115 mapper->ImmediateModeRenderingOn();
116#endif
117
118 mapper->ScalarVisibilityOn();
119 mapper->SetScalarModeToUsePointData();
120 mapper->UseLookupTableScalarRangeOn();
121
122 mapper->SetLookupTable(lookup);
123
124 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
125 actor->SetMapper(mapper);
126
127
128 vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
129 camera->SetPosition(0.0, -1.0, 1.0);
130 camera->SetFocalPoint(0, 0, 0);
131
132
133 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
134 vtkSmartPointer<vtkRenderWindow> renderWindow =
135 vtkSmartPointer<vtkRenderWindow>::New();
136 renderWindow->SetOffScreenRendering(1);
137 renderWindow->AddRenderer(renderer);
138
139
140 renderer->AddActor(actor);
141 renderer->SetBackground(0, 0, 0);
142 renderer->SetActiveCamera(camera);
143 renderer->ResetCamera();
144
145 renderWindow->Render();
146
147
148 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
149 vtkSmartPointer<vtkWindowToImageFilter>::New();
150 windowToImageFilter->SetInput(renderWindow);
151
152#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
153
154 windowToImageFilter->SetMagnification(4);
155#else
156 windowToImageFilter->SetScale(4, 4);
157#endif
158 windowToImageFilter->Update();
159
160
161 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
162 writer->SetFileName(vOutput.c_str());
163 writer->SetInputConnection(windowToImageFilter->GetOutputPort());
164 writer->Write();
165
166 return EXIT_SUCCESS;
167}