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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestColorSeries.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTestErrorObserver.h"
#include "vtkLookupTable.h"
#include "vtkColorSeries.h"
#include "vtkColor.h"
#include "vtkDoubleArray.h"
#include "vtkImageData.h"
#include "vtkPNGWriter.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkTesting.h"
#include "vtkTestUtilities.h"
#include "vtkTrivialProducer.h"
#include "vtkUnsignedCharArray.h"
#include <cstdio> // For EXIT_SUCCESS
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
int TestColorSeries( int argc, char* argv[] )
{
int valResult = vtkTesting::PASSED;
vtkTesting* t = vtkTesting::New();
for ( int cc = 1; cc < argc; ++ cc )
{
t->AddArgument(argv[cc]);
}
VTK_CREATE(vtkColorSeries,palettes);
vtkColor3ub color;
vtkColor3ub black( 0, 0, 0 );
// Create a new, custom palette:
int pid = palettes->SetColorSchemeByName( "Foo" );
// Should return black as there are no colors:
color = palettes->GetColor( 0 );
if ( ! black.Compare( color, 1 ) )
{
vtkGenericWarningMacro( "Failure: GetColor on empty palette" );
valResult = vtkTesting::FAILED;
}
// Should return black as there are no colors:
color = palettes->GetColorRepeating( 0 );
if ( ! black.Compare( color, 1 ) )
{
vtkGenericWarningMacro( "Failure: GetColorRepeating on empty palette" );
valResult = vtkTesting::FAILED;
}
// Test appending colors:
color = vtkColor3ub( 255, 255, 255 ); palettes->AddColor( color );
color = vtkColor3ub( 0, 255, 0 ); palettes->AddColor( color );
color = vtkColor3ub( 0, 0, 255 ); palettes->AddColor( color );
// Test insertion (as opposed to append):
color = vtkColor3ub( 255, 0, 0 ); palettes->InsertColor( 1, color );
// Test removing a color
palettes->RemoveColor( 0 );
// Iterate over all the palettes, testing GetColorRepeating
// (w/ a non-empty palette) and the palette iteration API
int np = palettes->GetNumberOfColorSchemes();
VTK_CREATE(vtkImageData,img);
VTK_CREATE(vtkTrivialProducer,exec);
VTK_CREATE(vtkUnsignedCharArray,pix);
exec->SetOutput( img );
pix->SetNumberOfComponents(3);
// First, find the largest number of colors in any palette:
int mps = 0; // maximum palette size
for ( int p = 0; p < np; ++ p )
{
palettes->SetColorScheme( p );
int nc = palettes->GetNumberOfColors(); // in the current scheme
if ( nc > mps )
mps = nc;
}
// Now size the test image properly and generate swatches
pix->SetNumberOfTuples( np * 5 * mps * 5 );
pix->FillComponent( 0, 255 );
pix->FillComponent( 1, 255 );
pix->FillComponent( 2, 255 );
img->SetExtent( 0, mps * 5 - 1, 0, np * 5 - 1, 0, 0 );
img->GetPointData()->SetScalars( pix.GetPointer() );
for ( int p = 0; p < np; ++ p )
{
palettes->SetColorScheme( p );
int nc = palettes->GetNumberOfColors(); // in the current scheme
/*
cout
<< " " << palettes->GetColorSchemeName()
<< ", " << palettes->GetNumberOfColors() << " colors\n";
*/
int yoff = ( np - p - 1 ) * 5; // Put palette 0 at the top of the image
for ( int c = 0; c < nc; ++ c )
{
color = palettes->GetColorRepeating( c );
for ( int i = 1; i < 4; ++ i )
{
for ( int j = 1; j < 4; ++ j )
{
vtkIdType coord = ( ( ( yoff + i ) * mps + c ) * 5 + j ) * 3;
/*
cout
<< "i " << i << " j " << j << " c " << c << " p " << p
<< " off " << coord << " poff " << coord/3 << "\n";
*/
pix->SetValue( coord ++, color.GetRed() );
pix->SetValue( coord ++, color.GetGreen() );
pix->SetValue( coord ++, color.GetBlue() );
}
}
}
}
/* Uncomment to save an updated test image.
VTK_CREATE(vtkPNGWriter,wri);
wri->SetFileName( "/tmp/TestColorSeries.png" );
wri->SetInputConnection( exec->GetOutputPort() );
wri->Write();
*/
int imgResult = t->RegressionTest( exec.GetPointer(), 0.);
palettes->SetColorScheme( vtkColorSeries::BREWER_SEQUENTIAL_BLUE_GREEN_9 );
// Adding a color now should create a copy of the palette. Verify the name changed.
color = vtkColor3ub( 255, 255, 255 ); palettes->AddColor( color );
vtkStdString palName = palettes->GetColorSchemeName();
vtkStdString expected( "Brewer Sequential Blue-Green (9) copy" );
if ( palName != expected )
{
vtkGenericWarningMacro(
<< "Failure: Palette copy-on-write: name should have been "
<< "\"" << expected.c_str() << "\" but was "
<< "\"" << palName.c_str() << "\" instead." );
valResult = vtkTesting::FAILED;
}
if ( palettes->GetNumberOfColors() != 10 )
{
vtkGenericWarningMacro(
<< "Modified palette should have had 10 entries but had "
<< palettes->GetNumberOfColors() << " instead."
);
valResult = vtkTesting::FAILED;
}
palettes->SetColorSchemeName(""); // Test bad name... should have no effect.
palName = palettes->GetColorSchemeName();
if ( palName != expected )
{
vtkGenericWarningMacro(
<< "Failure: Setting empty palette name should have no effect."
);
valResult = vtkTesting::FAILED;
}
// Check setting a custom palette name and non-copy-on-write
// behavior for custom palettes:
palettes->SetColorSchemeName("Unoriginal Blue-Green");
palettes->SetColorSchemeByName("Unoriginal Blue-Green");
if ( np != palettes->GetColorScheme() )
{
vtkGenericWarningMacro(
<< "Modified palette had ID " << palettes->GetColorScheme()
<< " not expected ID " << np
);
valResult = vtkTesting::FAILED;
}
palettes->SetNumberOfColors( 8 );
if ( palettes->GetNumberOfColors() != 8 )
{
vtkGenericWarningMacro(
<< "Resized palette should have had 8 entries but had "
<< palettes->GetNumberOfColors() << " instead."
);
valResult = vtkTesting::FAILED;
}
palettes->ClearColors();
if ( palettes->GetNumberOfColors() != 0 )
{
vtkGenericWarningMacro(
<< "Cleared palette should have had 0 entries but had "
<< palettes->GetNumberOfColors() << " instead."
);
valResult = vtkTesting::FAILED;
}
// Make sure our custom scheme is still around
palettes->SetColorScheme( pid );
// Now test GetColor on a non-empty palette
color = palettes->GetColor( 2 ); // Should return blue
vtkColor3ub blue( 0, 0, 255 );
if ( ! blue.Compare( color, 1 ) )
{
vtkGenericWarningMacro( "Failure: GetColor on small test palette" );
valResult = vtkTesting::FAILED;
}
// Test DeepCopy
VTK_CREATE(vtkColorSeries,other);
other->DeepCopy(palettes);
if ( other->GetColorScheme() != palettes->GetColorScheme() )
{
vtkGenericWarningMacro( "Failure: DeepCopy did not preserve current scheme" );
valResult = vtkTesting::FAILED;
}
other->DeepCopy(NULL);
// Test SetColor
other->SetColorScheme( pid );
other->SetColor( 0, blue );
color = other->GetColor( 0 );
if ( ! blue.Compare( color, 1 ) )
{
vtkGenericWarningMacro( "Failure: SetColor on test palette" );
valResult = vtkTesting::FAILED;
}
// Build a lookup table
vtkLookupTable* lut = palettes->CreateLookupTable();
lut->Print(std::cout);
lut->Delete();
// Test scheme out of range warning
VTK_CREATE(vtkTest::ErrorObserver, warningObserver);
palettes->AddObserver(vtkCommand::WarningEvent, warningObserver);
palettes->SetColorScheme(1000);
if (warningObserver->GetWarning())
{
std::cout << "Caught expected warning: "
<< warningObserver->GetWarningMessage() << std::endl;
}
else
{
vtkGenericWarningMacro( "Failure: SetColorScheme(1000) did not produce expected warning" );
valResult = vtkTesting::FAILED;
}
vtkIndent indent;
palettes->PrintSelf(std::cout, indent);
t->Delete();
return (
imgResult == vtkTesting::PASSED &&
valResult == vtkTesting::PASSED ) ?
EXIT_SUCCESS : EXIT_FAILURE;
}
|