-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLightnessComputer.hpp
More file actions
830 lines (741 loc) · 22.1 KB
/
LightnessComputer.hpp
File metadata and controls
830 lines (741 loc) · 22.1 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
#pragma once
#include <boost/functional/hash.hpp>
#include <halp/controls.hpp>
#include <halp/layout.hpp>
#include <halp/meta.hpp>
#include <halp/texture.hpp>
#include <chrono>
#include <halp/controls.enums.hpp>
namespace vo
{
// Graphical item which will display the texture
struct LightnessComputerTextureDisplay
{
using item_type = LightnessComputerTextureDisplay;
static constexpr double width() { return 64.; }
static constexpr double height() { return 64.; }
void paint(auto ctx)
{
if(m_w <= 0 || m_h <= 0 || !m_bytes.get())
return;
ctx.draw_bytes(0, 0, width(), height(), m_bytes.get(), m_w, m_h);
ctx.update();
}
void update(std::shared_ptr<float[]>&& bytes, int w, int h)
{
m_bytes = std::move(bytes);
m_w = w;
m_h = h;
}
std::shared_ptr<float[]> m_bytes;
int m_w{0};
int m_h{0};
};
template <halp::static_string swizzle, int eightbit, std::size_t N>
static void make_swizzle_static(auto* samples, int& i, const auto (&rgbw)[N], auto a)
{
for(char c : swizzle.value)
{
switch(c)
{
case 'r':
samples[i++] = rgbw[0] * eightbit;
break;
case 'g':
samples[i++] = rgbw[1] * eightbit;
break;
case 'b':
samples[i++] = rgbw[2] * eightbit;
break;
case 'w':
samples[i++] = rgbw[3] * eightbit;
break;
case 'h':
if constexpr(
std::find(std::begin(swizzle.value), std::end(swizzle.value), 'h')
!= std::end(swizzle.value))
samples[i++] = rgbw[4];
break;
case 's':
if constexpr(
std::find(std::begin(swizzle.value), std::end(swizzle.value), 's')
!= std::end(swizzle.value))
samples[i++] = rgbw[5];
break;
case 'l':
if constexpr(
std::find(std::begin(swizzle.value), std::end(swizzle.value), 'l')
!= std::end(swizzle.value))
samples[i++] = rgbw[6];
break;
case 'v': // FIXME
if constexpr(
std::find(std::begin(swizzle.value), std::end(swizzle.value), 'v')
!= std::end(swizzle.value))
samples[i++] = rgbw[6];
break;
case 'a':
samples[i++] = a * eightbit;
break;
case '0':
samples[i++] = 0;
break;
case '1':
samples[i++] = eightbit;
break;
default:
case 0:
break;
}
}
}
template <int eightbit, std::size_t N>
static void make_swizzle_dynamic(
std::string_view swizzle, auto* samples, int& i, const auto (&rgbw)[N], auto a)
{
if(swizzle.empty())
return;
for(char c : swizzle)
{
switch(c)
{
case 'r':
samples[i++] = rgbw[0] * eightbit;
break;
case 'g':
samples[i++] = rgbw[1] * eightbit;
break;
case 'b':
samples[i++] = rgbw[2] * eightbit;
break;
case 'w':
samples[i++] = rgbw[3] * eightbit;
break;
case 'h':
if constexpr(N > 4)
samples[i++] = rgbw[4];
break;
case 's':
if constexpr(N > 4)
samples[i++] = rgbw[5];
break;
case 'l':
if constexpr(N > 4)
samples[i++] = rgbw[6];
break;
case 'v': // FIXME
if constexpr(N > 4)
samples[i++] = rgbw[6];
break;
case 'a':
samples[i++] = a * eightbit;
break;
case '0':
samples[i++] = 0;
break;
case '1':
samples[i++] = eightbit;
break;
default:
case 0:
break;
}
}
}
// Main processor object
struct LightnessComputer
{
public:
halp_meta(name, "Lightness computer");
halp_meta(c_name, "lightness_computer");
halp_meta(category, "Visuals/Analysis");
halp_meta(author, "Jean-Michaël Celerier");
halp_meta(description, "Convert an image to a list of lightness values.");
halp_meta(
manual_url,
"https://ossia.io/score-docs/processes/pixel-utilities.html#lightness-computer")
halp_meta(uuid, "60a11c39-dc14-449d-bbe4-1c51e44ea99a");
struct ins
{
halp::fixed_texture_input<"In", halp::rgba32f_texture> image{
.request_width = 16, .request_height = 16};
halp::xy_spinboxes_i32<"Size", halp::range{1, 512, 10}> size;
struct mode
{
halp__enum_combobox(
"Mode", Lightness, Lightness, Value, Saturation, Hue, HSV, HSL, Red, Green,
Blue, Alpha, RGB, RGBW, Swizzle)
} mode;
struct : halp::lineedit<"Swizzle", "">
{
void update(LightnessComputer& self) { self.update_swizzle(); }
} swizzle;
struct : halp::toggle<"8-bit (* 255)">
{
void update(LightnessComputer& self) { self.update_swizzle(); }
} eightbit;
} inputs;
struct
{
halp::val_port<"Samples", std::vector<float>> samples;
} outputs;
// Some utility functions to extract lightness
static constexpr double linear_to_y(float r, float g, float b) noexcept
{
return 0.2126f * r + 0.7152f * g + 0.0722f * b;
}
static constexpr double y_to_lstar(float y) noexcept
{
if(y <= 216.f / 24389.f)
return y * (24.389f / 27.f);
else
return 0.01f * (std::cbrt(y) * 116.f - 16.f);
}
static std::array<double, 3> hsv(double r, double g, double b)
{
const double var_R = r;
const double var_G = g;
const double var_B = b;
const auto var_Min = std::min(std::min(var_R, var_G), var_B); // Min. value of RGB
const auto var_Max = std::max(std::max(var_R, var_G), var_B); // Max. value of RGB
const auto del_Max = var_Max - var_Min; // Delta RGB value
if(del_Max == 0.) // This is a gray, no chroma...
{
return {0., 0., (double)var_Max};
}
else // Chromatic data...
{
double H{};
auto S = del_Max / var_Max;
auto V = var_Max;
auto del_R = (((var_Max - var_R) / 6.) + (del_Max / 2.)) / del_Max;
auto del_G = (((var_Max - var_G) / 6.) + (del_Max / 2.)) / del_Max;
auto del_B = (((var_Max - var_B) / 6.) + (del_Max / 2.)) / del_Max;
if(var_R == var_Max)
H = del_B - del_G;
else if(var_G == var_Max)
H = (1. / 3.) + del_R - del_B;
else if(var_B == var_Max)
H = (2. / 3.) + del_G - del_R;
if(H < 0.)
H += 1.;
if(H > 1.)
H -= 1.;
return {H, S, V};
}
}
// https://gist.github.com/ciembor/1494530
static std::array<double, 3> hsl(float r, float g, float b)
{
struct hsl
{
double h, s, l;
} result;
float max = std::max(std::max(r, g), b);
float min = std::min(std::min(r, g), b);
result.h = result.s = result.l = (max + min) / 2;
if(max == min)
{
result.h = result.s = 0; // achromatic
}
else
{
float d = max - min;
result.s = (result.l > 0.5) ? d / (2 - max - min) : d / (max + min);
if(max == r)
{
result.h = (g - b) / d + (g < b ? 6 : 0);
}
else if(max == g)
{
result.h = (b - r) / d + 2;
}
else if(max == b)
{
result.h = (r - g) / d + 4;
}
result.h /= 6;
}
return {result.h, result.s, result.l};
}
//http://blog.saikoled.com/post/44677718712/how-to-convert-from-hsi-to-rgb-white
static void hsi2rgbw(double h, double s, double i, double* rgbw)
{
using namespace std;
double r, g, b, w;
double cos_h, cos_1047_h;
//h = fmod(h,360); // cycle h around to 0-360 degrees
h = h * 2. * 3.14159; // * h = 3.14159 * h / 180.; // Convert to radians.
// s /= 100.;
// i /= 100.; //from percentage to ratio
s = s > 0. ? (s < 1. ? s : 1.) : 0.; // clamp s and i to interval [0,1]
i = i > 0. ? (i < 1. ? i : 1.) : 0.;
i = i * sqrt(i); //shape intensity to have finer granularity near 0
if(h < 2.09439)
{
cos_h = cos(h);
cos_1047_h = cos(1.047196667 - h);
r = s * 255. * i / 3. * (1. + cos_h / cos_1047_h);
g = s * 255. * i / 3. * (1. + (1. - cos_h / cos_1047_h));
b = 0.;
w = 255. * (1. - s) * i;
}
else if(h < 4.188787)
{
h = h - 2.09439;
cos_h = cos(h);
cos_1047_h = cos(1.047196667 - h);
g = s * 255. * i / 3. * (1. + cos_h / cos_1047_h);
b = s * 255. * i / 3. * (1. + (1. - cos_h / cos_1047_h));
r = 0.;
w = 255. * (1. - s) * i;
}
else
{
h = h - 4.188787;
cos_h = cos(h);
cos_1047_h = cos(1.047196667 - h);
b = s * 255. * i / 3. * (1. + cos_h / cos_1047_h);
r = s * 255. * i / 3. * (1. + (1. - cos_h / cos_1047_h));
g = 0.;
w = 255. * (1. - s) * i;
}
rgbw[0] = r;
rgbw[1] = g;
rgbw[2] = b;
rgbw[3] = w;
}
// Communication with UI
struct processor_to_ui
{
halp_flag(relocatable);
std::shared_ptr<float[]> bytes;
int w, h;
};
std::function<void(processor_to_ui)> send_message;
using clk = std::chrono::steady_clock;
clk::time_point last_ui_message = clk::now();
void prepare()
{
inputs.image.request_width = inputs.size.value.x;
inputs.image.request_height = inputs.size.value.y;
}
void apply(auto func)
{
auto& in_tex = inputs.image.texture;
#pragma omp simd
for(int y = 0; y < in_tex.height; y++)
{
auto row = inputs.image.row(y);
for(int x = 0; x < in_tex.width; x++)
{
const auto [r, g, b, a] = inputs.image.get(x, row);
func(r, g, b, a);
}
}
}
std::string m_current_swizzle;
using swizzle_func_t = void (*)(LightnessComputer& self, std::string_view sw, int& i);
swizzle_func_t m_current_swizzle_func
= [](LightnessComputer& self, std::string_view sw, int& i) { };
void update_swizzle()
{
if(inputs.eightbit)
m_current_swizzle_func = make_swizzle_func<255>();
else
m_current_swizzle_func = make_swizzle_func<1>();
}
template <int eightbit>
swizzle_func_t make_swizzle_func()
{
std::string& sw = m_current_swizzle;
sw = inputs.swizzle.value;
bool has_w = false;
bool has_hsl = false;
// FIXME amber
for(char& c : sw)
{
if(c >= 'A' && c <= 'Z')
c += ('a' - 'A');
if(c == 'w')
has_w = true;
if(c == 'h' || c == 's' || c == 'l')
has_hsl = true;
}
#define RGBA_SWIZZLE(str) \
else if(sw == #str) \
{ \
return +[](LightnessComputer& self, std::string_view sw, int& i) { \
auto& samples = self.outputs.samples.value; \
self.apply([&](auto r, auto g, auto b, auto a) { \
decltype(r) rgbw[8]; \
rgbw[0] = r; \
rgbw[1] = g; \
rgbw[2] = b; \
rgbw[3] = 0; \
make_swizzle_static<#str, eightbit>(samples.data(), i, rgbw, a); \
}); \
}; \
}
#define RGBW_SWIZZLE(str) \
else if(sw == #str) \
{ \
return +[](LightnessComputer& self, std::string_view sw, int& i) { \
auto& samples = self.outputs.samples.value; \
self.apply([&](auto r, auto g, auto b, auto a) { \
const auto [h, s, l] = hsl(r, g, b); \
double rgbw[8]; \
hsi2rgbw(h, s, l, rgbw); \
make_swizzle_static<#str, eightbit>(samples.data(), i, rgbw, a); \
}); \
}; \
}
if(has_hsl && has_w)
{
return +[](LightnessComputer& self, std::string_view sw, int& i) {
auto& samples = self.outputs.samples.value;
self.apply([&](auto r, auto g, auto b, auto a) {
const auto [h, s, l] = hsl(r, g, b);
double rgbw[8];
hsi2rgbw(h, s, l, rgbw);
rgbw[4] = h;
rgbw[5] = s;
rgbw[6] = l;
make_swizzle_dynamic<eightbit>(sw, samples.data(), i, rgbw, a);
});
};
}
else if(has_hsl)
{
return +[](LightnessComputer& self, std::string_view sw, int& i) {
auto& samples = self.outputs.samples.value;
self.apply([&](auto r, auto g, auto b, auto a) {
const auto [h, s, l] = hsl(r, g, b);
double rgbw[8];
rgbw[0] = r;
rgbw[1] = g;
rgbw[2] = b;
// no w, only l
rgbw[4] = h;
rgbw[5] = s;
rgbw[6] = l;
make_swizzle_dynamic<eightbit>(sw, samples.data(), i, rgbw, a);
});
};
}
else if(has_w)
{
if(0)
{
}
RGBW_SWIZZLE(rgbw)
RGBW_SWIZZLE(rbgw)
RGBW_SWIZZLE(grbw)
RGBW_SWIZZLE(gbrw)
RGBW_SWIZZLE(bgrw)
RGBW_SWIZZLE(brgw)
else
{
return +[](LightnessComputer& self, std::string_view sw, int& i) {
auto& samples = self.outputs.samples.value;
self.apply([&](auto r, auto g, auto b, auto a) {
const auto [h, s, l] = hsl(r, g, b);
double rgbw[8];
hsi2rgbw(h, s, l, rgbw);
make_swizzle_dynamic<eightbit>(sw, samples.data(), i, rgbw, a);
});
};
}
}
else
{
if(0)
{
}
RGBA_SWIZZLE(rgb)
RGBA_SWIZZLE(rbg)
RGBA_SWIZZLE(grb)
RGBA_SWIZZLE(gbr)
RGBA_SWIZZLE(brg)
RGBA_SWIZZLE(bgr)
RGBA_SWIZZLE(rgba)
RGBA_SWIZZLE(rgb0)
RGBA_SWIZZLE(rgb1)
RGBA_SWIZZLE(argb)
RGBA_SWIZZLE(0rgb)
RGBA_SWIZZLE(1rgb)
RGBA_SWIZZLE(rbga)
RGBA_SWIZZLE(rbg0)
RGBA_SWIZZLE(rbg1)
RGBA_SWIZZLE(arbg)
RGBA_SWIZZLE(0rbg)
RGBA_SWIZZLE(1rbg)
RGBA_SWIZZLE(grba)
RGBA_SWIZZLE(grb0)
RGBA_SWIZZLE(grb1)
RGBA_SWIZZLE(agrb)
RGBA_SWIZZLE(0grb)
RGBA_SWIZZLE(1grb)
RGBA_SWIZZLE(gbra)
RGBA_SWIZZLE(gbr0)
RGBA_SWIZZLE(gbr1)
RGBA_SWIZZLE(agbr)
RGBA_SWIZZLE(0gbr)
RGBA_SWIZZLE(1gbr)
RGBA_SWIZZLE(brga)
RGBA_SWIZZLE(brg0)
RGBA_SWIZZLE(brg1)
RGBA_SWIZZLE(abrg)
RGBA_SWIZZLE(0brg)
RGBA_SWIZZLE(1brg)
RGBA_SWIZZLE(bgra)
RGBA_SWIZZLE(bgr0)
RGBA_SWIZZLE(bgr1)
RGBA_SWIZZLE(abgr)
RGBA_SWIZZLE(0bgr)
RGBA_SWIZZLE(1bgr)
else
{
return +[](LightnessComputer& self, std::string_view sw, int& i) {
auto& samples = self.outputs.samples.value;
self.apply([&](auto r, auto g, auto b, auto a) {
decltype(r) rgbw[8];
rgbw[0] = r;
rgbw[1] = g;
rgbw[2] = b;
rgbw[3] = 0;
make_swizzle_dynamic<eightbit>(sw, samples.data(), i, rgbw, a);
});
};
}
#undef RGBA_SWIZZLE
#undef RGBW_SWIZZLE
}
return +[](LightnessComputer& self, std::string_view sw, int& i) { };
}
// Main processing
void operator()()
{
auto& in_tex = inputs.image.texture;
if(!in_tex.changed)
{
return;
}
if(!in_tex.bytes || in_tex.width < 1 || in_tex.height < 1)
{
return;
}
// Sample the texture
auto& samples = outputs.samples.value;
samples.clear();
int i = 0;
switch(inputs.mode.value)
{
case ins::mode::Lightness: {
samples.resize(in_tex.height * in_tex.width);
if(inputs.eightbit)
{
apply([&](auto r, auto g, auto b, auto a) {
const auto lightness = linear_to_y(r, g, b);
const auto perceptual_lightness = y_to_lstar(lightness);
samples[i] = perceptual_lightness * 255.f;
i++;
});
}
else
{
apply([&](auto r, auto g, auto b, auto a) {
const auto lightness = linear_to_y(r, g, b);
const auto perceptual_lightness = y_to_lstar(lightness);
samples[i] = perceptual_lightness;
i++;
});
}
break;
}
case ins::mode::Value: {
samples.resize(in_tex.height * in_tex.width);
apply([&](auto r, auto g, auto b, auto a) {
auto [h, s, v] = hsv(r, g, b);
samples[i++] = v;
});
break;
}
case ins::mode::Hue: {
samples.resize(in_tex.height * in_tex.width);
apply([&](auto r, auto g, auto b, auto a) {
auto [h, s, v] = hsv(r, g, b);
samples[i++] = h;
});
break;
}
case ins::mode::Saturation: {
samples.resize(in_tex.height * in_tex.width);
apply([&](auto r, auto g, auto b, auto a) {
auto [h, s, v] = hsv(r, g, b);
samples[i++] = s;
});
break;
}
case ins::mode::HSL: {
samples.resize(in_tex.height * in_tex.width * 3);
apply([&](auto r, auto g, auto b, auto a) {
auto [h, s, l] = hsl(r, g, b);
samples[i++] = h;
samples[i++] = s;
samples[i++] = l;
});
break;
}
case ins::mode::HSV: {
samples.resize(in_tex.height * in_tex.width * 3);
apply([&](auto r, auto g, auto b, auto a) {
auto [h, s, v] = hsv(r, g, b);
samples[i++] = h;
samples[i++] = s;
samples[i++] = v;
});
break;
}
case ins::mode::Red: {
samples.resize(in_tex.height * in_tex.width);
if(inputs.eightbit)
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = r * 255.f; });
else
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = r; });
break;
}
case ins::mode::Green: {
samples.resize(in_tex.height * in_tex.width);
if(inputs.eightbit)
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = g * 255.f; });
else
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = g; });
break;
}
case ins::mode::Blue: {
samples.resize(in_tex.height * in_tex.width);
if(inputs.eightbit)
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = b * 255.f; });
else
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = b; });
break;
}
case ins::mode::Alpha: {
samples.resize(in_tex.height * in_tex.width);
if(inputs.eightbit)
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = a * 255.f; });
else
apply([&](auto r, auto g, auto b, auto a) { samples[i++] = a; });
break;
}
case ins::mode::RGB: {
samples.resize(in_tex.height * in_tex.width * 3);
if(inputs.eightbit)
{
apply([&](auto r, auto g, auto b, auto a) {
samples[i++] = r * 255.f;
samples[i++] = g * 255.f;
samples[i++] = b * 255.f;
});
}
else
{
apply([&](auto r, auto g, auto b, auto a) {
samples[i++] = r;
samples[i++] = g;
samples[i++] = b;
});
}
break;
}
case ins::mode::RGBW: {
samples.resize(in_tex.height * in_tex.width * 4);
if(inputs.eightbit)
{
apply([&](auto r, auto g, auto b, auto a) {
const auto [h, s, l] = hsl(r, g, b);
double rgbw[4];
hsi2rgbw(h, s, l, rgbw);
samples[i++] = rgbw[0];
samples[i++] = rgbw[1];
samples[i++] = rgbw[2];
samples[i++] = rgbw[3];
});
}
else
{
apply([&](auto r, auto g, auto b, auto a) {
const auto [h, s, l] = hsl(r, g, b);
double rgbw[4];
hsi2rgbw(h, s, l, rgbw);
samples[i++] = rgbw[0] / 255.;
samples[i++] = rgbw[1] / 255.;
samples[i++] = rgbw[2] / 255.;
samples[i++] = rgbw[3] / 255.;
});
}
break;
}
case ins::mode::Swizzle: {
auto& samples = outputs.samples.value;
samples.resize(in_tex.height * in_tex.width * m_current_swizzle.length());
if(m_current_swizzle_func)
m_current_swizzle_func(*this, m_current_swizzle, i);
break;
}
}
// Notify the UI with the new texture at a reduced rate
if(send_message && in_tex.bytes)
{
using namespace std::literals;
auto t = clk::now();
if((t - last_ui_message) > 30ms)
{
if(m_last_ui_buffer_size != in_tex.bytesize())
{
const std::size_t n = std::size_t(in_tex.width) * in_tex.height * 4;
ui_buffer = std::shared_ptr<float[]>{
static_cast<float*>(
::operator new[](n * sizeof(float), std::align_val_t{16})),
[](float* p) { ::operator delete[](p, std::align_val_t{16}); }};
m_last_ui_buffer_size = in_tex.bytesize();
}
if(ui_buffer)
{
std::copy_n(in_tex.bytes, in_tex.width * in_tex.height * 4, ui_buffer.get());
last_ui_message = t;
send_message({.bytes = ui_buffer, .w = in_tex.width, .h = in_tex.height});
}
}
}
}
std::shared_ptr<float[]> ui_buffer;
int m_last_ui_buffer_size{};
// UI layout definition... we only have one widget here
struct ui
{
halp_meta(name, "Main")
halp_meta(layout, halp::layouts::hbox)
struct bus
{
static void process_message(ui& self, processor_to_ui msg)
{
self.tex.update(std::move(msg.bytes), msg.w, msg.h);
}
};
halp::custom_actions_item<LightnessComputerTextureDisplay> tex;
struct
{
halp_meta(layout, halp::layouts::vbox)
halp::control<&ins::size> size;
halp::control<&ins::eightbit> eightbit;
halp::control<&ins::mode> mode;
struct : halp::control<&ins::swizzle>
{
halp_meta(width, 40)
} swizzle;
} controls;
};
};
}