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(
53   vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
54 {
55   // Set the scalar type we will produce in the output information for
56   // the first output port.
57   if (this->OutputScalarType != -1)
58   {
59     vtkInformation* outInfo = outputVector->GetInformationObject(0);
60     vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->OutputScalarType, -1);
61   }
62   return 1;
63 }
64 
65 //------------------------------------------------------------------------------
66 // This function template implements the filter for any combination of
67 // input and output data type.
68 template <class IT, class OT>
vtkImageFooExecute(vtkImageFoo * self,vtkImageData * inData,IT * inPtr,vtkImageData * outData,OT * outPtr,int outExt[6],int id)69 void vtkImageFooExecute(vtkImageFoo* self, vtkImageData* inData, IT* inPtr, vtkImageData* outData,
70   OT* outPtr, int outExt[6], int id)
71 {
72   float foo = self->GetFoo();
73 
74   int idxR, idxY, idxZ;
75   int maxY, maxZ;
76   vtkIdType inIncX, inIncY, inIncZ;
77   vtkIdType outIncX, outIncY, outIncZ;
78   int rowLength;
79 
80   unsigned long count = 0;
81   unsigned long target;
82 
83   // find the region to loop over
84 
85   rowLength = (outExt[1] - outExt[0] + 1) * inData->GetNumberOfScalarComponents();
86   maxY = outExt[3] - outExt[2];
87   maxZ = outExt[5] - outExt[4];
88   target = (unsigned long)((maxZ + 1) * (maxY + 1) / 50.0);
89   target++;
90 
91   // Get increments to march through data
92 
93   inData->GetContinuousIncrements(outExt, inIncX, inIncY, inIncZ);
94   outData->GetContinuousIncrements(outExt, outIncX, outIncY, outIncZ);
95 
96   // Loop through output pixels
97 
98   for (idxZ = 0; idxZ <= maxZ; idxZ++)
99   {
100     for (idxY = 0; !self->AbortExecute && idxY <= maxY; idxY++)
101     {
102       if (!id)
103       {
104         if (!(count % target))
105         {
106           self->UpdateProgress(count / (50.0 * target));
107         }
108         count++;
109       }
110       for (idxR = 0; idxR < rowLength; idxR++)
111       {
112         // Pixel operation. Add foo. Dumber would be impossible.
113         *outPtr = (OT)((float)(*inPtr) + foo);
114         outPtr++;
115         inPtr++;
116       }
117       outPtr += outIncY;
118       inPtr += inIncY;
119     }
120     outPtr += outIncZ;
121     inPtr += inIncZ;
122   }
123 }
124 
125 //------------------------------------------------------------------------------
126 // This function template is instantiated for each input data type and
127 // forwards the call to the above function template for each output
128 // data type.
129 template <class T>
vtkImageFooExecute1(vtkImageFoo * self,vtkImageData * inData,T * inPtr,vtkImageData * outData,int outExt[6],int id)130 void vtkImageFooExecute1(
131   vtkImageFoo* self, vtkImageData* inData, T* inPtr, vtkImageData* outData, int outExt[6], int id)
132 {
133   void* outPtr = outData->GetScalarPointerForExtent(outExt);
134   int outType = outData->GetScalarType();
135   switch (outType)
136   {
137     vtkTemplateMacro(
138       vtkImageFooExecute(self, inData, inPtr, outData, static_cast<VTK_TT*>(outPtr), outExt, id));
139     default:
140       vtkErrorWithObjectMacro(self, "Unknown output scalar type " << outType);
141       return;
142   }
143 }
144 
145 //------------------------------------------------------------------------------
146 // This method is passed an input and output data, and executes the
147 // filter algorithm to fill the output from the input.  It just
148 // executes a switch statement to call the correct function for the
149 // datas data types.
ThreadedRequestData(vtkInformation *,vtkInformationVector **,vtkInformationVector *,vtkImageData *** inData,vtkImageData ** outData,int outExt[6],int id)150 void vtkImageFoo::ThreadedRequestData(vtkInformation*, vtkInformationVector**,
151   vtkInformationVector*, vtkImageData*** inData, vtkImageData** outData, int outExt[6], int id)
152 {
153   void* inPtr = inData[0][0]->GetScalarPointerForExtent(outExt);
154   int inType = inData[0][0]->GetScalarType();
155   switch (inType)
156   {
157     vtkTemplateMacro(
158       vtkImageFooExecute1(this, inData[0][0], static_cast<VTK_TT*>(inPtr), outData[0], outExt, id));
159     default:
160       vtkErrorMacro("Unknown input scalar type " << inType);
161       return;
162   }
163 }
164