genericimagereader.cpp 5.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#include "genericimagereader.h"

#include <fstream>

29
#include "cgt/filesystem.h"
30
31
32
33
34
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationdisk.h"
#include "core/datastructures/genericimagerepresentationlocal.h"
#include "core/tools/textfileparser.h"

35
36
37
38
39
40
#include "modules/io/processors/csvdimagereader.h"
#include "modules/io/processors/ltfimagereader.h"
#include "modules/io/processors/mhdimagereader.h"
#include "modules/io/processors/niftiimagereader.h"
#include "modules/io/processors/rawimagereader.h"
#include "modules/io/processors/vtkimagereader.h"
41
42

namespace campvis {
43
44
45
46
47
48
49
50
51
52
53
54
55
    namespace {
        struct checkExtension {
            checkExtension(const std::string& str) : _str(str) {}

            bool operator()(const std::pair<AbstractImageReader*, MetaProperty*>& v) const { 
                return v.first->acceptsExtension(this->_str); 
            }

        private:
            std::string _str;
        };
    }

Hossain Mahmud's avatar
Hossain Mahmud committed
56
    const std::string GenericImageReader::loggerCat_ = "CAMPVis.modules.io.GenericImageReader";
57
58
59

    GenericImageReader::GenericImageReader() 
        : AbstractProcessor()
60
61
        , p_url("Url", "Image URL", "", StringProperty::OPEN_FILENAME)
        , p_targetImageID("TargetImageName", "Target Image ID", "AbstractImageReader.output", DataNameProperty::WRITE)
62
        , _currentlyVisible(nullptr)
63
64
    {
        addProperty(p_url);
65
        addProperty(p_targetImageID);
66
67
        p_url.s_changed.connect(this, &GenericImageReader::onUrlPropertyChanged);

68
69
70
71
72
73
        addReader(new CsvdImageReader());        
        addReader(new LtfImageReader());
        addReader(new MhdImageReader());
        addReader(new NiftiImageReader());
        addReader(new RawImageReader());
        addReader(new VtkImageReader());
74

75
#ifdef CAMPVIS_HAS_MODULE_DEVIL
76
        addReader(new DevilImageReader());
77
#endif
78
79
80
    }
    
    GenericImageReader::~GenericImageReader() {
81
        for (auto it = _readers.begin(); it != _readers.end(); ++it) {
82
83
84
            delete it->second;
            delete it->first;
        }
85
86
87
88
89
90
91
    }

    void GenericImageReader::init() {

    }

    void GenericImageReader::deinit() {
92
        for(std::map<AbstractImageReader*, MetaProperty*>::iterator it = _readers.begin(); it != _readers.end(); ++it) {
93
94
95
96
97
98
            // deinit MetaProperty first!
            if (nullptr != it->second) {
                it->second->deinit();
            }
            // then we can delete the reader!
            if (nullptr != it->first) {
99
                it->first->deinit();
100
            }
101
102
103
104
        }
    }

    void GenericImageReader::updateResult(DataContainer& data) {
105
106
107
108
        const std::string extension = cgt::FileSystem::fileExtension(p_url.getValue());
        auto it = findReader(extension);

        if (it != this->_readers.end()) {
109
            it->first->process(data);
110
111
112
        }
    }

113
114
115
116
117
118
119
120
121
    void GenericImageReader::updateVisibility(const std::string& extension) {
        if (_currentlyVisible != nullptr) {
            _currentlyVisible->setVisible(false);
            _currentlyVisible = nullptr;
        }

        auto it = findReader(extension);
        if (it != this->_readers.end()) {
            it->second->setVisible(true);
122
123
124
125
            this->_currentlyVisible = it->second;
        }
    }

126
    void GenericImageReader::addReader(AbstractImageReader* reader) {
127
        MetaProperty* meta = new MetaProperty(reader->getName() + "MetaProp", reader->getName());
128
        meta->addPropertyCollection(*reader);
129
        meta->setVisible(false);
130
        this->addProperty(*meta);
131

132
133
134
135
        p_url.addSharedProperty(&reader->p_url);
        reader->p_url.setVisible(false);
        p_targetImageID.addSharedProperty(&reader->p_targetImageID);
        reader->p_targetImageID.setVisible(false);
136

137
        _readers.insert(std::pair<AbstractImageReader*, MetaProperty*>(reader, meta));
138
139
    }

Hossain Mahmud's avatar
Hossain Mahmud committed
140
    void GenericImageReader::onUrlPropertyChanged(const AbstractProperty* prop) {
141
        // now update extension
142
143
        std::string extension = cgt::FileSystem::fileExtension(p_url.getValue());
        updateVisibility(extension);
144
145
    }

146
147
148
149
    std::map<AbstractImageReader*, MetaProperty*>::const_iterator GenericImageReader::findReader(const std::string& extension) const {
        auto it = std::find_if(_readers.begin(), _readers.end(), checkExtension(extension));
        if (it == _readers.end())
            it = std::find_if(_readers.begin(), _readers.end(), checkExtension("raw"));
150

151
        return it;
152
153
    }

154
}