[Insight-users] Need help: import/export of memory buffers

David Holmes holmesd3@yahoo.com
Fri, 14 Feb 2003 12:12:18 -0800 (PST)


Hans and Todd-

You are right; you need to be consistent (either
malloc/free or new/delete).  In the same vein, you
either need to copy the memory both directions, or
pass the pointer around and make sure that ITK knows
that you will take care of it.  With regards to the
second one, there is the SetContainerMemoryManagement
flag that should be explicitly set.  This, of course,
isn't an issue if you are lucky enough to just program
in c++ and only use new/delete.

thanks

david

--- Hans Johnson <hjohnson@mail.psychiatry.uiowa.edu>
wrote:
> Todd,
> 
> One caveate to be aware of:  The ITKSoftwareGuide
> assumes that the memory 
> you are passing was created with a "new" call (i.e.
> not a malloc).  If the 
> mrmory was created from some c library routines,
> then it was certainly 
> created with malloc an the ITKSoftwareGuide does not
> address this 
> situation.  I have not tested it, but I am almost
> certain that your 
> program will segfault if you give ITK the ability to
> "delete" the malloced 
> memory.
> 
> Regards,
> Hans
> 
> 
> 
> On Fri, 14 Feb 2003, David Holmes wrote:
> 
> > Todd-
> > 
> > there is a nice section on this in the
> > ITKSoftwareGuide (found on the website).  I
> recommend
> > that you start there.  I believe it is section
> 4.1.7. 
> > The only real requirement is that your data is
> located
> > in a linear, contiguous chunk of memory.
> > 
> > Here is some similar code that we use to
> accomplish
> > that task.  It's not the complete code, but it
> gives
> > you the idea.  If you need more of an
> explaination,
> > let me know.
> > 
> > //Our array to ITK
> > 
> > if (copyMemFlag) {
> >   outVolITK->Allocate();
> >   memcpy((void *)outVolITK->GetBufferPointer(),
> > inVol->Mem, inVol->BytesPerVolume);
> > } else {
> >   itk::ImportImageContainer<long unsigned
> > int,T>::Pointer import; 
> >   import = itk::ImportImageContainer<long unsigned
> > int,T>::New();
> >   import->Initialize();
> >   import->SetImportPointer((T*)inVol->Mem,
> > inVol->BytesPerVolume, false);
> >   outVolITK->SetPixelContainer(import);
> > }
> > 
> > 
> > //ITK to our array
> > 
> > if (copyMemFlag) {
> >   outVol = AVW_CreateVolume(NULL,
> region.GetSize(0),
> > region.GetSize(1), region.GetSize(2));
> >   outVol->Mem = memcpy(outVol->Mem, (void
> > *)inImgITK->GetBufferPointer(),
> > outVol->BytesPerVolume);
> > } else 	{
> >  
> >
>
inImgITK->GetPixelContainer()->SetContainerManageMemory(false);
> >   outVol =
> >
>
AVW_CreateVolume(inImgITK->GetPixelContainer()->GetImportPointer(),
> > region.GetSize(0), region.GetSize(1),
> > region.GetSize(2));
> > }
> > 
> > 
> > david
> > 
> > 
> > --- Todd Koumrian <todd@core-dump.stanford.edu>
> wrote:
> > > Hello,
> > > 
> > > I need help interfacing ITK functionality to our
> > > research framework.  In
> > > order to use ITK, I must New() appropriate
> itkImages
> > > and somehow load them
> > > with pixel data from another in-memory format. 
> > > After processing, I must
> > > return the results to another buffer (not ITK)
> also
> > > managed by the
> > > application.  While I understand the basics of
> image
> > > creation no problem, I
> > > don't understand how to do this bulk load/unload
> > > efficiently.
> > > 
> > > GetPixel()/SetPixel() is of course too
> inefficient.
> > > 
> > > itkImportImageFilter exists, but what about
> export?
> > > 
> > > The manual makes mention of using image
> iterators as
> > > a better alternative to
> > > GetPixel()/SetPixel(), but does not seem to
> explain
> > > them.  I'm lost here.
> > > 
> > > Regards,
> > > 
> > > Todd Koumrian
> > > 
> > > 
> > > _______________________________________________
> > > Insight-users mailing list
> > > Insight-users@public.kitware.com
> > >
> >
>
http://public.kitware.com/mailman/listinfo/insight-users
> > 
> > 
> > __________________________________________________
> > Do you Yahoo!?
> > Yahoo! Shopping - Send Flowers for Valentine's Day
> > http://shopping.yahoo.com
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users@public.kitware.com
> >
>
http://public.kitware.com/mailman/listinfo/insight-users
> > 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com