Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

glimageresampler.cpp 6.34 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
// ================================================================================================
// 
// 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 "glimageresampler.h"

27
28
29
30
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
#include "cgt/texture.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#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 GlImageResampler::loggerCat_ = "CAMPVis.modules.classification.GlImageResampler";

    GlImageResampler::GlImageResampler(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "GlImageResampler.out", DataNameProperty::WRITE)
        , p_resampleScale("ResampleScale", "Resampling Scale", .5f, .01f, 10.f)
47
        , p_targetSize("TargetSize", "Size of Resampled Image", cgt::ivec3(128), cgt::ivec3(1), cgt::ivec3(1024))
48
49
        , _shader2D(0)
        , _shader3D(0)
50
    {
51
        addProperty(p_inputImage, INVALID_RESULT | INVALID_PROPERTIES);
52
        addProperty(p_outputImage);
53
54
        addProperty(p_resampleScale, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_targetSize);
55
56
57
58
59
60
61
62
63
    }

    GlImageResampler::~GlImageResampler() {

    }

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

64
65
66
67
68
69
70
        _shader2D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimageresampler.frag", "#define GLRESAMPLER_3D\n");
        _shader2D->setAttributeLocation(0, "in_Position");
        _shader2D->setAttributeLocation(1, "in_TexCoord");

        _shader3D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimageresampler.frag", "#define GLRESAMPLER_2D\n");
        _shader3D->setAttributeLocation(0, "in_Position");
        _shader3D->setAttributeLocation(1, "in_TexCoord");
71
72
73
    }

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

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

        if (img != 0) {
83
            cgt::vec3 originalSize(img->getSize());
84
            const cgt::ivec3& resampledSize = p_targetSize.getValue();
85
86
87
88
89
90
            bool isTexture2D = img->getParent()->getDimensionality() == 2;

            // 2D textures should not be scaled along the z axis
            if (isTexture2D) {
                resampledSize.z = 1;
            }
91

92
            cgt::TextureUnit inputUnit;
93
94
95
            inputUnit.activate();

            // create texture for result
96
            cgt::Texture* resultTexture = new cgt::Texture(isTexture2D ? GL_TEXTURE_2D : GL_TEXTURE_3D, resampledSize, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR);
97

98
99
            // Select the right shader for the 2D and 3D case
            cgt::Shader *shader = isTexture2D ? _shader2D : _shader3D;
100

101
102
103
104
            // activate shader and bind textures
            shader->activate();
            img->bind(shader, inputUnit);
            
105
106
107
108
109
110
            // activate FBO and attach texture
            _fbo->activate();
            glViewport(0, 0, static_cast<GLsizei>(resampledSize.x), static_cast<GLsizei>(resampledSize.y));

            // render quad to compute difference measure by shader
            for (int z = 0; z < resampledSize.z; ++z) {
111
112
113
114
                if (!isTexture2D) {    
                    float zTexCoord = static_cast<float>(z)/static_cast<float>(resampledSize.z) + .5f/static_cast<float>(resampledSize.z);
                    shader->setUniform("_zTexCoord", zTexCoord);
                }
115
                _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
116
                LGL_ERROR;
117
118
119
120
                QuadRdr.renderQuad();
            }
            _fbo->detachAll();
            _fbo->deactivate();
121
            shader->deactivate();
122
123

            // put resulting image into DataContainer
124
            ImageData* id = new ImageData(img->getParent()->getDimensionality(), resampledSize, img->getParent()->getNumChannels());
125
126
            ImageRepresentationGL::create(id, resultTexture);
            const ImageMappingInformation& imi = img->getParent()->getMappingInformation();
127
            id->setMappingInformation(ImageMappingInformation(img->getSize(), imi.getOffset(), imi.getVoxelSize() / p_resampleScale.getValue(), imi.getCustomTransformation()));
128
129
            data.addData(p_outputImage.getValue(), id);

130
            cgt::TextureUnit::setZeroUnit();
131
132
133
            LGL_ERROR;
        }
        else {
134
            LDEBUG("No suitable input image found.");
135
136
137
        }
    }

138
139
140
141
142
143
144
145
146
    void GlImageResampler::updateProperties(DataContainer& dataContainer) {
        ImageRepresentationGL::ScopedRepresentation img(dataContainer, p_inputImage.getValue());

        if (img != 0) {
            p_targetSize.setMaxValue(cgt::ivec3(img->getSize()) * int(p_resampleScale.getMaxValue()));
            p_targetSize.setValue(cgt::ivec3(cgt::vec3(img->getSize()) * p_resampleScale.getValue()));
        }
    }

147
}
148