forked from AcademySoftwareFoundation/OpenImageIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagespeed_test.cpp
More file actions
626 lines (539 loc) · 21 KB
/
Copy pathimagespeed_test.cpp
File metadata and controls
626 lines (539 loc) · 21 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/benchmark.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/imagecache.h>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/timer.h>
#include <OpenImageIO/unittest.h>
#include <OpenImageIO/ustring.h>
#include <functional>
#include <iostream>
#include <vector>
using namespace OIIO;
static bool verbose = false;
static int iterations = 1;
static int ntrials = 1;
static int numthreads = 0;
static int autotile_size = 64;
static bool iter_only = false;
static bool no_iter = false;
static std::string conversionname;
static TypeDesc conversion = TypeDesc::UNKNOWN; // native by default
static std::vector<ustring> input_filename;
static std::string output_filename;
static std::string output_format;
static std::vector<char> buffer;
static ImageSpec bufspec, outspec;
static std::shared_ptr<ImageCache> imagecache;
static imagesize_t total_image_pixels = 0;
static float cache_size = 0;
static void
getargs(int argc, char* argv[])
{
ArgParse ap;
// clang-format off
ap.intro("imagespeed_test\n" OIIO_INTRO_STRING)
.usage("imagespeed_test [options]");
ap.arg("filename")
.hidden()
.action([&](cspan<const char*> argv){ input_filename.emplace_back(argv[0]); });
ap.arg("-v", &verbose)
.help("Verbose mode");
ap.arg("--threads %d", &numthreads)
.help(Strutil::fmt::format("Number of threads (default: {})", numthreads));
ap.arg("--iters %d", &iterations)
.help(Strutil::fmt::format("Number of iterations (default: {})", iterations));
ap.arg("--trials %d", &ntrials)
.help("Number of trials");
ap.arg("--autotile %d", &autotile_size)
.help(Strutil::fmt::format("Autotile size (when used; default: {})", autotile_size));
ap.arg("--iteronly", &iter_only)
.help("Run ImageBuf iteration tests only (not read tests)");
ap.arg("--noiter", &no_iter)
.help("Don't run ImageBuf iteration tests");
ap.arg("--convert %s", &conversionname)
.help("Convert to named type upon read (default: native)");
ap.arg("--cache %f", &cache_size)
.help("Specify ImageCache size, in MB");
ap.arg("-o %s", &output_filename)
.help("Test output by writing to this file");
ap.arg("-od %s", &output_format)
.help("Requested output format");
// clang-format on
ap.parse(argc, (const char**)argv);
}
static void
time_read_image()
{
for (ustring filename : input_filename) {
auto in = ImageInput::open(filename.c_str());
OIIO_ASSERT(in);
(void)in->read_image(0, 0, 0, in->spec().nchannels, conversion,
&buffer[0]);
in->close();
}
}
static void
time_read_scanline_at_a_time()
{
for (ustring filename : input_filename) {
auto in = ImageInput::open(filename.c_str());
OIIO_ASSERT(in);
const ImageSpec& spec(in->spec());
size_t pixelsize = spec.nchannels * conversion.size();
if (!pixelsize)
pixelsize = spec.pixel_bytes(true); // UNKNOWN -> native
imagesize_t scanlinesize = spec.width * pixelsize;
for (int y = 0; y < spec.height; ++y) {
in->read_scanline(y + spec.y, 0, conversion,
&buffer[scanlinesize * y]);
}
in->close();
}
}
static void
time_read_64_scanlines_at_a_time()
{
for (ustring filename : input_filename) {
auto in = ImageInput::open(filename.c_str());
OIIO_ASSERT(in);
ImageSpec spec = in->spec_dimensions(0);
size_t pixelsize = spec.nchannels * conversion.size();
if (!pixelsize)
pixelsize = spec.pixel_bytes(true); // UNKNOWN -> native
imagesize_t scanlinesize = spec.width * pixelsize;
for (int y = 0; y < spec.height; y += 64) {
bool ok = in->read_scanlines(
/*subimage=*/0, /*miplevel=*/0, y + spec.y,
std::min(y + spec.y + 64, spec.y + spec.height), 0, 0,
spec.nchannels, conversion, &buffer[scanlinesize * y]);
OIIO_ASSERT(ok);
}
in->close();
}
}
static void
time_read_imagebuf()
{
imagecache->invalidate_all(true);
for (ustring filename : input_filename) {
ImageBuf ib(filename, 0, 0, imagecache);
ib.read(0, 0, true, conversion);
}
}
static void
time_ic_get_pixels()
{
imagecache->invalidate_all(true);
for (ustring filename : input_filename) {
const ImageSpec spec = (*imagecache->imagespec(filename));
imagecache->get_pixels(filename, 0, 0, spec.x, spec.x + spec.width,
spec.y, spec.y + spec.height, spec.z,
spec.z + spec.depth, conversion, &buffer[0]);
}
}
static void
test_read(const std::string& explanation, void (*func)(), int autotile = 64,
int autoscanline = 1)
{
imagecache->invalidate_all(true); // Don't hold anything
imagecache->attribute("autotile", autotile);
imagecache->attribute("autoscanline", autoscanline);
double t = time_trial(func, ntrials);
double rate = double(total_image_pixels) / t;
OIIO::print(" {}: {} = {:5.1f} Mpel/s\n", explanation,
Strutil::timeintervalformat(t, 2), rate / 1.0e6);
}
static void
time_write_image()
{
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
ok = out->write_image(bufspec.format, &buffer[0]);
OIIO_CONTRACT_ASSERT(ok);
}
static void
time_write_scanline_at_a_time()
{
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
size_t pixelsize = outspec.nchannels * sizeof(float);
imagesize_t scanlinesize = outspec.width * pixelsize;
bool ok = true;
for (int y = 0; y < outspec.height; ++y) {
ok = out->write_scanline(y + outspec.y, outspec.z, bufspec.format,
&buffer[scanlinesize * y]);
}
}
static void
time_write_64_scanlines_at_a_time()
{
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
size_t pixelsize = outspec.nchannels * sizeof(float);
imagesize_t scanlinesize = outspec.width * pixelsize;
for (int y = 0; y < outspec.height; y += 64) {
(void)out->write_scanlines(y + outspec.y,
std::min(y + outspec.y + 64,
outspec.y + outspec.height),
outspec.z, bufspec.format,
&buffer[scanlinesize * y]);
}
}
static void
time_write_tile_at_a_time()
{
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
size_t pixelsize = outspec.nchannels * sizeof(float);
imagesize_t scanlinesize = outspec.width * pixelsize;
imagesize_t planesize = outspec.height * scanlinesize;
bool ok = true;
for (int z = 0; z < outspec.depth; z += outspec.tile_depth) {
for (int y = 0; y < outspec.height; y += outspec.tile_height) {
for (int x = 0; x < outspec.width; x += outspec.tile_width) {
ok = out->write_tile(x + outspec.x, y + outspec.y,
z + outspec.z, bufspec.format,
&buffer[scanlinesize * y + pixelsize * x],
pixelsize, scanlinesize, planesize);
}
}
}
}
static void
time_write_tiles_row_at_a_time()
{
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
size_t pixelsize = outspec.nchannels * sizeof(float);
imagesize_t scanlinesize = outspec.width * pixelsize;
bool ok = true;
for (int z = 0; z < outspec.depth; z += outspec.tile_depth) {
for (int y = 0; y < outspec.height; y += outspec.tile_height) {
ok = out->write_tiles(outspec.x, outspec.x + outspec.width,
y + outspec.y,
y + outspec.y + outspec.tile_height,
z + outspec.z,
z + outspec.z + outspec.tile_depth,
bufspec.format, &buffer[scanlinesize * y],
pixelsize /*xstride*/,
scanlinesize /*ystride*/);
}
}
}
static void
time_write_imagebuf()
{
ImageBuf ib(bufspec, span(buffer)); // wrap the buffer
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool ok = out->open(output_filename, outspec);
OIIO_ASSERT(ok);
ib.write(out.get());
}
static void
test_write(const std::string& explanation, void (*func)(), int tilesize = 0)
{
outspec.tile_width = tilesize;
outspec.tile_height = tilesize;
outspec.tile_depth = 1;
double t = time_trial(func, ntrials);
double rate = double(total_image_pixels) / t;
OIIO::print(" {}: {} = {:5.1f} Mpel/s\n", explanation,
Strutil::timeintervalformat(t, 2), rate / 1.0e6);
}
static float
time_loop_pixels_1D(ImageBuf& ib, int iters)
{
OIIO_ASSERT(ib.localpixels() && ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
int nchannels = spec.nchannels;
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
const float* f = (const float*)ib.pixeladdr(spec.x, spec.y, spec.z);
OIIO_DASSERT(f);
for (imagesize_t p = 0; p < npixels; ++p) {
sum += f[0];
f += nchannels;
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static float
time_loop_pixels_3D(ImageBuf& ib, int iters)
{
OIIO_ASSERT(ib.localpixels() && ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
int nchannels = spec.nchannels;
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
const float* f = (const float*)ib.pixeladdr(spec.x, spec.y, spec.z);
OIIO_DASSERT(f);
for (int z = spec.z, ze = spec.z + spec.depth; z < ze; ++z) {
for (int y = spec.y, ye = spec.y + spec.height; y < ye; ++y) {
for (int x = spec.x, xe = spec.x + spec.width; x < xe; ++x) {
sum += f[0];
f += nchannels;
}
}
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static float
time_loop_pixels_3D_getchannel(ImageBuf& ib, int iters)
{
OIIO_DASSERT(ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
for (int z = spec.z, ze = spec.z + spec.depth; z < ze; ++z) {
for (int y = spec.y, ye = spec.y + spec.height; y < ye; ++y) {
for (int x = spec.x, xe = spec.x + spec.width; x < xe; ++x) {
sum += ib.getchannel(x, y, 0, 0);
}
}
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static float
time_iterate_pixels(ImageBuf& ib, int iters)
{
OIIO_DASSERT(ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
for (ImageBuf::ConstIterator<float, float> p(ib); !p.done(); ++p) {
sum += p[0];
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static float
time_iterate_pixels_slave_pos(ImageBuf& ib, int iters)
{
OIIO_DASSERT(ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
ImageBuf::ConstIterator<float, float> slave(ib);
for (ImageBuf::ConstIterator<float, float> p(ib); !p.done(); ++p) {
slave.pos(p.x(), p.y());
sum += p[0];
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static float
time_iterate_pixels_slave_incr(ImageBuf& ib, int iters)
{
OIIO_DASSERT(ib.pixeltype() == TypeFloat);
const ImageSpec& spec(ib.spec());
imagesize_t npixels = spec.image_pixels();
double sum = 0.0f;
for (int i = 0; i < iters; ++i) {
ImageBuf::ConstIterator<float, float> slave(ib);
for (ImageBuf::ConstIterator<float, float> p(ib); !p.done(); ++p) {
sum += p[0];
++slave;
}
}
// std::cout << float(sum/npixels/iters) << "\n";
return float(sum / npixels / iters);
}
static void
test_pixel_iteration(const std::string& explanation,
float (*func)(ImageBuf&, int), bool preload,
int iters = 100, int autotile = 64)
{
imagecache->invalidate_all(true); // Don't hold anything
// Force the whole image to be read at once
imagecache->attribute("autotile", autotile);
imagecache->attribute("autoscanline", 1);
ImageBuf ib(input_filename[0], 0, 0, imagecache);
ib.read(0, 0, preload, TypeFloat);
double t = time_trial(std::bind(func, std::ref(ib), iters), ntrials);
double rate = double(ib.spec().image_pixels()) / (t / iters);
OIIO::print(" {}: {} = {:5.1f} Mpel/s\n", explanation,
Strutil::timeintervalformat(t / iters, 3), rate / 1.0e6);
}
static void
set_dataformat(const std::string& output_format, ImageSpec& outspec)
{
if (output_format == "uint8")
outspec.format = TypeDesc::UINT8;
else if (output_format == "int8")
outspec.format = TypeDesc::INT8;
else if (output_format == "uint16")
outspec.format = TypeDesc::UINT16;
else if (output_format == "int16")
outspec.format = TypeDesc::INT16;
else if (output_format == "half")
outspec.format = TypeDesc::HALF;
else if (output_format == "float")
outspec.format = TypeDesc::FLOAT;
else if (output_format == "double")
outspec.format = TypeDesc::DOUBLE;
// Otherwise leave at the default
}
int
main(int argc, char** argv)
{
getargs(argc, argv);
if (input_filename.size() == 0) {
std::cout << "Error: Must supply a filename.\n";
return -1;
}
OIIO::attribute("threads", numthreads);
OIIO::attribute("exr_threads", numthreads);
conversion.fromstring(conversionname);
imagecache = ImageCache::create();
if (cache_size)
imagecache->attribute("max_memory_MB", cache_size);
imagecache->attribute("forcefloat", 1);
// Allocate a buffer big enough (for floats)
bool all_scanline = true;
total_image_pixels = 0;
imagesize_t maxpelchans = 0;
for (auto&& fn : input_filename) {
ImageSpec spec;
if (!imagecache->get_imagespec(fn, spec)) {
std::cout << "File \"" << fn << "\" could not be opened.\n";
return -1;
}
total_image_pixels += spec.image_pixels();
maxpelchans = std::max(maxpelchans,
spec.image_pixels() * spec.nchannels);
all_scanline &= (spec.tile_width == 0);
}
imagecache->invalidate_all(true); // Don't hold anything
if (!iter_only) {
std::cout << "Timing various ways of reading images:\n";
if (conversion == TypeDesc::UNKNOWN)
std::cout
<< " ImageInput reads will keep data in native format.\n";
else
std::cout << " ImageInput reads will convert data to "
<< conversion << "\n";
buffer.resize(maxpelchans * sizeof(float), 0);
test_read("read_image ",
time_read_image, 0, 0);
if (all_scanline) {
test_read("read_scanline (1 at a time) ",
time_read_scanline_at_a_time, 0, 0);
test_read("read_scanlines (64 at a time) ",
time_read_64_scanlines_at_a_time, 0, 0);
}
test_read("ImageBuf read ",
time_read_imagebuf, 0, 0);
test_read("ImageCache get_pixels ",
time_ic_get_pixels, 0, 0);
test_read("ImageBuf read (autotile) ",
time_read_imagebuf, autotile_size, 0);
test_read("ImageCache get_pixels (autotile) ",
time_ic_get_pixels, autotile_size, 0);
if (all_scanline) { // don't bother for tiled images
test_read("ImageBuf read (autotile+autoscanline) ",
time_read_imagebuf, autotile_size, 1);
test_read("ImageCache get_pixels (autotile+autoscanline)",
time_ic_get_pixels, autotile_size, 1);
}
if (verbose)
std::cout << "\n" << imagecache->getstats(2) << "\n";
std::cout << std::endl;
}
if (output_filename.size()) {
// Use the first image
auto in = ImageInput::open(input_filename[0].c_str());
OIIO_ASSERT(in);
bufspec = in->spec(0, 0);
bool ok = in->read_image(0, 0, 0, bufspec.nchannels, conversion,
&buffer[0]);
OIIO_ASSERT(ok);
in->close();
in.reset();
std::cout << "Timing ways of writing images:\n";
// imagecache->get_imagespec (input_filename[0], bufspec, 0, 0, true);
auto out = ImageOutput::create(output_filename);
OIIO_ASSERT(out);
bool supports_tiles = out->supports("tiles");
out.reset();
outspec = bufspec;
set_dataformat(output_format, outspec);
std::cout << " writing as format " << outspec.format << "\n";
test_write("write_image (scanline) ",
time_write_image, 0);
if (supports_tiles)
test_write("write_image (tiled) ",
time_write_image, 64);
test_write("write_scanline (one at a time) ",
time_write_scanline_at_a_time, 0);
test_write("write_scanlines (64 at a time) ",
time_write_64_scanlines_at_a_time, 0);
if (supports_tiles) {
test_write("write_tile (one at a time) ",
time_write_tile_at_a_time, 64);
test_write("write_tiles (a whole row at a time) ",
time_write_tiles_row_at_a_time, 64);
}
test_write("ImageBuf::write (scanline) ",
time_write_imagebuf, 0);
if (supports_tiles)
test_write("ImageBuf::write (tiled) ",
time_write_imagebuf, 64);
std::cout << std::endl;
}
if (!no_iter) {
const int iters = 64;
std::cout << "Timing ways of iterating over an image:\n";
test_pixel_iteration("Loop pointers on loaded image (\"1D\") ",
time_loop_pixels_1D, true, iters);
test_pixel_iteration("Loop pointers on loaded image (\"3D\") ",
time_loop_pixels_3D, true, iters);
test_pixel_iteration("Loop + getchannel on loaded image (\"3D\")",
time_loop_pixels_3D_getchannel, true, iters / 32);
test_pixel_iteration("Loop + getchannel on cached image (\"3D\")",
time_loop_pixels_3D_getchannel, false, iters / 32);
test_pixel_iteration("Iterate over a loaded image ",
time_iterate_pixels, true, iters);
test_pixel_iteration("Iterate over a cache image ",
time_iterate_pixels, false, iters);
test_pixel_iteration("Iterate over a loaded image (pos slave) ",
time_iterate_pixels_slave_pos, true, iters);
test_pixel_iteration("Iterate over a cache image (pos slave) ",
time_iterate_pixels_slave_pos, false, iters);
test_pixel_iteration("Iterate over a loaded image (incr slave)",
time_iterate_pixels_slave_incr, true, iters);
test_pixel_iteration("Iterate over a cache image (incr slave) ",
time_iterate_pixels_slave_incr, false, iters);
}
if (verbose)
std::cout << "\n" << imagecache->getstats(2) << "\n";
return unit_test_failures;
}