1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkImageFoo.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 "vtkImageFoo.h"
16 
17 #include "vtkBar.h"
18 #include "vtkImageData.h"
19 #include "vtkInformationVector.h"
20 #include "vtkObjectFactory.h"
21 
22 //----------------------------------------------------------------------------
23 vtkStandardNewMacro(vtkImageFoo);
24 
25 //----------------------------------------------------------------------------
vtkImageFoo()26 vtkImageFoo::vtkImageFoo()
27 {
28   this->Foo = 0.0;
29   this->OutputScalarType = -1;
30   this->Bar = vtkBar::New();
31 }
32 
33 //----------------------------------------------------------------------------
~vtkImageFoo()34 vtkImageFoo::~vtkImageFoo()
35 {
36   if (this->Bar)
37   {
38     this->Bar->Delete();
39     this->Bar = nullptr;
40   }
41 }
42 
43 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)44 void vtkImageFoo::PrintSelf(ostream& os, vtkIndent indent)
45 {
46   this->Superclass::PrintSelf(os,indent);
47   os << indent << "Foo: " << this->Foo << "\n";
48   os << indent << "Output Scalar Type: " << this->OutputScalarType << "\n";
49 }
50 
51 //----------------------------------------------------------------------------
RequestInformation(vtkInformation *,vtkInformationVector **,vtkInformationVector * outputVector)52 int vtkImageFoo::RequestInformation(vtkInformation*,
53                                     vtkInformationVector**,
54                                     vtkInformationVector* outputVector)
55 {
56   // Set the scalar type we will produce in the output information for
57   // the first output port.
58   if(this->OutputScalarType != -1)
59   {
60     vtkInformation* outInfo = outputVector->GetInformationObject(0);
61     vtkDataObject::SetPointDataActiveScalarInfo(
62       outInfo, this->OutputScalarType, -1);
63   }
64   return 1;
65 }
66 
67 //----------------------------------------------------------------------------
68 // This function template implements the filter for any combination of
69 // input and output data type.
70 template <class IT, class OT>
vtkImageFooExecute(vtkImageFoo * self,vtkImageData * inData,IT * inPtr,vtkImageData * outData,OT * outPtr,int outExt[6],int id)71 void vtkImageFooExecute(vtkImageFoo* self,
72                         vtkImageData* inData, IT* inPtr,
73                         vtkImageData* outData, OT* outPtr,
74                         int outExt[6], int id)
75 {
76   float foo = self->GetFoo();
77 
78   int idxR, idxY, idxZ;
79   int maxY, maxZ;
80   vtkIdType inIncX, inIncY, inIncZ;
81   vtkIdType outIncX, outIncY, outIncZ;
82   int rowLength;
83 
84   unsigned long count = 0;
85   unsigned long target;
86 
87   // find the region to loop over
88 
89   rowLength = (outExt[1] - outExt[0]+1)*inData->GetNumberOfScalarComponents();
90   maxY = outExt[3] - outExt[2];
91   maxZ = outExt[5] - outExt[4];
92   target = (unsigned long)((maxZ+1)*(maxY+1)/50.0);
93   target++;
94 
95   // Get increments to march through data
96 
97   inData->GetContinuousIncrements(outExt, inIncX, inIncY, inIncZ);
98   outData->GetContinuousIncrements(outExt, outIncX, outIncY, outIncZ);
99 
100   // Loop through output pixels
101 
102   for (idxZ = 0; idxZ <= maxZ; idxZ++)
103   {
104     for (idxY = 0; !self->AbortExecute && idxY <= maxY; idxY++)
105     {
106       if (!id)
107       {
108         if (!(count%target))
109         {
110           self->UpdateProgress(count/(50.0*target));
111         }
112         count++;
113       }
114       for (idxR = 0; idxR < rowLength; idxR++)
115       {
116         // Pixel operation. Add foo. Dumber would be impossible.
117         *outPtr = (OT)((float)(*inPtr) + foo);
118         outPtr++;
119         inPtr++;
120       }
121       outPtr += outIncY;
122       inPtr += inIncY;
123     }
124     outPtr += outIncZ;
125     inPtr += inIncZ;
126   }
127 }
128 
129 //----------------------------------------------------------------------------
130 // This function template is instantiated for each input data type and
131 // forwards the call to the above function template for each output
132 // data type.
133 template <class T>
vtkImageFooExecute1(vtkImageFoo * self,vtkImageData * inData,T * inPtr,vtkImageData * outData,int outExt[6],int id)134 void vtkImageFooExecute1(vtkImageFoo* self,
135                          vtkImageData* inData, T* inPtr,
136                          vtkImageData* outData,
137                          int outExt[6], int id)
138 {
139   void *outPtr = outData->GetScalarPointerForExtent(outExt);
140   int outType = outData->GetScalarType();
141   switch (outType)
142   {
143     vtkTemplateMacro(
144       vtkImageFooExecute(self,
145                          inData, inPtr,
146                          outData, static_cast<VTK_TT*>(outPtr),
147                          outExt, id)
148       );
149     default:
150       vtkErrorWithObjectMacro(self, "Unknown output scalar type " << outType);
151       return;
152   }
153 }
154 
155 //----------------------------------------------------------------------------
156 // This method is passed an input and output data, and executes the
157 // filter algorithm to fill the output from the input.  It just
158 // executes a switch statement to call the correct function for the
159 // datas data types.
ThreadedRequestData(vtkInformation *,vtkInformationVector **,vtkInformationVector *,vtkImageData *** inData,vtkImageData ** outData,int outExt[6],int id)160 void vtkImageFoo::ThreadedRequestData(vtkInformation*,
161                                       vtkInformationVector**,
162                                       vtkInformationVector*,
163                                       vtkImageData*** inData,
164                                       vtkImageData** outData,
165                                       int outExt[6], int id)
166 {
167   void* inPtr = inData[0][0]->GetScalarPointerForExtent(outExt);
168   int inType = inData[0][0]->GetScalarType();
169   switch (inType)
170   {
171     vtkTemplateMacro(
172       vtkImageFooExecute1(this, inData[0][0], static_cast<VTK_TT*>(inPtr),
173                           outData[0], outExt, id)
174       );
175     default:
176       vtkErrorMacro("Unknown input scalar type " << inType);
177       return;
178   }
179 }
180