1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkImageLuminance.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkImageLuminance.h"
16 
17 #include "vtkImageData.h"
18 #include "vtkImageProgressIterator.h"
19 #include "vtkInformation.h"
20 #include "vtkInformationVector.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkStreamingDemandDrivenPipeline.h"
23 
24 #include <math.h>
25 
26 vtkStandardNewMacro(vtkImageLuminance);
27 
28 //----------------------------------------------------------------------------
vtkImageLuminance()29 vtkImageLuminance::vtkImageLuminance()
30 {
31   this->SetNumberOfInputPorts(1);
32   this->SetNumberOfOutputPorts(1);
33 }
34 
35 //----------------------------------------------------------------------------
36 // This method overrides information set by parent's ExecuteInformation.
RequestInformation(vtkInformation * vtkNotUsed (request),vtkInformationVector ** vtkNotUsed (inputVector),vtkInformationVector * outputVector)37 int vtkImageLuminance::RequestInformation (
38   vtkInformation       * vtkNotUsed( request ),
39   vtkInformationVector** vtkNotUsed( inputVector ),
40   vtkInformationVector * outputVector)
41 {
42   vtkDataObject::SetPointDataActiveScalarInfo(
43     outputVector->GetInformationObject(0), -1, 1);
44   return 1;
45 }
46 
47 //----------------------------------------------------------------------------
48 // This execute method handles boundaries.
49 // it handles boundaries. Pixels are just replicated to get values
50 // out of extent.
51 template <class T>
vtkImageLuminanceExecute(vtkImageLuminance * self,vtkImageData * inData,vtkImageData * outData,int outExt[6],int id,T *)52 void vtkImageLuminanceExecute(vtkImageLuminance *self, vtkImageData *inData,
53                               vtkImageData *outData,
54                               int outExt[6], int id, T *)
55 {
56   vtkImageIterator<T> inIt(inData, outExt);
57   vtkImageProgressIterator<T> outIt(outData, outExt, self, id);
58   float luminance;
59 
60   // Loop through output pixels
61   while (!outIt.IsAtEnd())
62     {
63     T* inSI = inIt.BeginSpan();
64     T* outSI = outIt.BeginSpan();
65     T* outSIEnd = outIt.EndSpan();
66     while (outSI != outSIEnd)
67       {
68       // now process the components
69       luminance =  0.30 * *inSI++;
70       luminance += 0.59 * *inSI++;
71       luminance += 0.11 * *inSI++;
72       *outSI = static_cast<T>(luminance);
73       ++outSI;
74       }
75     inIt.NextSpan();
76     outIt.NextSpan();
77     }
78 }
79 
80 
81 //----------------------------------------------------------------------------
82 // This method contains a switch statement that calls the correct
83 // templated function for the input data type.  The output data
84 // must match input type.  This method does handle boundary conditions.
ThreadedExecute(vtkImageData * inData,vtkImageData * outData,int outExt[6],int id)85 void vtkImageLuminance::ThreadedExecute (vtkImageData *inData,
86                                         vtkImageData *outData,
87                                         int outExt[6], int id)
88 {
89   vtkDebugMacro(<< "Execute: inData = " << inData
90   << ", outData = " << outData);
91 
92   // this filter expects that input is the same type as output.
93   if (inData->GetNumberOfScalarComponents() != 3)
94     {
95     vtkErrorMacro(<< "Execute: input must have 3 components, but has "
96                   << inData->GetNumberOfScalarComponents());
97     return;
98     }
99 
100   // this filter expects that input is the same type as output.
101   if (inData->GetScalarType() != outData->GetScalarType())
102     {
103     vtkErrorMacro(<< "Execute: input ScalarType, " << inData->GetScalarType()
104     << ", must match out ScalarType " << outData->GetScalarType());
105     return;
106     }
107 
108   switch (inData->GetScalarType())
109     {
110     vtkTemplateMacro(
111       vtkImageLuminanceExecute( this, inData, outData,
112                                 outExt, id, static_cast<VTK_TT *>(0)));
113     default:
114       vtkErrorMacro(<< "Execute: Unknown ScalarType");
115       return;
116     }
117 }
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128