Unable to get vtkImageViewer2 to display image

Hello,

I am having trouble getting some imaging data to appear correctly using the VTK C++ API. When I run the code below and read in my image, all I get is a blank white box where the image should be. The image is a “.png”. The values at each pixel are unsigned short. Their range is [8738, 61937]. When I load the image in Paraview it shows up correctly with the “Cool to Warm” colormap. I want to display the same thing, but all I see is a blank white box where the image should be (the box is the correct size for the image, it is obviously reading the image, but for some reason not displaying correctly).

If you know what the problem is and could help I would greatly appreciate it. I am sure it is some dumb mistake.

Here is my minimal reproducable code:
int main(int argc, char **argv) {
vtkNew colors;
//For setting the background color.
std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
colors->SetColor(“BkgColor”, bkg.data());
std::string inputFilePath("/my/in/path/here1.png");
// Read the image file
vtkNew reader;
reader->SetFileName(inputFilePath.data());
reader->Update();
//Initialize the image actor
vtkNew imageViewer;
imageViewer->SetInputConnection(reader->GetOutputPort());
std::cout << std::endl << std::endl;
// Visualize
vtkNew iren;
imageViewer->SetupInteractor(iren);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->GetRenderer()->SetBackground(colors->GetColor3d(“DarkSlateGray”).GetData());
imageViewer->GetRenderWindow()->SetWindowName(“PNGReader”);
imageViewer->Render();
iren->Start();
return EXIT_SUCCESS;
}

PS: I am not sure if it is my browser or what, but the preformatted text in the editor is not letting me copy code.

I think the issue is that I need to convert the scalar values stored in:

reader->GetOutputDataObject(0)->GetAttributes(0)->GetScalars("PNGImage")

into color. I tried creating a calculator to scale the values to (0,1) (I need to do this in order for some other methods to work) and then create lookupTable and scalarValuesToColors objects but I am having some issues. I get a segfault when I try to access my calculator data. Here is the code:
vtkNew calc1;
calc1->SetInputConnection(reader->GetOutputPort(0));
calc1->AddScalarArrayName(“PNGImage”);
std::string fn = “(PNGImage - " + std::to_string(range[0]) + “)/(” + std::to_string(range[1]) + “-” + std::to_string(range[0])+”)";
calc1->SetFunction(fn.c_str());
calc1->SetResultArrayName(“PNGImage”);
calc1->Update();
dbg.printMsg(“Segfault occurs on next line…”);
auto output1 = calc1->GetPolyDataOutput()->GetPointData()->GetArray(0);