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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageHistogram.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 "vtkImageHistogram.h"
#include "vtkObjectFactory.h"
#include "vtkMath.h"
#include "vtkIdTypeArray.h"
#include "vtkImageData.h"
#include "vtkImageStencilData.h"
#include "vtkImageStencilIterator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkMultiThreader.h"
#include "vtkTemplateAliasMacro.h"
#include <math.h>
// turn off 64-bit ints when templating over all types
# undef VTK_USE_INT64
# define VTK_USE_INT64 0
# undef VTK_USE_UINT64
# define VTK_USE_UINT64 0
vtkStandardNewMacro(vtkImageHistogram);
//----------------------------------------------------------------------------
// Constructor sets default values
vtkImageHistogram::vtkImageHistogram()
{
this->ActiveComponent = -1;
this->AutomaticBinning = false;
this->MaximumNumberOfBins = 65536;
this->NumberOfBins = 256;
this->BinOrigin = 0.0;
this->BinSpacing = 1.0;
this->GenerateHistogramImage = true;
this->HistogramImageSize[0] = 256;
this->HistogramImageSize[1] = 256;
this->HistogramImageScale = vtkImageHistogram::Linear;
this->Histogram = vtkIdTypeArray::New();
this->Total = 0;
this->SetNumberOfInputPorts(2);
this->SetNumberOfOutputPorts(1);
}
//----------------------------------------------------------------------------
vtkImageHistogram::~vtkImageHistogram()
{
if (this->Histogram)
{
this->Histogram->Delete();
}
}
//----------------------------------------------------------------------------
void vtkImageHistogram::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Stencil: " << this->GetStencil() << "\n";
os << indent << "ActiveComponent: " << this->ActiveComponent << "\n";
os << indent << "AutomaticBinning: "
<< (this->AutomaticBinning ? "On\n" : "Off\n") << "\n";
os << indent << "MaximumNumberOfBins: " << this->MaximumNumberOfBins << "\n";
os << indent << "NumberOfBins: " << this->NumberOfBins << "\n";
os << indent << "BinOrigin: " << this->BinOrigin << "\n";
os << indent << "BinSpacing: " << this->BinSpacing << "\n";
os << indent << "GenerateHistogramImage: "
<< (this->GenerateHistogramImage ? "On\n" : "Off\n") << "\n";
os << indent << "HistogramImageSize: " << this->HistogramImageSize[0] << " "
<< this->HistogramImageSize[1] << "\n";
os << indent << "HistogramImageScale: "
<< this->GetHistogramImageScaleAsString() << "\n";
os << indent << "Total: " << this->Total << "\n";
os << indent << "Histogram: " << this->Histogram << "\n";
}
//----------------------------------------------------------------------------
const char *vtkImageHistogram::GetHistogramImageScaleAsString()
{
const char *s = "Unknown";
switch (this->HistogramImageScale)
{
case vtkImageHistogram::Log:
s = "Log";
break;
case vtkImageHistogram::Sqrt:
s = "Sqrt";
break;
case vtkImageHistogram::Linear:
s = "Linear";
break;
}
return s;
}
//----------------------------------------------------------------------------
vtkIdTypeArray *vtkImageHistogram::GetHistogram()
{
return this->Histogram;
}
//----------------------------------------------------------------------------
void vtkImageHistogram::SetStencilData(vtkImageStencilData *stencil)
{
this->SetInputData(1, stencil);
}
//----------------------------------------------------------------------------
void vtkImageHistogram::SetStencilConnection(vtkAlgorithmOutput* algOutput)
{
this->SetInputConnection(1, algOutput);
}
//----------------------------------------------------------------------------
vtkImageStencilData *vtkImageHistogram::GetStencil()
{
if (this->GetNumberOfInputConnections(1) < 1)
{
return NULL;
}
return vtkImageStencilData::SafeDownCast(
this->GetExecutive()->GetInputData(1, 0));
}
//----------------------------------------------------------------------------
int vtkImageHistogram::FillInputPortInformation(int port, vtkInformation *info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageStencilData");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageHistogram::FillOutputPortInformation(int port, vtkInformation* info)
{
if (port == 0)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkImageData");
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageHistogram::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
int outWholeExt[6];
double outOrigin[3];
double outSpacing[3];
outWholeExt[0] = 0;
outWholeExt[1] = this->HistogramImageSize[0] - 1;
outWholeExt[2] = 0;
outWholeExt[3] = this->HistogramImageSize[1] - 1;
outWholeExt[4] = 0;
outWholeExt[5] = 0;
outOrigin[0] = 0.0;
outOrigin[1] = 0.0;
outOrigin[2] = 0.0;
outSpacing[0] = 1.0;
outSpacing[1] = 1.0;
outSpacing[2] = 1.0;
if (!this->GenerateHistogramImage)
{
outWholeExt[1] = -1;
outWholeExt[3] = -1;
outWholeExt[5] = -1;
}
if (this->GetNumberOfOutputPorts() > 0)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
outWholeExt, 6);
outInfo->Set(vtkDataObject::ORIGIN(), outOrigin, 3);
outInfo->Set(vtkDataObject::SPACING(), outSpacing, 3);
vtkDataObject::SetPointDataActiveScalarInfo(
outInfo, VTK_UNSIGNED_CHAR, 1);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageHistogram::RequestUpdateExtent(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *vtkNotUsed(outputVector))
{
int inExt[6];
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), inExt);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inExt, 6);
// need to set the stencil update extent to the input extent
if (this->GetNumberOfInputConnections(1) > 0)
{
vtkInformation *stencilInfo = inputVector[1]->GetInformationObject(0);
stencilInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
inExt, 6);
}
return 1;
}
//----------------------------------------------------------------------------
// anonymous namespace for internal classes and functions
namespace {
struct vtkImageHistogramThreadStruct
{
vtkImageHistogram *Algorithm;
vtkInformation *Request;
vtkInformationVector **InputsInfo;
vtkInformationVector *OutputsInfo;
};
//----------------------------------------------------------------------------
// override from vtkThreadedImageAlgorithm to split input extent, instead
// of splitting the output extent
VTK_THREAD_RETURN_TYPE vtkImageHistogramThreadedExecute(void *arg)
{
vtkMultiThreader::ThreadInfo *ti =
static_cast<vtkMultiThreader::ThreadInfo *>(arg);
vtkImageHistogramThreadStruct *ts =
static_cast<vtkImageHistogramThreadStruct *>(ti->UserData);
int extent[6] = { 0, -1, 0, -1, 0, -1 };
bool foundConnection = false;
int numPorts = ts->Algorithm->GetNumberOfInputPorts();
for (int inPort = 0; inPort < numPorts; ++inPort)
{
int numConnections = ts->Algorithm->GetNumberOfInputConnections(inPort);
if (numConnections)
{
vtkInformation *inInfo =
ts->InputsInfo[inPort]->GetInformationObject(0);
vtkImageData *inData = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
if (inData)
{
inData->GetExtent(extent);
foundConnection = true;
break;
}
}
}
if (foundConnection)
{
// execute the actual method with appropriate extent
// first find out how many pieces extent can be split into.
int splitExt[6];
int total = ts->Algorithm->SplitExtent(
splitExt, extent, ti->ThreadID, ti->NumberOfThreads);
if (ti->ThreadID < total &&
splitExt[1] >= splitExt[0] &&
splitExt[3] >= splitExt[2] &&
splitExt[5] >= splitExt[4])
{
ts->Algorithm->ThreadedRequestData(
ts->Request, ts->InputsInfo, ts->OutputsInfo, NULL, NULL,
splitExt, ti->ThreadID);
}
}
return VTK_THREAD_RETURN_VALUE;
}
//----------------------------------------------------------------------------
template<class T>
void vtkImageHistogramExecuteRange(
vtkImageData *inData, vtkImageStencilData *stencil,
T *inPtr, int extent[6], double range[2], int component)
{
vtkImageStencilIterator<T> inIter(inData, stencil, extent, NULL);
T xmin = vtkTypeTraits<T>::Max();
T xmax = vtkTypeTraits<T>::Min();
// set up components
int nc = inData->GetNumberOfScalarComponents();
int c = component;
if (c < 0)
{
nc = 1;
c = 0;
}
// iterate over all spans in the stencil
while (!inIter.IsAtEnd())
{
if (inIter.IsInStencil())
{
inPtr = inIter.BeginSpan();
T *inPtrEnd = inIter.EndSpan();
if (inPtr != inPtrEnd)
{
int n = static_cast<int>((inPtrEnd - inPtr)/nc);
inPtr += c;
do
{
T x = *inPtr;
xmin = (xmin < x ? xmin : x);
xmax = (xmax > x ? xmax : x);
inPtr += nc;
}
while (--n);
}
}
inIter.NextSpan();
}
range[0] = xmin;
range[1] = xmax;
}
//----------------------------------------------------------------------------
template<class T>
void vtkImageHistogramExecute(
vtkImageHistogram *self,
vtkImageData *inData, vtkImageStencilData *stencil,
T *inPtr, int extent[6], vtkIdType *outPtr, int binRange[2],
double o, double s, int component, int threadId)
{
vtkImageStencilIterator<T>
inIter(inData, stencil, extent, ((threadId == 0) ? self : NULL));
// set up components
int nc = inData->GetNumberOfScalarComponents();
int c = component;
if (c < 0)
{
nc = 1;
c = 0;
}
// compute shift/scale values for fast bin computation
double xmin = binRange[0];
double xmax = binRange[1];
double xshift = -o;
double xscale = 1.0/s;
// iterate over all spans in the stencil
while (!inIter.IsAtEnd())
{
if (inIter.IsInStencil())
{
inPtr = inIter.BeginSpan();
T *inPtrEnd = inIter.EndSpan();
// iterate over all voxels in the span
if (inPtr != inPtrEnd)
{
int n = static_cast<int>((inPtrEnd - inPtr)/nc);
inPtr += c;
do
{
double x = *inPtr;
x += xshift;
x *= xscale;
x = (x > xmin ? x : xmin);
x = (x < xmax ? x : xmax);
int xi = static_cast<int>(x + 0.5);
outPtr[xi]++;
inPtr += nc;
}
while (--n);
}
}
inIter.NextSpan();
}
}
//----------------------------------------------------------------------------
template<class T>
void vtkImageHistogramExecuteInt(
vtkImageHistogram *self,
vtkImageData *inData, vtkImageStencilData *stencil,
T *inPtr, int extent[6], vtkIdType *outPtr, int component, int threadId)
{
vtkImageStencilIterator<T>
inIter(inData, stencil, extent, ((threadId == 0) ? self : NULL));
// set up components
int nc = inData->GetNumberOfScalarComponents();
int c = component;
if (c < 0)
{
nc = 1;
c = 0;
}
// iterate over all spans in the stencil
while (!inIter.IsAtEnd())
{
if (inIter.IsInStencil())
{
inPtr = inIter.BeginSpan();
T *inPtrEnd = inIter.EndSpan();
// iterate over all voxels in the span
if (inPtr != inPtrEnd)
{
int n = static_cast<int>((inPtrEnd - inPtr)/nc);
inPtr += c;
do
{
outPtr[*inPtr]++;
inPtr += nc;
}
while (--n);
}
}
inIter.NextSpan();
}
}
// no-op version for float
void vtkImageHistogramExecuteInt(
vtkImageHistogram *, vtkImageData *, vtkImageStencilData *,
float *, int [6], vtkIdType *, int, int)
{
}
// no-op version for double
void vtkImageHistogramExecuteInt(
vtkImageHistogram *, vtkImageData *, vtkImageStencilData *,
double *, int [6], vtkIdType *, int, int)
{
}
//----------------------------------------------------------------------------
void vtkImageHistogramGenerateImage(
vtkIdType *histogram, int nx,
unsigned char *outPtr, int scale, int size[2], int extent[6])
{
vtkIdType incX = 1;
vtkIdType incY = (extent[1] - extent[0] + 1);
// find tallest peak in histogram
vtkIdType peak = 0;
int ix;
for (ix = 0; ix < nx; ++ix)
{
vtkIdType c = histogram[ix];
peak = (peak >= c ? peak : c);
}
// compute vertical scale factor
double b = 0.0;
if (peak > 0)
{
double sum = peak;
switch (scale)
{
case vtkImageHistogram::Log:
sum = log(sum) + 1.0;
break;
case vtkImageHistogram::Sqrt:
sum = sqrt(sum);
break;
case vtkImageHistogram::Linear:
break;
}
b = (size[1] - 1)/sum;
}
// compute horizontal scale factor
double a = 0.0;
if (size[0] > 0)
{
a = nx*1.0/size[0];
}
double x = extent[0]*a;
ix = static_cast<int>(x);
for (int i = extent[0]; i <= extent[1]; i++)
{
// use max of the original bins to compute new bin height
double sum = histogram[ix];
x = (i + 1)*a;
int ix1 = static_cast<int>(x);
for (; ix < ix1; ix++)
{
double v = histogram[ix];
sum = (sum > v ? sum : v);
}
// scale the bin height
if (sum > 0)
{
switch (scale)
{
case vtkImageHistogram::Log:
sum = log(sum) + 1;
break;
case vtkImageHistogram::Sqrt:
sum = sqrt(sum);
break;
case vtkImageHistogram::Linear:
break;
}
}
int height = static_cast<int>(sum*b);
height = (height < extent[3] ? height : extent[3]);
// draw the bin
unsigned char *outPtr1 = outPtr;
int j = extent[2];
for (; j <= height; j++)
{
*outPtr1 = 255;
outPtr1 += incY;
}
for (; j <= extent[3]; j++)
{
*outPtr1 = 0;
outPtr1 += incY;
}
outPtr += incX;
}
}
} // end anonymous namespace
//----------------------------------------------------------------------------
// override from vtkThreadedImageAlgorithm to customize the multithreading
int vtkImageHistogram::RequestData(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// clear the thread output pointers
int n = this->GetNumberOfThreads();
for (int k = 0; k < n; k++)
{
this->ThreadOutput[k] = 0;
}
vtkInformation* info = inputVector[0]->GetInformationObject(0);
vtkImageData *image = vtkImageData::SafeDownCast(
info->Get(vtkDataObject::DATA_OBJECT()));
int scalarType = image->GetScalarType();
double scalarRange[2];
// handle automatic binning
if (this->AutomaticBinning)
{
switch (scalarType)
{
case VTK_CHAR:
case VTK_UNSIGNED_CHAR:
case VTK_SIGNED_CHAR:
{
vtkDataArray::GetDataTypeRange(scalarType, scalarRange);
this->NumberOfBins = 256;
this->BinSpacing = 1.0;
this->BinOrigin = scalarRange[0];
}
break;
case VTK_SHORT:
case VTK_UNSIGNED_SHORT:
case VTK_INT:
case VTK_UNSIGNED_INT:
case VTK_LONG:
case VTK_UNSIGNED_LONG:
{
this->ComputeImageScalarRange(image, scalarRange);
if (scalarRange[0] > 0) { scalarRange[0] = 0; }
if (scalarRange[1] < 0) { scalarRange[1] = 0; }
unsigned long binMaxId =
static_cast<unsigned long>(scalarRange[1] - scalarRange[0]);
this->BinOrigin = scalarRange[0];
this->BinSpacing = 1.0;
if (binMaxId < 255)
{
binMaxId = 255;
}
if (binMaxId > static_cast<unsigned long>(this->MaximumNumberOfBins-1))
{
binMaxId = static_cast<unsigned long>(this->MaximumNumberOfBins-1);
if (binMaxId > 0)
{
this->BinSpacing = (scalarRange[1] - scalarRange[0])/binMaxId;
}
}
this->NumberOfBins = static_cast<int>(binMaxId + 1);
}
break;
default:
{
this->NumberOfBins = this->MaximumNumberOfBins;
this->ComputeImageScalarRange(image, scalarRange);
if (scalarRange[0] > 0) { scalarRange[0] = 0; }
if (scalarRange[1] < 0) { scalarRange[1] = 0; }
this->BinOrigin = scalarRange[0];
this->BinSpacing = 1.0;
if (scalarRange[1] > scalarRange[0])
{
if (this->NumberOfBins > 1)
{
this->BinSpacing =
(scalarRange[1] - scalarRange[0])/(this->NumberOfBins - 1);
}
}
}
break;
}
}
// start of code copied from vtkThreadedImageAlgorithm
// setup the threads structure
vtkImageHistogramThreadStruct ts;
ts.Algorithm = this;
ts.Request = request;
ts.InputsInfo = inputVector;
ts.OutputsInfo = outputVector;
// allocate the output data
int numberOfOutputs = this->GetNumberOfOutputPorts();
if (numberOfOutputs > 0)
{
for (int i = 0; i < numberOfOutputs; ++i)
{
vtkInformation* outInfo = outputVector->GetInformationObject(i);
vtkImageData *outData = vtkImageData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if (outData)
{
int updateExtent[6];
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
updateExtent);
this->AllocateOutputData(outData, outInfo, updateExtent);
}
}
}
// copy arrays from first input to output
int numberOfInputs = this->GetNumberOfInputPorts();
if (numberOfInputs > 0)
{
vtkInformationVector* portInfo = inputVector[0];
int numberOfConnections = portInfo->GetNumberOfInformationObjects();
if (numberOfConnections && numberOfOutputs)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkImageData *inData = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkImageData *outData = vtkImageData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
this->CopyAttributeData(inData, outData, inputVector);
}
}
this->Threader->SetNumberOfThreads(this->NumberOfThreads);
this->Threader->SetSingleMethod(vtkImageHistogramThreadedExecute, &ts);
// always shut off debugging to avoid threading problems with GetMacros
bool debug = this->Debug;
this->Debug = false;
this->Threader->SingleMethodExecute();
this->Debug = debug;
// end of code copied from vtkThreadedImageAlgorithm
// create the histogram array
this->Histogram->SetNumberOfComponents(1);
this->Histogram->SetNumberOfTuples(this->NumberOfBins);
vtkIdType *histogram = this->Histogram->GetPointer(0);
// clear histogram to zero
int nx = this->NumberOfBins;
int ix;
for (ix = 0; ix < nx; ++ix)
{
histogram[ix] = 0;
}
// piece together the histogram results from each thread
vtkIdType total = 0;
for (int j = 0; j < n; j++)
{
vtkIdType *outPtr2 = this->ThreadOutput[j];
if (outPtr2)
{
int xmin = this->ThreadBinRange[j][0];
int xmax = this->ThreadBinRange[j][1];
for (ix = xmin; ix <= xmax; ++ix)
{
vtkIdType c = *outPtr2++;
histogram[ix] += c;
total += c;
}
}
}
// set the total
this->Total = total;
// delete the temporary memory
for (int j = 0; j < n; j++)
{
delete [] this->ThreadOutput[j];
}
// generate the output image
if (this->GetNumberOfOutputPorts() > 0 &&
this->GenerateHistogramImage)
{
info = outputVector->GetInformationObject(0);
image = vtkImageData::SafeDownCast(
info->Get(vtkDataObject::DATA_OBJECT()));
int *outExt = image->GetExtent();
vtkImageHistogramGenerateImage(
this->Histogram->GetPointer(0), this->NumberOfBins,
static_cast<unsigned char *>(image->GetScalarPointerForExtent(outExt)),
this->HistogramImageScale, this->HistogramImageSize, outExt);
}
return 1;
}
//----------------------------------------------------------------------------
// This method is passed a input and output region, and executes the filter
// algorithm to fill the output from the input.
// It just executes a switch statement to call the correct function for
// the regions data types.
void vtkImageHistogram::ThreadedRequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *vtkNotUsed(outputVector),
vtkImageData ***vtkNotUsed(inData),
vtkImageData **vtkNotUsed(outData),
int extent[6], int threadId)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkImageData *inData = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
void *inPtr = inData->GetScalarPointerForExtent(extent);
vtkImageStencilData *stencil = this->GetStencil();
double binOrigin = this->BinOrigin;
double binSpacing = this->BinSpacing;
int scalarType = inData->GetScalarType();
int component = this->ActiveComponent;
// can use faster binning method for int data
bool useFastExecute = (binSpacing == 1.0 &&
scalarType != VTK_FLOAT && scalarType != VTK_DOUBLE);
double scalarRange[2];
int *binRange = this->ThreadBinRange[threadId];
// compute the scalar range of the data unless it is byte data,
// this allows us to allocate less memory for the histogram
if (scalarType == VTK_CHAR || scalarType == VTK_UNSIGNED_CHAR ||
scalarType == VTK_SIGNED_CHAR)
{
vtkDataArray::GetDataTypeRange(scalarType, scalarRange);
}
else
{
switch (scalarType)
{
vtkTemplateAliasMacro(
vtkImageHistogramExecuteRange(
inData, stencil, static_cast<VTK_TT *>(inPtr),
extent, scalarRange, component));
default:
vtkErrorMacro(<< "Execute: Unknown ScalarType");
}
// if no voxels (e.g. due to stencil) then return
if (scalarRange[0] > scalarRange[1])
{
return;
}
}
// convert to bin numbers
int maxBin = this->NumberOfBins - 1;
double scale = 1.0/binSpacing;
double minBinRange = (scalarRange[0] - binOrigin)*scale;
double maxBinRange = (scalarRange[1] - binOrigin)*scale;
if (minBinRange < 0)
{
minBinRange = 0;
useFastExecute = false;
}
if (maxBinRange > maxBin)
{
maxBinRange = maxBin;
useFastExecute = false;
}
binRange[0] = vtkMath::Floor(minBinRange + 0.5);
binRange[1] = vtkMath::Floor(maxBinRange + 0.5);
// allocate the histogram
int n = binRange[1] - binRange[0] + 1;
vtkIdType *histogram = new vtkIdType[n];
this->ThreadOutput[threadId] = histogram;
vtkIdType *tmpPtr = histogram;
do { *tmpPtr++ = 0; } while (--n);
// generate the histogram
if (useFastExecute)
{
// adjust the pointer to allow direct indexing
histogram -= binRange[0] + vtkMath::Floor(binOrigin + 0.5);
// fast path for integer data
switch(scalarType)
{
vtkTemplateAliasMacro(
vtkImageHistogramExecuteInt(
this, inData, stencil, static_cast<VTK_TT *>(inPtr),
extent, histogram, component, threadId));
default:
vtkErrorMacro(<< "Execute: Unknown ScalarType");
}
}
else
{
// adjust the pointer to allow direct indexing
histogram -= binRange[0];
// bin via floating point shift/scale
switch (scalarType)
{
vtkTemplateAliasMacro(
vtkImageHistogramExecute(
this, inData, stencil, static_cast<VTK_TT *>(inPtr),
extent, histogram, binRange, binOrigin, binSpacing,
component, threadId));
default:
vtkErrorMacro(<< "Execute: Unknown ScalarType");
}
}
}
//----------------------------------------------------------------------------
void vtkImageHistogram::ComputeImageScalarRange(
vtkImageData *data, double range[2])
{
if (data->GetNumberOfScalarComponents() == 1)
{
data->GetScalarRange(range);
return;
}
int *extent = data->GetExtent();
void *inPtr = data->GetScalarPointerForExtent(extent);
int component = this->ActiveComponent;
switch (data->GetScalarType())
{
vtkTemplateAliasMacro(
vtkImageHistogramExecuteRange(
data, 0, static_cast<VTK_TT *>(inPtr),
extent, range, component));
default:
vtkErrorMacro(<< "Execute: Unknown ScalarType");
}
}
|