Updates will be applied today, at 4 pm EDT (UTC-0400). No interruption, and site should remain available.

vtkImageLuminance.cxx 3.86 KB
Newer Older
Bill Lorensen's avatar
Bill Lorensen committed
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageLuminance.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Will Schroeder's avatar
Will Schroeder committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Bill Lorensen's avatar
Bill Lorensen committed
13 14 15

=========================================================================*/
#include "vtkImageLuminance.h"
16 17

#include "vtkImageData.h"
18
#include "vtkImageProgressIterator.h"
Ken Martin's avatar
Ken Martin committed
19 20
#include "vtkInformation.h"
#include "vtkInformationVector.h"
21
#include "vtkObjectFactory.h"
Ken Martin's avatar
Ken Martin committed
22
#include "vtkStreamingDemandDrivenPipeline.h"
23

Brad King's avatar
Brad King committed
24
#include <math.h>
25

Brad King's avatar
Brad King committed
26
vtkStandardNewMacro(vtkImageLuminance);
Bill Lorensen's avatar
Bill Lorensen committed
27

Ken Martin's avatar
Ken Martin committed
28 29 30 31 32 33 34
//----------------------------------------------------------------------------
vtkImageLuminance::vtkImageLuminance()
{
  this->SetNumberOfInputPorts(1);
  this->SetNumberOfOutputPorts(1);
}

Bill Lorensen's avatar
Bill Lorensen committed
35
//----------------------------------------------------------------------------
36
// This method overrides information set by parent's ExecuteInformation.
37
int vtkImageLuminance::RequestInformation (
Ken Martin's avatar
Ken Martin committed
38
  vtkInformation       * vtkNotUsed( request ),
39
  vtkInformationVector** vtkNotUsed( inputVector ),
Ken Martin's avatar
Ken Martin committed
40
  vtkInformationVector * outputVector)
Bill Lorensen's avatar
Bill Lorensen committed
41
{
42 43
  vtkDataObject::SetPointDataActiveScalarInfo(
    outputVector->GetInformationObject(0), -1, 1);
44
  return 1;
Bill Lorensen's avatar
Bill Lorensen committed
45 46 47 48 49 50 51
}

//----------------------------------------------------------------------------
// This execute method handles boundaries.
// it handles boundaries. Pixels are just replicated to get values 
// out of extent.
template <class T>
Ken Martin's avatar
Ken Martin committed
52 53 54
void vtkImageLuminanceExecute(vtkImageLuminance *self, vtkImageData *inData,
                              vtkImageData *outData,
                              int outExt[6], int id, T *)
Bill Lorensen's avatar
Bill Lorensen committed
55
{
56 57
  vtkImageIterator<T> inIt(inData, outExt);
  vtkImageProgressIterator<T> outIt(outData, outExt, self, id);
Bill Lorensen's avatar
Bill Lorensen committed
58 59 60
  float luminance;

  // Loop through ouput pixels
61
  while (!outIt.IsAtEnd())
Bill Lorensen's avatar
Bill Lorensen committed
62
    {
63 64 65 66
    T* inSI = inIt.BeginSpan();
    T* outSI = outIt.BeginSpan();
    T* outSIEnd = outIt.EndSpan();
    while (outSI != outSIEnd)
Bill Lorensen's avatar
Bill Lorensen committed
67
      {
68 69 70 71 72 73
      // now process the components
      luminance =  0.30 * *inSI++;
      luminance += 0.59 * *inSI++;
      luminance += 0.11 * *inSI++;
      *outSI = static_cast<T>(luminance);
      ++outSI;
Bill Lorensen's avatar
Bill Lorensen committed
74
      }
75 76
    inIt.NextSpan();
    outIt.NextSpan();
Bill Lorensen's avatar
Bill Lorensen committed
77 78 79 80 81 82 83 84
    }
}


//----------------------------------------------------------------------------
// This method contains a switch statement that calls the correct
// templated function for the input data type.  The output data
// must match input type.  This method does handle boundary conditions.
85
void vtkImageLuminance::ThreadedExecute (vtkImageData *inData, 
86 87
                                        vtkImageData *outData,
                                        int outExt[6], int id)
Bill Lorensen's avatar
Bill Lorensen committed
88 89 90 91 92 93 94
{
  vtkDebugMacro(<< "Execute: inData = " << inData 
  << ", outData = " << outData);
  
  // this filter expects that input is the same type as output.
  if (inData->GetNumberOfScalarComponents() != 3)
    {
95 96
    vtkErrorMacro(<< "Execute: input must have 3 components, but has " 
                  << inData->GetNumberOfScalarComponents());
Bill Lorensen's avatar
Bill Lorensen committed
97 98 99 100 101 102 103 104 105 106 107 108 109
    return;
    }
  
  // this filter expects that input is the same type as output.
  if (inData->GetScalarType() != outData->GetScalarType())
    {
    vtkErrorMacro(<< "Execute: input ScalarType, " << inData->GetScalarType()
    << ", must match out ScalarType " << outData->GetScalarType());
    return;
    }
  
  switch (inData->GetScalarType())
    {
110 111 112
    vtkTemplateMacro(
      vtkImageLuminanceExecute( this, inData, outData, 
                                outExt, id, static_cast<VTK_TT *>(0)));
Bill Lorensen's avatar
Bill Lorensen committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    default:
      vtkErrorMacro(<< "Execute: Unknown ScalarType");
      return;
    }
}