2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

cmbatchgeneration.cpp 9.04 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
25
26
27
// 
// ================================================================================================

#include "cmbatchgeneration.h"

#include "tgt/event/keyevent.h"
28
#include "tgt/filesystem.h"
29
30
31

#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"
32
33
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/renderdata.h"
34
#include "core/tools/opengljobprocessor.h"
35
36
37
#include "core/tools/simplejobprocessor.h"
#include "core/tools/job.h"

38
39
40
41
42
43
44
45
#ifdef CAMPVIS_HAS_MODULE_DEVIL
#include <IL/il.h>
#include <IL/ilu.h>
#endif

#include <sstream>
#include <iomanip>

46
47
namespace campvis {

48
49
    CmBatchGeneration::CmBatchGeneration(DataContainer* dc)
        : AutoEvaluationPipeline(dc)
50
        , _usReader(&_canvasSize)
51
        , _confidenceGenerator()
52
        , _usBlurFilter(&_canvasSize)
53
        , _usFusion(&_canvasSize)
54
        , p_autoExecution("AutoExecution", "Automatic Execution", false)
55
56
57
        , p_sourcePath("SourcePath", "Source Files Path", "", StringProperty::DIRECTORY)
        , p_targetPathColor("TargetPathColor", "Target Path Color Files", "", StringProperty::DIRECTORY)
        , p_targetPathFuzzy("TargetPathFuzzy", "Target Path Fuzzy Files", "", StringProperty::DIRECTORY)
58
        , p_range("Range", "Files Range", tgt::ivec2(0, 1), tgt::ivec2(0, 0), tgt::ivec2(10000, 10000))
59
        , p_execute("Execute", "Execute Batch Pipeline")
60
61
62
63
64
    {
        addProcessor(&_usReader);
        addProcessor(&_confidenceGenerator);
        addProcessor(&_usFusion);
        addProcessor(&_usBlurFilter);
65

66
67
68
69
70
71
        addProperty(p_autoExecution);
        addProperty(p_sourcePath);
        addProperty(p_targetPathColor);
        addProperty(p_targetPathFuzzy);
        addProperty(p_range);
        addProperty(p_execute);
72
73
74
75
76
77
    }

    CmBatchGeneration::~CmBatchGeneration() {
    }

    void CmBatchGeneration::init() {
78
        AutoEvaluationPipeline::init();
79

80
81
82
        p_sourcePath.setValue("D:\\Medical Data\\US Confidence Vis\\Pasing 13-02-26\\04-02-22-212506_Perez11_20040222_212506_20040222_220332\\gallenblase");
        p_targetPathColor.setValue("D:\\Medical Data\\US Confidence Vis\\Pasing 13-02-26\\04-02-22-212506_Perez11_20040222_212506_20040222_220332\\gallenblase\\color");
        p_targetPathFuzzy.setValue("D:\\Medical Data\\US Confidence Vis\\Pasing 13-02-26\\04-02-22-212506_Perez11_20040222_212506_20040222_220332\\gallenblase\\fuzzy");
83
84
        p_range.setValue(tgt::ivec2(0, 1));
        p_execute.s_clicked.connect(this, &CmBatchGeneration::execute);
85

86
        _usReader.p_url.setValue("D:\\Medical Data\\US Confidence Vis\\Pasing 13-02-26\\04-02-22-212506_Perez11_20040222_212506_20040222_220332\\11_niere_re_durch_leber2\\original\\export0000.bmp");
87
88
        _usReader.p_targetImageID.setValue("us.image");
        _usReader.p_importType.selectById("localIntensity");
89
90
        _usReader.p_targetImageID.addSharedProperty(&_confidenceGenerator.p_sourceImageID);
        _usReader.p_targetImageID.addSharedProperty(&_usFusion.p_usImageId);
91
        _usReader.p_targetImageID.addSharedProperty(&_usBlurFilter.p_inputImage);
92
93

        _confidenceGenerator.p_targetImageID.setValue("confidence.image.generated");
94
        _confidenceGenerator.p_targetImageID.addSharedProperty(&_usFusion.p_confidenceImageID);
95
        _confidenceGenerator.p_curvilinear.setValue(true);
96
97
98
        _confidenceGenerator.p_origin.setValue(tgt::vec2(340.f, 540.f));
        _confidenceGenerator.p_angles.setValue(tgt::vec2(4.064f, 5.363f));
        //_confidenceGenerator.p_angles.setValue(tgt::vec2(232.f / 180.f * tgt::PIf, 307.f / 180.f * tgt::PIf));
99
100
        //_confidenceGenerator.p_origin.setValue(tgt::vec2(320.f, 35.f));
        //_confidenceGenerator.p_angles.setValue(tgt::vec2(45.f / 180.f * tgt::PIf, 135.f / 180.f * tgt::PIf));
101
        _confidenceGenerator.p_lengths.setValue(tgt::vec2(116.f, 543.f));
102
103
104
105
106

        _usFusion.p_targetImageID.setValue("us.fused");
        _usFusion.p_view.selectById("mappingSharpness");
        _usFusion.p_sliceNumber.setValue(0);

107
108
109
        _usBlurFilter.p_outputImage.setValue("us.blurred");
        _usBlurFilter.p_outputImage.addSharedProperty(&_usFusion.p_blurredImageId);
        _usBlurFilter.p_sigma.setValue(2.f);
110
111
112
113
114
115
116
117
118
119


        Geometry1DTransferFunction* tf = new Geometry1DTransferFunction(128, tgt::vec2(0.f, 1.f));
        tf->addGeometry(TFGeometry1D::createQuad(tgt::vec2(0.f, 1.f), tgt::col4(0, 0, 0, 255), tgt::col4(255, 255, 255, 255)));
        _usFusion.p_transferFunction.replaceTF(tf);

        _renderTargetID.setValue("us.fused");
    }

    void CmBatchGeneration::deinit() {
120
        AutoEvaluationPipeline::deinit();
121
122
123
    }

    void CmBatchGeneration::execute() {
124
125
        if (p_range.getValue().x > p_range.getValue().y)
            return;
126

127
        p_autoExecution.setValue(false);
128
129
130
        for (int i = p_range.getValue().x; i < p_range.getValue().y; ++i) {
            GLJobProc.enqueueJob(_canvas, makeJobOnHeap(this, &CmBatchGeneration::executePass, i), OpenGLJobProcessor::SerialJob);
        }
131
132
    }

133
    void CmBatchGeneration::onProcessorInvalidated(AbstractProcessor* processor) {
134
        if (p_autoExecution.getValue())
135
            AutoEvaluationPipeline::onProcessorInvalidated(processor);
136
137
138
139
140
141
    }

    void CmBatchGeneration::executePass(int path) {
        std::stringstream ss;

        // set up processors:
142
        ss << p_sourcePath.getValue() << "\\" << "export" << std::setfill('0') << std::setw(4) << path << ".bmp";
143
        _usReader.p_url.setValue(ss.str());
144

145
        executeProcessor(&_usReader, false);
146

147
        DataHandle dh = _data->getData(_usReader.p_targetImageID.getValue());
148
149
150
151
152
153
        if (dh.getData() != 0) {
            if (const ImageData* tester = dynamic_cast<const ImageData*>(dh.getData())) {
            	_canvasSize.setValue(tester->getSize().xy());
            }
        }

154
155
        executeProcessor(&_confidenceGenerator, false);
        executeProcessor(&_usBlurFilter, false);
156
157

        _usFusion.p_view.selectById("mappingLAB");
158
        executeProcessor(&_usFusion, false);
159
160
161
162
163
164
165
        save(path, p_targetPathColor.getValue());

//         _usFusion.p_view.selectById("mappingSharpness");
//         executeProcessor(&_usFusion);
//         save(path, p_targetPathFuzzy.getValue());
        
    }
166

167
    void CmBatchGeneration::save(int path, const std::string& basePath) {
168
        // get result
169
        ScopedTypedData<RenderData> rd(*_data, _usFusion.p_targetImageID.getValue());
170
171
        const ImageRepresentationGL* rep = rd->getColorTexture()->getRepresentation<ImageRepresentationGL>(false);
        if (rep != 0) {
172
#ifdef CAMPVIS_HAS_MODULE_DEVIL
173
174
175
            if (! tgt::FileSystem::dirExists(basePath))
                tgt::FileSystem::createDirectory(basePath);

176
            std::stringstream sss;
177
            sss << basePath << "\\" << "export" << std::setfill('0') << std::setw(4) << path << ".bmp";
178
179
180
181
182
183
184
            std::string filename = sss.str();
            if (tgt::FileSystem::fileExtension(filename).empty()) {
                LERROR("Filename has no extension");
                return;
            }

            // get color buffer content
185
186
            GLubyte* colorBuffer = rep->getTexture()->downloadTextureToBuffer(GL_RGBA, GL_UNSIGNED_SHORT);
            tgt::ivec2 size = rep->getSize().xy();
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

            // create Devil image from image data and write it to file
            ILuint img;
            ilGenImages(1, &img);
            ilBindImage(img);

            // put pixels into IL-Image
            ilTexImage(size.x, size.y, 1, 4, IL_RGBA, IL_UNSIGNED_SHORT, colorBuffer);
            ilEnable(IL_FILE_OVERWRITE);
            ilResetWrite();
            ILboolean success = ilSaveImage(filename.c_str());
            ilDeleteImages(1, &img);

            delete[] colorBuffer;

            if (!success) {
                LERROR("Could not save image to file: " << ilGetError());
            }
#else
            return;
#endif
208
209
210
        }
    }

211
}