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

glimagecrop.cpp 5.72 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// ================================================================================================
// 
// 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))
    {
        addProperty(p_inputImage, INVALID_PROPERTIES | INVALID_RESULT);
        addProperty(p_outputImage);
        addProperty(p_llf);
        addProperty(p_urb);
    }

    GlImageCrop::~GlImageCrop() {

    }

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

65 66
        _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");
67 68 69
    }

    void GlImageCrop::deinit() {
70 71
        ShdrMgr.dispose(_shader2D);
        ShdrMgr.dispose(_shader3D);
72 73 74 75 76 77 78 79
        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());
80 81 82 83
            bool isTexture2D = img->getParent()->getDimensionality() == 2;
            if (isTexture2D) {
                outputSize.z = 1;
            }
84 85 86 87 88

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

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

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

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

            // 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) {
105 106 107
                if (!isTexture2D) {
                    shader->setUniform("_zTexel", z);
                }
108 109 110 111 112
                _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
                QuadRdr.renderQuad();
            }
            _fbo->detachAll();
            _fbo->deactivate();
113
            shader->deactivate();
114 115

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

}