1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "tga_utils.h"
8 
9 #include <fstream>
10 #include <iostream>
11 #include <limits.h>
12 #include <stdint.h>
13 #include <string>
14 
TGAImage()15 TGAImage::TGAImage()
16     : width(0), height(0), data(0)
17 {
18 }
19 
20 struct TGAHeader
21 {
22     uint8_t idSize;
23     uint8_t mapType;
24     uint8_t imageType;
25     uint16_t paletteStart;
26     uint16_t paletteSize;
27     uint8_t paletteEntryDepth;
28     uint16_t x;
29     uint16_t y;
30     uint16_t width;
31     uint16_t height;
32     uint8_t colorDepth;
33     uint8_t descriptor;
34 };
35 
36 #define INVERTED_BIT (1 << 5)
37 
38 template <typename dataType>
readBinary(std::ifstream & stream,dataType & item)39 void readBinary(std::ifstream &stream, dataType &item)
40 {
41     stream.read(reinterpret_cast<char *>(&item), sizeof(dataType));
42 }
43 
44 template <typename dataType>
readBinary(std::ifstream & stream,std::vector<dataType> & items)45 void readBinary(std::ifstream &stream, std::vector<dataType> &items)
46 {
47     stream.read(reinterpret_cast<char *>(items.data()), sizeof(dataType) * items.size());
48 }
49 
LoadTGAImageFromFile(const std::string & path,TGAImage * image)50 bool LoadTGAImageFromFile(const std::string &path, TGAImage *image)
51 {
52     std::ifstream stream(path, std::ios::binary);
53     if (!stream)
54     {
55         std::cerr << "error opening tga file " << path << " for reading.\n";
56         return false;
57     }
58 
59     TGAHeader header;
60     readBinary(stream, header.idSize);
61     readBinary(stream, header.mapType);
62     readBinary(stream, header.imageType);
63     readBinary(stream, header.paletteStart);
64     readBinary(stream, header.paletteSize);
65     readBinary(stream, header.paletteEntryDepth);
66     readBinary(stream, header.x);
67     readBinary(stream, header.y);
68     readBinary(stream, header.width);
69     readBinary(stream, header.height);
70     readBinary(stream, header.colorDepth);
71     readBinary(stream, header.descriptor);
72 
73     image->width = header.width;
74     image->height = header.height;
75 
76     size_t pixelComponentCount = header.colorDepth / CHAR_BIT;
77     std::vector<unsigned char> buffer(header.width * header.height * pixelComponentCount);
78     readBinary(stream, buffer);
79 
80     image->data.reserve(header.width * header.height);
81 
82     for (size_t y = 0; y < header.height; y++)
83     {
84         size_t rowIdx = ((header.descriptor & INVERTED_BIT) ? (header.height - 1 - y) : y) * header.width * pixelComponentCount;
85         for (size_t x = 0; x < header.width; x++)
86         {
87             size_t pixelIdx = rowIdx + x * pixelComponentCount;
88 
89             Byte4 pixel;
90             pixel[0] = (pixelComponentCount > 2) ? buffer[pixelIdx + 2] : 0;
91             pixel[2] = (pixelComponentCount > 0) ? buffer[pixelIdx + 0] : 0;
92             pixel[1] = (pixelComponentCount > 1) ? buffer[pixelIdx + 1] : 0;
93             pixel[3] = (pixelComponentCount > 3) ? buffer[pixelIdx + 3] : 255;
94 
95             image->data.push_back(pixel);
96         }
97     }
98 
99     std::cout << "loaded image " << path << ".\n";
100 
101     return true;
102 }
103 
LoadTextureFromTGAImage(const TGAImage & image)104 GLuint LoadTextureFromTGAImage(const TGAImage &image)
105 {
106     if (image.width > 0 && image.height > 0)
107     {
108         GLuint texture;
109         glGenTextures(1, &texture);
110         glBindTexture(GL_TEXTURE_2D, texture);
111         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
112         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
113         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
114         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(image.width), static_cast<GLsizei>(image.height), 0,
115                      GL_RGBA, GL_UNSIGNED_BYTE, image.data.data());
116         glGenerateMipmap(GL_TEXTURE_2D);
117         return texture;
118     }
119     else
120     {
121         return 0;
122     }
123 }
124