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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
|
#ifndef ROTOR_H
#define ROTOR_H
//definitions of base classes and types for rendering graph
#include <unordered_map>
#include <deque>
#include <math.h>
#include <memory>
#include <sys/time.h>
#include <iostream>
#include <json/json.h>
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Logger.h"
#include "Poco/Path.h"
#include "Poco/Base64Encoder.h"
#include "Poco/FileStream.h"
#include "Poco/CountingStream.h"
#include "Poco/StreamCopier.h"
#include "xmlIO.h"
#include "utils.h"
#include "cvimage.h"
#include "libavwrapper.h"
//using namespace cv;
namespace Rotor {
//forward declarations
class Node;
class Signal_node;
class Image_node;
class Parameter;
class Audio_frame{
public:
Audio_frame(uint16_t *_samples,int _channels,int _numsamples){
samples=_samples;
channels=_channels;
numsamples=_numsamples;
}
uint16_t *samples;
int channels,numsamples;
};
class Time_spec{
public:
Time_spec(){};
Time_spec(float _time,float _framerate,float _duration,Audio_frame *_audio=nullptr){ time=_time; framerate=_framerate; duration=_duration; audio=_audio;};
float time; //num/denom ?
float framerate;
float duration;
Audio_frame *audio;
Time_spec lastframe() const{
return Time_spec(time-(1.0f/framerate),framerate,duration);
}
int frame(){
return (int)((time*framerate)+0.5); //rounded to the nearest frame
}
};
class Frame_spec: public Time_spec{
public:
Frame_spec(float _time,float _framerate,float _duration,int _w,int _h,Audio_frame *_audio=nullptr)
{ time=_time; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
Frame_spec(int _frame,float _framerate,float _duration,int _w,int _h,Audio_frame *_audio=nullptr)
{ time=((float)_frame)/_framerate; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
int h,w;
Frame_spec lastframe(){
return Frame_spec(time-(1.0f/framerate),framerate,duration,w,h);
}
};
class Colour{
public:
Colour(){
r=g=b=0;
}
Colour(int c){
r=c&0xFF;
g=(c&0xFF00)>>8;
b=(c&0xFF0000)>>16;
}
Colour(std::string s){
r=(uint8_t)ofHexToChar(s.substr(0,2));
g=(uint8_t)ofHexToChar(s.substr(2,2));
b=(uint8_t)ofHexToChar(s.substr(4,2));
}
float Rfloat(){
return ((float)r)/255.0f;
}
float Gfloat(){
return ((float)g)/255.0f;
}
float Bfloat(){
return ((float)b)/255.0f;
}
uint8_t r,g,b;
};
class Command_response{
public:
Command_response() { status=Poco::Net::HTTPResponse::HTTP_OK; }
std::string description;
Poco::Net::HTTPResponse::HTTPStatus status;
};
class Input{
public:
Input(const string &_desc,const string &_title): connection(nullptr),description(_desc),title(_title){};
Node* connection;
string description;
string title;
};
class Image_input: public Input{
public:
virtual ~Image_input(){};
bool connect(Node *source);
Image_input(const string &_desc,const string &_title,Node* _connect): Input(_desc,_title){
connect(_connect);
};
Image* get(const Frame_spec& time);
};
class Signal_input: public Input{
public:
virtual ~Signal_input(){};
bool connect(Node *source);
Signal_input(const string &_desc,const string &_title,Node* _connect): Input(_desc,_title){
connect(_connect);
};
float get(const Time_spec& time);
};
class Parameter: public Signal_input{
public:
virtual ~Parameter(){};
void init(const float &_val){
value=_val;
}
Parameter(const string &_type,const string &_desc,const string &_title,float _value,float _min,float _max,Node* _connect): Signal_input(_desc,_title,_connect),value(_value),min(_min),max(_max),type(_type){};
float value,min,max;
float get(const Time_spec& time);
string type;
};
class Attribute{ //description of a static attribute which can be an enumerated string array
public:
virtual ~Attribute(){};
Attribute(const string &_desc,const string &_title,const string &_value,std::vector<std::string> _vals={}): description(_desc),title(_title),value(_value),intVal(0){
vals=_vals;
init(_value);
};
void init(const string &_key){ //inits int value from set::string vals index
value=_key;
std::vector<std::string>::iterator it=it = find(vals.begin(),vals.end(),value);
if (it!=vals.end()){
intVal = std::distance(vals.begin(),it)+1; //using 1-index for enums
}
else intVal=0;
}
string value,description,title;
std::vector<std::string> vals;
int intVal;
};
class Node{
public:
virtual Node* clone(map<string,string> &_settings)=0; //pure virtual
virtual ~Node(){
duplicate_inputs=false;
};
vector<Signal_input*> inputs; //simple node can have signal inputs, output depends on node type
unordered_map<string,Parameter*> parameters; //linked parameters can convert from settings to inputs
unordered_map<string,Attribute*> attributes;
void create_signal_input(const string &_desc,const string &_title,Node* _connect=nullptr ) {
inputs.push_back(new Signal_input(_desc,_title,_connect));
};
void create_parameter(const string &_name,const string &_type,const string &_desc,const string &_title,float _value=1.0f,float _min=0.0f,float _max=0.0f,Node* _connect=nullptr) {
parameters[_name]=new Parameter(_type,_desc,_title,_value,_min,_max,_connect);
};
void create_attribute(const string &_attr,const string &_desc,const string &_title,const string &_value,std::vector<std::string> _vals={}) {
attributes[_attr]=new Attribute(_desc,_title,_value,_vals);
};
void create_attribute(string *alias,const string &_attr,const string &_desc,const string &_title,const string &_value,std::vector<std::string> _vals={}) {
attributes[_attr]=new Attribute(_desc,_title,_value,_vals);
alias=&(attributes[_attr]->value);
};
void create_attribute(int *alias,const string &_attr,const string &_desc,const string &_title,const string &_value,std::vector<std::string> _vals={}) {
attributes[_attr]=new Attribute(_desc,_title,_value,_vals);
alias=&(attributes[_attr]->intVal);
};
string description;
string type;
string ID;
string title;
bool duplicate_inputs;
string find_setting(map<string,string> &settings,string key,string def=""){ if (settings.find(key)!=settings.end()) return settings[key]; else return def;};
float find_setting(map<string,string> &settings,string key,float def){ if (settings.find(key)!=settings.end()) return ofToFloat(settings[key]); else return def;};
int find_setting(map<string,string> &settings,string key,int def){ if (settings.find(key)!=settings.end()) return ofToInt(settings[key]); else return def;};
void base_settings(map<string,string> &settings) {
description=find_setting(settings,"description");
type=find_setting(settings,"type");
ID=find_setting(settings,"ID");
title=find_setting(settings,"title");
for (auto a: attributes){
if (find_setting(settings,a.first,"")!="") {
attributes[a.first]->init(find_setting(settings,a.first,""));
}
}
for (auto p: parameters){
if (find_setting(settings,p.first,"")!="") {
parameters[p.first]->init(find_setting(settings,p.first,0.0f));
cerr<<"setting parameter "<<p.first<<" to "<<find_setting(settings,p.first,0.0f)<<endl;
}
}
}
void update(const Time_spec &time){
for (auto p: parameters){
p.second->get(time);
}
}
virtual void set_parameter(const std::string &key,const std::string &value){};
};
class Signal_node: public Node{
public:
virtual ~Signal_node(){};
const float get_output(const Time_spec &time) {
update(time);
return output(time);
};
virtual const float output(const Time_spec &time) { return 0.0f; };
};
class Image_node: public Node{
public:
virtual ~Image_node(){};
vector<Image_input*> image_inputs; //image node also has image inputs and outputs
void create_image_input(const string &_title,const string &_desc,Node* _connect=nullptr) {
image_inputs.push_back(new Image_input(_desc,_title,_connect));
};
Image *get_output(const Frame_spec &frame) {
image.setup(frame.w,frame.h);
update((Time_spec)frame);
return output(frame);
}
virtual const Image *output(const Frame_spec &frame)=0;
Image image;
private:
float image_time; //? could be used to detect image reuse?
};
class Audio_processor: public Signal_node {
public:
virtual Audio_processor(){};
virtual ~Audio_processor(){};
virtual int process_frame(uint8_t *data,int samples)=0;
virtual bool init(int _channels,int _bits,int _samples,int _rate)=0;
virtual void cleanup()=0;
virtual void print_summary(){};
int channels,bits,samples,rate;
};
class LUT {
LUT(){
lut=nullptr;
};
~LUT(){if (lut) { delete[] lut;} };
void generate(float black_in,float white_in,float black_out,float white_out,float gamma){
//can check here if anything has changed
if (lut) delete[] lut;
lut=new unsigned char[256];
float fltmax=(255.0f/256.0f);
for (int i=0;i<256;i++){
lut[i]=(unsigned char)(((pow(min(fltmax,max(0.0f,(((((float)i)/256.0f)-black_in)/(white_in-black_in)))),(1.0/gamma))*(white_out-black_out))+black_out)*255.0f);
}
}
void apply(const cv::Mat& in,cv::Mat &out){ //facility to apply to other images for inherited classes
out.create(in.rows,in.cols,in.type());
for (int i=0;i<in.rows*in.cols*in.channels();i++){
out.data[i]=lut[in.data[i]];
}
}
protected:
unsigned char *lut;
};
//actual nodes-------------------------------------------------
class Time: public Signal_node {
public:
Time(){
title="Time";
description="Outputs the time in seconds as a signal";
};
Time(map<string,string> &settings): Time() {
base_settings(settings);
};
Time* clone(map<string,string> &_settings) { return new Time(_settings);};
const float output(const Time_spec &time) {
return time.time;
}
};
class Track_time: public Signal_node {
public:
Track_time(){
title="Track time";
description="Outputs the fraction of the track as a signal";
};
Track_time(map<string,string> &settings): Track_time() {
base_settings(settings);
};
Track_time* clone(map<string,string> &_settings) { return new Track_time(_settings);};
const float output(const Time_spec &time) {
return time.time/time.duration;
}
};
class Signal_output: public Signal_node {
public:
Signal_output(){
create_signal_input("signal","Signal Input");
title="Signal output";
description="Outputs a signal to xml for testing";
};
Signal_output(map<string,string> &settings): Signal_output() {
base_settings(settings);
};
Signal_output* clone(map<string,string> &_settings) { return new Signal_output(_settings);};
bool render(const float duration, const float framerate,string &xml_out);
const float output(const Time_spec &time) {
return inputs[0]->get(time);
}
};
class Testcard: public Image_node {
public:
Testcard(){
//internal testing node only
};
Testcard(map<string,string> &settings): Testcard() {
base_settings(settings);
};
~Testcard(){};
Testcard* clone(map<string,string> &_settings) { return new Testcard(_settings);};
Image *output(const Frame_spec &frame){
float hs=(255.0f/frame.h);
for (int i=0;i<frame.h;i++){
for (int j=0;j<frame.w;j++){
image.RGBdata[(i*frame.w+j)*3]=(uint8_t)((int)((i+(frame.time*25.0f)*hs))%255);
image.RGBdata[((i*frame.w+j)*3)+1]=(uint8_t)((int)((j+(frame.time*100.0f)*hs))%255);
image.RGBdata[((i*frame.w+j)*3)+2]=(uint8_t)(0);
//image->Adata[i*frame.w+j]=(uint8_t)255;
//image->Zdata[i*frame.w+j]=(uint16_t)512; //1.0 in fixed point 8.8 bits
}
}
return ℑ
}
private:
};
class Invert: public Image_node {
public:
Invert(){
create_image_input("Image to invert","Image input");
create_parameter("invert","number","Invert when greater than 0.0","Negative",1.0f,0.0f,1.0f);
title="Negative";
description="Inverts the input picture";
};
Invert(map<string,string> &settings) :Invert() {
base_settings(settings);
};
~Invert(){};
Invert* clone(map<string,string> &_settings) { return new Invert(_settings);};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in) {
if (parameters["invert"]->value>0.0f){
for (int i=0;i<in->w*in->h*3;i++) {
image.RGBdata[i]=255-in->RGBdata[i];
}
return ℑ
}
return in;
}
return nullptr;
}
private:
};
#define CYCLER_cut 1
#define CYCLER_mix 2
class Video_cycler: public Image_node {
public:
Video_cycler(){
create_image_input("Image input","Image input");
create_signal_input("Selector","Selector input");
create_attribute("mode","Cycling mode {cut|mix}","Mode","cut",{"cut","mix"});
title="Video cycler";
description="Cycles through video inputs according to selector signal";
duplicate_inputs=true;
}
Video_cycler(map<string,string> &settings):Video_cycler() {
base_settings(settings);
};
~Video_cycler(){};
bool load(const string &filename);
Image *output(const Frame_spec &frame){
if (attributes["mode"]->intVal==CYCLER_mix&&image_inputs.size()>1){
int im1=((int)inputs[0]->get((Time_spec)frame))%image_inputs.size();
int im2=(im1+1)%image_inputs.size();
float f=fmod(inputs[0]->get((Time_spec)frame),1.0f);
Image *in1=image_inputs[im1]->get(frame);
if (in1){
Image *in2=image_inputs[im2]->get(frame);
if (in2){
image=(*in1);
image*=(1.0f-f);
Image i2=(*in2);
i2*=f;
image+=i2;
return ℑ
}
return in1;
}
return nullptr;
}
return image_inputs[((int)inputs[0]->get((Time_spec)frame))%image_inputs.size()]->get(frame);
}
Video_cycler* clone(map<string,string> &_settings) { return new Video_cycler(_settings);};
};
class Signal_colour: public Image_node {
public:
Signal_colour(){
create_signal_input("Selector","Selector input");
create_attribute("palette","palette list of web colours","Colour palette","000000");
title="Signal colour";
description="Cycles through a palette of background colours according to selector signal";
};
Signal_colour(map<string,string> &settings):Signal_colour() {
base_settings(settings);
for (int i=0;i<attributes["palette"]->value.size()/6;i++){
palette.push_back(Colour(attributes["palette"]->value.substr(i*6,6)));
}
prevcol=-1;
};
~Signal_colour(){};
Image *output(const Frame_spec &frame){
if (palette.size()) {
int col=((int)inputs[0]->get((Time_spec)frame))%palette.size();
//if (col!=prevcol){ //how about when starting a new render?
for (int i=0;i<image.w*image.h;i++){
image.RGBdata[i*3]=palette[col].r;
image.RGBdata[i*3+1]=palette[col].g;
image.RGBdata[i*3+2]=palette[col].b;
}
prevcol=col;
//}
return ℑ
}
return nullptr;
}
Signal_colour* clone(map<string,string> &_settings) { return new Signal_colour(_settings);};
private:
vector<Rotor::Colour> palette;
int prevcol;
};
class Signal_greyscale: public Image_node {
//Draws signal bars in greyscale
public:
Signal_greyscale(){
create_signal_input("Signal","Signal input");
title="Signal greyscale";
description="Renders signal level as greyscale background";
};
Signal_greyscale(map<string,string> &settings):Signal_greyscale() {
base_settings(settings);
prevcol=-1;
};
~Signal_greyscale(){};
Image *output(const Frame_spec &frame){
uint8_t col=((uint8_t)(inputs[0]->get((Time_spec)frame)*255.0f));
if (col!=prevcol){ //how about when starting a new render?
for (int i=0;i<image.w*image.h*3;i++){
image.RGBdata[i]=col;
}
prevcol=col;
}
return ℑ
}
Signal_greyscale* clone(map<string,string> &_settings) { return new Signal_greyscale(_settings);};
private:
uint8_t prevcol;
};
#define ARITHMETIC_plus 1
#define ARITHMETIC_minus 2
#define ARITHMETIC_multiply 3
#define ARITHMETIC_divide 4
#define ARITHMETIC_modulo 5
class Image_arithmetic: public Image_node {
public:
Image_arithmetic(){
create_image_input("image input","Image input");
create_parameter("value","number","Value or signal for operation","Value",1.0f);
create_attribute("operator","operator for image","Operator","+",{"+","-","*","/"});
title="Image arithmetic";
description="Performs arithmetic on an image with a signal or value";
};
Image_arithmetic(map<string,string> &settings):Image_arithmetic() {
base_settings(settings);
}
~Image_arithmetic(){};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
switch (attributes["operator"]->intVal) {
case ARITHMETIC_plus:
image=(*in); //could be poss without copy?
image+=parameters["value"]->value;
break;
case ARITHMETIC_minus:
image=(*in);
image-=parameters["value"]->value;
break;
case ARITHMETIC_multiply:
image=(*in);
image*=parameters["value"]->value;
break;
case ARITHMETIC_divide:
image=(*in);
image/=parameters["value"]->value;
break;
}
}
return ℑ
}
Image_arithmetic* clone(map<string,string> &_settings) { return new Image_arithmetic(_settings);};
private:
};
#define BLEND_blend 1
#define BLEND_screen 2
#define BLEND_multiply 3
#define BLEND_alpha 4
#define BLEND_wrap 5
#define BLEND_xor 6
class Blend: public Image_node {
public:
Blend(){
create_image_input("image input 1","Image input 1");
create_image_input("image input 2","Image input 2");
create_parameter("amount","number","amount to blend input 2","Blend amount",0.5f,0.0f,1.0f);
create_attribute("mode","Blend mode","Blend mode","blend",{"blend","screen","multiply","alpha","wrap","xor"});
title ="Blend";
description="Blend images in various modes";
};
Blend(map<string,string> &settings):Blend() {
base_settings(settings);
};
~Blend(){};
Blend* clone(map<string,string> &_settings) { return new Blend(_settings);};
Image *output(const Frame_spec &frame){
Image *in1=image_inputs[0]->get(frame);
if (in1){
Image *in2=image_inputs[1]->get(frame);
if (in2) {
image=*(in1);
switch(attributes["mode"]->intVal){
case BLEND_screen:
image+=(*in2);
break;
case BLEND_multiply:
image*=(*in2);
break;
case BLEND_xor:
image^=(*in2);
break;
case BLEND_alpha:
image=image.alpha_blend(*in2);
break;
case BLEND_wrap:
image=image.add_wrap(*in2);
break;
case BLEND_blend: //has to be last because of initialser of *in? go figure
image*=(1.0f-parameters["amount"]->value);
Image *in=(*in2)*parameters["amount"]->value;
image+=(*in);
delete in;
break;
}
return ℑ
}
//if there aren't 2 image inputs connected just return the first
return in1;
}
return nullptr;
}
private:
};
#define MIRROR_horiz 1
#define MIRROR_vert 2
#define MIRROR_horizR 3
#define MIRROR_vertR 4
class Mirror: public Image_node {
public:
Mirror(){
create_image_input("image input","Image input");
create_attribute("mode","Mirror mode","Mirror mode","horiz",{"horiz","vert","horizR","vertR"});
title="Mirror";
description="Mirror video across a central axis";
};
Mirror(map<string,string> &settings):Mirror() {
base_settings(settings);
};
~Mirror(){ };
Mirror* clone(map<string,string> &_settings) { return new Mirror(_settings);};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
//copy incoming image **writable
image=(*in);
//could be more efficient here by only copying once
switch (attributes["mode"]->intVal) {
case MIRROR_horiz:
for (int i=0;i<image.w/2;i++){
for (int j=0;j<image.h;j++){
for (int k=0;k<3;k++){
image.RGBdata[(((j*image.w)+((image.w/2)+i))*3)+k]=image.RGBdata[(((j*image.w)+((image.w/2)-i))*3)+k];
}
}
}
break;
case MIRROR_vert:
for (int i=0;i<image.w;i++){
for (int j=0;j<image.h/2;j++){
for (int k=0;k<3;k++){
image.RGBdata[((((image.h/2+j)*image.w)+i)*3)+k]=image.RGBdata[((((image.h/2-j)*image.w)+i)*3)+k];
}
}
}
break;
case MIRROR_horizR:
for (int i=0;i<image.w/2;i++){
for (int j=0;j<image.h;j++){
for (int k=0;k<3;k++){
image.RGBdata[(((j*image.w)+((image.w/2)-i))*3)+k]=image.RGBdata[(((j*image.w)+((image.w/2)+i))*3)+k];
}
}
}
break;
case MIRROR_vertR:
for (int i=0;i<image.w;i++){
for (int j=0;j<image.h/2;j++){
for (int k=0;k<3;k++){
image.RGBdata[((((image.h/2-j)*image.w)+i)*3)+k]=image.RGBdata[((((image.h/2+j)*image.w)+i)*3)+k];
}
}
}
break;
}
return ℑ
}
return nullptr;
}
private:
};
class Monochrome: public Image_node {
public:
Monochrome(){
create_image_input("image input","Image input");
title="Monochrome";
description="Render video greyscale";
};
Monochrome(map<string,string> &settings):Monochrome() {
base_settings(settings);
};
~Monochrome(){
};
Monochrome* clone(map<string,string> &_settings) { return new Monochrome(_settings);};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
for (int i=0;i<image.w;i++){
for (int j=0;j<image.h;j++){
uint8_t luma=0;
for (int l=0;l<3;l++) luma+=pixels.mono_weights[l][in->RGBdata[(((j*image.w)+i)*3)+l]];
for (int k=0;k<3;k++) image.RGBdata[(((j*image.w)+i)*3)+k]=luma;
}
}
return ℑ
}
return nullptr;
}
private:
};
#define TRANSFORM_nearest 1
#define TRANSFORM_linear 2
#define TRANSFORM_area 3
#define TRANSFORM_cubic 4
#define TRANSFORM_lanczos 5
class Transform: public Image_node {
//what is the best coordinate system to use?
//origin: corner or centre
//units: pixel or fractional
//aspect: scaled or homogenous
public:
Transform(){
create_image_input("image input","Image input");
create_parameter("transformX","number","X transformation","Transform X",0.0f);
create_parameter("transformY","number","Y transformation","Transform X",0.0f);
create_parameter("originX","number","X transformation origin","Origin X",0.5f);
create_parameter("originY","number","Y transformation origin","Origin Y",0.5f);
create_parameter("rotation","number","Rotation about origin","Rotation",0.0f);
create_parameter("scale","number","Scale about origin","Scale",1.0f);
create_attribute("filter","Filtering mode","Filter mode","linear",{"nearest","linear","area","cubic","lanczos"});
title="Transform";
description="Apply 2D transformation";
};
Transform(map<string,string> &settings):Transform() {
base_settings(settings);
};
~Transform(){
};
Transform* clone(map<string,string> &_settings) { return new Transform(_settings);};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
int filtmode;
switch(attributes["filter"]->intVal){
case TRANSFORM_nearest:
filtmode=cv::INTER_NEAREST;
break;
case TRANSFORM_linear:
filtmode=cv::INTER_LINEAR;
break;
case TRANSFORM_area:
filtmode=cv::INTER_AREA;
break;
case TRANSFORM_cubic:
filtmode=cv::INTER_CUBIC;
break;
case TRANSFORM_lanczos:
filtmode=cv::INTER_LANCZOS4;
break;
}
float tX=parameters["transformX"]->value;
float tY=parameters["transformY"]->value;
float oX=parameters["originX"]->value;
float oY=parameters["originY"]->value;
float r=parameters["rotation"]->value;
float s=parameters["scale"]->value;
//do opencv transform
cv::Point2f srcTri[3], dstTri[3];
cv::Mat rot_mat(2,3,CV_32FC1);
cv::Mat trans_mat(2,3,CV_32FC1);
Image inter;
inter.setup(in->w,in->h);
// Compute matrix by creating triangle and transforming
//is there a better way - combine the 2? Just a bit of geometry
srcTri[0].x=0;
srcTri[0].y=0;
srcTri[1].x=in->w-1;
srcTri[1].y=0;
srcTri[2].x=0;
srcTri[2].y=in->h-1;
for (int i=0;i<3;i++){
dstTri[i].x=srcTri[i].x+(tX*in->w);
dstTri[i].y=srcTri[i].y+(tY*in->h);
}
trans_mat=getAffineTransform( srcTri, dstTri );
warpAffine( in->rgb, inter.rgb, trans_mat, inter.rgb.size(), filtmode, cv::BORDER_WRAP);
// Compute rotation matrix
//
cv::Point centre = cv::Point( oX*in->w, oY*in->h );
rot_mat = getRotationMatrix2D( centre, r, s );
// Do the transformation
//
warpAffine( inter.rgb, image.rgb, rot_mat, image.rgb.size(), filtmode, cv::BORDER_WRAP);
//BORDER_WRAP
//INTER_NEAREST - a nearest-neighbor interpolation
//INTER_LINEAR - a bilinear interpolation (used by default)
//INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
//INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
//INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
return ℑ
}
return nullptr;
}
private:
//todo - quality settings
};
class Alpha_merge: public Image_node {
public:
Alpha_merge(){
create_image_input("image input","Image input");
create_image_input("alpha input","Alpha input");
title="Alpha merge";
description="Alpha merge two images";
};
Alpha_merge(map<string,string> &settings):Alpha_merge() {
base_settings(settings);
};
~Alpha_merge(){};
Alpha_merge* clone(map<string,string> &_settings) { return new Alpha_merge(_settings);};
Image *output(const Frame_spec &frame){
Image *in1=image_inputs[0]->get(frame);
if (in1){
//copy incoming image **writable
Image *in2=image_inputs[1]->get(frame);
if (in2) {
image=(*in1);
image.alpha_merge(*in2);
return ℑ
}
//if there aren't 2 image inputs connected just return the first
return in1;
}
return nullptr;
}
private:
};
class Difference_matte: public Image_node {
public:
Difference_matte(){
create_image_input("image input","Image input");
create_image_input("background input","Background input");
create_parameter("threshold","number","Difference threshold","Threshold",0.2f,0.0f,1.0f);
create_parameter("feather","number","Feather width","Feather",0.1f,0.0f,1.0f);
create_parameter("weight_h","number","H component weight","Weight H",0.5f,0.0f,1.0f);
create_parameter("weight_s","number","S component weight","Weight S",0.5f,0.0f,1.0f);
create_parameter("weight_v","number","V component weight","Weight V",0.5f,0.0f,1.0f);
create_parameter("blursize","number","Blur size","Blur size",2.0f,0.0f,10.0f);
create_attribute("mode","Output {image|alpha}","output mode","alpha",{"image","alpha"});
title="Difference matte";
description="Create an alpha channel using a background reference picture";
LUT=nullptr;
};
Difference_matte(map<string,string> &settings):Difference_matte() {
base_settings(settings);
};
~Difference_matte(){if (LUT) delete[] LUT;};
Difference_matte* clone(map<string,string> &_settings) { return new Difference_matte(_settings);};
Image *output(const Frame_spec &frame){
Image *in1=image_inputs[0]->get(frame);
if (in1){
Image *in2=image_inputs[1]->get(frame);
if (in2) {
generate_LUT();
/*
cv::cvtColor(in1->rgb,greyfg,CV_RGB2GRAY);
cv::cvtColor(in2->rgb,greybg,CV_RGB2GRAY);
cv::absdiff(greyfg,greybg,greyDiff);
//parameters["threshold"]->value
cv::threshold(greyDiff,mask,parameters["threshold"]->value,255,CV_THRESH_BINARY); //int block_size=3, double param1=5); //int blockSize, int offset=0,bool invert=false, bool gauss=false);
//cv::adaptiveThreshold(greyDiff,mask,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY, 3,5); //int block_size=3, double param1=5); //int blockSize, int offset=0,bool invert=false, bool gauss=false);
*/
cv::cvtColor(in1->rgb, hsv1, CV_RGB2HSV);
cv::cvtColor(in2->rgb, hsv2, CV_RGB2HSV);
mask.create(frame.h,frame.w,CV_8UC1);
lutmask.create(frame.h,frame.w,CV_8UC1);
//get euclidean distance in HSV space
int dist,d;
uint8_t m;
float weights[3] = {parameters["weight_h"]->value,parameters["weight_s"]->value,parameters["weight_v"]->value};
float weight_total=255.0f/pow(pow(weights[0]*255,2)+pow(weights[1]*255,2)+pow(weights[2]*255,2),0.5);
for (int i=0;i<frame.w*frame.h;i++){
dist=0;
for (int j=0;j<3;j++){
d=((int)hsv1.data[i*3+j])-((int)hsv2.data[i*3+j]);
dist+=(d*d)*weights[j];
}
uint8_t id=(uint8_t)(sqrt((float)dist)*weight_total);
mask.data[i]=id;
}
/*
for (int i=0;i<frame.w*frame.h;i++){
dist=0;
for (int j=0;j<3;j++){
d=((int)hsv1.data[i*3+j])-((int)hsv2.data[i*3+j]);
dist+=(abs(d))*weights[j];
}
uint8_t id=(uint8_t)(((float)dist)/weight_total);
m=LUT[id];
mask.data[i]=m;
}
*/
//cv::bilateralFilter(mask,filtmask, 4,8,2 );
//cv::GaussianBlur(mask,filtmask,cv::Size( 4, 4 ), 2, 2);
int ksize=max((ceil(parameters["blursize"]->value/2.0)*2)+1,1.0);
//nb this doesn't do the intended: create 'continuously variable' blur
cv::GaussianBlur(mask,filtmask,cvSize(ksize,ksize),parameters["blursize"]->value);
for (int i=0;i<frame.w*frame.h;i++){
lutmask.data[i]=LUT[filtmask.data[i]];
}
image=(*in1);
if (attributes["mode"]->value=="image"){
cv::cvtColor(lutmask, image.rgb, CV_GRAY2RGB);
}
else image.alpha_from_cv(lutmask);
return ℑ
}
//if there aren't 2 image inputs connected just return the first
return in1;
}
return nullptr;
}
void generate_LUT(){
//can check here if anything has changed
//cerr<<"generating LUT: threshold "<<parameters["threshold"]->value<<", feather "<<parameters["feather"]->value<<endl;
if (LUT) delete[] LUT;
LUT=new uint8_t[256];
float fltmax=(255.0f/256.0f);
float minf=max(0.0f,parameters["threshold"]->value-(parameters["feather"]->value*0.5f));
float maxf=min(1.0f,parameters["threshold"]->value+(parameters["feather"]->value*0.5f));
for (int i=0;i<256;i++){
LUT[i]=(uint8_t)(min(1.0f,max(0.0f,((((float)i)/255.0f)-minf)/(maxf-minf)))*255.0f);
// cerr<<((int)LUT[i])<<" ";
}
//cerr<<endl;
}
private:
cv::Mat greyfg,greybg,greyDiff,mask,filtmask,lutmask;
cv::Mat hsv1,hsv2;
uint8_t *LUT;
};
#define VIDEOFRAMES_frame 1
#define VIDEOFRAMES_blend 2
class Video_loader: public Image_node {
public:
Video_loader(){
create_parameter("speed","number","video playback speed","Speed",1.0f,0.0f,0.0f);
create_parameter("framerate","number","framerate override","Frame rate",0.0f,0.0f,0.0f);
create_attribute("filename","name of video file to load","File name","");
create_attribute("mode","frame mode","Mode","frame",{"frame","blend"});
title="Video loader";
description="Loads a video file";
};
Video_loader(map<string,string> &settings): Video_loader() {
base_settings(settings);
isLoaded=false;
if (attributes["filename"]->value!="") {
load(find_setting(settings,"media_path","")+attributes["filename"]->value);
}
lastframe=0;
};
~Video_loader(){};
bool load(const string &filename);
Image *output(const Frame_spec &frame);
Video_loader* clone(map<string,string> &_settings) { return new Video_loader(_settings);};
bool isLoaded;
private:
libav::decoder player;
int lastframe;
};
class Video_output: public Image_node {
public:
Video_output(){
create_image_input("image to output","Image input");
title="Video output";
description="Outputs to video from here";
};
Video_output(map<string,string> &settings):Video_output() {
base_settings(settings);
};
~Video_output(){ };
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
//make copy of the image, for feedback
//optimise?
image=(*in);
return ℑ
}
return nullptr;
};
Video_output* clone(map<string,string> &_settings) { return new Video_output(_settings);};
bool render(const float duration, const float framerate,const string &output_filename,const string &audio_filename,float& progress,int w,int h);
private:
};
class Video_feedback: public Image_node {
public:
Video_feedback(){
title="Video feedback";
description="Repeats output of the last frame";
feedback=nullptr;
};
Video_feedback(map<string,string> &settings):Video_feedback() {
base_settings(settings);
};
~Video_feedback(){ };
void set_feedback(Image *iptr){
feedback=iptr;
}
Image *output(const Frame_spec &frame){
if (feedback->RGBdata){
return feedback;
}
image.setup(frame.w,frame.h);
image.clear();
return ℑ
};
Video_feedback* clone(map<string,string> &_settings) { return new Video_feedback(_settings);};
private:
Image *feedback;
};
//-------------------------------------------------------------------
class Node_factory{
public:
Node_factory();
~Node_factory(){
for (auto t:type_map) delete t.second;
}
void add_type(string type,Node* proto){
type_map[type]=proto;
};
Node *create(map<string,string> &settings){
if (settings.find("type")!=settings.end()) {
if (type_map.find(settings["type"])!=type_map.end()) {
return type_map[settings["type"]]->clone(settings);
}
}
return NULL;
};
bool list_node(const string &t,xmlIO XML){
for (auto& type: type_map) {
if (type.first==t) {
list_node(type,XML);
return true;
}
}
XML.addValue("error","Node /"+t+"/ not found");
};
void list_node(std::pair<const std::basic_string<char>, Rotor::Node*> &type,xmlIO XML,int i=0){
XML.addTag("node");
XML.addAttribute("node","type",type.first,i);
XML.addAttribute("node","inputs",type.second->duplicate_inputs?"expandable":"fixed",i);
XML.addAttribute("node","title",type.second->title,i);
XML.addAttribute("node","description",type.second->description,i);
if (dynamic_cast<Signal_node*> (type.second)!=nullptr) XML.addAttribute("node","output","signal",i);
if (dynamic_cast<Image_node*> (type.second)!=nullptr) XML.addAttribute("node","output","image",i);
XML.pushTag("node",i);
//if (type.second->description!="") {
// XML.addTag("description");
// XML.setValue("description",type.second->description,0);
//}
int j=0;
for (auto& input: type.second->inputs) {
XML.addTag("signal_input");
XML.addAttribute("signal_input","title",input->title,j);
XML.addAttribute("signal_input","description",input->description,j);
j++;
}
j=0;
if (dynamic_cast<Image_node*> (type.second)!=nullptr) {
for (auto& input: (dynamic_cast<Image_node*>(type.second))->image_inputs) {
XML.addTag("image_input");
XML.addAttribute("image_input","title",input->title,j);
XML.addAttribute("image_input","description",input->description,j);
j++;
}
}
j=0;
for (auto& parameter: type.second->parameters) {
XML.addTag("parameter");
XML.addAttribute("parameter","name",parameter.first,j);
XML.addAttribute("parameter","type",parameter.second->type,j);
XML.addAttribute("parameter","title",parameter.second->title,j);
XML.addAttribute("parameter","description",parameter.second->description,j);
XML.addAttribute("parameter","value",parameter.second->value,j);
XML.addAttribute("parameter","min",parameter.second->min,j);
XML.addAttribute("parameter","max",parameter.second->max,j);
j++;
}
j=0;
for (auto& attribute: type.second->attributes) {
XML.addTag("attribute");
XML.addAttribute("attribute","name",attribute.first,j);
XML.addAttribute("attribute","title",attribute.second->title,j);
XML.addAttribute("attribute","description",attribute.second->description,j);
XML.addAttribute("attribute","value",attribute.second->value,j);
if (attribute.second->vals.size()){ //document attribute enumeration
XML.addAttribute("attribute","type","enum",j);
XML.pushTag("attribute",j);
int k=0;
for (auto val: attribute.second->vals){
XML.addTag("option");
XML.addAttribute("option","value",val,k);
k++;
}
XML.popTag();
}
else XML.addAttribute("attribute","type","string",j);
j++;
}
XML.popTag();
}
void list_nodes(xmlIO XML){
int i=0;
for (auto& type: type_map) {
if (type.second->description!="") { //blank description = internal/ testing node
list_node(type,XML,i);
i++;
}
}
}
void list_nodes(Json::Value &JSON){
JSON["nodeslist"]=Json::arrayValue;
for (auto& type: type_map) {
if (type.second->description!="") { //blank description = internal/ testing node
Json::Value node;
node["type"]=type.first;
node["title"]=type.second->title;
node["inputs"]=type.second->duplicate_inputs?"expandable":"fixed";
if (dynamic_cast<Signal_node*> (type.second)!=nullptr) node["output"]="signal";
if (dynamic_cast<Image_node*> (type.second)!=nullptr) node["output"]="image";
node["description"]=type.second->description;
if (type.second->inputs.size()){
node["signal_inputs"]=Json::arrayValue;
for (auto& input: type.second->inputs) {
Json::Value signal_input;
signal_input["title"]=input->title;
signal_input["description"]=input->description;
node["signal_inputs"].append(signal_input);
}
}
if (dynamic_cast<Image_node*> (type.second)!=nullptr) {
if ((dynamic_cast<Image_node*>(type.second))->image_inputs.size()){
node["image_inputs"]=Json::arrayValue;
for (auto& input: (dynamic_cast<Image_node*>(type.second))->image_inputs) {
Json::Value image_input;
image_input["title"]=input->title;
image_input["description"]=input->description;
node["image_inputs"].append(image_input);
}
}
}
if (type.second->parameters.size()){
node["parameters"]=Json::arrayValue;
for (auto& param: type.second->parameters) {
Json::Value parameter;
parameter["name"]=param.first;
parameter["type"]=param.second->type;
parameter["title"]=param.second->title;
parameter["description"]=param.second->description;
parameter["value"]=param.second->value;
parameter["min"]=param.second->min;
parameter["max"]=param.second->max;
node["parameters"].append(parameter);
}
}
if (type.second->attributes.size()){
node["attributes"]=Json::arrayValue;
for (auto& attr: type.second->attributes) {
Json::Value attribute;
attribute["name"]=attr.first;
attribute["title"]=attr.second->title;
attribute["description"]=attr.second->description;
attribute["value"]=attr.second->value;
if (attr.second->vals.size()){ //document attribute enumeration
attribute["type"]="enum";
attribute["options"]=Json::arrayValue;
for (auto val: attr.second->vals){
attribute["options"].append(val);
}
}
else attribute["type"]="string";
node["attributes"].append(attribute);
}
}
JSON["nodeslist"].append(node);
}
}
}
private:
unordered_map<string,Node*> type_map;
};
class Graph{
public:
Graph(){duration=20.0f;loaded = false;outW=640;outH=360;};
Graph(const string& _uid,const string& _desc){
init(_uid,_desc);
};
void init(const string& _uid,const string& _desc){
uid=_uid;
description=_desc;
duration=20.0f;
};
string uid; //every version of a graph has a UUID, no particular need to actually read its data(?)
//?? is it faster than using strings??
string description;
std::unordered_map<string,Node*> nodes;
vector<Node*> find_nodes(const string &type); //could be a way of finding a set based on capabilities?
Node* find_node(const string &type);
bool signal_render(string &signal_xml,const float framerate);
bool video_render(const string &output_filename,const string &audio_filename,const float framerate,float& progress);
bool load(string data,string media_path);
bool loadFile(string &filename,string media_path);
bool parseXml(string media_path);
bool parseJson(string &data,string &media_path);
bool set_resolution(int w,int h);
bool preview(xmlIO &XML,int w,int h);
bool loaded;
float duration;
const string toString();
xmlIO xml;
private:
Node_factory factory;
int outW,outH;
};
class Audio_thumbnailer: public Audio_processor {
public:
Audio_thumbnailer(){
height=128;
width=512; //fit
data=new uint8_t[height*width];
memset(data,0,height*width);
vectordata =new float[width];
};
~Audio_thumbnailer(){
delete[] data;
delete[] vectordata;
};
Audio_thumbnailer* clone(map<string,string> &_settings) { return new Audio_thumbnailer();};
bool init(int _channels,int _bits,int _samples,int _rate);
void cleanup(){};
int process_frame(uint8_t *data,int samples_in_frame);
string print();
void print_vector(xmlIO XML);
uint8_t *data;
float *vectordata;
int height,width,samples_per_column;
int column,out_sample,sample,samples;
int offset;
double scale,accum;
};
}
#endif
|