-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTGAImage.hpp
143 lines (122 loc) · 3.8 KB
/
TGAImage.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
#include <fstream>
#include "Image.hpp"
#include "File.hpp"
#include "Debug.hpp"
#include "Logger.hpp"
// https://stackoverflow.com/a/20596072/4811285
namespace img {
struct TGAImage : public Image {
TGAImage(const char *filename):
Image(filename)
{
init();
}
void init() {
FILE *file = fopen(filename.c_str(), "r");
if(file == nullptr) {
TERMINATE("bmp: unable to open file '%s'\n", filename.c_str());
}
sys::File::Lock fl(file);
uint8_t header[18] = {0};
constexpr static uint8_t is_decompressed_mask[12] = {0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
constexpr static uint8_t is_compressed_mask[12] = {0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
fread(&header, 1, sizeof(header), file);
bool is_compressed = false;
if(!memcmp(is_decompressed_mask, &header, sizeof(is_decompressed_mask))) {
// decompressed
int bpp8 = header[16];
int bpp = (bpp8 / 8);
width = header[13] * 256 + header[12];
height = header[15] * 256 + header[14];
size_t size = width * height * bpp;
if ((bpp8 != 24) && (bpp8 != 32)) {
fclose(file);
TERMINATE("tga: invalid file format: required 24 (RGB) or 32 (RGBA) bpp image\n");
}
is_compressed = false;
data = new unsigned char[size];
if(bpp == 4) {
format = Image::Format::RGBA;
} else if(bpp == 3) {
format = Image::Format::RGB;
} else if(bpp == 1) {
format = Image::Format::ALPHA8;
}
unsigned char *dpixel = data;
unsigned char pixel[bpp];
for(int i = 0; i < width * height; ++i) {
fread(pixel, 1, bpp, file);
if(bpp >= 3) {
dpixel[0] = pixel[2];
dpixel[1] = pixel[1];
dpixel[2] = pixel[0];
}
if(bpp == 4) {
dpixel[3] = pixel[3];
}
dpixel += bpp;
}
} else if(!memcmp(is_compressed_mask, &header, sizeof(is_compressed_mask))) {
// compressed
int bpp8 = header[16];
int bpp = (bpp8 / 8);
width = header[13] * 256 + header[12];
height = header[15] * 256 + header[14];
size_t size = ((width * bpp8 + 31) / 32) * 4 * height;
if ((bpp8 != 24) && (bpp8 != 32)) {
fclose(file);
TERMINATE("tga: invalid file format: required 24 (RGB) or 32 (RGBA) bpp image\n");
}
is_compressed = true;
data = new unsigned char[width * height * bpp];
if(bpp == 4) {
format = Image::Format::RGBA;
} else if(bpp == 3) {
format = Image::Format::RGB;
} else if(bpp == 1) {
format = Image::Format::ALPHA8;
}
unsigned char *dpixel = data;
while(dpixel < data + width * height*bpp) {
uint8_t chunk_header;
fread(&chunk_header, 1, sizeof(chunk_header), file);
uint8_t pixel[bpp];
if(chunk_header < 128) {
++chunk_header;
for(int i = 0; i < chunk_header; ++i) {
fread(pixel, 1, bpp, file);
if(bpp >= 3) {
dpixel[0] = pixel[2];
dpixel[1] = pixel[1];
dpixel[2] = pixel[0];
}
if(bpp == 4) {
dpixel[3] = pixel[3];
}
dpixel += bpp;
}
} else {
chunk_header -= 127;
fread(pixel, 1, bpp, file);
for(int i = 0; i < chunk_header; ++i) {
if(bpp >= 3) {
dpixel[0] = pixel[2];
dpixel[1] = pixel[1];
dpixel[2] = pixel[0];
}
if(bpp == 4) {
dpixel[3] = pixel[3];
}
dpixel += bpp;
}
}
};
} else {
TERMINATE("tga: invalid file format: required 24 (RGB) or 32 (RGBA) bpp image\n");
}
fl.drop();
fclose(file);
}
};
}