Repository navigation
Expand file tree
/
Copy pathtest_render.cpp
More file actions
1879 lines (1554 loc) · 73.6 KB
/
Copy pathtest_render.cpp
File metadata and controls
1879 lines (1554 loc) · 73.6 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: Tests for the rendering layer (render*, util_simd) -- software blends, YUV conversion for the software backend, packed-surface area downscaling, surface transforms (resize, rotate) and the drawn logo.
#include "pch.h"
#include "test.h"
#include "libdesktop/core/simd.h"
#include "av_format.h"
#include "libdesktop/render/software.h"
#include "test_fixtures.h"
#include "test_runner.h"
#include "ui_elements.h"
#include "ui_charts.h"
#include "ui_globe.h"
#include "ui_panorama.h"
static uint8_t blend_opaque_channel(const uint8_t dest, const float src, const float alpha)
{
return static_cast<uint8_t>(std::clamp(static_cast<int>(src * alpha + dest * (1.0f - alpha) + 0.5f), 0, 255));
}
static void should_unpremultiply_surface_alpha()
{
core::surface surface;
assert_equal(true, surface.alloc(3, 1, core::texture_format::ARGB) != nullptr, "alpha fixture allocated");
surface.set_pixel(0, 0, 0x80402010u);
surface.set_pixel(1, 0, 0x00112233u);
surface.set_pixel(2, 0, 0xff336699u);
surface.unpremultiply_alpha();
assert_equal(0x80804020u, surface.get_pixel(0, 0), "associated channels are divided by alpha");
assert_equal(0u, surface.get_pixel(1, 0), "transparent pixels have defined zero colors");
assert_equal(0xff336699u, surface.get_pixel(2, 0), "opaque pixels are unchanged");
}
static uint8_t blend_opaque_normalized_channel(const uint8_t dest, const float src, const float alpha)
{
const auto value = src * alpha + (dest / 255.0f) * (1.0f - alpha);
return static_cast<uint8_t>(std::clamp(static_cast<int>(value * 255.0f + 0.5f), 0, 255));
}
static void should_match_simd_software_blends()
{
#if defined(COMPILE_SIMD_INTRINSIC)
constexpr size_t max_pixels = 19;
alignas(16) std::array<uint8_t, max_pixels * 4> original{};
alignas(16) std::array<uint8_t, max_pixels * 4> source{};
alignas(16) std::array<uint8_t, max_pixels> coverage{};
for (size_t i = 0; i < original.size(); ++i) original[i] = static_cast<uint8_t>(i * 47u + 13u);
for (size_t i = 0; i < source.size(); ++i) source[i] = static_cast<uint8_t>(i * 29u + 7u);
for (size_t i = 0; i < coverage.size(); ++i) coverage[i] = static_cast<uint8_t>(i * 61u + 3u);
constexpr std::array<float, 5> alphas = {0.0f, 0.13f, 0.5f, 0.87f, 1.0f};
constexpr float sb = 37.0f;
constexpr float sg = 149.0f;
constexpr float sr = 231.0f;
for (size_t count = 0; count <= max_pixels; ++count)
{
for (const auto alpha : alphas)
{
auto check = [&](auto&& scalar, auto&& simd, const std::string_view message)
{
auto expected = original;
auto actual = original;
for (size_t i = 0; i < count; ++i) scalar(expected.data() + i * 4, i);
const auto processed = simd(actual.data());
assert_equal(count & ~size_t{3}, processed, message);
for (size_t i = processed; i < count; ++i) scalar(actual.data() + i * 4, i);
assert_equal(true, expected == actual, message);
};
auto blend_solid_pixel = [](uint8_t* dest, const float b, const float g, const float r, const float a)
{
dest[0] = blend_opaque_normalized_channel(dest[0], b, a);
dest[1] = blend_opaque_normalized_channel(dest[1], g, a);
dest[2] = blend_opaque_normalized_channel(dest[2], r, a);
dest[3] = 255;
};
check([&](uint8_t* dest, size_t)
{ blend_solid_pixel(dest, sb / 255.0f, sg / 255.0f, sr / 255.0f, alpha); }, [&](uint8_t* dest) {
return blend_solid_opaque_sse2(dest, count, sb / 255.0f, sg / 255.0f, sr / 255.0f, alpha);
}, "SIMD solid blend");
check([&](uint8_t* dest, const size_t i) {
blend_solid_pixel(dest, sb / 255.0f, sg / 255.0f, sr / 255.0f, alpha * coverage[i] / 255.0f);
}, [&](uint8_t* dest) {
return blend_glyph_opaque_sse2(dest, coverage.data(), count, sb / 255.0f, sg / 255.0f, sr / 255.0f,
alpha);
}, "SIMD glyph blend");
for (const auto has_alpha : {false, true})
{
check([&](uint8_t* dest, const size_t i)
{
const auto* src = source.data() + i * 4;
const auto pixel_alpha = (has_alpha ? src[3] / 255.0f : 1.0f) * alpha;
dest[0] = blend_opaque_channel(dest[0], src[0], pixel_alpha);
dest[1] = blend_opaque_channel(dest[1], src[1], pixel_alpha);
dest[2] = blend_opaque_channel(dest[2], src[2], pixel_alpha);
dest[3] = 255;
}, [&](uint8_t* dest) {
return blend_bgra_opaque_sse2(dest, source.data(), count, has_alpha, alpha);
}, "SIMD BGRA blend");
}
}
}
#endif
}
// The CPU renderer and every CPU conversion map HDR through the cube the GPU samples, so the lookup has
// to be the arithmetic of a trilinear texture sample: the signal scaled onto the samples, with 0 and
// 65535 on the end samples, and the eight around it blended by distance. Each output channel is a
// different function of the axes, so a swapped axis, channel or byte order shows; the SIMD path and
// the one other architectures run must both agree with an independent double-precision sample.
static void should_tone_map_through_a_cube_as_a_trilinear_sample()
{
constexpr int n = 5;
core::tone_map_lut lut;
lut.size = n;
lut.rgba.resize(static_cast<size_t>(n) * n * n * 4);
const auto at = [](const int r, const int g, const int b)
{
const auto fr = r / (n - 1.0);
const auto fg = g / (n - 1.0);
const auto fb = b / (n - 1.0);
return std::array{fr, fg * fg * (1.0 - 0.5 * fb), fb * (1.0 - 0.5 * fr) + 0.25 * fg * fr};
};
for (auto b = 0; b < n; ++b)
{
for (auto g = 0; g < n; ++g)
{
for (auto r = 0; r < n; ++r)
{
const auto v = at(r, g, b);
auto* const out = lut.rgba.data() + ((static_cast<size_t>(b) * n + g) * n + r) * 4;
for (auto c = 0; c < 3; ++c)
out[c] = static_cast<uint16_t>(std::lround(std::clamp(v[c], 0.0, 1.0) * 65535.0));
out[3] = 0xffff;
}
}
}
// The sample a texture unit would take, in the cube's own 16-bit units.
const auto expected = [&lut](const uint16_t* const px)
{
std::array<double, 3> result = {};
std::array<int, 3> index = {};
std::array<double, 3> weight = {};
for (auto axis = 0; axis < 3; ++axis)
{
const auto x = px[axis] / 65535.0 * (n - 1);
index[axis] = std::min(static_cast<int>(x), n - 2);
weight[axis] = x - index[axis];
}
for (auto corner = 0; corner < 8; ++corner)
{
auto w = 1.0;
std::array<int, 3> sample = {};
for (auto axis = 0; axis < 3; ++axis)
{
const auto upper = (corner >> axis & 1) != 0;
sample[axis] = index[axis] + (upper ? 1 : 0);
w *= upper ? weight[axis] : 1.0 - weight[axis];
}
const auto* const s = lut.rgba.data() +
((static_cast<size_t>(sample[2]) * n + sample[1]) * n + sample[0]) * 4;
for (auto c = 0; c < 3; ++c) result[c] += w * s[c];
}
return result;
};
std::vector<uint16_t> signal = {0, 0, 0, 65535, 65535, 65535, 65535, 0, 0,
0, 65535, 0, 0, 0, 65535, 32768, 16384, 49152};
uint32_t seed = 12345;
for (auto i = 0; i < 3 * 500; ++i)
{
seed = seed * 1664525u + 1013904223u;
signal.push_back(static_cast<uint16_t>(seed >> 16));
}
const auto pixels = signal.size() / 3;
std::vector<core::color32> fast(pixels);
std::vector<core::color32> baseline(pixels);
lut.apply(signal.data(), pixels, fast.data());
lut.apply_baseline(signal.data(), pixels, baseline.data());
auto worst = 0;
for (size_t i = 0; i < pixels; ++i)
{
const auto want = expected(signal.data() + i * 3);
for (const auto& [name, got] : {std::pair{"SIMD", fast[i]}, std::pair{"baseline", baseline[i]}})
{
// Blue is the low byte of a packed RGB pixel, red the third, and the cube's alpha is opaque.
const std::array channels = {static_cast<int>(got >> 16 & 0xff), static_cast<int>(got >> 8 & 0xff),
static_cast<int>(got & 0xff)};
assert_equal(0xffu, got >> 24, std::format("{} pixel {} is opaque", name, i));
for (auto c = 0; c < 3; ++c)
{
worst = std::max(worst,
std::abs(channels[c] - static_cast<int>(std::lround(want[c] * 255.0 / 65535.0))));
}
}
}
assert_equal(true, worst <= 1,
std::format("both lookups take the trilinear sample to within one level ({})", worst));
const auto sample_pixel = [&lut](const int r, const int g, const int b)
{
const auto* const s = lut.rgba.data() + ((static_cast<size_t>(b) * n + g) * n + r) * 4;
const auto byte = [](const uint16_t v)
{
return static_cast<uint32_t>(std::lround(v * 255.0 / 65535.0));
};
return 0xff000000u | byte(s[0]) << 16 | byte(s[1]) << 8 | byte(s[2]);
};
assert_equal(sample_pixel(n - 1, n - 1, n - 1), fast[1], "the brightest signal takes the last sample");
assert_equal(sample_pixel(0, 0, 0), fast[0], "and the darkest the first");
assert_equal(sample_pixel(n - 1, 0, 0), fast[2], "full red takes the sample at the end of the red axis");
}
static void should_convert_yuv_surfaces_for_software_rendering()
{
auto check_output = [](const core::surface_ptr& output)
{
assert_equal(true, core::is_valid(output), "converted YUV surface is valid");
assert_equal(uint32_t{4}, output->width(), "converted YUV width");
assert_equal(uint32_t{2}, output->height(), "converted YUV height");
for (auto y = 0; y < 2; ++y)
{
const auto* const row = output->pixels_line(y);
for (auto x = 0; x < 4; ++x)
{
const auto* const pixel = row + x * 4;
const auto expected = x < 2 ? 0 : 255;
assert_equal(true, std::abs(static_cast<int>(pixel[0]) - expected) <= 2, "YUV blue channel");
assert_equal(true, std::abs(static_cast<int>(pixel[1]) - expected) <= 2, "YUV green channel");
assert_equal(true, std::abs(static_cast<int>(pixel[2]) - expected) <= 2, "YUV red channel");
assert_equal(255, pixel[3], "YUV alpha channel");
}
}
};
av_scaler scaler;
const auto output = std::make_shared<core::surface>();
core::surface nv12;
nv12.alloc(4, 2, core::texture_format::NV12);
nv12.color_space(color_space::rec601_limited);
for (auto y = 0; y < 2; ++y)
{
auto* const row = nv12.pixels_line(y);
row[0] = row[1] = 16;
row[2] = row[3] = 235;
}
auto* const nv12_chroma = nv12.pixels() + nv12.stride() * nv12.height();
std::fill_n(nv12_chroma, nv12.stride(), uint8_t{128});
assert_equal(true, scaler.convert_yuv_surface(nv12, output), "convert NV12 surface");
check_output(output);
core::surface_ptr scaled;
const auto nv12_view = core::const_surface_ptr(&nv12, [](const core::surface*) {});
assert_equal(true, scaler.scale_surface(nv12_view, scaled, {2, 1}), "scale NV12 surface");
assert_equal(true, scaled->dimensions() == sizei{2, 1}, "scaled NV12 dimensions");
assert_equal(true, scaled->format() == core::texture_format::RGB, "scaled NV12 format");
core::surface p010;
p010.alloc(4, 2, core::texture_format::P010);
p010.color_space(color_space::rec601_limited);
for (auto y = 0; y < 2; ++y)
{
auto* const row = std::bit_cast<uint16_t*>(p010.pixels_line(y));
row[0] = row[1] = 64u << 6;
row[2] = row[3] = 940u << 6;
}
auto* const p010_chroma = std::bit_cast<uint16_t*>(p010.pixels() + p010.stride() * p010.height());
std::fill_n(p010_chroma, p010.stride() / sizeof(uint16_t), uint16_t{512u << 6});
assert_equal(true, scaler.convert_yuv_surface(p010, output), "convert P010 surface");
check_output(output);
const auto p010_view = core::const_surface_ptr(&p010, [](const core::surface*) {});
assert_equal(true, scaler.scale_surface(p010_view, scaled, {2, 1}), "scale P010 surface");
assert_equal(true, scaled->dimensions() == sizei{2, 1}, "scaled P010 dimensions");
assert_equal(true, scaled->format() == core::texture_format::RGB, "scaled P010 format");
}
// An enlargement is what area_downscale refuses, and it is the case the software backend hits every
// frame when the window is larger than the video. swscale answers it with an RGB->YUV->RGB round
// trip through its scalar full-chroma converter, so this path exists to keep it out of that.
static void should_bilinear_resize_packed_surfaces()
{
const auto make_source = [](const int cx, const int cy)
{
auto s = std::make_shared<core::surface>();
s->alloc(cx, cy, core::texture_format::ARGB, core::orientation::left_bottom);
s->color_space(color_space::rec709_limited);
return s;
};
// Two samples widened to four. Sampling at centres and clamping the outer half sample is what
// keeps the ends exact; extrapolating instead is what puts a dark fringe round an enlargement.
{
const auto src = make_source(2, 1);
auto* const row = src->pixels_line(0);
row[0] = row[1] = row[2] = row[3] = 0;
row[4] = row[5] = row[6] = row[7] = 200;
core::surface_ptr dst;
assert_equal(true, core::bilinear_resize(src, dst, {4, 1}), "widen a two sample ramp");
assert_equal(true, dst->dimensions() == sizei{4, 1}, "enlarged dimensions");
assert_equal(true, dst->format() == core::texture_format::ARGB, "enlarged format");
assert_equal(true, dst->orientation() == core::orientation::left_bottom, "enlarged orientation");
assert_equal(true, dst->color_space() == color_space::rec709_limited, "enlarged colour space");
const auto* const out = dst->pixels_line(0);
assert_equal(0, static_cast<int>(out[0]), "first sample keeps the source end");
assert_equal(50, static_cast<int>(out[4]), "quarter weight");
assert_equal(150, static_cast<int>(out[8]), "three quarter weight");
assert_equal(200, static_cast<int>(out[12]), "last sample keeps the source end");
}
// A flat surface must survive any enlargement untouched, which a pair of weights not summing to
// 256 would shift.
{
const auto src = make_source(7, 5);
for (auto y = 0; y < 5; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 7; ++x)
{
row[x * 4 + 0] = 19;
row[x * 4 + 1] = 140;
row[x * 4 + 2] = 233;
row[x * 4 + 3] = 77;
}
}
core::surface_ptr dst;
assert_equal(true, core::bilinear_resize(src, dst, {31, 22}), "enlarge a flat surface");
auto flat = true;
for (auto y = 0; y < 22; ++y)
{
const auto* const row = dst->pixels_line(y);
for (auto x = 0; x < 31; ++x)
{
flat = flat && row[x * 4 + 0] == 19 && row[x * 4 + 1] == 140 && row[x * 4 + 2] == 233 &&
row[x * 4 + 3] == 77;
}
}
assert_equal(true, flat, "an enlarged flat surface is unchanged");
}
// One axis growing while the other shrinks is neither a reduction nor an enlargement, so
// area_downscale refuses it and this is what answers it.
{
const auto src = make_source(16, 16);
core::surface_ptr dst;
assert_equal(false, core::area_downscale(src, dst, {32, 8}), "the area filter refuses a mixed resize");
assert_equal(true, core::bilinear_resize(src, dst, {32, 8}), "bilinear takes a mixed resize");
assert_equal(true, dst->dimensions() == sizei{32, 8}, "mixed resize dimensions");
}
// Planar surfaces are four bytes a pixel to this walk; they belong to swscale.
{
core::surface nv12;
nv12.alloc(8, 8, core::texture_format::NV12);
const auto nv12_view = core::const_surface_ptr(&nv12, [](const core::surface*) {});
core::surface_ptr dst;
assert_equal(false, core::bilinear_resize(nv12_view, dst, {16, 16}), "refuse a planar format");
}
}
static void should_area_downscale_packed_and_planar_surfaces()
{
const auto make_source = [](const int cx, const int cy, const core::texture_format format)
{
auto s = std::make_shared<core::surface>();
s->alloc(cx, cy, format, core::orientation::left_bottom);
s->color_space(color_space::rec709_limited);
return s;
};
// A flat source must survive a reduction untouched. Any weight run that does not sum to exactly
// 256 shifts the level here, which is the failure this whole scheme has to rule out.
{
const auto src = make_source(97, 61, core::texture_format::ARGB);
for (auto y = 0; y < 61; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 97; ++x)
{
row[x * 4 + 0] = 17;
row[x * 4 + 1] = 134;
row[x * 4 + 2] = 251;
row[x * 4 + 3] = 200;
}
}
core::surface_ptr dst;
assert_equal(true, core::area_downscale(src, dst, {13, 9}), "reduce a flat surface");
assert_equal(true, dst->dimensions() == sizei{13, 9}, "reduced dimensions");
assert_equal(true, dst->format() == core::texture_format::ARGB, "reduced format");
assert_equal(true, dst->orientation() == core::orientation::left_bottom, "reduced orientation");
assert_equal(true, dst->color_space() == color_space::rec709_limited, "reduced colour space");
for (auto y = 0; y < 9; ++y)
{
const auto* const row = dst->pixels_line(y);
for (auto x = 0; x < 13; ++x)
{
assert_equal(17, static_cast<int>(row[x * 4 + 0]), "flat blue");
assert_equal(134, static_cast<int>(row[x * 4 + 1]), "flat green");
assert_equal(251, static_cast<int>(row[x * 4 + 2]), "flat red");
assert_equal(200, static_cast<int>(row[x * 4 + 3]), "flat alpha");
}
}
}
// An exact 2:1 reduction must be the mean of each 2x2 block, so a checkerboard of 0 and 200
// answers 100 everywhere. Nearest neighbour or a dropped contribution cannot produce that.
{
const auto src = make_source(64, 64, core::texture_format::RGB);
for (auto y = 0; y < 64; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 64; ++x)
{
const auto v = static_cast<uint8_t>(((x + y) & 1) ? 200 : 0);
row[x * 4 + 0] = row[x * 4 + 1] = row[x * 4 + 2] = row[x * 4 + 3] = v;
}
}
core::surface_ptr dst;
assert_equal(true, core::area_downscale(src, dst, {32, 32}), "reduce a checkerboard");
for (auto y = 0; y < 32; ++y)
{
const auto* const row = dst->pixels_line(y);
for (auto x = 0; x < 32; ++x)
{
assert_equal(100, static_cast<int>(row[x * 4 + 1]), "checkerboard mean");
}
}
}
// A horizontal ramp reduced 4:1 must stay monotonic and keep its ends, which a wrong first index
// or a truncated run would break.
{
const auto src = make_source(256, 8, core::texture_format::RGB);
for (auto y = 0; y < 8; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 256; ++x)
{
row[x * 4 + 0] = row[x * 4 + 1] = row[x * 4 + 2] = static_cast<uint8_t>(x);
row[x * 4 + 3] = 255;
}
}
core::surface_ptr dst;
assert_equal(true, core::area_downscale(src, dst, {64, 2}), "reduce a ramp");
const auto* const row = dst->pixels_line(0);
assert_equal(2, static_cast<int>(row[0]), "ramp first sample");
assert_equal(254, static_cast<int>(row[63 * 4]), "ramp last sample");
auto monotonic = true;
for (auto x = 1; x < 64; ++x) monotonic = monotonic && row[x * 4] > row[(x - 1) * 4];
assert_equal(true, monotonic, "ramp stays monotonic");
}
// Refusals hand the work back to swscale rather than producing a wrong picture.
{
const auto rgb = make_source(8, 8, core::texture_format::RGB);
core::surface_ptr dst;
assert_equal(false, core::area_downscale(rgb, dst, {16, 4}), "refuse an enlargement");
assert_equal(false, core::area_downscale(rgb, dst, {0, 4}), "refuse an empty extent");
// P010 samples are 16 bit, which the Q8 accumulators cannot hold.
core::surface p010;
p010.alloc(8, 8, core::texture_format::P010);
const auto p010_view = core::const_surface_ptr(&p010, [](const core::surface*) {});
assert_equal(false, core::area_downscale(p010_view, dst, {4, 4}), "refuse a 16 bit planar format");
// NV12 chroma is one pair per 2x2 luma block, so an odd extent has no whole answer.
core::surface nv12;
nv12.alloc(8, 8, core::texture_format::NV12);
const auto nv12_view = core::const_surface_ptr(&nv12, [](const core::surface*) {});
assert_equal(false, core::area_downscale(nv12_view, dst, {5, 4}), "refuse an odd nv12 extent");
}
// NV12 reduces as two planes: the luma on its own, and the interleaved chroma pair at half the
// extent. A flat picture must survive both, and U must not bleed into V.
{
core::surface nv12;
nv12.alloc(64, 32, core::texture_format::NV12);
nv12.color_space(color_space::rec601_full);
const auto stride = nv12.stride();
auto* const luma = nv12.pixels();
auto* const chroma = luma + stride * 32;
for (auto y = 0; y < 32; ++y)
{
auto* const row = luma + stride * y;
for (auto x = 0; x < 64; ++x) row[x] = static_cast<uint8_t>(((x + y) & 1) ? 200 : 0);
}
for (auto y = 0; y < 16; ++y)
{
auto* const row = chroma + stride * y;
for (auto x = 0; x < 32; ++x)
{
row[x * 2] = 90;
row[x * 2 + 1] = 210;
}
}
const auto nv12_view = core::const_surface_ptr(&nv12, [](const core::surface*) {});
core::surface_ptr dst;
assert_equal(true, core::area_downscale(nv12_view, dst, {32, 16}), "reduce an nv12 surface");
assert_equal(true, dst->format() == core::texture_format::NV12, "reduced nv12 format");
assert_equal(true, dst->color_space() == color_space::rec601_full, "reduced nv12 colour space");
const auto dst_stride = dst->stride();
auto luma_correct = true;
for (auto y = 0; y < 16; ++y)
{
const auto* const row = dst->pixels() + dst_stride * y;
for (auto x = 0; x < 32; ++x) luma_correct = luma_correct && row[x] == 100;
}
assert_equal(true, luma_correct, "nv12 luma is the 2x2 mean");
auto chroma_correct = true;
for (auto y = 0; y < 8; ++y)
{
const auto* const row = dst->pixels() + dst_stride * 16 + dst_stride * y;
for (auto x = 0; x < 16; ++x)
{
chroma_correct = chroma_correct && row[x * 2] == 90 && row[x * 2 + 1] == 210;
}
}
assert_equal(true, chroma_correct, "nv12 chroma keeps u and v apart");
}
// Same size on one axis is still a reduction; the identity run must not be rejected or shifted.
{
const auto src = make_source(40, 10, core::texture_format::RGB);
for (auto y = 0; y < 10; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 40; ++x)
{
row[x * 4 + 0] = row[x * 4 + 1] = row[x * 4 + 2] = row[x * 4 + 3] = static_cast<uint8_t>(
(x == 20) ? 240 : 10);
}
}
core::surface_ptr dst;
assert_equal(true, core::area_downscale(src, dst, {40, 5}), "reduce one axis only");
assert_equal(240, static_cast<int>(dst->pixels_line(0)[20 * 4]), "identity axis keeps the column");
assert_equal(10, static_cast<int>(dst->pixels_line(0)[19 * 4]), "identity axis keeps its neighbour");
}
// The AVX2 and SSE2 accumulators must agree byte for byte, or a picture would depend on the CPU
// it was reduced on. Sizes are deliberately not multiples of the vector widths so both the wide
// loops and their scalar tails are covered.
{
const auto src = make_source(211, 97, core::texture_format::ARGB);
auto seed = 0x9e3779b9u;
for (auto y = 0; y < 97; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 211 * 4; ++x)
{
seed = seed * 1664525u + 1013904223u;
row[x] = static_cast<uint8_t>(seed >> 24);
}
}
for (const auto extent : {sizei{53, 29}, sizei{211, 12}, sizei{7, 97}, sizei{210, 96}})
{
core::surface_ptr wide;
core::surface_ptr baseline;
assert_equal(true, core::area_downscale(src, wide, extent), "reduce with the selected width");
assert_equal(true, core::area_downscale_baseline(src, baseline, extent), "reduce with the SSE2 baseline");
auto identical = true;
for (auto y = 0; y < extent.cy; ++y)
{
identical = identical && memcmp(wide->pixels_line(y), baseline->pixels_line(y),
static_cast<size_t>(extent.cx) * 4u) == 0;
}
assert_equal(true, identical, std::format("SIMD widths agree at {}x{}", extent.cx, extent.cy));
}
}
}
static void should_estimate_decode_cost()
{
// The estimate reads only the header fields, but an image with no bytes counts as empty.
const auto make_image = [](const core::image_format format)
{
core::blob bytes;
bytes.resize(16);
return std::make_shared<core::image>(std::move(bytes), sizei{4000, 3000}, format, core::orientation::top_left);
};
const auto png = make_image(core::image_format::PNG);
assert_equal(4000ll * 3000ll * 4, static_cast<uint64_t>(files::estimate_decode_bytes(png, {400, 300})),
"a PNG builds the whole frame however small a target is asked for");
const auto jpeg = make_image(core::image_format::JPEG);
assert_equal(500ll * 375ll * 4, static_cast<uint64_t>(files::estimate_decode_bytes(jpeg, {400, 300})),
"libjpeg reduces by up to 1/8 while decoding");
assert_equal(4000ll * 3000ll * 4, static_cast<uint64_t>(files::estimate_decode_bytes(jpeg, {4000, 3000})),
"a full-size request costs a JPEG its whole frame");
assert_equal(1000ll * 1000ll * 4, static_cast<uint64_t>(files::estimate_decode_bytes(sizei{1000, 1000})),
"a bare source size costs four bytes a pixel");
}
static void should_refuse_over_budget_sources()
{
const auto restore_budget = render::max_decode_bytes;
const core::scope_exit restore([restore_budget] { render::max_decode_bytes = restore_budget; });
render::max_decode_bytes = 64ll * 1024ll * 1024ll; // exactly 4096 x 4096
load_diagnostic fits;
assert_equal(false, reject_over_budget_source(&fits, {4096, 4096}, "test"), "a source at the budget is decoded");
assert_equal(false, fits.over_budget, "a source that fits is not flagged");
assert_equal(4096, fits.source_dimensions.cx, "the size is reported whether or not it fits");
load_diagnostic refused;
assert_equal(true, reject_over_budget_source(&refused, {4097, 4096}, "test"), "one column over is refused");
assert_equal(true, refused.over_budget, "the refusal reaches the caller");
assert_equal(4097, refused.source_dimensions.cx, "the refused size is reported so it can be shown");
assert_equal(true, files::exceeds_decode_budget(sizei{8000, 8000}), "the predicate agrees with the loader");
assert_equal(true, reject_over_budget_source(nullptr, {8000, 8000}, "test"),
"a caller that wants no diagnostic is still told to refuse");
}
// LibRaw builds the whole frame inside unpack/dcraw_process, so the budget has to be asked before
// that starts. RAW was the one source that allocated a full frame without asking, so a raw large
// enough to exhaust the machine walked straight past the ceiling every other decoder observes.
static void should_refuse_an_over_budget_raw()
{
const auto path = test_files_folder().combine("raw").combine_file("Screws.CR2");
const auto restore_budget = render::max_decode_bytes;
const core::scope_exit restore([restore_budget] { render::max_decode_bytes = restore_budget; });
files ff;
// The preview path answers first and is not the subject, so it is asked for the full decode.
const auto whole = ff.load(path, false);
assert_equal(true, whole.success, "the fixture decodes within the default budget");
const auto dimensions = whole.dimensions();
assert_equal(false, dimensions.is_empty(), "and reports its size");
// One pixel's worth under what this source needs.
render::max_decode_bytes = static_cast<int64_t>(dimensions.cx) * dimensions.cy * 4 - 4;
const auto refused = ff.load(path, false);
assert_equal(false, refused.success, "a raw past the budget is refused");
assert_equal(true, refused.reason == file_load_result::failure::too_large, "and says so as a property of the file");
assert_equal(false, is_valid(refused.s), "no frame was committed");
assert_equal(false, is_valid(refused.i), "and no image either");
}
static void should_animate_alpha_between_values()
{
// The gate is off when the CPU software backend is active, so force the animated
// behaviour under test and restore whatever the running renderer selected.
const auto restore_animations = render::animations_enabled;
const core::scope_exit restore_scope([restore_animations] { render::animations_enabled = restore_animations; });
render::animations_enabled = true;
ui::animate_alpha alpha;
alpha.reset(0.0f, 1.0f);
assert_equal(0.0f, alpha.val(), "fade-in starts at zero");
assert_equal(1.0f, alpha.target(), "fade-in targets one");
assert_equal(true, alpha.step(), "fade-in advances");
assert_equal(true, alpha.val() > 0.0f && alpha.val() < 1.0f, "fade-in interpolates");
alpha.target(0.0f);
assert_equal(true, alpha.val() > 0.0f, "retarget preserves current value");
assert_equal(true, alpha.step(), "fade-out advances");
for (auto i = 0; i < 100; ++i) alpha.step();
assert_equal(0.0f, alpha.val(), "fade-out reaches target");
assert_equal(false, alpha.step(), "completed animation stops");
alpha.reset(0.0005f, 0.0f);
assert_equal(true, alpha.step(), "final snap requests a frame");
assert_equal(0.0f, alpha.val(), "final snap reaches target");
assert_equal(false, alpha.step(), "final snap completes once");
}
// A fade used to advance a fixed fraction per frame, so the same fade ran twice as fast on a 120Hz
// display as on a 60Hz one. It now advances by elapsed time, so equal wall-clock time must leave it
// in the same place whatever the refresh rate that produced it.
static void should_fade_at_the_same_rate_on_any_refresh_rate()
{
const auto restore_animations = render::animations_enabled;
const auto restore_factor = ui::animation_step_factor;
const core::scope_exit restore_scope([restore_animations, restore_factor]
{
render::animations_enabled = restore_animations;
ui::animation_step_factor = restore_factor;
});
render::animations_enabled = true;
const auto fade_for = [](const int frames_per_second, const double seconds)
{
const auto dt = 1.0 / frames_per_second;
ui::animation_step_factor = static_cast<float>(1.0 - std::exp(-ui::alpha_fade_rate * dt));
ui::animate_alpha alpha;
alpha.reset(0.0f, 1.0f);
for (auto i = 0; i < static_cast<int>(seconds * frames_per_second); ++i) alpha.step();
return alpha.val();
};
constexpr auto quarter_second = 0.25;
const auto at_60 = fade_for(60, quarter_second);
const auto at_120 = fade_for(120, quarter_second);
const auto at_30 = fade_for(30, quarter_second);
assert_equal(true, at_60 > 0.1f && at_60 < 0.999f, "a quarter second is mid-fade");
assert_equal(true, std::abs(at_120 - at_60) < 0.02f, "120Hz matches 60Hz at the same elapsed time");
assert_equal(true, std::abs(at_30 - at_60) < 0.02f, "30Hz matches 60Hz at the same elapsed time");
// The replaced constant: a 60Hz frame must still close a third of the remaining distance, so
// nothing about the feel of a fade changes on the refresh rate almost every display reports.
ui::animation_step_factor = static_cast<float>(1.0 - std::exp(-ui::alpha_fade_rate / 60.0));
assert_equal(true, std::abs(ui::animation_step_factor - 0.333f) < 0.001f, "60Hz reproduces the old step");
}
static void should_skip_alpha_animation_when_disabled()
{
// CPU software rendering cannot afford per-frame fades: every alpha reaches its target
// immediately so draws stay on the opaque fast paths and no extra frames are requested.
const auto restore_animations = render::animations_enabled;
const core::scope_exit restore_scope([restore_animations] { render::animations_enabled = restore_animations; });
render::animations_enabled = false;
ui::animate_alpha alpha;
alpha.reset(0.0f, 1.0f);
assert_equal(1.0f, alpha.val(), "fade-in starts complete");
assert_equal(false, alpha.step(), "fade-in requests no frame");
alpha.target(0.0f);
assert_equal(0.0f, alpha.val(), "fade-out completes immediately");
assert_equal(false, alpha.step(), "fade-out requests no frame");
}
static void should_resize()
{
const auto save_path = test_temps.next_path();
const auto load_path = test_files_folder().combine_file("Test.jpg");
image_edits edits;
edits.scale(sizei(200, 150));
files ff;
ff.update(load_path, save_path, {}, edits, {}, false, {});
const auto actual = extract_properties(save_path);
if (!actual)
{
throw test_assert_exception(
std::format("Should resize: could not extract properties from {}", save_path.str()));
}
assert_equal(200, actual->width);
assert_equal(133, actual->height);
}
// Issue #102 - Rotating an image (the transform behind the [ and ] shortcuts and
// the rotate_clockwise/anticlockwise commands). These tests validate the 90-degree
// rotation pipeline across JPEG (incl. lossless + EXIF-oriented), PNG and WebP;
// the keyboard-shortcut dispatch reported in #102 is a UI-level concern.
static void should_rotate()
{
files ff;
{
const auto save_path = test_temps.next_path();
const auto load_path = test_files_folder().combine_file("Test.jpg");
const auto loaded = ff.load(load_path, false);
image_edits edits;
const quadd crop(loaded.i->dimensions());
edits.crop_bounds(crop.transform(core::simple_transform::rot_90));
metadata_edits md_edits;
md_edits.orientation = core::orientation::top_left;
ff.update(load_path, save_path, md_edits, edits, {}, false, {});
const auto expected = extract_properties(test_files_folder().combine_file("Test90.jpg"));
const auto actual = extract_properties(save_path);
// Test.jpg is 683 pixels tall, which is not a whole number of MCUs, so the lossless path
// declines it. The rotate must still keep every row rather than trimming to the MCU grid,
// which the Test90.jpg fixture does not - it was captured from the old trimming path.
expected->width = static_cast<uint16_t>(loaded.i->height());
expected->height = static_cast<uint16_t>(loaded.i->width());
assert_metadata(*expected, *actual);
}
{
const auto save_path = test_temps.next_path();
const auto load_path = test_files_folder().combine_file("exif-rotated.jpg");
const auto loaded = ff.load(load_path, false);
image_edits edits;
const quadd crop(loaded.i->dimensions());
edits.crop_bounds(crop.transform(core::simple_transform::rot_90));
metadata_edits md_edits;
md_edits.orientation = core::orientation::top_left;
ff.update(load_path, save_path, md_edits, edits, {}, false, {});
const auto actual_exif = extract_properties(save_path, metadata_type::EXIF);
assert_equal(core::orientation::top_left, actual_exif->orientation, "orientation");
}
{
// PNG
const auto save_path = test_temps.next_path(".png");
const auto load_path = test_files_folder().combine_file("engine.png");
const auto loaded = ff.load(load_path, false);
image_edits edits;
const quadd crop(loaded.i->dimensions());
edits.crop_bounds(crop.transform(core::simple_transform::rot_90));
ff.update(load_path, save_path, {}, edits, {}, false, {});
const auto updated = ff.load(save_path, false);
assert_equal(loaded.i->height(), updated.i->width(), "png width");
assert_equal(loaded.i->width(), updated.i->height(), "png height");
}
{
// WEBP
const auto save_path = test_temps.next_path(".webp");
const auto load_path = test_files_folder().combine_file("lake.webp");
const auto loaded = ff.load(load_path, false);
image_edits edits;
const quadd crop(loaded.i->dimensions());
edits.crop_bounds(crop.transform(core::simple_transform::rot_90));
ff.update(load_path, save_path, {}, edits, {}, false, {});
const auto updated = ff.load(save_path, false);
assert_equal(false, updated.is_empty(), "webp result empty");
assert_equal(loaded.i->height(), updated.i->width(), "webp width");
assert_equal(loaded.i->width(), updated.i->height(), "webp height");
}
}
static void should_not_cpu_transform_planar_surfaces()
{
const auto planar = std::make_shared<core::surface>();
assert_equal(true, planar->alloc(8, 8, core::texture_format::NV12), "nv12 surface allocates");
const auto rotated = planar->transform(core::simple_transform::rot_90);
assert_equal(false, core::is_valid(rotated), "planar surfaces are not transformed as packed pixels");
}
static void should_rotate133()
{
const auto save_path = test_temps.next_path();
const auto load_path = test_files_folder().combine_file("Test.jpg");
files ff;
const auto loaded = ff.load(load_path, false);
const quadd crop(loaded.i->dimensions());
image_edits edits;
edits.crop_bounds(crop.rotate(133, crop.center_point()));
ff.update(load_path, save_path, {}, edits, {}, false, {});
const auto actual = extract_properties(save_path);
const auto expected = expected_test_jpg();
expected->width = 576;
expected->height = 384;
assert_metadata(*expected, *actual);
}
// The neutral parameter set is the one a picture with no edits is rendered through, so any drift in
// the tone curve, the luma ratio or the rounding shows up here as a picture the app quietly
// altered. Truncating instead of rounding moves every channel a level and fails this.
static void should_leave_colour_unchanged_when_neutral()
{
const auto src = std::make_shared<core::surface>();
assert_equal(true, src->alloc(16, 4, core::texture_format::ARGB), "neutral source allocated");
for (auto y = 0; y < 4; ++y)
{
auto* const row = src->pixels_line(y);
for (auto x = 0; x < 16; ++x)
{
row[x * 4 + 0] = static_cast<uint8_t>(x * 17);
row[x * 4 + 1] = static_cast<uint8_t>(255 - x * 17);
row[x * 4 + 2] = static_cast<uint8_t>((x * 37 + y * 11) & 0xff);
row[x * 4 + 3] = 255;
}
}
const auto dst = std::make_shared<core::surface>();
assert_equal(true, dst->alloc(16, 4, core::texture_format::ARGB), "neutral destination allocated");
render::color_adjust adjust;
adjust.color_params(0, 0, 0, 0, 0, 0, 0);
adjust.apply(src, dst->pixels(), dst->stride(), {});
auto worst = 0;
auto bias = 0;
auto samples = 0;
for (auto y = 0; y < 4; ++y)
{
const auto* const in = src->pixels_line(y);
const auto* const out = dst->pixels_line(y);
for (auto i = 0; i < 16 * 4; ++i)
{
const auto delta = static_cast<int>(out[i]) - static_cast<int>(in[i]);
worst = std::max(worst, std::abs(delta));
bias += delta;
++samples;
}
}
// One level of slack for floating point; two would hide a real curve error.
assert_equal(true, worst <= 1, "neutral adjustment leaves the picture alone");
// The residual must also be unbiased. A worst-case bound alone cannot see truncation, which costs
// at most one level but costs it in the same direction on every pixel, darkening the whole picture.
assert_equal(true, std::abs(bias) * 8 <= samples, "the neutral residual is not biased in one direction");
}
// The GPU preview and the saved result must agree: populate_texture_transform is a decimation of the
// same curve apply() uses, so a preview that does not track it shows the user one picture and commits
// another. The tolerance here is a level either side of the byte quantisation -- it catches a curve
// that is missing, stale, inverted or wrongly scaled, not a one-bin sampling offset.
static void should_match_the_preview_curve_to_the_applied_curve()
{
// A neutral adjustment must leave the preview at the identity ramp the transform is born with,
// or an unedited picture is previewed through a curve.
{