2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

texturereadertga.cpp 8.63 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

    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;
    }

106
    size_t bpp = static_cast<size_t>(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
    size_t numBytes = static_cast<size_t>(bpp * cgt::hmul(dimensions));
132
    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
            LERROR("Failed to read uncompressed image! file: " << filename);
            delete file;
140
            delete [] buffer;
schultezub's avatar
schultezub committed
141
142
            return 0;
        }
143
144
    } 
    else {
schultezub's avatar
schultezub committed
145
146
147
148
149
        // file is compressed
        LDEBUG("Reading compressed TGA file " << filename << " ...");

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

    file->close();
    delete file;

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

185
186
187
    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
188

189
    delete [] buffer;
schultezub's avatar
schultezub committed
190
191
192
    return t;
}

193
Texture* TextureReaderTga::loadTextureArray(const std::vector<std::string>& filenames, Texture::Filter filter) {
194
195
196
    if (filenames.empty())
        return nullptr;

197
    GLubyte* data = nullptr;
198
199
    GLint format = 0;
    GLint internalFormat = 0;
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    GLenum dataType = GL_UNSIGNED_BYTE;
    size_t numBytesPerTexture = 0;
    cgt::ivec3 dimensions;

    // quick and dirty hack with bad performance (unnecessary copies)
    for (size_t i = 0; i < filenames.size(); ++i) {
        Texture* tmp = loadTexture(filenames[i], filter);

        if (data == nullptr) {
            switch (tmp->getNumChannels()) {
                case 1:
                    format = GL_RED;
                    LDEBUG("GL_RED");
                    break;
                case 3:
                    format = GL_RGB;
                    LDEBUG("RGB");
                    break;
                case 4:
                    format = GL_RGBA;
                    LDEBUG("RGBA");
                    break;
                default:
                    cgtAssert(false, "Should not reach this! Wrong number of channels.");
                    break;
            }

            internalFormat = tmp->getInternalFormat();
            dimensions = tmp->getDimensions();
            numBytesPerTexture = cgt::hmul(dimensions) * tmp->getNumChannels();
            data = new GLubyte[filenames.size() * numBytesPerTexture];
        }
        else {
            if (dimensions != tmp->getDimensions() || internalFormat != tmp->getInternalFormat()) {
                LERROR("Dimensions of texture array textures or internal formats mismatch, aborting!");
                delete [] data;
                delete tmp;
                return nullptr;
            }
        }

        GLubyte* buffer = tmp->downloadTextureToBuffer(format, dataType);
        memcpy(data + (i * numBytesPerTexture), buffer, numBytesPerTexture);
        delete [] buffer;
        delete tmp;
    }

    Texture* toReturn = new Texture(GL_TEXTURE_2D_ARRAY, cgt::ivec3(dimensions.xy(), int(filenames.size())), internalFormat, data, format, dataType, filter);
    delete data;
    return toReturn;
}

252
} // namespace cgt