glimagecrop.cpp 5.78 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
44
45
46
47
48
49
50
// ================================================================================================
// 
// 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 "glimagecrop.h"

#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
#include "cgt/texture.h"

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

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

#include "core/tools/quadrenderer.h"

namespace campvis {

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

    GlImageCrop::GlImageCrop(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "GlImageCrop.out", DataNameProperty::WRITE)
        , p_llf("Llf", "LLF of Cropped Region", cgt::ivec3(0), cgt::ivec3(0), cgt::ivec3(1))
        , p_urb("Urb", "URB of Cropped Region", cgt::ivec3(1), cgt::ivec3(0), cgt::ivec3(1))
51
52
        , _shader2D(nullptr)
        , _shader3D(nullptr)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    {
        addProperty(p_inputImage, INVALID_PROPERTIES | INVALID_RESULT);
        addProperty(p_outputImage);
        addProperty(p_llf);
        addProperty(p_urb);
    }

    GlImageCrop::~GlImageCrop() {

    }

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

67
68
        _shader2D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimagecrop.frag", "#define GLIMAGECROP_2D\n");
        _shader3D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimagecrop.frag", "#define GLIMAGECROP_3D\n");
69
70
71
    }

    void GlImageCrop::deinit() {
72
73
        ShdrMgr.dispose(_shader2D);
        ShdrMgr.dispose(_shader3D);
74
75
76
77
78
79
80
81
        VisualizationProcessor::deinit();
    }

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

        if (img != 0) {
            cgt::ivec3 outputSize = cgt::abs(p_urb.getValue() - p_llf.getValue());
82
83
84
85
            bool isTexture2D = img->getParent()->getDimensionality() == 2;
            if (isTexture2D) {
                outputSize.z = 1;
            }
86
87
88
89
90

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

            // create texture for result
91
            cgt::Texture* resultTexture = new cgt::Texture(isTexture2D ? GL_TEXTURE_2D : GL_TEXTURE_3D, outputSize, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR);
92
93

            // activate shader and bind textures
94
95
96
97
98
99
            auto shader = isTexture2D ? _shader2D : _shader3D;

            shader->activate();
            shader->setUniform("_offset", p_llf.getValue());
            shader->setUniform("_outputSize", outputSize);
            img->bind(shader, inputUnit);
100
101
102
103
104
105
106

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

            // render quad to compute difference measure by shader
            for (int z = 0; z < outputSize.z; ++z) {
107
108
109
                if (!isTexture2D) {
                    shader->setUniform("_zTexel", z);
                }
110
111
112
113
114
                _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
                QuadRdr.renderQuad();
            }
            _fbo->detachAll();
            _fbo->deactivate();
115
            shader->deactivate();
116
117

            // put resulting image into DataContainer
118
            ImageData* id = new ImageData(isTexture2D ? 2 : 3, outputSize, img.getImageData()->getNumChannels());
119
120
            ImageRepresentationGL::create(id, resultTexture);
            const ImageMappingInformation& imi = img->getParent()->getMappingInformation();
121
            id->setMappingInformation(ImageMappingInformation(img->getSize(), imi.getOffset() + (cgt::vec3(p_llf.getValue()) * imi.getVoxelSize()), imi.getVoxelSize(), imi.getCustomTransformation()));
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
            data.addData(p_outputImage.getValue(), id);

            cgt::TextureUnit::setZeroUnit();
            LGL_ERROR;
        }
        else {
            LDEBUG("No suitable input image found.");
        }
    }

    void GlImageCrop::updateProperties(DataContainer& dataContainer) {
        ImageRepresentationGL::ScopedRepresentation img(dataContainer, p_inputImage.getValue());
        if (img != nullptr) {
            cgt::ivec3 size(img->getSize());

            if (p_llf.getMaxValue() != size-1)
                p_llf.setMaxValue(size-1);
            if (p_urb.getMaxValue() != size) {
                p_urb.setMaxValue(size);
                p_urb.setValue(size);
            }
        }
    }

}