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