[vtkusers] vtkImageStencil problem

liulewes lewes_infor at hotmail.com
Thu Aug 23 16:52:03 EDT 2012


Hi all, I'm using vtkImageStencil to create ROIs on an image. The vtkImageStencilData::Add() works well for me, but vtkImageStencilData::Subtract() will cause my program to crash form time to time.
By looking at the source code, the program crashes in vtkImageStencil.cxx line 194 where "tmpPtr" becomes invalid and (outSpanEndPtr - outPtr) becomes negative. I'm not sure whether vtkImageStencilData::Subtract() causes this problem or it's sth. else. Can anyone give some suggestions? 
Lewes.
//////////////////////////
Environment: VC2009, WinXP, VTK5.8
crash point:
//----------------------------------------------------------------------------
template <class T>
void vtkImageStencilExecute(vtkImageStencil *self,
                            vtkImageData *inData, T *,
                            vtkImageData *inData2, T *,
                            vtkImageData *outData, T *,
                            int outExt[6], int id,
                            vtkInformation *outInfo)
{
  vtkImageStencilData *stencil = self->GetStencil();

  vtkImageIterator<T> inIter(inData, outExt);
  vtkImageStencilIterator<T> outIter(outData, stencil, outExt, self, id);

  int numscalars = outData->GetNumberOfScalarComponents();

  // whether to reverse the stencil
  bool reverseStencil = (self->GetReverseStencil() != 0);

  // if no background image is provided in inData2
  if (inData2 == 0)
    {
    // set color for area outside of input volume extent
    T *background;
    vtkAllocBackground(self, background, outInfo);

    T *inPtr = inIter.BeginSpan();
    T *inSpanEndPtr = inIter.EndSpan();
    while (!outIter.IsAtEnd())
      {
      T* outPtr = outIter.BeginSpan();
      T* outSpanEndPtr = outIter.EndSpan();

      T *tmpPtr = inPtr;
      int tmpInc = numscalars;
      if (!(outIter.IsInStencil() ^ reverseStencil))
        {
        tmpPtr = background;
        tmpInc = 0;
        }

      // move inPtr forward by the span size
      inPtr += (outSpanEndPtr - outPtr);

      while (outPtr != outSpanEndPtr)
        {
        // CopyPixel increments outPtr but not tmpPtr
        vtkCopyPixel(outPtr, tmpPtr, numscalars); // crashes here
        tmpPtr += tmpInc;
        }

      outIter.NextSpan();

      // this occurs at the end of a full row
      if (inPtr == inSpanEndPtr)
        {
        inIter.NextSpan();
        inPtr = inIter.BeginSpan();
        inSpanEndPtr = inIter.EndSpan();
        }
      }

    vtkFreeBackground(self, background);
    }

  // if a background image is given in inData2
  else
    {
    vtkImageIterator<T> inIter2(inData2, outExt);

    T *inPtr = inIter.BeginSpan();
    T *inPtr2 = inIter2.BeginSpan();
    T *inSpanEndPtr = inIter.EndSpan();
    while (!outIter.IsAtEnd())
      {
      T* outPtr = outIter.BeginSpan();
      T* outSpanEndPtr = outIter.EndSpan();

      T *tmpPtr = inPtr;
      if (!(outIter.IsInStencil() ^ reverseStencil))
        {
        tmpPtr = inPtr2;
        }

      // move inPtr forward by the span size
      inPtr += (outSpanEndPtr - outPtr);
      inPtr2 += (outSpanEndPtr - outPtr);

      while (outPtr != outSpanEndPtr)
        {
        // CopyPixel increments outPtr but not tmpPtr
        vtkCopyPixel(outPtr, tmpPtr, numscalars);
        tmpPtr += numscalars;
        }

      outIter.NextSpan();

      // this occurs at the end of a full row
      if (inPtr == inSpanEndPtr)
        {
        inIter.NextSpan();
        inIter2.NextSpan();
        inPtr = inIter.BeginSpan();
        inPtr2 = inIter2.BeginSpan();
        inSpanEndPtr = inIter.EndSpan();
        }
      }
    }
}

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120823/256523be/attachment.htm>


More information about the vtkusers mailing list