summaryrefslogtreecommitdiff
path: root/rotord/src/nodes_drawing.h
blob: 9a7e30fc1e3bf746f18f917cb7da5ebad6720f12 (plain)
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
#ifndef ROTOR_NODES_DRAWING
#define ROTOR_NODES_DRAWING

#include "rotor.h"
#include <cairo.h>
#include <rsvg.h>

namespace Rotor {
	class Draw_node: public Image_node {
		public:
			Draw_node(){
				create_image_input("image input","Image input");
				//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
			   	vector_output(cr,frame);
				//convert frame back to 24 bits
				cv::cvtColor(chans,image.rgb,CV_RGBA2BGR,3);
				cairo_destroy(cr);
				cairo_surface_destroy(cs);
				return &image;
			}
		protected:
			Colour colour;
		private:

	};
	class Text: public Draw_node {
		public:
			Text(){
				title="Text";
				description="Draws text";
				create_attribute("text","Text to draw","Text","hello, world!");
				create_attribute("colour","Colour to fill","Colour","FFFFFF");
				create_parameter("number","number","Number to draw","Number",-99999999.0f);
				UID="7da93b94-2d0b-11e3-8940-77bce0f9d3e8";
			};
			Text(map<string,string> &settings):Text() {
				base_settings(settings);
			};
			~Text(){};
			Text* clone(map<string,string> &_settings) { return new Text(_settings);};
			void vector_output(cairo_t * cr,const Frame_spec &frame){
				colour=Colour(attributes["colour"]->value);
				string text;
				if (parameters["number"]->value>-99999998.0f){
					text=toString(parameters["number"]->value,4);
				}
				else text=attributes["text"]->value;
				cairo_text_extents_t te;
				cairo_set_source_rgb(cr, colour.Rfloat(),colour.Gfloat(),colour.Bfloat());
				cairo_select_font_face (cr, "Georgia",
				                  CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
				cairo_set_font_size (cr, 120);
				cairo_text_extents(cr, text.c_str(), &te);
				cairo_move_to (cr,(frame.w-te.width)/2,frame.h*0.6);
				cairo_show_text (cr, text.c_str());
				cairo_fill(cr);
			}
		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_parameter("x","number","X coordinate","X",0.0f);
				create_parameter("y","number","Y coordinate","Y",0.0f);
				create_parameter("scale","number","Scale","Scale",1.0f);
				create_parameter("rotation","number","Rotation","Rotation",0.0f);
				create_attribute("shape","Shape to draw","Shape","square",{"circle","square","triangle"});
				create_attribute("colour","Colour to fill","Colour","FFFFFF");
				UID="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.Rfloat(),colour.Gfloat(),colour.Bfloat());
				cairo_save(cr); //not really even necessary?
				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.0f)*M_PI);
				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_restore (cr); //not really even necessary?
				cairo_fill(cr);
			}
		private:
	};
	class Audio_viz: public Draw_node {
		public:
			Audio_viz(){}
			Audio_viz(map<string,string> &settings):Audio_viz() {
				base_settings(settings);
			};
			~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";
				UID="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.0f,0.0f,25.0f);
				create_parameter("scale","number","Scale","Scale",1.0f);
				create_parameter("alpha","number","Alpha blend","Alpha",1.0f,0.0f,1.0f);
			}
			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.Rfloat(),fill.Gfloat(),fill.Bfloat(),parameters["alpha"]->value);
					cairo_save(cr);
					cairo_translate(cr, 0, frame.h/2);
					cairo_scale(cr,1.0f,parameters["scale"]->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_close_path(cr);
					cairo_restore (cr);
					cairo_fill(cr);
				}	
				if (attributes["mode"]->value=="line"||attributes["mode"]->value=="both") {
					cairo_set_source_rgba(cr, stroke.Rfloat(),stroke.Gfloat(),stroke.Bfloat(),parameters["alpha"]->value);
					cairo_save(cr);
					cairo_translate(cr, 0, frame.h/2);
					cairo_scale(cr,1.0f,parameters["scale"]->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:
			Svg(){
				title="SVG";
				description="Draws svg files";
				create_parameter("x","number","X coordinate","X",0.0f);
				create_parameter("y","number","Y coordinate","Y",0.0f);
				create_parameter("scale","number","Scale","Scale",1.0f);
				create_parameter("rotation","number","Rotation","Rotation",0.0f);
				create_attribute("filename","SVG file to draw","Filename","");
				UID="88c30140-2d0b-11e3-8db2-679d596166c1";
			};
			Svg(map<string,string> &settings):Svg() {
				base_settings(settings);
			};
			~Svg(){};
			Svg* clone(map<string,string> &_settings) { return new Svg(_settings);};
			void vector_output(cairo_t * cr,const Frame_spec &frame){
				colour=Colour(attributes["colour"]->value);
				cairo_save(cr); //not really even necessary?
				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.0f)*M_PI);
				

				cairo_restore (cr); //not really even necessary?
				cairo_fill(cr);
			}
		private:
	};
}

#endif