genericimagereader.cpp 7.92 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
29
30
31
32
33
34
35
36
// ================================================================================================
// 
// 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>

#include "tgt/filesystem.h"
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationdisk.h"
#include "core/datastructures/genericimagerepresentationlocal.h"
#include "core/tools/textfileparser.h"


namespace campvis {
Hossain Mahmud's avatar
Hossain Mahmud committed
37
    const std::string GenericImageReader::loggerCat_ = "CAMPVis.modules.io.GenericImageReader";
38
39
40
41
42
43
44
45
46
47
48
49
50
51

    GenericImageReader::GenericImageReader() 
        : AbstractProcessor()
        , p_url("url", "Image URL", "", StringProperty::OPEN_FILENAME)
    {
        addProperty(p_url);
        p_url.s_changed.connect(this, &GenericImageReader::onUrlPropertyChanged);

        this->addReader(new CsvdImageReader());        
        this->addReader(new LtfImageReader());
        this->addReader(new MhdImageReader());
        this->addReader(new RawImageReader());
        this->addReader(new VtkImageReader());

52
53
54
#ifdef CAMPVIS_HAS_MODULE_DEVIL
        this->addReader(new DevilImageReader());
#endif
55
56
57
58
59
60

        this->_ext = "";
        this->_currentlyVisible = nullptr;
    }
    
    GenericImageReader::~GenericImageReader() {
61
62
63
64
        for(std::map<AbstractImageReader*, MetaProperty*>::iterator it = this->_readers.begin(); it != this->_readers.end(); ++it) {
            delete it->second;
            delete it->first;
        }
65
66
67
68
69
70
71
72
    }

    void GenericImageReader::init() {

    }

    void GenericImageReader::deinit() {
        for(std::map<AbstractImageReader*, MetaProperty*>::iterator it = this->_readers.begin(); it != this->_readers.end(); ++it) {
73
74
75
76
77
78
            // deinit MetaProperty first!
            if (nullptr != it->second) {
                it->second->deinit();
            }
            // then we can delete the reader!
            if (nullptr != it->first) {
79
                it->first->deinit();
80
            }
81
82
83
84
85
86
87
88
89
90
91
92
93
        }
    }

    void GenericImageReader::updateResult(DataContainer& data) {
        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(this->_ext));
        if(it != this->_readers.end()) {
            if (this->_currentlyVisible != it->second) {
                if(nullptr != this->_currentlyVisible) {
                    this->_currentlyVisible->setVisible(false);
                }
                (it->second)->setVisible(true);
                this->_currentlyVisible = it->second;
            }
94
            it->first->process(data);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        }
    }

    void GenericImageReader::setVisibibility(const std::string& extention, bool visibility) {
        std::string _ext = extention;
        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(_ext));
        if(it != this->_readers.end()) {
            if(nullptr != this->_currentlyVisible) {
                this->_currentlyVisible->setVisible(!visibility);
            }
            (it->second)->setVisible(visibility);
            this->_currentlyVisible = it->second;
            //(it->first)->process(data);
        }
    }

    void GenericImageReader::setURL(std::string p_url) {
        this->p_url.setValue(p_url);

        std::string url = this->p_url.getValue();
        size_t extPos = url.rfind('.');
        if (extPos != std::string::npos) {
            this->_ext = url.substr(extPos);
        }

        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(this->_ext));
        if(it != this->_readers.end()) {
            (it->first)->p_url.setValue(this->p_url.getValue());
        }
        return;
    }

    void GenericImageReader::setURL(StringProperty p_url) {
        return this->setURL(p_url.getValue());
    }
    
    void GenericImageReader::setURL(const char* p_url) {
        return this->setURL(std::string(p_url));
    }

    void GenericImageReader::setTargetImageId(DataNameProperty& targetImageId) {
        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(this->_ext));
        if(it != this->_readers.end()) {
            (it->first)->p_targetImageID.setValue(targetImageId.getValue());
            std::set<AbstractProperty*> sharedProperties = targetImageId.getSharedProperties();
            for(std::set<AbstractProperty*>::iterator jt = sharedProperties.begin(); jt != sharedProperties.end(); ++jt) {
                (it->first)->p_targetImageID.addSharedProperty(*jt);
            }
        }
        return;
    }
    
    void GenericImageReader::setTargetImageId(const char* imageId) {
        return this->setTargetImageId(std::string(imageId));
    }
    void GenericImageReader::setTargetImageId(std::string imageId) {
        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(this->_ext));
        if(it != this->_readers.end()) {
            (it->first)->p_targetImageID.setValue(imageId);
        }
        return;
    }

    void GenericImageReader::setTargetImageIdSharedProperty(DataNameProperty* sharedProperty) {
        std::map<AbstractImageReader*, MetaProperty*>::iterator it = std::find_if(this->_readers.begin(), this->_readers.end(), checkExt(this->_ext));
        if(it != this->_readers.end()) {
            (it->first)->p_targetImageID.addSharedProperty(sharedProperty);
        }
        return;
    }

    int GenericImageReader::addReader(AbstractImageReader* reader) {
167
168
        MetaProperty* meta = new MetaProperty(reader->getName()+"MetaProp", reader->getName());
        meta->addPropertyCollection(*reader);
169
        meta->setVisible(false);
170
        this->addProperty(*meta);
171
172
173
174
175
176
177
178
179
180
181
182

        StringProperty* sp = dynamic_cast<StringProperty*>(meta->getProperty("url"));
        tgtAssert(sp != 0, "This should not happen.");
        if (sp != 0) {
            p_url.addSharedProperty(sp);
            sp->setVisible(false);
        }

        this->_readers.insert(std::pair<AbstractImageReader*, MetaProperty*>(reader, meta));
        return 0;
    }

Hossain Mahmud's avatar
Hossain Mahmud committed
183
    void GenericImageReader::onUrlPropertyChanged(const AbstractProperty* prop) {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        // first set visibility of old extension to false
        setVisibibility(_ext, false);

        // now update extension
        const std::string& url = this->p_url.getValue();
        size_t extPos = url.rfind('.');
        if (extPos != std::string::npos) {
            this->_ext = url.substr(extPos);
        }

        // set visibility of new extension's properties to true
        setVisibibility(_ext, true);

    }

    void GenericImageReader::adjustToNewExtension() {

    }

203
}