-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
226 lines (183 loc) · 5.44 KB
/
Image.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
#include "Image.h"
#include "Debug.h"
#include <iostream>
#include <fstream>
#include "GLIncludes.h"
ERR_CODE Image::load(const std::string &file)
{
name = file;
std::string file_extension = extension(file);
if (strieq(file_extension, "TGA"))
return __loadTGA(file);
else
return INVALID_EXTENSION;
}
ERR_CODE Image::loadTGA(const std::string &file)
{
name = file;
std::string file_extension = extension(file);
if (strieq(file_extension, "TGA"))
return __loadTGA(file);
else
return INVALID_EXTENSION;
}
ERR_CODE Image::__loadTGA(const std::string &filename) //TODO: load other formats 6
{
ASSERT(!pixels);
name = filename;
FILE *file = file_open(filename.c_str(), "rb");
if (!file)
return FILE_NOT_FOUND;
GLubyte uncompressed_top_header[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
GLubyte compressed_top_header[12] = { 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
GLubyte real_top_header[12];
fread(real_top_header, 1, 12, file);
if (memcmp(real_top_header, uncompressed_top_header, 12) == 0)
return __loadUncompressedTGA(file);
else if (memcmp(real_top_header, compressed_top_header, 12) == 0)
return __loadCompressedTGA(file);
else
{
fclose(file);
return UNSUPPORTED_FILE_TYPE;
}
}
ERR_CODE Image::__loadUncompressedTGA(FILE *file)
{
GLubyte header[6];
fread(header, 1, 6, file);
if (header[4] != 24 && header[4] != 32)
{
fclose(file);
return BAD_BYTES_PER_PIXEL;
}
bpp = header[4] / 8;
width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];
int filesize = width * height * bpp;
pixels = new GLubyte[filesize];
int bytesRead = fread(pixels, 1, filesize, file);
if (bytesRead != filesize)
{
fclose(file);
return CORRUPTED_FILE;
}
GLubyte temp;
for (int c = 0; c < filesize; c += bpp)
{
temp = pixels[c];
pixels[c] = pixels[c + 2];
pixels[c + 2] = temp;
}
fclose(file);
return NONE;
}
ERR_CODE Image::__loadCompressedTGA(FILE *file)
{
GLubyte header[6];
fread(header, 1, 6, file);
if (header[4] != 24 && header[4] != 32)
{
fclose(file);
return BAD_BYTES_PER_PIXEL;
}
bpp = header[4] / 8;
width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];
int filesize = width * height * bpp;
pixels = new GLubyte[filesize];
GLubyte *color_buffer = new GLubyte[bpp];
GLuint current_pixel = 0, current_byte = 0, pixel_count = width * height;
do
{
GLubyte chunk_header;
if (fread(&chunk_header, sizeof(GLubyte), 1, file) == 0)
{
fclose(file);
return CORRUPTED_FILE;
}
if (chunk_header < 128)
{
++chunk_header;
for (int counter = 0; counter < chunk_header; counter++)
{
if (fread(color_buffer, 1, bpp, file) != bpp)
{
fclose(file);
if (color_buffer)
delete[] color_buffer;
return CORRUPTED_FILE;
}
pixels[current_byte] = color_buffer[2];
pixels[current_byte + 1] = color_buffer[1];
pixels[current_byte + 2] = color_buffer[0];
if (bpp == 4)
pixels[current_byte + 3] = color_buffer[3];
current_byte += bpp;
++current_pixel;
if (current_pixel > pixel_count)
{
fclose(file);
delete[] color_buffer;
return CORRUPTED_FILE;
}
}
}
else
{
chunk_header -= 127;
if (fread(color_buffer, 1, bpp, file) != bpp)
{
fclose(file);
delete[] color_buffer;
return CORRUPTED_FILE;
}
for (int counter = 0; counter < chunk_header; counter++)
{
pixels[current_byte] = color_buffer[2];
pixels[current_byte + 1] = color_buffer[1];
pixels[current_byte + 2] = color_buffer[0];
if (bpp == 4)
pixels[current_byte + 3] = color_buffer[3];
current_byte += bpp;
++current_pixel;
if (current_pixel > pixel_count)
{
fclose(file);
delete[] color_buffer;
return CORRUPTED_FILE;
}
}
}
}
while (current_pixel < pixel_count);
fclose(file);
delete[] color_buffer;
return NONE;
}
ERR_CODE Image::loadRAW(const std::string &file, const GLsizei w, const GLsizei h)
{
name = file;
std::string file_extension = extension(file);
if (strieq(file_extension, "RAW"))
return __loadRAW(file, w, h);
else
return INVALID_EXTENSION;
}
ERR_CODE Image::__loadRAW(const std::string &file, const GLsizei w, const GLsizei h)
{
ASSERT(!pixels);
name = file;
width = w;
height = h;
bpp = 3;
FILE *in = file_open(file.c_str(), "rb");
if (!in)
return FILE_NOT_FOUND;
pixels = new GLubyte[w * h * 3];
int bytesRead = fread(pixels, 1, w * h * 3, in);
if (bytesRead != w * h * 3)
return CORRUPTED_FILE;
fclose(in);
return NONE;
}