texturereadertga.cpp 6.41 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
/**********************************************************************
 *                                                                    *
3
4
5
6
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2014               *
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8
9
10
11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
#include "cgt/texturereadertga.h"
schultezub's avatar
schultezub committed
30

31
32
#include "cgt/logmanager.h"
#include "cgt/filesystem.h"
schultezub's avatar
schultezub committed
33
34
35

#include <cstring>

36
namespace cgt {
schultezub's avatar
schultezub committed
37
38
39
40
41

//------------------------------------------------------------------------------
// TextureReaderTga
//------------------------------------------------------------------------------

42
const std::string TextureReaderTga::loggerCat_("cgt.Texture.Reader.Tga");
schultezub's avatar
schultezub committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

TextureReaderTga::TextureReaderTga() {
    name_ = "TGA Reader";
    extensions_.push_back("tga");
}

Texture* TextureReaderTga::loadTexture(const std::string& filename, Texture::Filter filter,
                                       bool compress, bool keepPixels, bool createOGLTex,
                                       bool textureRectangle)
{

    GLubyte TGAheader[12];
    GLubyte header[6];
//    GLuint  bytesPerPixel;//FIXME: this is never initialized!

    File* file = FileSys.open(filename);

    // Check if file is open
    if (!file) {
        LERROR("Failed to open file " << filename);
        return 0;
    }

    if (!file->isOpen()) {
        LERROR("Failed to open file " << filename);
        delete file;
        return 0;
    }

    size_t len = file->size();

    // check if file is empty
    if (len == 0) {
        delete file;
        return 0;
    }

    if (file->read(reinterpret_cast<char*>(&TGAheader), sizeof(TGAheader)) != sizeof(TGAheader) ||
        file->read(reinterpret_cast<char*>(&header), sizeof(header)) != sizeof(header))
    {
        delete file;
        LERROR("Failed to read header! file: " << filename);
        return 0;
    }

    ivec3 dimensions;
    dimensions.x = header[1] * 256 + header[0];           // determine the TGA width  (highbyte*256+lowbyte)
    dimensions.y = header[3] * 256 + header[2];           // determine the TGA height (highbyte*256+lowbyte)
    dimensions.z = 1;
92
    LDEBUG("Image dimensions: " << dimensions);
schultezub's avatar
schultezub committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106

    if (dimensions.x <= 0 || dimensions.y <= 0) {
        delete file;
        LERROR("wrong dimensions: " << dimensions << " file: " << filename);
        return 0;
    }

    if (header[4] != 24 && header[4] != 32) {
        delete file;
        LERROR("Illegal bpp! file: " << filename);
        return 0;
    }

    int bpp = header[4];
107
    bpp /= 8;  // divide by 8 to get the bytes per pixel
schultezub's avatar
schultezub committed
108

109
110
111
112
113
114
    GLint format = GL_RGBA;
    switch (bpp) {
        case 1:
            format = GL_RED;
            LDEBUG("GL_RED");
            break;
schultezub's avatar
schultezub committed
115
        case 3:
116
            format = GL_RGB;
schultezub's avatar
schultezub committed
117
118
119
            LDEBUG("RGB");
            break;
        case 4:
120
            format = GL_RGBA;
schultezub's avatar
schultezub committed
121
122
123
124
125
            LDEBUG("RGBA");
            break;

        default:
            LERROR("unsupported bpp " << filename);
126
            break;
schultezub's avatar
schultezub committed
127
128
    }

129
    GLenum dataType = GL_UNSIGNED_BYTE;
schultezub's avatar
schultezub committed
130

131
132
    int numBytes = bpp * cgt::hmul(dimensions);
    GLubyte* buffer = new GLubyte[numBytes];
schultezub's avatar
schultezub committed
133
134
135
136

    if (TGAheader[2] == 2) {
        // file is not compressed
        LDEBUG("Reading uncompressed TGA file...");
137
        if (file->read(buffer, numBytes) != numBytes) {
schultezub's avatar
schultezub committed
138
139
140
141
            LERROR("Failed to read uncompressed image! file: " << filename);
            delete file;
            return 0;
        }
142
143
    } 
    else {
schultezub's avatar
schultezub committed
144
145
146
147
148
        // file is compressed
        LDEBUG("Reading compressed TGA file " << filename << " ...");

        //TODO: error handling
        unsigned char chunk[4];
149
150
        unsigned char* at = buffer;
        for (int bytesDone=0; bytesDone < numBytes; /* nothing here */) {
schultezub's avatar
schultezub committed
151
152
153
154
155
            unsigned char packetHead;
            file->read(reinterpret_cast<char*>(&packetHead), 1);
            if (packetHead > 128) {
                //RLE
                packetHead -= 127;
156
                file->read(reinterpret_cast<char*>(&chunk), bpp);
schultezub's avatar
schultezub committed
157
                for (unsigned char b=0; b < packetHead; b++) {
158
159
160
                        std::memcpy(at, chunk, bpp);
                        bytesDone += bpp;
                        at += bpp;
schultezub's avatar
schultezub committed
161
162
163
164
                }
            } else {
                //RAW
                packetHead++;
165
166
167
                file->read(reinterpret_cast<char*>(at), bpp * packetHead);
                bytesDone += packetHead * bpp;
                at += packetHead * bpp;
schultezub's avatar
schultezub committed
168
169
170
171
172
173
174
175
            }
        }
    }

    file->close();
    delete file;

    // switch r & b
176
177
178
179
180
181
    if (bpp >= 3) {
        GLubyte* at = buffer;
        while (at - buffer < numBytes) {
            std::swap(at[0], at[2]);
            at += bpp;
        }
schultezub's avatar
schultezub committed
182
183
    }

184
185
186
    Texture* t = new Texture(GL_TEXTURE_2D, dimensions, Texture::calcInternalFormat(format, dataType), filter);
    t->uploadTexture(buffer, format, dataType);
    t->setName(filename);
schultezub's avatar
schultezub committed
187
188
189
190

    return t;
}

191
} // namespace cgt