-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
240 lines (233 loc) · 7.07 KB
/
main.cpp
File metadata and controls
240 lines (233 loc) · 7.07 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
#include <string>
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <cstring>
#include <stdint.h>
#include "textureinfo.h"
//*-------------------------------
// Global variables
//-------------------------------*/
static const std::string s_version = "1.2";
namespace Defaults {
enum Types {
PVRV3,
PVRLegacy,
KTX,
DDS
};
const std::vector<std::string> kContainerNames {
"PVR (v3)",
"PVR (legacy)",
"KTX",
"DDS"
};
const std::vector<std::string> kCsvNames {
"pvrV3.csv",
"pvrLegacy.csv",
"ktx.csv",
"dds.csv"
};
};
static struct Parameters {
bool print_csv;
Parameters():print_csv(false){}
}s_parameters;
static const
std::vector<std::pair<std::string, std::string>> ParameterInfo {
{"--csv","Write header information to a CSV file (calling directory). Disables stdout print"}
};
//*-------------------------------
// Global functions
//-------------------------------*/
static std::string GetFilenameExt(std::string filename) {
std::string ext_name("");
std::string::size_type index = filename.rfind('.');
if(index != std::string::npos)
ext_name = filename.substr(index+1);
return ext_name;
}
static void PrintDivider() {
printf("========================================\n");
}
static void PrintHeaderInfo(
const std::string& file_name,
const std::string& container_name,
const std::string& header_info) {
PrintDivider();
std::cout << file_name << std::endl;
PrintDivider();
std::cout << "Container: " << container_name << std::endl;
std::cout << header_info << std::endl;
}
static void PrintHelp() {
std::cout << "version: " << s_version <<std::endl;
std::cout << "usage: textureinfo ";
for(const auto¶meter:ParameterInfo)
std::cout << "[" << std::get<0>(parameter) << "] ";
std::cout << "..." << std::endl << std::endl;
std::cout << "Print a texture container's header information to stdout. Supported containers:" << std::endl;
for(const auto& container:Defaults::kContainerNames)
std::cout << "\t* " << container << std::endl;
std::cout << std::endl;
std::cout << "options:" << std::endl;
for(const auto¶meter:ParameterInfo)
std::cout << "\t" << std::get<0>(parameter) << ": "
<< std::get<1>(parameter) << std::endl;
}
//*-------------------------------
// main
//-------------------------------*/
int main (int argc, char *argv[]) {
std::vector<std::string> pvr_files, ktx_files, dds_files;
//*-------------------------------
// Loop through args
//-------------------------------*/
if(argc == 1) {
PrintHelp();
return 0;
}
for(unsigned int index = 1; index < argc; index++) {
// Flags
if(strcmp(argv[index], "/?") == 0 ||
strcmp(argv[index], "-h") == 0 ||
strcmp(argv[index], "--help") == 0) {
PrintHelp();
}
if(argv[index] == std::get<0>(ParameterInfo[0])) {
s_parameters.print_csv = true;
continue;
}
// Files
std::string file_name(argv[index]);
std::string ext_name(GetFilenameExt(file_name.c_str()));
if(ext_name == "pvr") {
pvr_files.push_back(std::move(file_name));
}
else if(ext_name == "ktx") {
ktx_files.push_back(std::move(file_name));
}
else if(ext_name == "dds") {
dds_files.push_back(std::move(file_name));
}
}
//*-------------------------------
// CSV setup
//-------------------------------*/
std::vector<std::ofstream> output_streams(Defaults::kCsvNames.size());
if(s_parameters.print_csv) {
for(unsigned int index = 0; index < output_streams.size(); ++index) {
const std::string& output_name(Defaults::kCsvNames.at(index));
std::ofstream& output(output_streams.at(index));
output.open(output_name);
if(!output.is_open()) {
printf("ERROR: Unable to open %s", output_name.c_str());
}
std::vector<std::string> column_titles;
switch (index) {
case Defaults::Types::PVRV3:
column_titles = PvrV3Info::column_names;
break;
case Defaults::Types::PVRLegacy:
column_titles = PvrLegacyInfo::column_names;
break;
case Defaults::Types::KTX:
column_titles = KTXInfo::column_names;
break;
case Defaults::Types::DDS:
column_titles = DDSInfo::column_names;
break;
default:
assert(0);
break;
}
output << "File name" << ',';
for(auto& column_title : column_titles)
output << column_title << ',';
output << std::endl;
}
}
//*-------------------------------
// Load files & print
//-------------------------------*/
std::string error_string("");
// PVR files
for(const auto& file_name: pvr_files) {
std::ifstream file(file_name, std::ifstream::binary);
if(!file.is_open()) {
printf("ERROR: Unable to open %s\n", file_name.c_str());
continue;
}
unsigned int file_type(Defaults::Types::PVRV3);
IHeader* header(nullptr);
PvrV3Header pvr_header;
PvrLegacyHeader pvr_legacy_header;
// PVR v3
if(pvr_header.LoadHeader(file, error_string)) header = &pvr_header;
else file_type = Defaults::Types::PVRLegacy;
// Legacy container
if(header == nullptr) {
if(pvr_legacy_header.LoadHeader(file, error_string)) {
header = &pvr_legacy_header;
}
else {
printf("ERROR: %s - %s\n",file_name.c_str(), error_string.c_str());
continue;
}
}
// Print
if(s_parameters.print_csv)
output_streams[file_type] << file_name << "," << header->ToCsvString() << std::endl;
else
PrintHeaderInfo(
file_name,
file_type == Defaults::Types::PVRV3?"PVR (v3)":"PVR (legacy)",
header->ToString());
}
// KTX files
for(const auto& file_name: ktx_files) {
std::ifstream file(file_name, std::ifstream::binary);
if(!file.is_open()) {
printf("ERROR: Unable to open %s\n", file_name.c_str());
continue;
}
KTXHeader ktx_header;
if(!ktx_header.LoadHeader(file, error_string)) {
printf("ERROR: %s - %s\n",file_name.c_str(), error_string.c_str());
continue;
}
// Print
if(s_parameters.print_csv)
output_streams.at(Defaults::Types::KTX) << file_name << "," << ktx_header.ToCsvString() << std::endl;
else
PrintHeaderInfo(
file_name,
Defaults::kContainerNames.at(Defaults::Types::KTX),
ktx_header.ToString());
}
// DDS files
for(const auto& file_name: dds_files) {
std::ifstream file(file_name, std::ifstream::binary);
if(!file.is_open()) {
printf("ERROR: Unable to open %s\n", file_name.c_str());
continue;
}
DDSHeader header;
if(!header.LoadHeader(file, error_string)) {
printf("ERROR: %s - %s\n",file_name.c_str(), error_string.c_str());
continue;
}
// Print
if(s_parameters.print_csv)
output_streams.at(Defaults::Types::DDS) << file_name << "," << header.ToCsvString() << std::endl;
else
PrintHeaderInfo(
file_name,
Defaults::kContainerNames.at(Defaults::Types::DDS),
header.ToString());
}
// Shutdown
for(auto& stream:output_streams)
stream.close();
}