glintensityquantizer.cpp 5.22 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 "glintensityquantizer.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 47 48 49 50 51 52 53 54 55 56 57 58

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

    GlIntensityQuantizer::GlIntensityQuantizer(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "GlIntensityQuantizer.out", DataNameProperty::WRITE)
        , p_numberOfBins("NumberOfBins", "Number of Bins", 8, 1, 256)
        , p_transferFunction("TransferFunction", "Transfer Function", new Geometry1DTransferFunction(256))
        , _shader(0)
    {
        addProperty(p_inputImage);
        addProperty(p_outputImage);
        addProperty(p_numberOfBins);
        addProperty(p_transferFunction);

        Geometry1DTransferFunction* gtf = static_cast<Geometry1DTransferFunction*>(p_transferFunction.getTF());
59
        gtf->addGeometry(TFGeometry1D::createQuad(cgt::vec2(0.f, 1.f), cgt::col4(0, 0, 0, 0), cgt::col4(255, 255, 255, 255)));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }

    GlIntensityQuantizer::~GlIntensityQuantizer() {

    }

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

        _shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glintensityquantizer.frag", "");
    }

    void GlIntensityQuantizer::deinit() {
        ShdrMgr.dispose(_shader);
        VisualizationProcessor::deinit();
    }

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

        if (img != 0) {
81
            cgt::ivec3 size = img->getSize();
82

83
            cgt::TextureUnit inputUnit, tfUnit;
84 85 86
            inputUnit.activate();

            // create texture for result
87
            cgt::Texture* resultTexture = new cgt::Texture(GL_TEXTURE_3D, size, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR);
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

            // activate shader and bind textures
            _shader->activate();
            _shader->setUniform("_numberOfBins", p_numberOfBins.getValue());
            img->bind(_shader, inputUnit);
            p_transferFunction.getTF()->bind(_shader, tfUnit);

            // 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) {
                float zTexCoord = static_cast<float>(z)/static_cast<float>(size.z) + .5f/static_cast<float>(size.z);
                _shader->setUniform("_zTexCoord", zTexCoord);
                _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
                QuadRdr.renderQuad();
            }
            _fbo->detachAll();
            _fbo->deactivate();
            _shader->deactivate();

            // put resulting image into DataContainer
            ImageData* id = new ImageData(3, size, img.getImageData()->getNumChannels());
            ImageRepresentationGL::create(id, resultTexture);
            const ImageMappingInformation& imi = img->getParent()->getMappingInformation();
114
            id->setMappingInformation(ImageMappingInformation(img->getSize(), imi.getOffset(), imi.getVoxelSize(), imi.getCustomTransformation()));
115 116
            data.addData(p_outputImage.getValue(), id);

117
            cgt::TextureUnit::setZeroUnit();
118 119 120
            LGL_ERROR;
        }
        else {
121
            LDEBUG("No suitable input image found.");
122 123 124 125
        }
    }

}