weaklytypedpointer.h 7.1 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
26
27
28
#ifndef WEAKLYTYPEDPOINTER_H__
#define WEAKLYTYPEDPOINTER_H__

#include "tgt/assert.h"
29
30
31
#include "tgt/logmanager.h"
#include "tgt/texture.h"
#include "tgt/tgt_gl.h"
schultezub's avatar
schultezub committed
32
#include "tgt/types.h"
schultezub's avatar
schultezub committed
33

34
35
36
37
38
#ifdef CAMPVIS_HAS_MODULE_DEVIL
#include <IL/il.h>
#include <IL/ilu.h>
#endif

schultezub's avatar
schultezub committed
39
namespace campvis {
schultezub's avatar
schultezub committed
40

schultezub's avatar
schultezub committed
41
42
    /**
     * Struct for handling void pointers slightly more typesafe.
43
44
     * Therfore it stores an enum value together with the pointer for deducing its data type.
     * \note    WeaklyTypedPointer takes _not_ take ownership of its pointer, hence it won't take care of deleting it!
45
     */
schultezub's avatar
schultezub committed
46
    struct WeaklyTypedPointer {
schultezub's avatar
schultezub committed
47
48
49
        /**
         * Base data type.
         **/
50
        enum BaseType {
schultezub's avatar
schultezub committed
51
52
53
54
55
56
            UINT8,      ///< unsigned 8 bit integer
            INT8,       ///< signed 8 bit integer
            UINT16,     ///< unsigned 16 bit integer
            INT16,      ///< signed 16 bit integer
            UINT32,     ///< unsigned 32 bit integer
            INT32,      ///< signed 32 bit integer
schultezub's avatar
schultezub committed
57
            FLOAT      ///< float
schultezub's avatar
schultezub committed
58
59
60
        };

        /**
schultezub's avatar
schultezub committed
61
         * Returns the number of bytes occupied by one element of the type \a bt with \a numChannels channels.
62
63
64
         * 
         * \note    There is a compiletime version in WeaklyTypedPointerTraits.
         * \sa      WeaklyTypedPointerTraits::numBytes()
schultezub's avatar
schultezub committed
65
66
67
         * \param   bt          Image base type
         * \param   numChannels Number of channels per element.
         * \returns The number of bytes occupied by one image element with \a numChannels channels of the type \a bt.
68
         */
schultezub's avatar
schultezub committed
69
        static size_t numBytes(BaseType bt, size_t numChannels = 1);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

        /**
         * Calculates the number of channels for the given OpenGL format.
         * Kinda inverse function of getGlFormat().
         * \sa      WeaklyTypedPointer::getGlFormat()
         * \param   glFormat    OpenGL format
         * \return  Number of channels of the given OpenGL format.
         */
        static size_t numChannels(GLint glFormat);

        /**
         * Calculates the base type for the given OpenGL data type.
         * Kinda inverse function of getGlDataType().
         * \sa      WeaklyTypedPointer::getGlDataTypet()
         * \param   glDataType  OpenGL format
         * \return  Base type of the given OpenGL data type.
         */
        static WeaklyTypedPointer::BaseType baseType(GLenum glDataType);

// = Starting with the real stuff =================================================================
schultezub's avatar
schultezub committed
90
91

        /**
schultezub's avatar
schultezub committed
92
         * Constructs a new weakly typed pointer.
93
94
         * \param pt            Base data type of the pointer.
         * \param numChannels   Number of channels, must be in [1, 4]!
95
         * \param ptr           Pointer to the data, WeaklyTypedPointer will _not_ take ownership of it.
schultezub's avatar
schultezub committed
96
         */
97
98
99
        WeaklyTypedPointer(BaseType pt, size_t numChannels, void* ptr);

        /**
100
101
102
103
104
         * Empty default constructor, you have to initialize everything yourself.
         */
        WeaklyTypedPointer();

        /**
105
106
107
108
         * Destructor, does _not_ delete the handled pointer!
         */
        virtual ~WeaklyTypedPointer();

109
110
        bool operator==(const WeaklyTypedPointer& rhs) const;

schultezub's avatar
schultezub committed
111

schultezub's avatar
schultezub committed
112
113
114
115
        /**
         * Returns the number of bytes occupied by one element of the type of this pointer.
         * \returns The number of bytes occupied by one element of the type of this pointer.
         */
116
117
118
119
120
        size_t getNumBytesPerElement() const;

        GLint getGlFormat() const;

        GLenum getGlDataType() const;
schultezub's avatar
schultezub committed
121

122
123
        GLint getGlInternalFormat() const;

124
125
126
127
        bool isInteger() const;

        bool isSigned() const;

128
129
130
131
132
133
#ifdef CAMPVIS_HAS_MODULE_DEVIL
        ILenum getIlFormat() const;

        ILenum getIlDataType() const;
#endif

schultezub's avatar
schultezub committed
134

135
        BaseType _baseType;         ///< Base data type of the pointer
136
        size_t _numChannels;        ///< Number of channels, must be in [1, 4]!
schultezub's avatar
schultezub committed
137
        void* _pointer;             ///< Pointer to the data
138
139
140
141

    private:

        static const std::string loggerCat_;
schultezub's avatar
schultezub committed
142
143
    };

144
// = Some traits for WeaklyTypedPointers ==========================================================
145
146
147
148
149

    /**
     * Some compile time traits for WeaklyTypedPointers.
     * \tparam  pt  Data type to check.
     */
150
    template<WeaklyTypedPointer::BaseType pt>
151
152
153
154
155
156
    struct WeaklyTypedPointerTraits {
        /**
         * Returns the number of bytes occupied by one element of the given type.
         * 
         * \note    There is a runtime version in WeaklyTypedPointer.
         * \sa      WeaklyTypedPointer::numBytes()
157
         * \tparam  bt  Data type to check.
158
159
160
161
162
163
164
165
         * \returns The number of bytes occupied by one element of the given type.
         */
        static size_t numBytes() { return 0; };
    };

// - Template Specialization ----------------------------------------------------------------------

    template<>
schultezub's avatar
schultezub committed
166
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT8> {
167
        static size_t numBytes() { return 1; };
168
169
170
    };

    template<>
schultezub's avatar
schultezub committed
171
172
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT8> {
        static size_t numBytes() { return 1; };
173
174
175
    };

    template<>
schultezub's avatar
schultezub committed
176
177
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT16> {
        static size_t numBytes() { return 2; };
178
179
180
    };

    template<>
schultezub's avatar
schultezub committed
181
182
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT16> {
        static size_t numBytes() { return 2; };
183
184
185
    };

    template<>
schultezub's avatar
schultezub committed
186
187
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT32> {
        static size_t numBytes() { return 4; };
188
189
190
    };

    template<>
schultezub's avatar
schultezub committed
191
192
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT32> {
        static size_t numBytes() { return 4; };
193
194
195
196
197
198
199
    };

    template<>
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::FLOAT> {
        static size_t numBytes() { return sizeof(float); };
    };

schultezub's avatar
schultezub committed
200
201
202
}

#endif // WEAKLYTYPEDPOINTER_H__