GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vtkImageBlendWithMask.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: vtkINRIA3D
4 Module: $Id: vtkImageBlendWithMask.cxx 477 2007-11-20 17:46:10Z filus $
5 Language: C++
6 Author: $Author: arnaudgelas $
7 Date: $Date: 2009-07-31 14:33:39 -0400 (Fri, 31 Jul 2009) $
8 Version: $Revision: 490 $
9 
10 Copyright (c) 2007 INRIA - Asclepios Project. All rights reserved.
11 
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 /*========================================================================
19  Copyright (c) INRIA - ASCLEPIOS Project (http://www-sop.inria.fr/asclepios).
20  All rights reserved.
21 
22  Redistribution and use in source and binary forms, with or without
23  modification, are permitted provided that the following conditions are met:
24 
25  * Redistributions of source code must retain the above copyright notice,
26  this list of conditions and the following disclaimer.
27 
28  * Redistributions in binary form must reproduce the above copyright notice,
29  this list of conditions and the following disclaimer in the documentation
30  and/or other materials provided with the distribution.
31 
32  * Neither the name of INRIA or ASCLEPIOS, nor the names of any contributors
33  may be used to endorse or promote products derived from this software
34  without specific prior written permission.
35 
36  * Modified source versions must be plainly marked as such, and must not be
37  misrepresented as being the original software.
38 
39  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
40  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
43  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49  =========================================================================*/
50 
51 /*=========================================================================
52  Modifications were made by the GoFigure Dev. Team.
53  while at Megason Lab, Systems biology, Harvard Medical school, 2009-11
54 
55  Copyright (c) 2009-11, President and Fellows of Harvard College.
56  All rights reserved.
57 
58  Redistribution and use in source and binary forms, with or without
59  modification, are permitted provided that the following conditions are met:
60 
61  Redistributions of source code must retain the above copyright notice,
62  this list of conditions and the following disclaimer.
63  Redistributions in binary form must reproduce the above copyright notice,
64  this list of conditions and the following disclaimer in the documentation
65  and/or other materials provided with the distribution.
66  Neither the name of the President and Fellows of Harvard College
67  nor the names of its contributors may be used to endorse or promote
68  products derived from this software without specific prior written
69  permission.
70 
71  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
72  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
73  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
75  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
76  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
77  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
78  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
80  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
81  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 
83  =========================================================================*/
84 
85 #include "vtkImageBlendWithMask.h"
86 
87 #include "vtkImageData.h"
88 #include "vtkInformation.h"
89 #include "vtkInformationVector.h"
90 #include "vtkObjectFactory.h"
91 #include "vtkStreamingDemandDrivenPipeline.h"
92 
93 vtkCxxRevisionMacro (vtkImageBlendWithMask, "$Revision: 490 $");
95 
97 {
98  LookupTable = 0;
99  this->SetNumberOfInputPorts (2);
100 }
101 
103 {
104  if ( LookupTable ) { LookupTable->Delete(); }
105 }
106 
107 //----------------------------------------------------------------------------
109 {
110  this->SetInput1(in);
111 }
112 
113 //----------------------------------------------------------------------------
115 {
116  this->SetInput2(in);
117 }
118 
119 //----------------------------------------------------------------------------
120 // The output extent is the intersection.
122  vtkInformation *vtkNotUsed(request),
123  vtkInformationVector **inputVector,
124  vtkInformationVector *outputVector)
125 {
126  // get the info objects
127  vtkInformation *outInfo = outputVector->GetInformationObject(0);
128  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
129  vtkInformation *inInfo2 = inputVector[1]->GetInformationObject(0);
130 
131  int ext[6], ext2[6], idx;
132 
133  inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext);
134  inInfo2->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext2);
135  for ( idx = 0; idx < 3; ++idx )
136  {
137  if ( ext2[idx * 2] > ext[idx * 2] )
138  {
139  ext[idx * 2] = ext2[idx * 2];
140  }
141  if ( ext2[idx * 2 + 1] < ext[idx * 2 + 1] )
142  {
143  ext[idx * 2 + 1] = ext2[idx * 2 + 1];
144  }
145  }
146 
147  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext, 6);
148 
149  return 1;
150 }
151 
152 void vtkImageBlendWithMask::PrintSelf(ostream & os, vtkIndent indent)
153 {
154  this->Superclass::PrintSelf(os, indent);
155  if ( LookupTable )
156  {
157  os << indent << "LookupTable: \n";
158  os << indent << *LookupTable << endl;
159  }
160 }
161 
162 //----------------------------------------------------------------------------
163 // This templated function executes the filter for any type of data.
164 template< class T >
166  vtkImageData *in1Data, T *in1Ptr,
167  vtkImageData *in2Data, T *in2Ptr,
168  vtkImageData *outData, T *outPtr, int id)
169 {
170  int num0, num1, num2, numC, numM;
171  int idx0, idx1, idx2, idxC;
172  vtkIdType in1Inc0, in1Inc1, in1Inc2;
173  vtkIdType in2Inc0, in2Inc1, in2Inc2;
174  vtkIdType outInc0, outInc1, outInc2;
175  double maskAlpha, oneMinusMaskAlpha;
176  unsigned long count = 0;
177  unsigned long target;
178 
179  numC = outData->GetNumberOfScalarComponents();
180  unsigned long pixSize = numC * sizeof( T );
181  maskAlpha = 0.5;
182  oneMinusMaskAlpha = 0.5;
183 
184  numM = in2Data->GetNumberOfScalarComponents();
185 
186  // Get information to march through data
187  in1Data->GetContinuousIncrements(ext, in1Inc0, in1Inc1, in1Inc2);
188  in2Data->GetContinuousIncrements(ext, in2Inc0, in2Inc1, in2Inc2);
189  outData->GetContinuousIncrements(ext, outInc0, outInc1, outInc2);
190  num0 = ext[1] - ext[0] + 1;
191  num1 = ext[3] - ext[2] + 1;
192  num2 = ext[5] - ext[4] + 1;
193 
194  target = (unsigned long)( num2 * num1 / 50.0 );
195  target++;
196 
197  // Loop through ouput pixels
198  for ( idx2 = 0; idx2 < num2; ++idx2 )
199  {
200  for ( idx1 = 0; !self->AbortExecute && idx1 < num1; ++idx1 )
201  {
202  if ( !id )
203  {
204  if ( !( count % target ) )
205  {
206  self->UpdateProgress( static_cast< double >( count )
207  / ( 50.0 * static_cast< double >( target ) ) );
208  }
209  count++;
210  }
211 
212  for ( idx0 = 0; idx0 < num0; ++idx0 )
213  {
214  if ( int(*in2Ptr) == 0 )
215  {
216  memcpy (outPtr, in1Ptr, pixSize);
217  in1Ptr += numC;
218  outPtr += numC;
219  }
220  else
221  {
222  double color[4];
223  self->GetLookupTable()->GetTableValue ( (int)( *in2Ptr ), color );
224  maskAlpha = color[3];
225  oneMinusMaskAlpha = 1.0 - maskAlpha;
226 
227  for ( idxC = 0; idxC < numC; idxC++ )
228  {
229  *outPtr = (T)( (int)( *in1Ptr ) * oneMinusMaskAlpha + (int)( color[idxC] * 255.0 ) * maskAlpha );
230  ++outPtr;
231  ++in1Ptr;
232  }
233  }
234 
235  in2Ptr += numM;
236  }
237  in1Ptr += in1Inc1;
238  in2Ptr += in2Inc1;
239  outPtr += outInc1;
240  }
241  in1Ptr += in1Inc2;
242  in2Ptr += in2Inc2;
243  outPtr += outInc2;
244  }
245 }
246 
247 //----------------------------------------------------------------------------
248 // This method is passed a input and output Datas, and executes the filter
249 // algorithm to fill the output from the inputs.
250 // It just executes a switch statement to call the correct function for
251 // the Datas data types.
253  vtkInformation *vtkNotUsed(request),
254  vtkInformationVector **vtkNotUsed(inputVector),
255  vtkInformationVector *vtkNotUsed(outputVector),
256  vtkImageData ***inData,
257  vtkImageData **outData,
258  int outExt[6], int id)
259 {
260  void *inPtr1;
261  void *inPtr2;
262  void *outPtr;
263  int * tExt;
264 
265  if ( !LookupTable )
266  {
267  vtkErrorMacro("LookupTable not set");
268  return;
269  }
270 
271  vtkImageData *mask = vtkImageData::SafeDownCast (inData[1][0]);
272 
273  if ( !mask )
274  {
275  vtkErrorMacro("Mask is not set");
276  return;
277  }
278 
279  inPtr1 = inData[0][0]->GetScalarPointerForExtent(outExt);
280  inPtr2 = inData[1][0]->GetScalarPointerForExtent(outExt);
281  outPtr = outData[0]->GetScalarPointerForExtent(outExt);
282 
283  tExt = inData[1][0]->GetExtent();
284  if ( tExt[0] > outExt[0] || tExt[1] < outExt[1]
285  || tExt[2] > outExt[2] || tExt[3] < outExt[3]
286  || tExt[4] > outExt[4] || tExt[5] < outExt[5] )
287  {
288  vtkErrorMacro("Mask extent not large enough");
289  return;
290  }
291  /*
292  if (inData[1][0]->GetNumberOfScalarComponents() != 1)
293  {
294  vtkErrorMacro("Mask can have one component");
295  }*/
296 
297  if ( inData[0][0]->GetScalarType() != outData[0]->GetScalarType()
298  || inData[0][0]->GetScalarType() != VTK_UNSIGNED_CHAR
299  || ( inData[0][0]->GetNumberOfScalarComponents() != 3 && inData[0][0]->GetNumberOfScalarComponents() != 4 )
300  /*inData[1][0]->GetScalarType() != VTK_UNSIGNED_CHAR*/ )
301  {
302  vtkErrorMacro( << "Execute: image ScalarType ("
303  << inData[0][0]->GetScalarType() << ") must match out ScalarType ("
304  << outData[0]->GetScalarType() << "), and mask scalar type ("
305  << inData[1][0]->GetScalarType() << ") must be unsigned char."
306  << "Number of input components: " << inData[0][0]->GetNumberOfScalarComponents() );
307  return;
308  }
309 
310  switch ( inData[0][0]->GetScalarType() )
311  {
312  vtkTemplateMacro(
313  vtkImageBlendWithMaskExecute(this, outExt,
314  inData[0][0], (VTK_TT *)( inPtr1 ),
315  //inData[1][0],(unsigned char *)(inPtr2),
316  inData[1][0], (VTK_TT *)( inPtr2 ),
317  outData[0], (VTK_TT *)( outPtr ), id) );
318  default:
319  vtkErrorMacro(<< "Execute: Unknown ScalarType");
320  return;
321  }
322 }
void vtkImageBlendWithMaskExecute(vtkImageBlendWithMask *self, int ext[6], vtkImageData *in1Data, T *in1Ptr, vtkImageData *in2Data, T *in2Ptr, vtkImageData *outData, T *outPtr, int id)
virtual void ThreadedRequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *vtkNotUsed(outputVector), vtkImageData ***inData, vtkImageData **outData, int extent[6], int threadId)
void SetImageInput(vtkImageData *in)
virtual void SetInput2(vtkDataObject *in)
virtual int RequestInformation(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
virtual void SetInput1(vtkDataObject *in)
void SetMaskInput(vtkImageData *in)
void PrintSelf(ostream &os, vtkIndent indent)
vtkStandardNewMacro(vtkImageBlendWithMask)
vtkCxxRevisionMacro(vtkImageBlendWithMask,"$Revision: 490 $")