Image was upside down; flip it over, demo usage.

This commit is contained in:
Nick 2020-04-17 07:56:03 -04:00
parent 4e76c00a42
commit 1d4ebaec9f
3 changed files with 8 additions and 3 deletions

@ -202,7 +202,7 @@ scene.AddActor(vtkm::rendering::Actor(outputData.GetCellSet(),
vtkm::rendering::View3D view(scene, mapper, canvas, camera, bg);
view.Initialize();
view.Paint();
view.SaveAs("demo_output.pnm");
view.SaveAs("demo_output.png");
```
A minimal CMakeLists.txt such as the following one can be used to build this

@ -124,8 +124,12 @@ void TubeThatSpiral(vtkm::FloatDefault radius, vtkm::Id numLineSegments, vtkm::I
vtkm::rendering::View3D view(scene, mapper, canvas, camera, bg);
view.Initialize();
view.Paint();
// We can save the file as a .NetBPM:
std::string output_filename = "tube_output_" + std::to_string(numSides) + "_sides.pnm";
view.SaveAs(output_filename);
// Or as a .png:
output_filename = "tube_output_" + std::to_string(numSides) + "_sides.png";
view.SaveAs(output_filename);
}

@ -583,13 +583,14 @@ void Canvas::SaveAs(const std::string& fileName) const
if (ends_with(fileName, ".png"))
{
std::vector<unsigned char> img(static_cast<size_t>(width * height));
std::vector<unsigned char> img(static_cast<size_t>(4 * width * height));
for (vtkm::Id yIndex = height - 1; yIndex >= 0; yIndex--)
{
for (vtkm::Id xIndex = 0; xIndex < width; xIndex++)
{
vtkm::Vec4f_32 tuple = colorPortal.Get(yIndex * width + xIndex);
size_t idx = static_cast<size_t>(4 * width * yIndex + 4 * xIndex);
// y = 0 is the top of a .png file.
size_t idx = static_cast<size_t>(4 * width * (height - 1 - yIndex) + 4 * xIndex);
img[idx + 0] = (unsigned char)(tuple[0] * 255);
img[idx + 1] = (unsigned char)(tuple[1] * 255);
img[idx + 2] = (unsigned char)(tuple[2] * 255);