1 /* === S Y N F I G ========================================================= */
2 /*!	\file synfig/renddesc.h
3 **	\brief Class that defines the parameters needed by the Renderer to
4 * render a context to a surface.
5 **
6 **	$Id$
7 **
8 **	\legal
9 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 **	Copyright (c) 2008 Chris Moore
11 **
12 **	This package is free software; you can redistribute it and/or
13 **	modify it under the terms of the GNU General Public License as
14 **	published by the Free Software Foundation; either version 2 of
15 **	the License, or (at your option) any later version.
16 **
17 **	This package is distributed in the hope that it will be useful,
18 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **	General Public License for more details.
21 **	\endlegal
22 */
23 /* ========================================================================= */
24 
25 /* === S T A R T =========================================================== */
26 
27 #ifndef __SYNFIG_RENDERDESC_H
28 #define __SYNFIG_RENDERDESC_H
29 
30 /* === H E A D E R S ======================================================= */
31 
32 #include "vector.h"
33 #include "color.h"
34 #include "types.h"
35 #include <cmath>
36 #include "rect.h"
37 #include "matrix.h"
38 
39 /* === M A C R O S ========================================================= */
40 
41 #ifndef DPM2DPI
42 #define DPM2DPI(x)	(float(x)/39.3700787402f)
43 #define DPI2DPM(x)	(float(x)*39.3700787402f)
44 #endif
45 
46 /* === T Y P E D E F S ===================================================== */
47 
48 /* === C L A S S E S & S T R U C T S ======================================= */
49 
50 namespace synfig {
51 
52 /*!	\class RendDesc
53 **	\todo writeme
54 */
55 class RendDesc
56 {
57 public:
58 	enum Lock
59 	{
60 		PX_ASPECT=(1<<0),		// "Pixel Aspect" in Locks and Links
61 		PX_AREA=(1<<1),			// not used
62 		PX_W=(1<<2),			// "Pixel Width" in Locks and Links - not used
63 		PX_H=(1<<3),			// "Pixel Height" in Locks and Links - not used
64 
65 		IM_ASPECT=(1<<4),		// "Image Aspect" in Locks and Links
66 		IM_SPAN=(1<<5),			// "Image Span" in Locks and Links
67 		IM_W=(1<<6),			// "Image Width" in Locks and Links
68 		IM_H=(1<<7),			// "Image Height" in Locks and Links
69 		IM_ZOOMIN=(1<<8),		// not used
70 		IM_ZOOMOUT=(1<<9),		// not used
71 
72 		LINK_PX_ASPECT=(1<<10),	// not used
73 		LINK_PX_AREA=(1<<11),	// not used
74 		LINK_IM_ASPECT=(1<<12),	// "Width and Height pixel ratio" in Image Size
75 		LINK_IM_SPAN=(1<<13),	// not used
76 		LINK_IM_CENTER=(1<<14),	// not used
77 		LINK_RES=(1<<15)		// Resolution ratio in Image Size
78 	};
79 
80 private:
81 	//! Width and height of the composition in pixels
82 	int w_,h_;
83 	//! Width and height used for fixed ratio
84 	int w_ratio_, h_ratio_;
85 	//! Horizontal resolution of the composition in pixels per meter
86 	Real x_res;
87 	//! Vertical resolution of the composition in pixels per meter
88 	Real y_res;
89 	//! Horizontal resolution for fixed ratio
90 	Real x_res_ratio_;
91 	//! Vertical resolution for fixed ratio
92 	Real y_res_ratio_;
93 	//! The Top Left and the Bottom Right Points of the composition
94 	Point tl_, br_;
95 	//! The Focus Point of the composition. Used when zooming in
96 	Point focus;
97 	//! Anti-alias value
98 	int a;
99 	//! The background color used when alpha is not supported or avoided
100 	Color background;
101 	//! The result of the flags combination.
102 	//! \see enum Lock
103 	int flags;
104 	//! Interlaced flag for targets that supports it
105 	bool interlaced;
106 	//! Clamp flag to decide if color must be clamped or not
107 	bool clamp;
108 	//! When \c true layers with exclude_from_rendering flag should be rendered
109 	bool render_excluded_contexts;
110 	//! Frame rate of the composition to be rendered
111 	float frame_rate;
112 	//! Begin time and end time of the Composition to render
113 	Time time_begin, time_end;
114 	//! Transformation matrix which should be applied for each primitive before rendering
115 	Matrix transformation_matrix;
116 	//! Root outline grow value
117 	Real outline_grow;
118 
119 public:
120 	//! Anti alias filers types. Seems never implemented
121 	enum
122 	{
123 		ANTIALIAS_UNIFORM,
124 		ANTIALIAS_MONTE_CARLO,
125 		ANTIALIAS_JITTERED,
126 		ANTIALIAS_ADAPTIVE,
127 		ANTIALIAS_QUINTCUNX
128 	} AntialiasFilter;
129 
130 	//! Default Constructor
RendDesc()131 	RendDesc():
132 		w_			(480),
133 		h_			(270),
134 		x_res		(DPI2DPM(72.0f)),
135 		y_res		(DPI2DPM(72.0f)),
136 		tl_			(-4,2.25),
137 		br_			(4,-2.25),
138 		focus		(0,0),
139 		a			(2),
140 		background	(Color::gray()),
141 		flags		(0),
142 		interlaced	(false),
143 		clamp		(false),
144 		render_excluded_contexts(false),
145 		frame_rate	(24),
146 		time_begin	(0),
147 		time_end	(0),
148 		outline_grow(0),
149 		AntialiasFilter(ANTIALIAS_UNIFORM)
150 	{ }
151 
zero()152 	static RendDesc zero()
153 	{
154 		RendDesc desc;
155 		desc.set_wh(0, 0);
156 		return desc;
157 	}
158 
is_zero()159 	bool is_zero() const
160 		{ return get_w() <= 0 || get_h() <= 0; }
161 
162 	//! Applies the given Render Description \x to the current one
163 	RendDesc &apply(const RendDesc &x);
164 
165 	//! Gets the background color
166 	const Color &get_bg_color()const;
167 
168 	//! Sets the background color
169 	RendDesc &set_bg_color(const Color &bg);
170 
171 	//! Return the width of the composition in pixels
172 	int get_w()const;
173 
174 	//! Set the width of the composition in pixels.
175 	/*! The other parameters are adjusted according to the
176 	**	constraints placed on the flags.
177 	* Seems to be incomplete and doesn't use all the possible
178 	* flags.
179 	* \todo write the needed code to keep the flags usage
180 	*/
181 	RendDesc &set_w(int x);
182 
183 	//! Return the height of the composition in pixels
184 	int	get_h()const;
185 
186 	//! Set the height of the composition in pixels.
187 	/*! The other parameters are adjusted according to the
188 	**	constraints placed on the flags.
189 	* Seems to be incomplete and doesn't use all the possible
190 	* flags.
191 	* \todo write the needed code to keep the flags usage
192 	*/
193 	RendDesc &set_h(int y);
194 
195 	//!	Sets the width and height of the composition in pixels
196 	RendDesc &set_wh(int x, int y);
197 
198     //! Returns the horizontal resolution (in dots per meter)
199 	Real get_x_res()const;
200 
201 	//! Sets the horizontal resolution (in dots per meter)
202 	RendDesc &set_x_res(Real x);
203 
204     //! Returns the vertical resolution (in dots per meter)
205 	Real get_y_res()const;
206 
207 	//! Sets the vertical resolution (in dots per meter)
208 	RendDesc &set_y_res(Real y);
209 
210 
211 	//! Return the physical width of the composition in meters
212 	Real get_physical_w()const;
213 
214 	//! Return the physical height of the composition in meters
215 	Real get_physical_h()const;
216 
217 	//! Set the physical width of the composition in meters
218 	RendDesc &set_physical_w(Real w);
219 
220 	//! Set the physical height of the composition in meters
221 	RendDesc &set_physical_h(Real h);
222 
223 
224 	//!	Return the index of the first frame
225 	int get_frame_start()const;
226 
227 	//! Set the index of the first frame
228 	RendDesc &set_frame_start(int x);
229 
230 	//!	Return the index of the last frame
231 	int get_frame_end()const;
232 
233 	//! Set the index of the last frame
234 	RendDesc &set_frame_end(int x);
235 
236 	//!	Return the starting time of the animation
237 	const Time get_time_start()const;
238 
239 	//!	Set the time that the animation will start
240 	RendDesc &set_time_start(Time x);
241 
242 	//! Return the end time of the animation
243 	const Time get_time_end()const;
244 
245 	//!	Set the time that the animation will end
246 	RendDesc &set_time_end(Time x);
247 
248 	//!	Setup for one frame at the given time
249 	RendDesc &set_time(Time x);
250 
251 	//!	Setup for one frame
252 	RendDesc &set_frame(int x);
253 
254 	//!	Return the frame rate (frames-per-second)
255 	const float &get_frame_rate()const;
256 
257 	//! Set the frame rate (frames-per-second)
258 	RendDesc &set_frame_rate(float x);
259 
260 	//! Return the status of the interlaced flag
261 	const bool &get_interlaced()const;
262 
263 	//! Set the interlace flag
264 	RendDesc &set_interlaced(bool x);
265 
266 	//! Return the status of the clamp flag
267 	const bool &get_clamp()const;
268 
269 	//! Set the clamp flag
270 	RendDesc &set_clamp(bool x);
271 
272 	//! Return the status of the render_excluded_contexts flag
273 	const bool &get_render_excluded_contexts()const;
274 
275 	//! Set the render_excluded_contexts flag
276 	RendDesc &set_render_excluded_contexts(bool x);
277 
278 	//! Set constraint flags
279 	RendDesc &set_flags(const int &x);
280 
281 	//! Clear constraint flags
282 	RendDesc &clear_flags();
283 
284 	//! Get constraint flags
285 	int get_flags()const;
286 
287 	//!	Return the aspect ratio of a single pixel
288 	Point::value_type get_pixel_aspect()const;
289 
290 	//!	Return the aspect ratio of the entire image
291 	Point::value_type get_image_aspect()const;
292 
293 	//! Set the pixel ratio for LINK_IM_ASPECT flag
294 	void set_pixel_ratio(const int &x, const int &y);
295 
296 	//! Get the reduced pixel ratio (based on euclide reduction)
297 	void get_pixel_ratio_reduced(int &w_ratio_reduced, int &h_ratio_reduced);
298 
299 	//! Set the resolution ratio for LINK_RES flag
300 	void set_res_ratio(const Real &x, const Real &y);
301 
302 	//! Return the antialias amount
303 	const int &get_antialias()const;
304 
305 	//! Set the antialias amount
306 	RendDesc &set_antialias(const int &x);
307 
308 	//! Return the distance from the bottom-right to the top-left
309 	Real get_span()const;
310 
311 	//! Set the span distance
312 	RendDesc& set_span(const Real &x);
313 
314 	//! Gets the focus Point
315 	const Point &get_focus()const;
316 	//! Sets the focus Point
317 	RendDesc &set_focus(const Point &x);
318 	//! Gets the top left point of the compostion
319 	const Point &get_tl()const;
320 	//! Sets the top left point of the compostion
321 	RendDesc &set_tl(const Point &x);
322 	//! Gets the bottom right point of the compostion
323 	const Point &get_br()const;
324 	//! Sets the bottom right point of the compostion
325 	RendDesc &set_br(const Point &x);
326 	//! Sets the top left and the bottom right of the composition
327 	// Use this when the individual set_tl or set_br
328 	// produce degenerate w or h
329 	RendDesc &set_tl_br( const Point &x, const Point &y);
330 	//! Returns the rectangle of the composition
get_rect()331 	Rect get_rect()const { return Rect(get_tl(),get_br()); }
332 	//! Sets the view port by the top left and right bottom corners
333 	RendDesc &set_viewport(const Point &__tl, const Point &__br);
334 	//! Sets the view port by the four corners values
335 	RendDesc &set_viewport(Vector::value_type a,Vector::value_type b,Vector::value_type c,Vector::value_type d);
336 	//! Returns the width of one pixel
337 	Real get_pw()const;
338 	//! Returns the height of one pixel
339 	Real get_ph()const;
340 	//! Sets viewport to represent the screen at the given pixel coordinates
341 	RendDesc &set_subwindow(int x, int y, int w, int h);
342 	//! Sets the duration of the animation.
343 	// Keeps the start time and modifies the end time to match the duration
344 	RendDesc &set_duration(Time t);
345 	//! Gets the duration of the animation
346 	const Time get_duration();
347 	//! Sets the transformation matrix
set_transformation_matrix(const Matrix & x)348 	void set_transformation_matrix(const Matrix &x) { transformation_matrix = x; }
349 	//! Gets the transformation matrix
get_transformation_matrix()350 	const Matrix& get_transformation_matrix() const { return transformation_matrix; }
351 
get_world_to_pixels_matrix()352 	Matrix get_world_to_pixels_matrix() const
353 	{
354 		const Real epsilon = 1e-20;
355 		Vector size = get_br() - get_tl();
356 		Vector ratio(
357 			fabs(size[0]) < epsilon ? 0.0 : 1.0/size[0] * Real(get_w()),
358 			fabs(size[1]) < epsilon ? 0.0 : 1.0/size[1] * Real(get_h()) );
359 		return Matrix(
360 			ratio[0], 0.0, 0.0,
361 			0.0, ratio[1], 0.0,
362 			-get_tl()[0]*ratio[0], -get_tl()[1]*ratio[1], 1.0 );
363 	}
364 
get_pixels_to_world_matrix()365 	Matrix get_pixels_to_world_matrix() const
366 	{
367 		return Matrix(
368 			(get_w() > 0 ? 0.0 : 1.0/Real(get_w())*(get_br()[0] - get_tl()[0])), 0.0, 0.0,
369 			0.0, (get_h() > 0 ? 0.0 : 1.0/Real(get_h())*(get_br()[1] - get_tl()[1])), 0.0,
370 			get_tl()[0], get_tl()[1], 1.0 );
371 	}
372 
get_outline_grow()373 	Real get_outline_grow() const { return outline_grow; }
set_outline_grow(Real x)374 	void set_outline_grow(Real x) { outline_grow = x; }
375 
376 };	// END of class RendDesc
377 
378 //! This operator allows the combining of RendDesc::Lock flags using the '|' operator
379 /*!	\see RendDesc::Lock, RendDesc */
380 inline RendDesc::Lock operator|(RendDesc::Lock lhs, RendDesc::Lock rhs)
381 {
382 	return static_cast<RendDesc::Lock>((int)lhs|(int)rhs);
383 }
384 
385 //! This operator allows the masking of RendDesc::Lock flags using the '&' operator
386 /*!	\see RendDesc::Lock, RendDesc */
387 inline RendDesc::Lock operator&(RendDesc::Lock lhs, RendDesc::Lock rhs)
388 {
389 	return static_cast<RendDesc::Lock>((int)lhs&(int)rhs);
390 }
391 
392 //! This operator allows the inverting of RendDesc::Lock flags using the '~' operator
393 /*!	\see RendDesc::Lock, RendDesc */
394 inline RendDesc::Lock operator~(RendDesc::Lock rhs)
395 {
396 	return static_cast<RendDesc::Lock>(~(int)rhs);
397 }
398 
399 }; /* end namespace synfig */
400 
401 /* === E N D =============================================================== */
402 
403 #endif
404