// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen // SPDX-License-Identifier: BSD-3-Clause #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { vtkSmartPointer CreateColorImage(unsigned int dim, bool transparent) { vtkSmartPointer image = vtkSmartPointer::New(); image->SetDimensions(dim, dim, 1); image->AllocateScalars(VTK_UNSIGNED_CHAR, 4); for (unsigned int x = 0; x < dim; x++) { for (unsigned int y = 0; y < dim; y++) { unsigned char* pixel = static_cast(image->GetScalarPointer(x, y, 0)); pixel[0] = 255; pixel[1] = 0; pixel[2] = 255; pixel[3] = transparent ? 127 : 255; } } return image; } vtkSmartPointer CreateImageActor(int dim, int displayLocation, bool transparent) { vtkSmartPointer colorImage = CreateColorImage(dim, transparent); vtkSmartPointer imageMapper = vtkSmartPointer::New(); imageMapper->SetInputData(colorImage); imageMapper->SetColorWindow(255); imageMapper->SetColorLevel(127.5); vtkSmartPointer imageActor = vtkSmartPointer::New(); imageActor->SetMapper(imageMapper); imageActor->SetPosition(dim, 0); imageActor->GetProperty()->SetDisplayLocation(displayLocation); return imageActor; } } // namespace int TestImageAndAnnotations(int argc, char* argv[]) { // Setup renderer vtkSmartPointer renderer = vtkSmartPointer::New(); // Setup render window vtkSmartPointer renderWindow = vtkSmartPointer::New(); renderWindow->SetSize(600, 600); renderWindow->AddRenderer(renderer); // Setup render window interactor vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New(); vtkSmartPointer style = vtkSmartPointer::New(); renderWindowInteractor->SetInteractorStyle(style); // Setup corner annotation vtkSmartPointer cornerAnnotation = vtkSmartPointer::New(); cornerAnnotation->SetLinearFontScaleFactor(2); cornerAnnotation->SetNonlinearFontScaleFactor(1); cornerAnnotation->SetMaximumFontSize(20); cornerAnnotation->SetText(0, "background/opaque"); // lower left cornerAnnotation->SetText(1, "foreground/opaque"); // lower right cornerAnnotation->SetText(2, "background/transparent"); // upper left cornerAnnotation->SetText(3, "foreground/transparent"); // upper right cornerAnnotation->GetTextProperty()->SetColor(1, 1, 1); double rgba[4] = {}; cornerAnnotation->GetTextProperty()->SetBackgroundRGBA(.25, .50, .75, 1.0); cornerAnnotation->GetTextProperty()->GetBackgroundRGBA(rgba); assert("SetBackgroundRGBA correctly get/set values" && (rgba[0] == .25 && rgba[1] == .50 && rgba[2] == .75 && rgba[3] == 1.0)); { double rgb[3] = {}; cornerAnnotation->GetTextProperty()->GetBackgroundColor(rgb); assert("SetBackgroundRGBA correctly sets BackgroundColor" && (rgba[0] == .25 && rgba[1] == .50 && rgba[2] == .75)); assert("SetBackgroundRGBA correctly sets opacity" && (cornerAnnotation->GetTextProperty()->GetBackgroundOpacity() == 1)); } cornerAnnotation->GetTextProperty()->SetBackgroundRGBA(0, 0, 0, 0); cornerAnnotation->GetTextProperty()->GetBackgroundRGBA(rgba); assert("SetBackgroundRGBA correctly get/sets values" && (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 0)); renderer->AddViewProp(cornerAnnotation); // Setup images constexpr unsigned int Dim = 300; { // lower left: background/opaque bool transparent = false; vtkSmartPointer imageActor = CreateImageActor(Dim, VTK_BACKGROUND_LOCATION, transparent); imageActor->SetPosition(0, 0); renderer->AddActor(imageActor); } { // lower right: foreground/opaque bool transparent = false; vtkSmartPointer imageActor = CreateImageActor(Dim, VTK_FOREGROUND_LOCATION, transparent); imageActor->SetPosition(Dim, 0); renderer->AddActor(imageActor); } { // upper left: background/transparent bool transparent = true; vtkSmartPointer imageActor = CreateImageActor(Dim, VTK_BACKGROUND_LOCATION, transparent); imageActor->SetPosition(0, Dim); renderer->AddActor(imageActor); } { // upper right: foreground/transparent bool transparent = true; vtkSmartPointer imageActor = CreateImageActor(Dim, VTK_FOREGROUND_LOCATION, transparent); imageActor->SetPosition(Dim, Dim); renderer->AddActor(imageActor); } renderer->ResetCamera(); // Render and start interaction renderWindowInteractor->SetRenderWindow(renderWindow); renderWindowInteractor->Initialize(); int retVal = vtkRegressionTestImage(renderWindow); if (retVal == vtkRegressionTester::DO_INTERACTOR) { renderWindowInteractor->Start(); } return !retVal; }