vtkImageBlendWithMask.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003 Program:   vtkINRIA3D
00004 Module:    $Id: vtkImageBlendWithMask.cxx 1080 2009-02-18 13:27:02Z acanale $
00005 Language:  C++
00006 Author:    $Author: acanale $
00007 Date:      $Date: 2009-02-18 14:27:02 +0100 (Wed, 18 Feb 2009) $
00008 Version:   $Revision: 1080 $
00009 
00010 Copyright (c) 2007 INRIA - Asclepios Project. All rights reserved.
00011 See Copyright.txt for details.
00012 
00013 This software is distributed WITHOUT ANY WARRANTY; without even
00014 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015 PURPOSE.  See the above copyright notices for more information.
00016 
00017 =========================================================================*/
00018 // version vtkRenderingAddOn
00019 #include <vtkRenderingAddOn/vtkImageBlendWithMask.h>
00020 
00021 #include "vtkImageData.h"
00022 #include "vtkInformation.h"
00023 #include "vtkInformationVector.h"
00024 #include "vtkObjectFactory.h"
00025 #include "vtkStreamingDemandDrivenPipeline.h"
00026 
00027 vtkCxxRevisionMacro (vtkImageBlendWithMask, "$Revision: 1080 $");
00028 vtkStandardNewMacro (vtkImageBlendWithMask);
00029 
00030 vtkImageBlendWithMask::vtkImageBlendWithMask()
00031 {
00032   LookupTable=0;
00033   this->SetNumberOfInputPorts (2);
00034 }
00035 
00036 vtkImageBlendWithMask::~vtkImageBlendWithMask()
00037 {
00038   if( LookupTable )
00039     LookupTable->Delete();
00040 }
00041 
00042 //----------------------------------------------------------------------------
00043 void vtkImageBlendWithMask::SetImageInput(vtkImageData *in)
00044 {
00045   this->SetInput1(in);
00046 }
00047 
00048 //----------------------------------------------------------------------------
00049 void vtkImageBlendWithMask::SetMaskInput(vtkImageData *in) 
00050 {
00051   this->SetInput2(in);
00052 }
00053 
00054 //----------------------------------------------------------------------------
00055 // The output extent is the intersection.
00056 int vtkImageBlendWithMask::RequestInformation (
00057   vtkInformation * vtkNotUsed(request),
00058   vtkInformationVector **inputVector,
00059   vtkInformationVector *outputVector)
00060 {
00061   // get the info objects
00062   vtkInformation* outInfo = outputVector->GetInformationObject(0);
00063   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
00064   vtkInformation *inInfo2 = inputVector[1]->GetInformationObject(0);
00065 
00066   int ext[6], ext2[6], idx;
00067 
00068   inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext);
00069   inInfo2->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext2);
00070   for (idx = 0; idx < 3; ++idx)
00071     {
00072     if (ext2[idx*2] > ext[idx*2])
00073       {
00074       ext[idx*2] = ext2[idx*2];
00075       }
00076     if (ext2[idx*2+1] < ext[idx*2+1])
00077       {
00078       ext[idx*2+1] = ext2[idx*2+1];
00079       }
00080     }
00081   
00082   outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext,6);
00083 
00084   return 1;
00085 }
00086 
00087 
00088 void vtkImageBlendWithMask::PrintSelf(ostream& os, vtkIndent indent)
00089 {
00090   this->Superclass::PrintSelf(os,indent);
00091   if( LookupTable )
00092   {
00093     os << indent << "LookupTable: \n";
00094     os << indent << *LookupTable << endl;
00095   }  
00096 }
00097 
00098 
00099 
00100 
00101 //----------------------------------------------------------------------------
00102 // This templated function executes the filter for any type of data.
00103 template <class T>
00104 void vtkImageBlendWithMaskExecute(vtkImageBlendWithMask *self, int ext[6],
00105                            vtkImageData *in1Data, T *in1Ptr,
00106                            vtkImageData *in2Data, T *in2Ptr,
00107                            vtkImageData *outData, T *outPtr, int id)
00108 {
00109   int num0, num1, num2, numC, numM, pixSize;
00110   int idx0, idx1, idx2, idxC;
00111   vtkIdType in1Inc0, in1Inc1, in1Inc2;
00112   vtkIdType in2Inc0, in2Inc1, in2Inc2;
00113   vtkIdType outInc0, outInc1, outInc2;
00114   double maskAlpha, oneMinusMaskAlpha;
00115   unsigned long count = 0;
00116   unsigned long target;
00117   
00118   numC = outData->GetNumberOfScalarComponents();
00119   pixSize = numC * sizeof(T);
00120   maskAlpha = 0.5;
00121   oneMinusMaskAlpha = 0.5;
00122 
00123   numM = in2Data->GetNumberOfScalarComponents();
00124   
00125   // Get information to march through data 
00126   in1Data->GetContinuousIncrements(ext, in1Inc0, in1Inc1, in1Inc2);
00127   in2Data->GetContinuousIncrements(ext, in2Inc0, in2Inc1, in2Inc2);
00128   outData->GetContinuousIncrements(ext, outInc0, outInc1, outInc2);
00129   num0 = ext[1] - ext[0] + 1;
00130   num1 = ext[3] - ext[2] + 1;
00131   num2 = ext[5] - ext[4] + 1;
00132 
00133   
00134   target = (unsigned long)(num2*num1/50.0);
00135   target++;
00136 
00137   // Loop through ouput pixels
00138   for (idx2 = 0; idx2 < num2; ++idx2)
00139   {
00140     for (idx1 = 0; !self->AbortExecute && idx1 < num1; ++idx1)
00141     {
00142       if (!id) 
00143       {
00144         if (!(count%target))
00145           self->UpdateProgress(count/(50.0*target));
00146         count++;
00147       }
00148       
00149       for (idx0 = 0; idx0 < num0; ++idx0)
00150       {
00151         
00152         if( int(*in2Ptr)==0 )
00153         {
00154           memcpy (outPtr, in1Ptr, pixSize);
00155           in1Ptr += numC;
00156           outPtr += numC;
00157         }
00158         else
00159         {
00160           double color[4];
00161           self->GetLookupTable()->GetTableValue ((int)(*in2Ptr), color);
00162           maskAlpha = color[3];
00163           oneMinusMaskAlpha = 1.0-maskAlpha;
00164           
00165           for(idxC = 0; idxC<numC; idxC++)
00166           {
00167             *outPtr = (T)((int)(*in1Ptr)*oneMinusMaskAlpha+(int)(color[idxC]*255.0)*maskAlpha);
00168             ++outPtr;
00169             ++in1Ptr;
00170           }
00171         }
00172 
00173         in2Ptr += numM;
00174                 
00175       }
00176       in1Ptr += in1Inc1;
00177       in2Ptr += in2Inc1;
00178       outPtr += outInc1;
00179     }
00180     in1Ptr += in1Inc2;
00181     in2Ptr += in2Inc2;
00182     outPtr += outInc2;
00183     }
00184   
00185 }
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 //----------------------------------------------------------------------------
00194 // This method is passed a input and output Datas, and executes the filter
00195 // algorithm to fill the output from the inputs.
00196 // It just executes a switch statement to call the correct function for
00197 // the Datas data types.
00198 void vtkImageBlendWithMask::ThreadedRequestData(
00199   vtkInformation * vtkNotUsed( request ), 
00200   vtkInformationVector ** vtkNotUsed( inputVector ), 
00201   vtkInformationVector * vtkNotUsed( outputVector ),
00202   vtkImageData ***inData, 
00203   vtkImageData **outData,
00204   int outExt[6], int id)
00205 {
00206   void *inPtr1;
00207   void *inPtr2;
00208   void *outPtr;
00209   int *tExt;
00210 
00211   if(!LookupTable)
00212   {
00213     vtkErrorMacro("LookupTable not set");
00214     return;
00215   }
00216 
00217   vtkImageData* mask = vtkImageData::SafeDownCast (inData[1][0]);
00218 
00219   if( !mask )
00220   {
00221     vtkErrorMacro("Mask is not set");
00222     return;
00223   }
00224   
00225   
00226   inPtr1 = inData[0][0]->GetScalarPointerForExtent(outExt);
00227   inPtr2 = inData[1][0]->GetScalarPointerForExtent(outExt);
00228   outPtr = outData[0]->GetScalarPointerForExtent(outExt);
00229 
00230   tExt = inData[1][0]->GetExtent();
00231   if (tExt[0] > outExt[0] || tExt[1] < outExt[1] || 
00232       tExt[2] > outExt[2] || tExt[3] < outExt[3] ||
00233       tExt[4] > outExt[4] || tExt[5] < outExt[5])
00234     {
00235     vtkErrorMacro("Mask extent not large enough");
00236     return;
00237     }
00238   /*
00239   if (inData[1][0]->GetNumberOfScalarComponents() != 1)
00240     {
00241     vtkErrorMacro("Mask can have one component");
00242     }*/
00243     
00244   if (inData[0][0]->GetScalarType() != outData[0]->GetScalarType() ||
00245       inData[0][0]->GetScalarType() != VTK_UNSIGNED_CHAR ||
00246       (inData[0][0]->GetNumberOfScalarComponents() != 3 && inData[0][0]->GetNumberOfScalarComponents() != 4)
00247       /*inData[1][0]->GetScalarType() != VTK_UNSIGNED_CHAR*/)
00248     {
00249       vtkErrorMacro(<< "Execute: image ScalarType (" 
00250                     << inData[0][0]->GetScalarType() << ") must match out ScalarType (" 
00251                     << outData[0]->GetScalarType() << "), and mask scalar type (" 
00252                     << inData[1][0]->GetScalarType() << ") must be unsigned char."
00253                     << "Number of input components: " << inData[0][0]->GetNumberOfScalarComponents());
00254     return;
00255     }
00256   
00257   switch (inData[0][0]->GetScalarType())
00258     {
00259     vtkTemplateMacro(
00260                      vtkImageBlendWithMaskExecute(this, outExt,
00261                                                   inData[0][0],(VTK_TT *)(inPtr1),
00262                                                   //inData[1][0],(unsigned char *)(inPtr2),
00263                                                   inData[1][0],(VTK_TT *)(inPtr2),
00264                                                   outData[0], (VTK_TT *)(outPtr),id));
00265         default:
00266           vtkErrorMacro(<< "Execute: Unknown ScalarType");
00267           return;
00268     }
00269 }
00270 
00271 
00272 

Generated on Tue Jun 30 01:09:54 2009 for vtkINRIA3D by  doxygen 1.4.4