forked from wolfpld/etcpak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
380 lines (355 loc) · 12.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <future>
#include <stdio.h>
#include <limits>
#include <math.h>
#include <memory>
#include <string.h>
#ifdef _MSC_VER
# include "getopt/getopt.h"
#else
# include <unistd.h>
# include <getopt.h>
#endif
#include "Bitmap.hpp"
#include "BlockData.hpp"
#include "DataProvider.hpp"
#include "Debug.hpp"
#include "Error.hpp"
#include "System.hpp"
#include "TaskDispatch.hpp"
#include "Timing.hpp"
struct DebugCallback_t : public DebugLog::Callback
{
void OnDebugMessage( const char* msg ) override
{
fprintf( stderr, "%s\n", msg );
}
} DebugCallback;
void Usage()
{
fprintf( stderr, "Usage: etcpak [options] input.png {output.pvr}\n" );
fprintf( stderr, " Options:\n" );
fprintf( stderr, " -v view mode (loads pvr/ktx file, decodes it and saves to png)\n" );
fprintf( stderr, " -s display image quality measurements\n" );
fprintf( stderr, " -b benchmark mode\n" );
fprintf( stderr, " -M switch benchmark to multi-threaded mode\n" );
fprintf( stderr, " -m generate mipmaps\n" );
fprintf( stderr, " -d enable dithering\n" );
fprintf( stderr, " -a alpha.pvr save alpha channel in a separate file\n" );
fprintf( stderr, " --etc1 use ETC1 mode (ETC2 is used by default)\n" );
fprintf( stderr, " --rgba enable RGBA in ETC2 mode (RGB is used by default\n" );
fprintf( stderr, " --disable-heuristics disable heuristic selector of compression mode\n" );
fprintf( stderr, " --dxtc use DXT1 compression\n" );
fprintf( stderr, " --linear input data is in linear space (disable sRGB conversion for mips)\n\n" );
fprintf( stderr, "Output file name may be unneeded for some modes.\n" );
}
int main( int argc, char** argv )
{
DebugLog::AddCallback( &DebugCallback );
bool viewMode = false;
bool stats = false;
bool benchmark = false;
bool benchMt = false;
bool mipmap = false;
bool dither = false;
bool etc2 = true;
bool rgba = false;
bool dxtc = false;
bool linearize = true;
bool useHeuristics = true;
const char* alpha = nullptr;
unsigned int cpus = System::CPUCores();
if( argc < 3 )
{
Usage();
return 1;
}
enum Options
{
OptEtc1,
OptRgba,
OptDxtc,
OptLinear,
OptNoHeuristics
};
struct option longopts[] = {
{ "etc1", no_argument, nullptr, OptEtc1 },
{ "rgba", no_argument, nullptr, OptRgba },
{ "dxtc", no_argument, nullptr, OptDxtc },
{ "linear", no_argument, nullptr, OptLinear },
{ "disable-heuristics", no_argument, nullptr, OptNoHeuristics },
{}
};
int c;
while( ( c = getopt_long( argc, argv, "vo:a:sbMmd", longopts, nullptr ) ) != -1 )
{
switch( c )
{
case '?':
Usage();
return 1;
case 'v':
viewMode = true;
break;
case 'a':
alpha = optarg;
break;
case 's':
stats = true;
break;
case 'b':
benchmark = true;
break;
case 'M':
benchMt = true;
break;
case 'm':
mipmap = true;
break;
case 'd':
dither = true;
break;
case OptEtc1:
etc2 = false;
break;
case OptRgba:
rgba = true;
etc2 = true;
break;
case OptDxtc:
etc2 = false;
dxtc = true;
break;
case OptLinear:
linearize = false;
break;
case OptNoHeuristics:
useHeuristics = false;
default:
break;
}
}
if( etc2 && dither )
{
printf( "Dithering is disabled in ETC2 mode, as it degrades image quality.\n" );
dither = false;
}
const char* input = nullptr;
const char* output = nullptr;
if( benchmark )
{
if( argc - optind < 1 )
{
Usage();
return 1;
}
input = argv[optind];
}
else
{
if( argc - optind < 2 )
{
Usage();
return 1;
}
input = argv[optind];
output = argv[optind+1];
}
if( benchmark )
{
if( viewMode )
{
auto bd = std::make_shared<BlockData>( input );
constexpr int NumTasks = 9;
uint64_t timeData[NumTasks];
for( int i=0; i<NumTasks; i++ )
{
const auto start = GetTime();
auto res = bd->Decode();
const auto end = GetTime();
timeData[i] = end - start;
}
std::sort( timeData, timeData+NumTasks );
const auto median = timeData[NumTasks/2] / 1000.f;
printf( "Median decode time for %i runs: %0.3f ms (%0.3f Mpx/s)\n", NumTasks, median, bd->Size().x * bd->Size().y / ( median * 1000 ) );
}
else
{
auto start = GetTime();
auto bmp = std::make_shared<Bitmap>( input, std::numeric_limits<unsigned int>::max(), !dxtc );
auto data = bmp->Data();
auto end = GetTime();
printf( "Image load time: %0.3f ms\n", ( end - start ) / 1000.f );
constexpr int NumTasks = 9;
uint64_t timeData[NumTasks];
if( benchMt )
{
TaskDispatch taskDispatch( cpus );
const unsigned int parts = ( ( bmp->Size().y / 4 ) + 32 - 1 ) / 32;
for( int i=0; i<NumTasks; i++ )
{
BlockData::Type type;
Channels channel;
if( alpha ) channel = Channels::Alpha;
else channel = Channels::RGB;
if( rgba ) type = BlockData::Etc2_RGBA;
else if( etc2 ) type = BlockData::Etc2_RGB;
else if( dxtc ) type = bmp->Alpha() ? BlockData::Dxt5 : BlockData::Dxt1;
else type = BlockData::Etc1;
auto bd = std::make_shared<BlockData>( bmp->Size(), false, type );
auto ptr = bmp->Data();
const auto width = bmp->Size().x;
const auto localStart = GetTime();
auto linesLeft = bmp->Size().y / 4;
size_t offset = 0;
if( rgba || type == BlockData::Dxt5 )
{
for( int j=0; j<parts; j++ )
{
const auto lines = std::min( 32, linesLeft );
taskDispatch.Queue( [bd, ptr, width, lines, offset, useHeuristics] {
bd->ProcessRGBA( ptr, width * lines / 4, offset, width, useHeuristics );
} );
linesLeft -= lines;
ptr += width * lines;
offset += width * lines / 4;
}
}
else
{
for( int j=0; j<parts; j++ )
{
const auto lines = std::min( 32, linesLeft );
taskDispatch.Queue( [bd, ptr, width, lines, offset, channel, dither, useHeuristics] {
bd->Process( ptr, width * lines / 4, offset, width, channel, dither, useHeuristics );
} );
linesLeft -= lines;
ptr += width * lines;
offset += width * lines / 4;
}
}
taskDispatch.Sync();
const auto localEnd = GetTime();
timeData[i] = localEnd - localStart;
}
}
else
{
for( int i=0; i<NumTasks; i++ )
{
BlockData::Type type;
Channels channel;
if( alpha ) channel = Channels::Alpha;
else channel = Channels::RGB;
if( rgba ) type = BlockData::Etc2_RGBA;
else if( etc2 ) type = BlockData::Etc2_RGB;
else if( dxtc ) type = bmp->Alpha() ? BlockData::Dxt5 : BlockData::Dxt1;
else type = BlockData::Etc1;
auto bd = std::make_shared<BlockData>( bmp->Size(), false, type );
const auto localStart = GetTime();
if( rgba || type == BlockData::Dxt5 )
{
bd->ProcessRGBA( bmp->Data(), bmp->Size().x * bmp->Size().y / 16, 0, bmp->Size().x, useHeuristics );
}
else
{
bd->Process( bmp->Data(), bmp->Size().x * bmp->Size().y / 16, 0, bmp->Size().x, channel, dither, useHeuristics );
}
const auto localEnd = GetTime();
timeData[i] = localEnd - localStart;
}
}
std::sort( timeData, timeData+NumTasks );
const auto median = timeData[NumTasks/2] / 1000.f;
printf( "Median compression time for %i runs: %0.3f ms (%0.3f Mpx/s)", NumTasks, median, bmp->Size().x * bmp->Size().y / ( median * 1000 ) );
if( benchMt )
{
printf( " multi threaded (%i cores)\n", cpus );
}
else
{
printf( " single threaded\n" );
}
}
}
else if( viewMode )
{
auto bd = std::make_shared<BlockData>( input );
auto out = bd->Decode();
out->Write( output );
}
else
{
DataProvider dp( input, mipmap, !dxtc, linearize );
auto num = dp.NumberOfParts();
BlockData::Type type;
if( etc2 )
{
if( rgba && dp.Alpha() )
{
type = BlockData::Etc2_RGBA;
}
else
{
type = BlockData::Etc2_RGB;
}
}
else if( dxtc )
{
if( dp.Alpha() )
{
type = BlockData::Dxt5;
}
else
{
type = BlockData::Dxt1;
}
}
else
{
type = BlockData::Etc1;
}
TaskDispatch taskDispatch( cpus );
auto bd = std::make_shared<BlockData>( output, dp.Size(), mipmap, type );
BlockDataPtr bda;
if( alpha && dp.Alpha() && !rgba )
{
bda = std::make_shared<BlockData>( alpha, dp.Size(), mipmap, type );
}
for( int i=0; i<num; i++ )
{
auto part = dp.NextPart();
if( type == BlockData::Etc2_RGBA || type == BlockData::Dxt5 )
{
TaskDispatch::Queue( [part, i, &bd, &dither, useHeuristics]()
{
bd->ProcessRGBA( part.src, part.width / 4 * part.lines, part.offset, part.width, useHeuristics );
} );
}
else
{
TaskDispatch::Queue( [part, i, &bd, &dither, useHeuristics]()
{
bd->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::RGB, dither, useHeuristics );
} );
if( bda )
{
TaskDispatch::Queue( [part, i, &bda, useHeuristics]()
{
bda->Process( part.src, part.width / 4 * part.lines, part.offset, part.width, Channels::Alpha, false, useHeuristics );
} );
}
}
}
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 ) );
}
}
return 0;
}