moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / surfacenew.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file surfacenew.h
3 **      \brief Template Header
4 **
5 **      $Id: surfacenew.h,v 1.1 2005/01/21 19:29:10 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SYNFIG_SURFACENEW_H
25 #define __SYNFIG_SURFACENEW_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <ETL/handle>
30 #include <ETL/ref_count>
31 #include "color.h"
32 #include "mutex.h"
33 #include <map>
34
35 /* === M A C R O S ========================================================= */
36
37 /* === T Y P E D E F S ===================================================== */
38
39 /* === C L A S S E S & S T R U C T S ======================================= */
40
41 namespace synfig {
42
43 class Surface;
44 class SurfaceChannelLock;
45 class SurfaceChannelLockConst;
46
47 //! \writeme
48 enum SurfaceColorSystem
49 {
50         COLORSYS_RGB,
51         COLORSYS_YUV,
52         
53         COLORSYS_END
54 }; // END of enum SurfaceColorSystem
55
56 //! \writeme
57 enum SurfaceChannel
58 {
59         CHAN_A,
60         CHAN_R,
61         CHAN_G,
62         CHAN_B,
63
64         CHAN_Y,
65         CHAN_U,
66         CHAN_V,
67
68         CHAN_END
69 }; // END of enum SurfaceChannel
70
71 class SurfaceNew : etl::shared_object
72 {
73         friend class SurfaceChannelLock;
74
75         /*
76  --     ** -- T Y P E S -----------------------------------------------------------
77         */
78
79 public: 
80
81         //! \writeme
82         typedef etl::handle<SurfaceNew> Handle;
83
84         //! \writeme
85         typedef etl::handle<const SurfaceNew> HandleConst;
86         
87         //! \writeme
88         typedef etl::loose_handle<SurfaceNew> LooseHandle;
89
90         //! \writeme
91         typedef SurfaceChannel;
92
93         //! \writeme
94         typedef SurfaceChannelLock ChannelLock;
95
96         //! \writeme
97         typedef SurfaceChannelLockConst ChannelLockConst;
98
99         //! \writeme
100         typedef SurfaceColorSystem;
101         
102         //! \writeme
103         class Lock
104         {
105                 Handle x;
106         public:
107                 Lock(const Handle& x):x(x) { x->lock(); }
108                 void unlock() { if(x){ x->unlock(); x=0; } }
109                 ~Lock() { unlock(); }           
110         }; // END of class Lock
111         friend class Lock;
112         
113 private:
114
115         //! \writeme
116         class ChannelData;
117         
118         /*
119  --     ** -- D A T A -------------------------------------------------------------
120         */
121
122 private:
123         
124         //! \writeme
125         RecMutex mutex_;
126         
127         //! \writeme
128         int w_,h_;
129
130         //! \writeme
131         ColorSystem color_system_;
132         
133         //! \writeme
134         bool premult_flag_;
135
136         //! \writeme
137         std::map<Channel,ChannelData> channel_map_;
138         
139         /*
140  -- ** -- S I G N A L S -------------------------------------------------------
141         */
142
143 private:
144
145         /*
146  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
147         */
148
149 public:
150
151         /*
152  --     ** -- C O N S T R U C T O R S ---------------------------------------------
153         */
154
155 protected:
156         
157         //! \writeme
158         SurfaceNew();
159
160 public:
161
162         //! \writeme
163         virtual ~SurfaceNew();
164
165         /*
166  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
167         */
168
169 public:
170
171         //! \writeme
172         int get_w()const;
173         
174         //! \writeme
175         int get_h()const;
176         
177         //! \writeme
178         void set_wh(int w, int h);
179
180         //! \writeme
181         ColorSystem get_color_system()const;
182
183         //! \writeme
184         void set_color_system(ColorSystem x);
185
186         //! Should only be used in certain circumstances
187         Color get_color(int x, int y)const;
188         
189         //! \writeme
190         void lock();
191         
192         //! \writeme
193         void unlock();
194         
195         //! \writeme
196         bool trylock();
197         
198         //! \writeme
199         ChannelLock lock_channel(Channel chan);
200
201         //! \writeme
202         ChannelLockConst lock_channel_const(Channel chan)const;
203
204         //! \writeme
205         ChannelLock lock_channel_alpha(Channel chan);
206
207         //! \writeme
208         ChannelLockConst lock_channel_alpha_const(Channel chan)const;
209
210         //! \writeme
211         bool is_channel_defined(Channel chan)const;
212         
213         //! \writeme
214         bool get_premult()const;
215
216         //! \writeme
217         void set_premult();
218         
219         /*
220  --     ** -- S T A T I C   F U N C T I O N S -------------------------------------
221         */
222
223 public:
224
225         //! Normal SurfaceNew Constructor
226         static Handle create(int w=0, int h=0, ColorSystem sys=COLORSYS_RGB);
227
228         //! Converts an old Surface to a SurfaceNew
229         static Handle create(const Surface&);
230
231         //! Dupilcates a surface
232         static Handle create(HandleConst);
233
234         //! Creates a cropped copy of a surface
235         static Handle crop(HandleConst, int x, int y, int w, int h);
236         
237         static void blit(
238                 Handle dest,
239                 int x_dest,
240                 int y_dest,
241                 HandleConst src,
242                 float amount=1.0,
243                 Color::BlendMethod bm=Color::BLEND_COMPOSITE
244         );
245         
246         static void blit(
247                 Handle dest,
248                 int x_dest,
249                 int y_dest,
250                 Handle src,
251                 int x_src,
252                 int y_src,
253                 int w_src,
254                 int h_src,
255                 float amount=1.0,
256                 Color::BlendMethod bm=Color::BLEND_COMPOSITE
257         );
258         
259         
260         static void chan_mlt(ChannelLock& dest, float x);
261         static void chan_mlt(ChannelLock& dest, const ChannelLockConst& x);
262
263         static void chan_div(ChannelLock& dest, float x);
264         static void chan_div(ChannelLock& dest, const ChannelLockConst& x);
265
266         static void chan_add(ChannelLock& dest, float x);
267         static void chan_add(ChannelLock& dest, const ChannelLockConst& x);
268
269         static void chan_sub(ChannelLock& dest, float x);
270         static void chan_sub(ChannelLock& dest, const ChannelLockConst& x);
271 }; // END of class SurfaceNew
272
273 //! \writeme
274 class SurfaceChannelLockConst
275 {
276         friend class SurfaceNew;
277
278         /*
279  --     ** -- T Y P E S -----------------------------------------------------------
280         */
281
282 public: 
283
284         /*
285  --     ** -- D A T A -------------------------------------------------------------
286         */
287
288 protected:
289         
290         //! \writeme
291         SurfaceNew::Handle surface_;
292         
293         //! \writeme
294         etl::reference_counter ref_count_;
295
296         //! \writeme
297         SurfaceChannel channel_;
298
299         //! \writeme
300         bool data_ptr_checked_out_;
301         
302         /*
303  --     ** -- C O N S T R U C T O R S ---------------------------------------------
304         */
305
306 public:
307
308         SurfaceChannelLockConst();
309         
310         //! \writeme
311         ~SurfaceChannelLockConst();
312
313         /*
314  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
315         */
316
317 public:
318         
319         //! \writeme
320         SurfaceChannel get_channel()const;
321         
322         //! \writeme
323         int get_w()const;
324         
325         //! \writeme
326         int get_h()const;
327         
328         //! \writeme
329         float get_value(int x, int y);
330         
331         //! \writeme
332         const float* get_data_ptr()const;
333         
334         //! \writeme
335         int get_data_ptr_stride()const;
336         
337         //! Releases the pointer obtained with get_data_ptr()
338         void release_data_ptr()const;
339         
340         //! \writeme
341         operator bool()const;
342 }; // END of class SurfaceChannelLockConst
343
344
345 //! \writeme
346 class SurfaceChannelLock : public SurfaceChannelLockConst
347 {
348         friend class SurfaceNew;
349
350         using SurfaceChannelLock::get_data_ptr;
351         
352         /*
353  --     ** -- T Y P E S -----------------------------------------------------------
354         */
355
356 public: 
357
358         /*
359  --     ** -- D A T A -------------------------------------------------------------
360         */
361
362 private:
363         
364         /*
365  --     ** -- C O N S T R U C T O R S ---------------------------------------------
366         */
367
368 public:
369
370         //! \writeme
371         SurfaceChannelLock();
372
373         //! \writeme
374         ~SurfaceChannelLock();
375
376         /*
377  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
378         */
379
380 public:
381
382         //! \writeme
383         void clear();
384         
385         //! \writeme
386         void fill(float value);
387                 
388         //! \writeme
389         void set_value(int x, int y, float v);
390
391         float* get_data_ptr();
392 }; // END of class ChannelLock
393
394
395 }; // END of namespace synfig
396
397 /* === E N D =============================================================== */
398
399 #endif