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