-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathImageWriter.cxx
More file actions
189 lines (167 loc) · 4.72 KB
/
ImageWriter.cxx
File metadata and controls
189 lines (167 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkBMPWriter.h>
#include <vtkImageWriter.h>
#include <vtkJPEGWriter.h>
#include <vtkPNGWriter.h>
#include <vtkPNMWriter.h>
#include <vtkPostScriptWriter.h>
#include <vtkTIFFWriter.h>
#include <vtkWindowToImageFilter.h>
#include <algorithm>
#include <array>
#include <locale>
#include <string>
namespace
{
/**
* Write the render window view to an image file.
*
* Image types supported are:
* BMP, JPEG, PNM, PNG, PostScript, TIFF.
* The default parameters are used for all writers, change as needed.
*
* @param fileName The file name, if no extension then PNG is assumed.
* @param renWin The render window.
* @param rgba Used to set the buffer type.
*
*/
void WriteImage(std::string const& fileName, vtkRenderWindow* renWin,
bool rgba = true);
}
int main(int, char* [])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Set the background color.
std::array<unsigned char , 4> bkg{{26, 51, 102, 255}};
colors->SetColor("BkgColor", bkg.data());
// Create the rendering window, renderer, and interactive renderer.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
// Create the source.
vtkSmartPointer<vtkSphereSource> source =
vtkSmartPointer<vtkSphereSource>::New();
source->SetCenter(0, 0, 0);
source->SetRadius(5.0);
// mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(source->GetOutputPort());
// actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// color the actor
actor->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());
// assign actor to the renderer
ren->AddActor(actor);
ren->SetBackground(colors->GetColor3d("BkgColor").GetData());
renWin->Render();
std::vector<std::string> ext = {{""}, {".png"}, {".jpg"}, {".ps"},
{".tiff"}, {".bmp"}, {".pnm"}};
std::vector<std::string> filenames;
for (auto e : ext)
{
filenames.push_back("ImageWriter" + e);
}
filenames[0] = filenames[0] + '1';
for (auto f : filenames)
{
WriteImage(f, renWin, false);
}
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
namespace
{
void WriteImage(std::string const& fileName, vtkRenderWindow* renWin, bool rgba)
{
if (!fileName.empty())
{
std::string fn = fileName;
std::string path;
std::string ext;
std::size_t found = fn.find_last_of(".");
if (found == std::string::npos)
{
path = fn;
ext = ".png";
fn += ext;
}
else
{
path = fileName.substr(0, found);
ext = fileName.substr(found, fileName.size());
}
std::locale loc;
std::transform(ext.begin(), ext.end(), ext.begin(),
[=](char const& c) { return std::tolower(c, loc); });
vtkSmartPointer<vtkImageWriter> writer =
vtkSmartPointer<vtkImageWriter>::New();
if (ext == ".bmp")
{
writer = vtkSmartPointer<vtkBMPWriter>::New();
}
else if (ext == ".jpg")
{
writer = vtkSmartPointer<vtkJPEGWriter>::New();
}
else if (ext == ".pnm")
{
writer = vtkSmartPointer<vtkPNMWriter>::New();
}
else if (ext == ".ps")
{
if (rgba)
{
rgba = false;
}
writer = vtkSmartPointer<vtkPostScriptWriter>::New();
}
else if (ext == ".tiff")
{
writer = vtkSmartPointer<vtkTIFFWriter>::New();
}
else
{
writer = vtkSmartPointer<vtkPNGWriter>::New();
}
vtkSmartPointer<vtkWindowToImageFilter> window_to_image_filter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
window_to_image_filter->SetInput(renWin);
window_to_image_filter->SetScale(1); // image quality
if (rgba)
{
window_to_image_filter->SetInputBufferTypeToRGBA();
}
else
{
window_to_image_filter->SetInputBufferTypeToRGB();
}
// Read from the front buffer.
window_to_image_filter->ReadFrontBufferOff();
window_to_image_filter->Update();
writer->SetFileName(fn.c_str());
writer->SetInputConnection(window_to_image_filter->GetOutputPort());
writer->Write();
}
else
{
std::cerr << "No filename provided." << std::endl;
}
return;
}
}