TextureData.cc 9.37 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
6
7
8
9
10
11
12
/***********************************************************************
 * Copyright 2011-2013 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/

#include <ACGL/OpenGL/Data/TextureData.hh>
#include <ACGL/Utils/Memory.hh>

using namespace ACGL;
using namespace ACGL::OpenGL;

13
14
GLsizei  TextureData::getPackAlignment() const
{
Robert Menzel's avatar
Robert Menzel committed
15
    size_t dataAlignment = Utils::pointerAlignment( pData );
16
    size_t rowAlignment  = Utils::pointerAlignment( pData + getBytesPerScanline() );
Robert Menzel's avatar
Robert Menzel committed
17
18
19
20
21
22
23
24
25

    return std::min( dataAlignment, rowAlignment ); //minimum of the data and the begining of the second row
}


GLsizei  TextureData::getNumberOfChannels() const
{
    return ACGL::OpenGL::getNumberOfChannels( format );
}
26
27


28
void TextureData::flipVertically()
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
    size_t scanlineInBytes = getBytesPerScanline();
    GLubyte *tmpScanLine = new GLubyte[ scanlineInBytes ];

    for (GLsizei line = 0; line < height/2; ++line) {
        size_t topLine    = line;
        size_t bottomLine = height - line - 1;
        void *topLinePtr    = pData + topLine*scanlineInBytes;
        void *bottomLinePtr = pData + bottomLine*scanlineInBytes;
        memcpy( tmpScanLine,   topLinePtr,    scanlineInBytes ); // top    -> tmp
        memcpy( topLinePtr,    bottomLinePtr, scanlineInBytes ); // bottom -> top
        memcpy( bottomLinePtr, tmpScanLine,   scanlineInBytes ); // tmp    -> bottom
    }

    delete[] tmpScanLine;
}


size_t TextureData::getBytesPerScanline() const
{
49
50
51
52
53
54
55
56
57
58
59
    return width*getNumberOfChannels()*getGLTypeSize(mType) + paddingBytesPerRow;
}


glm::vec4 TextureData::getTexel( glm::uvec2 _texCoord )
{
    // clamp negative to 0:
    _texCoord.x = std::max( 0u, _texCoord.x );
    _texCoord.y = std::max( 0u, _texCoord.y );

    // clamp values larger than the texture size to the maximum:
60
61
    _texCoord.x = std::min( _texCoord.x, (unsigned int) getWidth()  );
    _texCoord.y = std::min( _texCoord.y, (unsigned int) getHeight() );
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    // the byte offset into pData of the desired texel:
    size_t texelOffset  = _texCoord.y * getBytesPerScanline();
    texelOffset        += _texCoord.x * getNumberOfChannels()*getGLTypeSize(mType);

    // default values:
    glm::vec4 result = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);

    if ( mType == GL_BYTE ) {
        GLbyte *data = (GLbyte *) (pData+texelOffset);
        result.r = data[0]/128.0f; // to -1..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/128.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/128.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/128.0f;

    } else if ( mType == GL_UNSIGNED_BYTE ) {
78
        GLubyte *data = (GLubyte *) (pData+texelOffset);
79
80
81
82
83
84
        result.r = data[0]/255.0f; // to 0..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/255.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/255.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/255.0f;

    } else if ( mType == GL_SHORT ) {
85
        GLshort *data = (GLshort *) (pData+texelOffset);
86
87
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        result.r = data[0]/32768.0f; // to -1..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/32768.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/32768.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/32768.0f;

    } else if ( mType == GL_UNSIGNED_SHORT ) {
        GLushort *data = (GLushort *) (pData+texelOffset);
        result.r = data[0]/65535.0f; // to 0..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/65535.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/65535.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/65535.0f;

    } else if ( mType == GL_INT ) {
        GLint *data = (GLint *) (pData+texelOffset);
        result.r = data[0]/2147483648.0f; // to -1..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/2147483648.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/2147483648.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/2147483648.0f;

    } else if ( mType == GL_UNSIGNED_INT ) {
        GLuint *data = (GLuint *) (pData+texelOffset);
        result.r = data[0]/4294967295.0f; // to 0..1
        if ( getNumberOfChannels() > 1 ) result.g = data[1]/4294967295.0f;
        if ( getNumberOfChannels() > 2 ) result.b = data[2]/4294967295.0f;
        if ( getNumberOfChannels() > 3 ) result.a = data[3]/4294967295.0f;

    } else if ( mType == GL_FLOAT ) {
        GLfloat *data = (GLfloat *) (pData+texelOffset);
        result.r = data[0];
        if ( getNumberOfChannels() > 1 ) result.g = data[1];
        if ( getNumberOfChannels() > 2 ) result.b = data[2];
        if ( getNumberOfChannels() > 3 ) result.a = data[3];

    } else if ( mType == GL_DOUBLE ) {
        GLdouble *data = (GLdouble *) (pData+texelOffset);
        result.r = (float) data[0];
        if ( getNumberOfChannels() > 1 ) result.g = (float) data[1];
        if ( getNumberOfChannels() > 2 ) result.b = (float) data[2];
        if ( getNumberOfChannels() > 3 ) result.a = (float) data[3];

    } else {
        ACGL::Utils::error() << "datatype " << mType << " not supported for getTexel! Return (0,0,0,1)." << std::endl;
    }

    return result;
}

void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
{
Robert Menzel's avatar
Robert Menzel committed
135
    assert( pData && "can't set texels if there is no data store, define data using setData()" );
136
137
138
139
140
    // clamp negative to 0:
    _texCoord.x = std::max( 0u, _texCoord.x );
    _texCoord.y = std::max( 0u, _texCoord.y );

    // clamp values larger than the texture size to the maximum:
141
142
    _texCoord.x = std::min( _texCoord.x, (unsigned int) getWidth()  );
    _texCoord.y = std::min( _texCoord.y, (unsigned int) getHeight() );
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    // the byte offset into pData of the desired texel:
    size_t texelOffset  = _texCoord.y * getBytesPerScanline();
    texelOffset        += _texCoord.x * getNumberOfChannels()*getGLTypeSize(mType);

    if ( mType == GL_BYTE ) {
        GLbyte *data = (GLbyte *) (pData+texelOffset);

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(128.0f) );
        color = glm::clamp( color, glm::ivec4(-128), glm::ivec4(127) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_UNSIGNED_BYTE ) {
160
        GLubyte *data = (GLubyte *) (pData+texelOffset);
161
162
163
164
165
166
167
168
169
170

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(255.0f) );
        color = glm::clamp( color, glm::ivec4(0), glm::ivec4(255) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_SHORT ) {
171
        GLshort *data = (GLshort *) (pData+texelOffset);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(32768.0f) );
        color = glm::clamp( color, glm::ivec4(-32768), glm::ivec4(32767) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_UNSIGNED_SHORT ) {
        GLushort *data = (GLushort *) (pData+texelOffset);

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(65535.0f) );
        color = glm::clamp( color, glm::ivec4(0), glm::ivec4(65535) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_INT ) {
        GLint *data = (GLint *) (pData+texelOffset);

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(2147483648.0f) );
        color = glm::clamp( color, glm::ivec4(-2147483648), glm::ivec4(2147483647) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_UNSIGNED_INT ) {
        GLuint *data = (GLuint *) (pData+texelOffset);

        glm::ivec4 color = glm::ivec4( _color * glm::vec4(4294967295.0f) );
        color = glm::clamp( color, glm::ivec4(0), glm::ivec4(4294967295) );

        data[0] = color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = color.a;

    } else if ( mType == GL_FLOAT ) {
        GLfloat *data = (GLfloat *) (pData+texelOffset);

        data[0] = _color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = _color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = _color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = _color.a;

    } else if ( mType == GL_DOUBLE ) {
        GLdouble *data = (GLdouble *) (pData+texelOffset);

        data[0] = (float) _color.r;
        if ( getNumberOfChannels() > 1 ) data[1] = (float) _color.g;
        if ( getNumberOfChannels() > 2 ) data[2] = (float) _color.b;
        if ( getNumberOfChannels() > 3 ) data[3] = (float) _color.a;

    } else {
        ACGL::Utils::error() << "datatype " << mType << " not supported for setTexel!" << std::endl;
    }
233
234
}