TextureData.hh 6.7 KB
Newer Older
1 2 3 4 5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
6 7 8 9

#ifndef ACGL_OPENGL_DATA_TEXTUREDATA_HH
#define ACGL_OPENGL_DATA_TEXTUREDATA_HH

Robert Menzel's avatar
Robert Menzel committed
10 11 12 13 14
/**
 * TextureData holds the data of a 1,2 or 3 dimensional image to be used as a texture.
 * This structure is used for texture loading.
 */

15 16 17 18
#include <ACGL/ACGL.hh>

#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
19
#include <ACGL/OpenGL/Data/ColorSpace.hh>
Robert Menzel's avatar
Robert Menzel committed
20
#include <ACGL/OpenGL/Tools.hh>
21
#include <ACGL/Math/Math.hh>
22 23 24 25 26 27 28 29 30 31 32

namespace ACGL{
namespace OpenGL{

class TextureData
{
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    TextureData(void)
33 34 35 36 37
    :   mData(NULL),
        mWidth(0),
        mHeight(0),
        mDepth(0),
        mFormat(GL_RGBA),
38
        mType(GL_UNSIGNED_BYTE),
39
        mPaddingBytesPerRow(0),
Janis Born's avatar
Janis Born committed
40
        mColorSpace(ColorSpace::AUTO_DETECT)
41 42 43
    {}
    virtual ~TextureData(void)
    {
44
        delete[] mData;
45 46 47 48 49 50
    }

    // ========================================================================================================= \/
    // ================================================================================================= GETTERS \/
    // ========================================================================================================= \/
public:
51 52
    //! pointer to the raw pixel data
    GLubyte* getData() const { return mData; }
53

54 55
    //! width in pixels
    GLsizei getWidth() const { return mWidth; }
56

57 58
    //! height in pixels
    GLsizei getHeight() const { return mHeight; }
59

60 61
    //! depth in pixels
    GLsizei getDepth() const { return mDepth; }
62

63 64
    //! channels etc. (e.g. GL_RGB)
    GLenum  getFormat() const { return mFormat; }
65 66

    //! data type (e.g. GL_BYTE)
67
    GLenum getType() const { return mType; }
68 69

    //! each line can have a few bytes of padding, number of bytes is returned
70 71 72 73
    GLsizei getPadding() const { return mPaddingBytesPerRow; }

    //! the color space in which the data is represented
    ColorSpace getColorSpace() const { return mColorSpace; }
74

75
    //! in pixel
76
    glm::uvec3 getSize() const { return glm::uvec3( mWidth, mHeight, mDepth ); }
Robert Menzel's avatar
Robert Menzel committed
77

78 79 80
    //! in bytes
    size_t getSizeInBytes() const;

81
    //! the byte alignment of each pixel row, (e.g. 1, 2, 4, 8 bytes)
82
    GLsizei  getPackAlignment() const;
Robert Menzel's avatar
Robert Menzel committed
83

84 85 86 87 88
    //! 1, 2, 3 or 4 based on format
    GLsizei getNumberOfChannels() const;

    //! A recommended value for the internalFormat enum of a Texture object. Based on the format, type and color space
    GLenum getRecommendedInternalFormat() const;
89

90 91
    //! flips the image vertically as some image formats have a different coordinate system as OpenGL has. (flip it upside down)
    void flipVertically();
92 93 94 95 96 97 98 99 100 101

    //! returns the texel converted to float (0..1 in case the data was int, -1..1 in case it was unsigned int).
    //! If the Image had less than 4 components it get's filled with (0,0,0,1)
    //! _texCoord is NOT normalized to 0..1! and gets clamped to the actual image size
    //! NOTE: this might be slow, for performance get the raw pointer and work on that!
    glm::vec4 getTexel( glm::uvec2 _texCoord );

    //! sets one texel, if the texture has less color components than 4, the superfluous components get ignored.
    //! in case the texture is int, the values from 0..1 will get scaled and clamped if needed.
    void setTexel( glm::uvec2 _texCoord, glm::vec4 _color );
102 103 104 105

    //! returns true if the data is stored as one of the compressed formats in OpenGL, the compression type is stored in mFormat, mType might be invalid
    bool dataIsCompressed() const;

106 107 108 109
    // ========================================================================================================= \/
    // ================================================================================================= SETTERS \/
    // ========================================================================================================= \/
public:
110
    //! mData has to be created by new GLubyte[...] and will get deleted by this TextureData object!
111
    void setData      (GLubyte* _data)          { mData   = _data; }
112 113 114 115
    void setWidth     (GLsizei  _width)         { mWidth  = _width; }
    void setHeight    (GLsizei  _height)        { mHeight = _height; }
    void setDepth     (GLsizei  _depth)         { mDepth  = _depth; }
    void setFormat    (GLenum   _format)        { mFormat = _format; }
116
    void setType      (GLenum   _type)          { mType   = _type; }
117 118 119
    void setPadding   (GLsizei _padding)        { mPaddingBytesPerRow = _padding; }
    void setColorSpace(ColorSpace _colorSpace)  { mColorSpace = _colorSpace; }
    void setSize      (const glm::uvec3& _size) { mWidth = _size.x; mHeight = _size.y; mDepth = _size.z; }
120

121
    //! deletes the data attached to this object
122 123 124 125
    void deleteData() { delete[] mData; mData = NULL; }

private:
    size_t getBytesPerScanline() const;
126

127 128 129
    //! returns the size per texel in bits(!) - bits to support compressed types!
    size_t getTexelSizeInBits() const;

130 131 132
    // ========================================================================================================= \/
    // ================================================================================================== FIELDS \/
    // ========================================================================================================= \/
133
    GLubyte*   mData;               // TODO: make this a std::unique_ptr to allow custom deleters
134 135 136 137
    GLsizei    mWidth;
    GLsizei    mHeight;
    GLsizei    mDepth;
    GLenum     mFormat;             // channel types and count
138
    GLenum     mType;               // data type, invalid if the format is a compressed format
139 140 141
    GLsizei    mPaddingBytesPerRow; // number of padding bytes added per row: glReadPixel can read with padding and
                                    // some image writers support/need this as well (e.g. QT)
    ColorSpace mColorSpace;
142 143
};

Robert Menzel's avatar
Robert Menzel committed
144
ACGL_SMARTPOINTER_TYPEDEFS(TextureData)
145

Janis Born's avatar
Janis Born committed
146 147 148 149 150
//! Converts the texture data in _from to the target format and type given in
//! _to. Overwrites width, height, and depth in _to. Old texture data is removed
//! and new memory is allocated.
void convertTextureData(const SharedTextureData& _from, const SharedTextureData& _to);

151 152 153 154
} // OpenGL
} // ACGL

#endif // ACGL_OPENGL_DATA_TEXTUREDATA_HH