ImageReader.cpp 4.44 KB
Newer Older
1
/* ***** BEGIN LICENSE BLOCK *****
Julien Waechter's avatar
Julien Waechter committed
2
 * FW4SPL - Copyright (C) IRCAD, 2009-2012.
3
4
5
6
7
8
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

#include <boost/filesystem.hpp>

Julien WAECHTER's avatar
Julien WAECHTER committed
9
10
#include <fwCore/base.hpp>

11
12
13
14
15
16
17
18
#include <itkImageFileReader.h>
#include <itkImageIOFactory.h>

#include <fwData/Image.hpp>

#include <fwTools/IntrinsicTypes.hpp>
#include <fwTools/Dispatcher.hpp>
#include <fwTools/TypeInfoKeyTypeMapping.hpp>
19
20

#include <fwDataIO/reader/registry/macros.hpp>
21

Julien WAECHTER's avatar
Julien WAECHTER committed
22
#include "inr2itk/itkInrImageIOFactory.hpp"
23

Julien WAECHTER's avatar
Julien WAECHTER committed
24
#include "fwItkIO/itk.hpp"
25
26
#include "fwItkIO/ImageReader.hpp"
#include "fwItkIO/helper/ProgressItkToFw.hpp"
27

28
fwDataIOReaderRegisterMacro( ::fwItkIO::ImageReader );
29
30


31
namespace fwItkIO
32
33
34
35
{

//------------------------------------------------------------------------------

36
ImageReader::ImageReader(::fwDataIO::reader::IObjectReader::Key key)  : ::fwData::location::enableSingleFile< IObjectReader >(this)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
    SLM_TRACE_FUNC();
}

//------------------------------------------------------------------------------

ImageReader::~ImageReader()
{
    SLM_TRACE_FUNC();
}

//------------------------------------------------------------------------------

struct ITKLoaderFunctor
{
    struct Parameter
    {
        ::fwData::Image::sptr       m_dataImage;
        std::string                 m_filename;
56
        ::fwItkIO::ImageReader::sptr  m_fwReader;
57
58
59
60
61
    };

    template<class PIXELTYPE>
    void operator()(Parameter &param)
    {
62
        OSLM_INFO( "::fwItkIO::ImageReader::ITKLoaderFunctor with PIXELTYPE "<<  ::fwTools::DynamicType::string<PIXELTYPE>() );
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

        // VAG attention : ImageFileReader ne notifie AUCUNE progressEvent mais son ImageIO oui!!!! mais ImageFileReader ne permet pas de l'atteindre
        // car soit mis a la mano ou alors construit lors de l'Update donc trop tard
        // Il faut dont creer une ImageIO a la mano (*1*): affecter l'observation  sur IO (*2*) et mettre le IO dans le reader (voir *3*)

        // Reader IO (*1*)
        typename itk::ImageIOBase::Pointer imageIORead = itk::ImageIOFactory::CreateImageIO( param.m_filename.c_str(), itk::ImageIOFactory::ReadMode);

        // set observation (*2*)
        Progressor progress(imageIORead, param.m_fwReader, param.m_filename);

        // the reader
        typedef itk::Image< PIXELTYPE, 3 >              ImageType;
        typedef itk::ImageFileReader< ImageType >       ReaderType;
        typename ReaderType::Pointer reader = ReaderType::New();
        reader->SetFileName( param.m_filename.c_str() );
        // attach its IO (*3*)
        reader->SetImageIO( imageIORead );

        reader->Update();
        typename ImageType::Pointer itkimage = reader->GetOutput();
84
        ::fwItkIO::dataImageFactory< ImageType>( itkimage, param.m_dataImage );
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    }

    //// get pixel type from Header
    static const std::type_info& getImageType( const std::string &imageFileName )
    {
        itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(imageFileName.c_str(), itk::ImageIOFactory::ReadMode);

        if( !imageIO )
        {
            std::string errMsg;
            errMsg  = "no ImageIOFactory found to read header of file : ";
            errMsg.append( imageFileName );

            throw( std::ios_base::failure( errMsg ) );
        }

        imageIO->SetFileName( imageFileName.c_str() );
        imageIO->ReadImageInformation();
        return imageIO->GetComponentTypeInfo();
    }
};

//------------------------------------------------------------------------------

void ImageReader::read()
{
    ::boost::filesystem::path file = getFile();
112
    OSLM_ASSERT("File: "<<file<<" doesn't exist", ::boost::filesystem::exists( file ) );
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    assert( !m_object.expired() );
    assert( m_object.lock() );

    const std::type_info& ti    = ITKLoaderFunctor::getImageType( file.string() );

    ITKLoaderFunctor::Parameter param;
    param.m_filename =  file.string();
    param.m_dataImage = this->getConcreteObject();
    param.m_fwReader = this->getSptr();

    ::fwTools::Dispatcher< ::fwTools::IntrinsicTypes , ITKLoaderFunctor >::invoke(ti , param );

    assert( m_object.lock() ); // verify that ::fwData::Image is well produced
    // Post Condition image with a pixel type
127
    SLM_ASSERT("Image has an unspecified type", getConcreteObject()->getPixelType() != ::fwTools::DynamicType() );
128
129
130
131
}

//------------------------------------------------------------------------------

132
} // namespace fwItkIO