-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cpp
280 lines (257 loc) · 7.52 KB
/
Application.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <future>
#include <stdio.h>
#include <limits>
#include <math.h>
#include <memory>
#include <string.h>
#include "Bitmap.hpp"
#include "BlockData.hpp"
#include "CpuArch.hpp"
#include "DataProvider.hpp"
#include "Debug.hpp"
#include "Dither.hpp"
#include "Error.hpp"
#include "System.hpp"
#include "TaskDispatch.hpp"
#include "Timing.hpp"
#define WIN32_LEARN_AND_MEAN
#include <Windows.h>
struct DebugCallback_t : public DebugLog::Callback
{
void OnDebugMessage( const char* msg ) override
{
fprintf( stderr, "%s\n", msg );
}
} DebugCallback;
void Usage()
{
fprintf( stderr, "Usage: etcpak input.png [options]\n" );
#ifdef __SSE4_1__
if( can_use_intel_core_4th_gen_features() )
{
fprintf( stderr, " Using AVX 2 instructions.\n" );
}
else
{
fprintf( stderr, " Using SSE 4.1 instructions.\n" );
}
#else
fprintf( stderr, " SIMD not available.\n" );
#endif
fprintf( stderr, " Options:\n" );
fprintf( stderr, " -v view mode (loads pvr/ktx file, decodes it and saves to png)\n" );
fprintf( stderr, " -t dir set output folder\n" );
fprintf( stderr, " -o 1 output selection (sum of: 1 - save pvr file; 2 - save png file)\n" );
fprintf( stderr, " note: pvr files are written regardless of this option\n" );
fprintf( stderr, " -a disable alpha channel processing\n" );
fprintf( stderr, " -s display image quality measurements\n" );
fprintf( stderr, " -b benchmark mode\n" );
fprintf( stderr, " -m generate mipmaps\n" );
fprintf( stderr, " -d enable dithering\n" );
fprintf( stderr, " -debug dissect ETC texture\n" );
fprintf( stderr, " -etc2 enable ETC2 mode\n" );
fprintf( stderr, " -pkm output to PKM(.pkm) format\n" );
fprintf( stderr, " -atlas make pixel+alpha atlas(etc1)\n" );
fprintf( stderr, " -dds export DDS texture\n" );
}
int main( int argc, char** argv )
{
DebugLog::AddCallback( &DebugCallback );
bool viewMode = false;
int save = 1;
bool alpha = true;
bool stats = false;
bool benchmark = false;
bool mipmap = false;
bool dither = false;
bool debug = false;
bool etc2 = false;
bool etc_pkm = false;
bool atlas = false;
bool dds = false;
const char *target_dir = "output";
if( argc < 2 )
{
Usage();
return 1;
}
#define CSTR(x) strcmp( argv[i], x ) == 0
for( int i=2; i<argc; i++ )
{
if( CSTR( "-v" ) )
{
viewMode = true;
}
else if( CSTR( "-t" ))
{
i++;
target_dir = argv[i];
assert(target_dir != NULL);
}
else if( CSTR( "-o" ) )
{
i++;
save = atoi( argv[i] );
assert( ( save & 0x3 ) != 0 );
}
else if( CSTR( "-a" ) )
{
alpha = false;
}
else if( CSTR( "-s" ) )
{
stats = true;
}
else if( CSTR( "-b" ) )
{
benchmark = true;
}
else if( CSTR( "-m" ) )
{
mipmap = true;
}
else if( CSTR( "-d" ) )
{
dither = true;
}
else if( CSTR( "-debug" ) )
{
debug = true;
}
else if( CSTR( "-pkm" ) )
{
etc_pkm = true;
}
else if( CSTR( "-etc2" ) )
{
etc2 = true;
}
else if( CSTR( "-atlas" ) )
{
atlas = true;
}
else if( CSTR( "-dds" ) )
{
dds = true;
}
else
{
Usage();
return 1;
}
}
#undef CSTR
if( dither )
{
InitDither();
}
TaskDispatch taskDispatch( System::CPUCores() );
if( benchmark )
{
auto start = GetTime();
auto bmp = std::make_shared<Bitmap>( argv[1], std::numeric_limits<uint>::max() );
auto data = bmp->Data();
auto end = GetTime();
printf( "Image load time: %0.3f ms\n", ( end - start ) / 1000.f );
const int NumTasks = System::CPUCores() * 10;
start = GetTime();
for( int i=0; i<NumTasks; i++ )
{
TaskDispatch::Queue( [&bmp, &dither, i, etc2]()
{
auto bd = std::make_shared<BlockData>( bmp->Size(), false );
bd->Process( bmp->Data(), bmp->Size().x * bmp->Size().y / 16, 0, bmp->Size().x, Channels::RGB, dither, etc2 );
} );
}
TaskDispatch::Sync();
end = GetTime();
printf( "Mean compression time for %i runs: %0.3f ms\n", NumTasks, ( end - start ) / ( NumTasks * 1000.f ) );
}
else if( viewMode )
{
auto bd = std::make_shared<BlockData>( argv[1] );
auto out = bd->Decode();
out->Write( "out.png" );
}
else if( debug )
{
auto bd = std::make_shared<BlockData>( argv[1] );
bd->Dissect();
}
else
{
DataProvider dp( argv[1], mipmap );
auto num = dp.NumberOfParts();
CreateDirectoryA(target_dir, NULL);
std::string fn = argv[1];
fn = std::string(target_dir) + "/" + fn.substr(0, fn.rfind("."));
auto bd = std::make_shared<BlockData>( fn.c_str(), dp.Size(), mipmap, atlas, etc_pkm, dds );
BlockDataPtr bda;
if( alpha && dp.Alpha() && !atlas )
{
bda = std::make_shared<BlockData>( (fn + "_alpha").c_str(), dp.Size(), mipmap, atlas, etc_pkm, dds );
}
if( bda )
{
for( int i=0; i<num; i++ )
{
auto part = dp.NextPart();
TaskDispatch::Queue( [part, i, &bd, &dither, etc2]()
{
bd->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::RGB, dither, etc2 );
} );
TaskDispatch::Queue( [part, i, &bda, etc2]()
{
bda->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::Alpha, false, etc2 );
} );
}
}
else
{
for( int i=0; i<num; i++ )
{
auto part = dp.NextPart();
TaskDispatch::Queue( [part, i, &bd, &dither, etc2]()
{
bd->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::RGB, dither, etc2 );
} );
if(atlas) {
TaskDispatch::Queue( [part, i, &bd, etc2]()
{
bd->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::Alpha, false, etc2 );
} );
}
}
}
TaskDispatch::Sync();
if( stats )
{
auto out = bd->Decode();
float mse = CalcMSE3( dp.ImageData(), *out );
printf( "RGB data\n" );
printf( " RMSE: %f\n", sqrt( mse ) );
printf( " PSNR: %f\n", 20 * log10( 255 ) - 10 * log10( mse ) );
if( bda )
{
auto out = bda->Decode();
float mse = CalcMSE1( dp.ImageData(), *out );
printf( "A data\n" );
printf( " RMSE: %f\n", sqrt( mse ) );
printf( " PSNR: %f\n", 20 * log10( 255 ) - 10 * log10( mse ) );
}
}
if( save & 0x2 )
{
auto out = bd->Decode();
out->Write( (fn + ".png").c_str() );
if( bda )
{
auto outa = bda->Decode();
outa->Write( (fn + "_alpha.png").c_str() );
}
}
bd.reset();
bda.reset();
}
return 0;
}