-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadtga.cpp
More file actions
256 lines (225 loc) · 7.28 KB
/
readtga.cpp
File metadata and controls
256 lines (225 loc) · 7.28 KB
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
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
252
253
254
255
/*
Copyright (c) 2021, Eric Haines
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "readtga.h"
#include <assert.h>
#include <iostream>
// calls https://github.com/aseprite/tga
//Decode from disk to raw pixels with a single function call
// return 0 on success
int readtga(progimage_info *im, wchar_t *filename, LodePNGColorType colortype)
{
im->width = im->height = 0;
FILE* f;
_wfopen_s(&f, filename, L"rb");
tga::StdioFileInterface file(f);
tga::Decoder decoder(&file);
tga::Header header;
if (!decoder.readHeader(header))
return 102;
bool match = false;
int channels_in = header.bytesPerPixel();
switch (channels_in) {
case 1:
if ( colortype == LCT_GREY ) {
match = true;
}
break;
case 3:
if (colortype == LCT_RGB) {
match = true;
}
break;
case 4:
if (colortype == LCT_RGBA) {
match = true;
}
break;
default:
// unsupported file type, we assume
return 104;
}
im->width = (int)header.width;
im->height = (int)header.height;
tga::Image image;
image.bytesPerPixel = header.bytesPerPixel();
image.rowstride = header.width * header.bytesPerPixel();
std::vector<uint8_t> buffer(image.rowstride * header.height);
image.pixels = &buffer[0];
if (!decoder.readImage(header, image, nullptr))
return 103;
// Optional post-process to fix the alpha channel in
// some TGA files where alpha=0 for all pixels when
// it shouldn't.
decoder.postProcessImage(header, image);
if (match) {
im->image_data = buffer;
}
else {
int channels_out = 0;
// color types don't match - convert from one to the other
switch (channels_in) {
case 1:
// gray to RGB or RGBA
channels_out = (colortype == LCT_RGB) ? 3 : 4;
break;
case 3:
// RGB to gray or RGBA
channels_out = (colortype == LCT_RGBA) ? 1 : 4;
break;
case 4:
// RGBA to gray or RGB (ignore alpha, I guess...)
channels_out = (colortype == LCT_RGB) ? 3 : 1;
break;
}
int num_pixels = header.width * header.height;
im->image_data.resize(channels_out * num_pixels);
int i;
unsigned char* src_data = &buffer[0];
unsigned char* dst_data = &im->image_data[0];
switch (channels_in) {
case 1:
// gray to RGB or RGBA
for (i = 0; i < num_pixels; i++) {
if (channels_out == 4) {
*dst_data++ = *src_data;
}
*dst_data++ = *src_data;
*dst_data++ = *src_data;
*dst_data++ = *src_data++;
}
break;
case 3:
// RGB to gray or RGBA
if (channels_out == 4) {
for (i = 0; i < num_pixels; i++) {
*dst_data++ = *src_data++;
*dst_data++ = *src_data++;
*dst_data++ = *src_data++;
*dst_data++ = 255;
}
}
else {
// RGB to gray? Why? Is the incoming RGB image actually gray?
assert(0);
for (i = 0; i < num_pixels; i++) {
*dst_data++ = *src_data++;
// skip green and blue
src_data++;
src_data++;
}
}
break;
case 4:
// RGBA to gray or RGB (ignore alpha, I guess...)
if (channels_out == 3) {
for (i = 0; i < num_pixels; i++) {
*dst_data++ = *src_data++;
*dst_data++ = *src_data++;
*dst_data++ = *src_data++;
src_data++; // skip alpha
}
}
else {
// RGBA to gray? Why? Is the incoming RGB image actually gray?
assert(0);
for (i = 0; i < num_pixels; i++) {
*dst_data++ = *src_data++;
// skip green and blue and alpha
src_data++;
src_data++;
src_data++;
}
}
break;
}
}
return 0;
}
// same as PNG's
//void readtga_cleanup(int mode, progimage_info *im)
//{
// // mode was important back when libpng was in use
// if ( mode == 1 )
// {
// im->image_data.clear();
// }
//}
int readtgaheader(progimage_info* im, wchar_t* filename, LodePNGColorType& colortype)
{
im->width = im->height = 0;
FILE* f;
_wfopen_s(&f, filename, L"rb");
tga::StdioFileInterface file(f);
tga::Decoder decoder(&file);
tga::Header header;
if (!decoder.readHeader(header))
return 102;
im->width = (int)header.width;
im->height = (int)header.height;
switch (header.bytesPerPixel()) {
case 1:
colortype = LCT_GREY;
break;
default:
assert(0);
case 3:
colortype = LCT_RGB;
break;
case 4:
colortype = LCT_RGBA;
break;
}
fclose(f);
return 0;
}
//============================
// Yes, these should be in yet another separate file, but alas
int readImage(progimage_info* im, wchar_t* filename, LodePNGColorType colortype, int imageFileType)
{
if (imageFileType == 1) {
return readpng(im, filename, colortype);
} else if (imageFileType == 2) {
return readtga(im, filename, colortype);
}
assert(0);
// unknown image type
return 1;
}
void readImage_cleanup(int mode, progimage_info* im)
{
// same for TGA
readpng_cleanup(mode, im);
}
int readImageHeader(progimage_info* im, wchar_t* filename, LodePNGColorType& colortype, int imageFileType)
{
if (imageFileType == 1) {
return readpngheader(im, filename, colortype);
}
else if (imageFileType == 2) {
return readtgaheader(im, filename, colortype);
}
assert(0);
// unknown image type
return 999;
}