root/trunk/cairo/cairooo/imagebuffersurface.d

Revision 179, 5.6 kB (checked in by DRK, 5 years ago)

* Updated everything in cairo so that it compiles in the latest version of DMD. Also re-ran all the snippets, demos and tutorials, and checked that they worked.

Line 
1 /**
2     XXX
3     
4 Authors: Daniel Keep
5 Copyright: 2006, Daniel Keep
6 License: BSD v2 (http://opensource.org/licenses/bsd-license.php).
7 **/
8 /**
9     Copyright © 2006 Daniel Keep
10     All rights reserved.
11     
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions are
14     met:
15     
16     * Redistributions of source code must retain the above copyright
17       notice, this list of conditions and the following disclaimer.
18       
19     * Redistributions in binary form must reproduce the above copyright
20       notice, this list of conditions and the following disclaimer in the
21       documentation and/or other materials provided with the distribution.
22     
23     * Neither the name of this software, nor the names of its contributors
24       may be used to endorse or promote products derived from this software
25       without specific prior written permission.
26     
27     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30     PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 **/
39 module cairooo.imagebuffersurface;
40
41 // TODO: Allow the this(cairo_surface_t*, bool) constructor to be used
42 // *INTERNALLY* iff the handle has keys on it for the data pointer, refcount
43 // and allocation flag.
44
45 private
46 {
47     import cairo.cairo;
48     import cairooo.enums;
49     import cairooo.exceptions;
50     import cairooo.imagesurface;
51 }
52
53 /+private
54 Surface
55 recastToImageBuffer(cairo_surface_t* handle)
56 {
57     return new ImageBufferSurface(handle, true);
58 }+/
59
60 class ImageBufferSurface : ImageSurface
61 {
62 private:
63     ubyte[] _data = null;
64     uint* _refcount = null;
65     bool selfAllocated = false;
66
67     // Because we can't extract the data buffer from the surface handle, we
68     // won't allow this constructor.
69     // TODO: Allow this constructor :P
70     /+this(cairo_surface_t* handle, bool takeref)
71     {
72         super(handle, takeref);
73     }+/
74    
75 public:
76     this(ImageBufferSurface imageBufferSurface)
77     out
78     {
79         assert( this._data != null );
80         if( this.selfAllocated )
81         {
82             assert( this._refcount != null );
83             assert( (*this._refcount) > 1 );
84         }
85     }
86     body
87     {
88         super(imageBufferSurface);
89         this._data = imageBufferSurface._data;
90         this._refcount = imageBufferSurface._refcount;
91         this.selfAllocated = imageBufferSurface.selfAllocated;
92
93         // ONLY grab a reference if it is self-allocated
94         if( this.selfAllocated )
95             (*this._refcount) ++;
96     }
97
98     this(ubyte[] data, Format format, int width, int height, int stride)
99     out
100     {
101         assert( this._data == data );
102         assert( this.selfAllocated == false );
103         assert( this._refcount == null );
104     }
105     body
106     {
107         // TODO: Check the size of the array to make sure it's large enough,
108         // and check that stride is valid.
109         this._data = data;
110         this.selfAllocated = false;
111         super(cairo_image_surface_create_for_data(data.ptr,
112                     cast(cairo_format_t) format, width, height, stride),
113                 true);
114     }
115
116     this(Format format, int width, int height)
117     out
118     {
119         assert( this._data != null );
120     }
121     body
122     {
123         int stride;
124         switch(format)
125         {
126             case Format.ARGB32: stride = width * 4;     break;
127             case Format.RGB24:  stride = width * 3;     break;
128             case Format.A8:     stride = width;         break;
129             case Format.A1:     stride = (width+31)/8;  break;
130         }
131        
132         this(format, width, height, stride);
133     }
134
135     this(Format format, int width, int height, int stride)
136     out
137     {
138         assert( this._data != null );
139         assert( this.selfAllocated == true );
140         assert( this._refcount != null );
141         assert( (*this._refcount) == 1 );
142     }
143     body
144     {
145         // TODO: Check stride is valid
146         this(new ubyte[stride*height], format, width, height, stride);
147         this.selfAllocated = true;
148        
149         // We created this buffer, so we need to allocate the refcount
150         this._refcount = new uint;
151         (*this._refcount) = 1;
152     }
153
154     ~this()
155     {
156         // Deal with the case where we allocated the buffer ourselves.
157         if( this.selfAllocated )
158         {
159             // Ok, drop a reference; if the refcount is then zero, destroy the
160             // buffer.  If the user wants a copy, they'll just have to dup it.
161             (*this._refcount) --;
162
163             if( (*this._refcount) == 0 )
164             {
165                 delete this._data;      this._data = null;
166                 delete this._refcount;  this._refcount = null;
167             }
168         }
169     }
170
171     //
172     // miscellaneous members
173     //
174     ImageBufferSurface
175     dup()
176     {
177         return new ImageBufferSurface(this);
178     }
179
180     ubyte[]
181     data()
182     {
183         return this._data;
184     }
185 }
Note: See TracBrowser for help on using the browser.