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
|
#ifndef ROTOR_NODES_DRAWING
#define ROTOR_NODES_DRAWING
#include "rotor.h"
#include <cairo.h>
//#include <fontconfig.h>
namespace Rotor {
class Draw_node: public Image_node {
public:
Draw_node(){
create_image_input("image input","Image input");
create_parameter("x","number","X coordinate","X",0.0);
create_parameter("y","number","Y coordinate","Y",0.0);
create_parameter("scale","number","Scale","Scale",1.0);
create_parameter("rotation","number","Rotation","Rotation",0.0);
//no title or description as it isn't intended for the user
};
Draw_node(map<string,string> &settings):Draw_node() {
};
~Draw_node(){};
Draw_node* clone(map<string,string> &_settings) { return new Draw_node(_settings);};
virtual void vector_output(cairo_t * cr,const Frame_spec &frame){};
Image *output(const Frame_spec &frame){
Image *in=image_inputs[0]->get(frame);
if (in){
image=(*in);
}
else image.clear();
//convert to 32 bit - this can probably be optimised further
cv::Mat chans;
cv::cvtColor(image.rgb, chans, CV_BGR2RGBA, 4);
cairo_surface_t * cs = cairo_image_surface_create_for_data (chans.data,
CAIRO_FORMAT_RGB24,
image.w,
image.h,
image.w*4);
cairo_t * cr = cairo_create (cs);
//do any kind of vector drawing
cairo_save(cr); //not really even necessary?
translate(cr,frame);
vector_output(cr,frame);
cairo_restore (cr); //not really even necessary?
//convert frame back to 24 bits
cv::cvtColor(chans,image.rgb,CV_RGBA2BGR,3);
cairo_destroy(cr);
cairo_surface_destroy(cs);
return ℑ
}
virtual void translate(cairo_t * cr,const Frame_spec &frame){
cairo_translate(cr, frame.w/2, frame.h/2);
cairo_translate(cr, parameters["x"]->value * frame.w, parameters["y"]->value * frame.h);
cairo_scale(cr, parameters["scale"]->value , parameters["scale"]->value );
cairo_rotate(cr,(parameters["rotation"]->value/180.0)*M_PI);
}
protected:
Colour colour;
private:
};
class Text_base: public Draw_node {
public:
Text_base(){
create_attribute("colour","Colour to fill","Colour","FFFFFF");
create_attribute("font","font to use","Font","Akzidenz",{"Sans","Sans Mono","Serif","Akzidenz"});
create_parameter("size","number","Point size of font","size",50.0);
NODEID="7da93b94-2d0b-11e3-8940-77bc.09d3e8";
};
Text_base(map<string,string> &settings):Text_base() {
base_settings(settings);
};
~Text_base(){};
Text_base* clone(map<string,string> &_settings) { return new Text_base(_settings);};
void vector_output(cairo_t * cr,const Frame_spec &frame){
colour=Colour(attributes["colour"]->value);
string text=select_text(frame);
cairo_text_extents_t te;
cairo_set_source_rgb(cr, colour.Rdouble(),colour.Gdouble(),colour.Bdouble());
string fontname;
switch (attributes["font"]->intVal){
case 1:
case 2:
case 3:
fontname="DejaVu "+attributes["font"]->value;
break;
case 4:
fontname="Akzidenz-Grotesk Condensed BQ";
break;
}
cairo_select_font_face (cr,fontname.c_str(),
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, parameters["size"]->value*(((double)frame.w)/360.0));
cairo_text_extents(cr, text.c_str(), &te);
cairo_move_to (cr,-te.width/2,te.height/2);
cairo_show_text (cr, text.c_str());
cairo_fill(cr);
}
virtual string select_text(const Frame_spec &frame){
return "hello, world!";
}
private:
};
class Text: public Text_base {
public:
Text(){
title="Text";
description="Draws text";
create_attribute("text","Text to draw","Text","hello, world!");
create_parameter("number","number","Number to draw","Number",-99999999.0);
NODEID="fdea0b88-4de7-11e3-9235-74d02b29f6a6";
};
Text(map<string,string> &settings):Text() {
base_settings(settings);
};
~Text(){};
Text* clone(map<string,string> &_settings) { return new Text(_settings);};
string select_text(const Frame_spec &frame){
if (parameters["number"]->value>-99999998.0){
return toString(parameters["number"]->value,4);
}
else return attributes["text"]->value;
}
private:
};
class UI_text: public Text_base {
public:
UI_text(){
title="UI_text";
description="Draws text entered by the user";
create_attribute("text","Text to draw","Text","hello, world!");
UItype="text";
NODEID="22b47bea-52a9-11e3-b2b3-74d02b29f6a6";
};
UI_text(map<string,string> &settings):UI_text() {
base_settings(settings);
};
~UI_text(){};
UI_text* clone(map<string,string> &_settings) { return new UI_text(_settings);};
string select_text(const Frame_spec &frame){
return attributes["text"]->value;
}
private:
};
class Lyrics: public Text_base {
public:
Lyrics(){
title="Lyrics text";
description="Draws lyrics text";
create_empty_attribute("lyrics","Lyrics to draw","Lyrics","lyrics");
NODEID="ac4318b8-4de9-11e3-b2eb-74d02b29f6a6";
};
Lyrics(map<string,string> &settings):Lyrics() {
base_settings(settings);
};
~Lyrics(){};
Lyrics* clone(map<string,string> &_settings) { return new Lyrics(_settings);};
string select_text(const Frame_spec &frame){
return ((Lyrics_attribute*)attributes["lyrics"])->get_lyric((Time_spec)frame);
}
private:
};
#define SHAPE_circle 1
#define SHAPE_square 2
#define SHAPE_triangle 3
class Shape: public Draw_node {
public:
Shape(){
title="Shape";
description="Draws filled shapes";
create_attribute("shape","Shape to draw","Shape","square",{"circle","square","triangle"});
create_attribute("colour","Colour to fill","Colour","FFFFFF");
NODEID="88c30140-2d0b-11e3-8db2-679d596166c1";
};
Shape(map<string,string> &settings):Shape() {
base_settings(settings);
};
~Shape(){};
Shape* clone(map<string,string> &_settings) { return new Shape(_settings);};
void vector_output(cairo_t * cr,const Frame_spec &frame){
colour=Colour(attributes["colour"]->value);
cairo_set_source_rgb(cr, colour.Rdouble(),colour.Gdouble(),colour.Bdouble());
switch(attributes["shape"]->intVal) {
case SHAPE_square:
cairo_rectangle(cr,-frame.w/2,-frame.w/2,frame.w,frame.w);
break;
case SHAPE_circle:
cairo_arc(cr,0,0,frame.w/2,0.0, 2 * M_PI);
break;
case SHAPE_triangle:
//subtracting PI/2 =(1.5*PI)/3 so the triangle is pointing up
cairo_line_to(cr,0,-frame.w/2);
cairo_line_to(cr,cos((0.5 * M_PI)/3)*frame.w/2,sin((0.5 * M_PI)/3)*frame.w/2);
cairo_line_to(cr,cos((2.5 * M_PI)/3)*frame.w/2,sin((2.5 * M_PI)/3)*frame.w/2);
cairo_close_path(cr);
break;
}
cairo_fill(cr);
}
private:
};
class Mosaic: public Draw_node {
//the difficulty here is in keeping the pixels centred
//the center of the screen should remain in between pixels
public:
Mosaic(){
title="Mosaic";
description="Increases size of pixels";
create_image_input("Image to mosaic","Image input");
create_parameter("pixels","number","Height of screen in pixels","Pixels",20.0,0.0,1.0);
NODEID="a33a1a6a-8420-11e3-831b-74d02b29f6a6";
};
Mosaic(map<string,string> &settings):Mosaic() {
base_settings(settings);
};
~Mosaic(){};
Mosaic* clone(map<string,string> &_settings) { return new Mosaic(_settings);};
void vector_output(cairo_t * cr,const Frame_spec &frame){
double vpix=max(parameters["pixels"]->value,1.0);
double pixel_size=frame.h/vpix;
int h_padded=ceil(vpix/2)*2;
int w_padded=ceil((((vpix/frame.h)*frame.w))/2)*2;
Image *in1=image_inputs[0]->get(frame);
if (in1){
Image in=*(in1);
in.crop(w_padded*pixel_size,h_padded*pixel_size);
in.resize(w_padded,h_padded);
int x_start=-((w_padded*pixel_size)/2);
int y_start=-((h_padded*pixel_size)/2);
cairo_translate(cr,x_start,y_start);
for (int i=0;i<w_padded;i++){
for (int j=0;j<h_padded;j++){
double R=((double)in.rgb.data[3*(w_padded*j + i) + 0])/255.0;
double G=((double)in.rgb.data[3*(w_padded*j + i) + 1])/255.0;
double B=((double)in.rgb.data[3*(w_padded*j + i) + 2])/255.0;
cairo_set_source_rgb(cr, R,G,B);
cairo_rectangle(cr,i*pixel_size,j*pixel_size,pixel_size,pixel_size);
cairo_fill(cr);
}
}
}
}
private:
};
class Audio_viz: public Draw_node {
public:
Audio_viz(){}
Audio_viz(map<string,string> &settings):Audio_viz() {
base_settings(settings);
audioactive=true;
};
~Audio_viz(){};
Audio_viz* clone(map<string,string> &_settings) { return new Audio_viz(_settings);};
void vector_output(cairo_t * cr,const Frame_spec &frame){
if (audioactive){
if (!frame.audio){
cerr<<"Audio_viz: audio not found"<<endl;
audioactive=false;
}
}
if (frame.audio){
vector_audio_output(cr,frame);
audioactive=true;
}
}
virtual void vector_audio_output(cairo_t * cr,const Frame_spec &frame){};
private:
bool audioactive;
};
#define WAVES_line 1;
#define WAVES_fill 2;
#define WAVES_both 3;
#define WAVES_mix 4;
#define WAVES_left 5;
#define WAVES_right 6;
class Waves: public Audio_viz {
public:
Waves(){
title="Waves";
description="Draws audio waveforms";
NODEID="4dd50c56-3b2d-11e3-8691-74d02b29f6a6";
create_attribute("stroke","Colour of line stroke","Stroke","FFFFFF");
create_attribute("fill","Colour of line fill","Fill","11AA11");
create_attribute("mode","Drawing mode","Mode","line",{"line","fill","both"});
create_attribute("channel","Channel to draw","Channel","left",{"left","right"});
create_parameter("width","number","Line width","Width",1.0,0.0,25.0);
create_parameter("height","number","Height","Height",1.0);
create_parameter("alpha","number","Alpha blend","Alpha",1.0,0.0,1.0);
}
Waves(map<string,string> &settings):Waves() {
base_settings(settings);
};
~Waves(){};
Waves* clone(map<string,string> &_settings) { return new Waves(_settings);};
void vector_audio_output(cairo_t * cr,const Frame_spec &frame){
stroke=Colour(attributes["stroke"]->value);
fill=Colour(attributes["fill"]->value);
int channel=attributes["channel"]->intVal-1;
if (attributes["mode"]->value=="fill"||attributes["mode"]->value=="both") {
cairo_set_source_rgba(cr, fill.Rdouble(),fill.Gdouble(),fill.Bdouble(),parameters["alpha"]->value);
cairo_save(cr);
//cairo_translate(cr, 0, frame.h/2);
cairo_translate(cr,-frame.w/2,0);
cairo_scale(cr,1.0,parameters["height"]->value);
cairo_line_to(cr, 0, 0);
for (int i=0;i<frame.w;i++){
cairo_line_to(cr,i,(((int16_t)frame.audio->samples[i*frame.audio->channels]+channel)*frame.h)>>16);
}
cairo_line_to(cr, frame.w/2, 0);
cairo_close_path(cr);
cairo_restore (cr);
cairo_fill(cr);
}
if (attributes["mode"]->value=="line"||attributes["mode"]->value=="both") {
cairo_set_source_rgba(cr, stroke.Rdouble(),stroke.Gdouble(),stroke.Bdouble(),parameters["alpha"]->value);
cairo_save(cr);
cairo_translate(cr,-frame.w/2,0);
cairo_scale(cr,1.0,parameters["height"]->value);
cairo_set_line_width (cr, parameters["width"]->value);
//cairo_line_to(cr, 0, 0);
for (int i=0;i<frame.w;i++){
cairo_line_to(cr,i,(((int16_t)frame.audio->samples[i*frame.audio->channels+channel])*frame.h)>>16);
}
//cairo_line_to(cr, frame.w, 0);
cairo_restore (cr);
cairo_stroke(cr);
}
}
private:
Colour stroke;
Colour fill;
};
class Svg: public Draw_node {
public:
//rsvg should be cleanup?
Svg(){
title="SVG";
description="Draws svg files";
create_attribute("filename","SVG file to draw","Filename","");
NODEID="88c30140-2d0b-11e3-8db2-679d596166c1";
rsvg=nullptr;
//g_type_init(); //does this have to be done only once?
};
Svg(map<string,string> &settings):Svg() {
base_settings(settings);
rsvg=rsvg_handle_new_from_file((settings["media_path"]+attributes["filename"]->value).c_str(),nullptr);
if (rsvg) {
rsvg_handle_get_dimensions(rsvg,&dims);
cerr<<"Rotor: SVG loaded "<<attributes["filename"]->value<<" , "<<dims.width<<"x"<<dims.height<<endl;
}
else cerr<<"Rotor: SVG failed to load "<<attributes["filename"]->value<<endl;
};
~Svg(){
rsvg_handle_close(rsvg,nullptr);
//g_object_unref(rsvg);
};
Svg* clone(map<string,string> &_settings) { return new Svg(_settings);};
void vector_output(cairo_t * cr,const Frame_spec &frame){
if (rsvg){
rsvg_handle_render_cairo(rsvg,cr);
cairo_fill(cr);
}
}
void translate(cairo_t * cr,const Frame_spec &frame){
if (rsvg) {
//to make it resolution independent
//translate the difference between screen and drawing
double scale=frame.w/dims.width;
cairo_translate(cr, frame.w/2, frame.h/2);
cairo_translate(cr, parameters["x"]->value * frame.w, parameters["y"]->value * frame.h);
cairo_scale(cr, parameters["scale"]->value*scale, parameters["scale"]->value*scale);
cairo_rotate(cr,(parameters["rotation"]->value/180.0)*M_PI);
cairo_translate(cr, -frame.w/2, -frame.h/2);
cairo_translate(cr, frame.w/2-(dims.width/2), frame.h/2-(dims.height/2));
}
}
private:
RsvgHandle * rsvg;
RsvgDimensionData dims;
};
}
#endif
|