devilimagewriter.cpp 4.89 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
// ================================================================================================
// 
// 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 "devilimagewriter.h"


#include <cstring>

30
31
32
33
34
#include "cgt/logmanager.h"
#include "cgt/filesystem.h"
#include "cgt/shadermanager.h"
#include "cgt/texturereaderdevil.h"
#include "cgt/textureunit.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48

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

#include "core/tools/stringutils.h"


namespace campvis {
    const std::string DevilImageWriter::loggerCat_ = "CAMPVis.modules.io.DevilImageWriter";

    DevilImageWriter::DevilImageWriter()
        : AbstractProcessor()
        , p_inputImage("InputImage", "Input Image ID", "DevilImageWriter.input", DataNameProperty::READ)
49
50
        , p_url("Url", "Image URL", "", StringProperty::SAVE_FILENAME)
        , p_writeDepthImage("WriteDepthImage", "Write Depth Image", false)
51
52
53
    {
        addProperty(p_inputImage);
        addProperty(p_url);
54
        addProperty(p_writeDepthImage);
55
56
57
58
59
60
61
62
63
64
    }

    DevilImageWriter::~DevilImageWriter() {
    }

    void DevilImageWriter::updateResult(DataContainer& data) {
        ScopedTypedData<RenderData> image(data, p_inputImage.getValue());

        if (image != 0) {
            std::string filename = p_url.getValue();
65
66
            std::string extension = cgt::FileSystem::fileExtension(filename);
            std::string filebase = cgt::FileSystem::fullBaseName(filename);
67
68
            if (extension.empty()) {
                LINFO("Filename has no extension, defaulting to .PNG.");
69
                extension = "png";
70
71
72
73
74
75
76
77
78
79
            }

            for (size_t i = 0; i < image->getNumColorTextures(); ++i) {
                const ImageRepresentationLocal* id = image->getColorTexture(i)->getRepresentation<ImageRepresentationLocal>(true);
                if (id == 0) {
                    LERROR("Could not download color texture " << i << " from RenderData, skipping.");
                    continue;
                }

                WeaklyTypedPointer wtp = id->getWeaklyTypedPointer();
80
                writeIlImage(wtp, id->getSize().xy(), filebase + ((image->getNumColorTextures() > 1) ? StringUtils::toString(i) : "") + "." + extension);
81
            }
82
            if (p_writeDepthImage.getValue() && image->hasDepthTexture()) {
83
84
85
86
87
88
                const ImageRepresentationLocal* id = image->getDepthTexture()->getRepresentation<ImageRepresentationLocal>(true);
                if (id == 0) {
                    LERROR("Could not download depth texture from RenderData, skipping.");
                }
                else {
                    WeaklyTypedPointer wtp = id->getWeaklyTypedPointer();
89
                    writeIlImage(wtp, id->getSize().xy(), filebase + ".depth." + extension);
90
91
92
93
94
95
96
97
                }
            }
        }
        else {
            LDEBUG("Could not load RenderData with given ID from pipeline.");
        }
    }

98
    void DevilImageWriter::writeIlImage(const WeaklyTypedPointer& wtp, const cgt::ivec2& size, const std::string& filename) const {
99
100
101
102
103
104
105
106
107
108
109
110
        // create Devil image from image data and write it to file
        ILuint img;
        ilGenImages(1, &img);
        ilBindImage(img);

        // put pixels into IL-Image
        ilTexImage(size.x, size.y, 1, static_cast<ILubyte>(wtp._numChannels), wtp.getIlFormat(), wtp.getIlDataType(), wtp._pointer);
        ilEnable(IL_FILE_OVERWRITE);
        ilResetWrite();
        ILboolean success = ilSaveImage(filename.c_str());
        ilDeleteImages(1, &img);

111
        if (! success) {
112
113
            ILenum errorcode;
            while ((errorcode = ilGetError()) != IL_NO_ERROR) {
114
                LERROR("Error while writing '" << filename << "': "<< (errorcode));
115
116
            } 
        }
117

118
119
    }

120
}