[Insight-users] Adding custom IO class

Parag Chandra pchandra@radonc.unc.edu
Thu, 14 Nov 2002 11:34:47 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_0142_01C28BD1.D5FC9080
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi all,

I've been using various parts of Insight for a couple of years now, but =
yesterday was the first time I tried to extend it. I created a very =
simple IO class (LPImageIO) derived from ImageIOBase, and corresponding =
factory (LPImageIOFactory) derived from ObjectFactoryBase. I followed =
the example of MetaImageIO very closely, but I put these two classes =
into their own library and linked it into my custom application along =
with all the other ITK libraries. My app is very simple, it's just this:

typedef itk::Image<double, 2> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter<ImageType> WriterType;

        itk::LPImageIOFactory::RegisterOneFactory();
        ReaderType::Pointer reader =3D ReaderType::New();
        reader->SetFileName(argv[1]);
        WriterType::Pointer writer =3D WriterType::New();
        writer->SetImageIO(itk::LPImageIO::New());
        writer->SetInput(reader->GetOutput());
        writer->SetFileName(argv[2]);
        writer->Update();

My custom library and my test app build without problems (MSVC6 w/ =
latest service pack), but running the program causes all sorts of =
strange, unhandled exceptions in odd places. The effects I'm seeing seem =
to be caused by improperly constructed objects. If I change one of the =
lines above to read:=20

writer->SetImageIO(itk::PNGImageIO::New());=20

and give it a PNG file as input, the program runs just fine, so I'm =
pretty certain that ITK is working correctly. So I tried a little =
experiment and made LPImageIO and LPImageIOFactory part of  =
ITKCommon.lib. Now my test program runs just fine. So my question is, do =
extensions to Insight (or at least the IO framework) need to actually be =
part of ITK, or can they be in their own libraries? Seems like the =
latter is the preferred case, and should be possible, but I suspect that =
there's some problems during construction/static initialization when =
crossing boundaries between libraries. Maybe I've forgotten something =
very simple. I can send the .h and .cxx files (4 total) on request if =
that would help. On a related note, how can I get the IO framework to =
automatically register my new IO class so that the call to =
::RegisterOneFactory() is no longer necessary? Thanks in advance for any =
help on this matter.=20

Best regards,
Parag Chandra
------=_NextPart_000_0142_01C28BD1.D5FC9080
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi all,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I've been using various parts of =
Insight for a=20
couple of years now, but yesterday was the first time I tried to extend =
it. I=20
created a very simple IO class (LPImageIO) derived from ImageIOBase, and =

corresponding factory (LPImageIOFactory) derived from ObjectFactoryBase. =
I=20
followed the example of MetaImageIO very closely, but I put these two =
classes=20
into their own library and linked it into my custom application along =
with all=20
the other ITK libraries. My app is very simple, it's just =
this:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>typedef itk::Image&lt;double, 2&gt;=20
ImageType;<BR>typedef itk::ImageFileReader&lt;ImageType&gt;=20
ReaderType;<BR>typedef itk::ImageFileWriter&lt;ImageType&gt;=20
WriterType;<BR></FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
itk::LPImageIOFactory::RegisterOneFactory();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
ReaderType::Pointer reader =3D=20
ReaderType::New();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
reader-&gt;SetFileName(argv[1]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;=20
WriterType::Pointer writer =3D=20
WriterType::New();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
writer-&gt;SetImageIO(itk::LPImageIO::New());<BR>&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;=20
writer-&gt;SetInput(reader-&gt;GetOutput());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
writer-&gt;SetFileName(argv[2]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;=20
writer-&gt;Update();<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>My custom library and my test app build =
without=20
problems (MSVC6 w/ latest service pack), but running the program causes =
all=20
sorts of strange, unhandled exceptions in odd places. The effects I'm =
seeing=20
seem to be caused by improperly constructed objects. If I change one of =
the=20
lines above to read: </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>writer-&gt;SetImageIO(itk::PNGImageIO::New());=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>and give it&nbsp;a PNG file as input, =
the program=20
runs just fine, so I'm pretty certain that ITK is working correctly. So =
I tried=20
a little experiment and made LPImageIO and LPImageIOFactory&nbsp;part of =

&nbsp;ITKCommon.lib. Now my test program runs just fine. So my question =
is, do=20
extensions to Insight (or at least the IO framework) need to actually be =
part of=20
ITK, or can they be in their own libraries? Seems like the =
latter&nbsp;is the=20
preferred case, and should be possible, but I suspect that there's some =
problems=20
during construction/static initialization when crossing boundaries =
between=20
libraries. Maybe I've forgotten something very simple. I can send the .h =
and=20
.cxx files (4 total) on request if that would help. On a related note, =
how can I=20
get the IO framework to automatically register my new IO class so that =
the call=20
to ::RegisterOneFactory() is no longer necessary? Thanks in advance for =
any help=20
on this matter. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Best regards,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Parag =
Chandra</DIV></FONT></BODY></HTML>

------=_NextPart_000_0142_01C28BD1.D5FC9080--