glmorphologyfilter.cpp 7.7 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
37
38
39
40
41
42
43
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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 "glmorphologyfilter.h"

#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/renderdata.h"
#include "core/pipeline/processordecoratorgradient.h"

#include "core/classification/simpletransferfunction.h"
#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"

#include "core/tools/quadrenderer.h"
#include "core/tools/stringutils.h"

namespace campvis {
44
    GenericOption<std::string> structuringElementOptions[2] = {
45
46
        GenericOption<std::string>("cross", "Cross", "CROSS_ELEMENT"),
        GenericOption<std::string>("cube", "Cube", "CUBE_ELEMENT")
47
    };
48
49
50
51
52

    const std::string GlMorphologyFilter::loggerCat_ = "CAMPVis.modules.classification.GlMorphologyFilter";

    GlMorphologyFilter::GlMorphologyFilter(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
53
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
54
55
        , p_outputImage("OutputImage", "Output Image", "GlMorphologyFilter.out", DataNameProperty::WRITE)
        , p_filterOperation("FilterOperation", "Operations to Apply ([edoc]+)", "ed")
56
        , p_structuringElement("StructuringElement", "Structuring Element", structuringElementOptions, 2)
57
58
59
        , _erosionFilter(nullptr)
        , _dilationFilter(nullptr)
    {
60
61
62
63
        addProperty(p_inputImage);
        addProperty(p_outputImage);
        addProperty(p_filterOperation);
        addProperty(p_structuringElement, INVALID_SHADER | INVALID_RESULT);
64
65
66
67
68
69
70
71
72
    }

    GlMorphologyFilter::~GlMorphologyFilter() {

    }

    void GlMorphologyFilter::init() {
        VisualizationProcessor::init();

73
        _erosionFilter = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/GlMorphologyFilter.frag", generateGlslHeader("min"));
74
75
76
        _erosionFilter->setAttributeLocation(0, "in_Position");
        _erosionFilter->setAttributeLocation(1, "in_TexCoord");

77
        _dilationFilter = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/GlMorphologyFilter.frag", generateGlslHeader("max"));
78
79
80
81
82
83
84
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
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
167
168
169
170
171
172
173
174
175
176
        _dilationFilter->setAttributeLocation(0, "in_Position");
        _dilationFilter->setAttributeLocation(1, "in_TexCoord");
    }

    void GlMorphologyFilter::deinit() {
        ShdrMgr.dispose(_erosionFilter);
        ShdrMgr.dispose(_dilationFilter);
        VisualizationProcessor::deinit();
    }

    void GlMorphologyFilter::updateResult(DataContainer& data) {
        ImageRepresentationGL::ScopedRepresentation img(data, p_inputImage.getValue());

        if (img != 0) {
            if (img->getDimensionality() == 3) {
                std::string ops = p_filterOperation.getValue();
                ops = StringUtils::replaceAll(ops, "o", "ed"); // opening := erosion, dilation
                ops = StringUtils::replaceAll(ops, "c", "de"); // closing := dilation, erosion

                const tgt::Texture* inputTexture = img->getTexture();
                tgt::Texture* outputTexture = nullptr;

                for (size_t i = 0; i < ops.length(); ++i) {
                    if (ops[i] == 'e') {
                        outputTexture = applyFilter(inputTexture, _erosionFilter);
                    }
                    else if (ops[i] == 'd') {
                        outputTexture = applyFilter(inputTexture, _dilationFilter);
                    }
                    else {
                        continue;
                    }

                    if (inputTexture != img->getTexture())
                        delete inputTexture;
                    inputTexture = outputTexture;
                }

                // put resulting image into DataContainer
                if (outputTexture != 0) {
                    ImageData* id = new ImageData(3, img->getSize(), 1);
                    ImageRepresentationGL::create(id, outputTexture);
                    id->setMappingInformation(img->getParent()->getMappingInformation());
                    data.addData(p_outputImage.getValue(), id);
                }
                else {
                    data.addDataHandle(p_outputImage.getValue(), img.getDataHandle());
                }

                tgt::TextureUnit::setZeroUnit();
                LGL_ERROR;
            }
            else {
                // yes I was too lazy... But have a look in the shader: Half of the 2D support is already there. :)
                LERROR("Sorry, currently only 3D images are supported.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }

        validate(INVALID_RESULT);
    }

    tgt::Texture* GlMorphologyFilter::applyFilter(const tgt::Texture* inputTexture, tgt::Shader* filter) const {
        tgtAssert(inputTexture != 0, "Input texture must not be 0.");
        const tgt::ivec3& size = inputTexture->getDimensions();

        tgt::TextureUnit inputUnit;
        inputUnit.activate();

        // create texture for result
        tgt::Texture* resultTexture = new tgt::Texture(0, size, inputTexture->getFormat(), inputTexture->getInternalFormat(), inputTexture->getDataType(), inputTexture->getFilter());
        resultTexture->uploadTexture();

        // activate shader and bind textures
        inputTexture->bind();
        filter->activate();
        filter->setUniform("_texture", inputUnit.getUnitNumber());
        filter->setUniform("_textureSize", size);

        // activate FBO and attach texture
        _fbo->activate();
        glViewport(0, 0, static_cast<GLsizei>(size.x), static_cast<GLsizei>(size.y));

        // render quad to compute difference measure by shader
        for (int z = 0; z < size.z; ++z) {
            filter->setUniform("_zTexCoord", z);
            _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
            QuadRdr.renderQuad();
        }

        _fbo->detachAll();
        _fbo->deactivate();
        filter->deactivate();

        return resultTexture;
    }

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    void GlMorphologyFilter::updateShader() {
        _erosionFilter->setHeaders(generateGlslHeader("min"));
        _erosionFilter->rebuild();
        _dilationFilter->setHeaders(generateGlslHeader("max"));
        _dilationFilter->rebuild();
        validate(INVALID_SHADER);
    }

    std::string GlMorphologyFilter::generateGlslHeader(const std::string& filerOp) const {
        std::string toReturn =  "#define FILTER_OP " + filerOp + "\n";
        toReturn += "#define " + p_structuringElement.getOptionValue() + "\n";

        return toReturn;
    }

192
}