Added Evas::Image#data, data= and data_update_add.
[ruby-evas.git] / src / rb_image.c
1 /*
2  * $Id: rb_image.c 376 2006-02-25 10:10:31Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Evas.h>
24
25 #include "rb_evas_main.h"
26 #include "rb_evas.h"
27 #include "rb_evas_object.h"
28
29 /*
30  * call-seq:
31  *  Evas::Image.new(evas) => img
32  *
33  * Creates an Evas::Image object.
34  */
35 static VALUE c_init (VALUE self, VALUE evas)
36 {
37         CHECK_CLASS (evas, cEvas);
38         GET_OBJ (evas, RbEvas, e);
39         GET_OBJ (self, RbEvasObject, img);
40
41         img->real = evas_object_image_add (e->real);
42
43         rb_call_super (1, &evas);
44
45         return self;
46 }
47
48 /*
49  * call-seq:
50  *  img.get_file => array
51  *
52  * Returns an array containing the filename and the key of <i>img</i>.
53  *
54  *  img.set_file("foo", "bar") #=> nil
55  *  img.get_file               #=> ["foo", "bar"]
56  */
57 static VALUE c_get_file (VALUE self)
58 {
59         char *file = NULL, *key = NULL;
60
61         GET_OBJ (self, RbEvasObject, e);
62
63         evas_object_image_file_get (e->real, &file, &key);
64
65         return rb_ary_new3 (2, file ? rb_str_new2 (file) : Qnil,
66                             key ? rb_str_new2 (key) : Qnil);
67 }
68
69 /*
70  * call-seq:
71  *  img.set_file(file [, key]) => nil
72  *
73  * Sets the filename and optionally the key of <i>img</i>.
74  *
75  *  img.set_file("foo.png")             #=> nil
76  *  img.set_file("foo.edb", "/bar/baz") #=> nil
77  */
78 static VALUE c_set_file (int argc, VALUE *argv, VALUE self)
79 {
80         VALUE file, key;
81         char *k = NULL;
82
83         GET_OBJ (self, RbEvasObject, e);
84
85         rb_scan_args (argc, argv, "11", &file, &key);
86
87         Check_Type (file, T_STRING);
88
89         if (!NIL_P (key)) {
90                 Check_Type (key, T_STRING);
91                 k = StringValuePtr (key);
92         }
93
94         evas_object_image_file_set (e->real, StringValuePtr (file), k);
95
96         return Qnil;
97 }
98
99 /*
100  * call-seq:
101  *  img.has_alpha? => true or false
102  *
103  * Returns true if <i>img</i> has an alpha channel, else returns false.
104  */
105 static VALUE c_has_alpha_get (VALUE self)
106 {
107         GET_OBJ (self, RbEvasObject, e);
108
109         return evas_object_image_alpha_get (e->real) ? Qtrue : Qfalse;
110 }
111
112 /*
113  * call-seq:
114  *  img.has_alpha(true or false)
115  *
116  * Sets whether <i>img</i> has an alpha channel.
117  */
118 static VALUE c_has_alpha_set (VALUE self, VALUE val)
119 {
120         GET_OBJ (self, RbEvasObject, e);
121
122         CHECK_BOOL (val);
123
124         evas_object_image_alpha_set (e->real, val == Qtrue);
125
126         return Qnil;
127 }
128
129 /*
130  * call-seq:
131  *  img.get_size => array
132  *
133  * Returns an array containing the size of <i>img</i>.
134  *
135  *  img.set_size(100, 200) #=> nil
136  *  img.get_size           #=> [100, 200]
137  */
138 static VALUE c_get_size (VALUE self)
139 {
140         int w = 0, h = 0;
141
142         GET_OBJ (self, RbEvasObject, e);
143
144         evas_object_image_size_get (e->real, &w, &h);
145
146         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
147 }
148
149 /*
150  * call-seq:
151  *  img.set_size(x, y) => nil
152  *
153  * Returns an array containing the size of <i>img</i>.
154  *
155  *  img.set_size(100, 200) #=> nil
156  *  img.get_size           #=> [100, 200]
157  */
158 static VALUE c_set_size (VALUE self, VALUE w, VALUE h)
159 {
160         GET_OBJ (self, RbEvasObject, e);
161
162         Check_Type (w, T_FIXNUM);
163         Check_Type (h, T_FIXNUM);
164
165         evas_object_image_size_set (e->real, FIX2INT (w), FIX2INT (h));
166
167         return Qnil;
168 }
169
170 /*
171  * call-seq:
172  *  img.get_fill => array
173  *
174  * Returns an array containing the dimensions of the rectangle
175  * on <i>img</i> that the image will be drawn to.
176  *
177  *  img.set_fill(1, 2, 3, 4) #=> nil
178  *  img.get_fill             #=> [1, 2, 3, 4]
179  */
180 static VALUE c_get_fill (VALUE self)
181 {
182         Evas_Coord x = 0, y = 0, w = 0, h = 0;
183
184         GET_OBJ (self, RbEvasObject, e);
185
186         evas_object_image_fill_get (e->real, &x, &y, &w, &h);
187
188         return rb_ary_new3 (4, INT2FIX ((int) x), INT2FIX ((int) y),
189                             INT2FIX ((int) w), INT2FIX ((int) h));
190 }
191
192 /*
193  * call-seq:
194  *  img.set_fill(x, y, w, h) => nil
195  *
196  * Sets the dimensions of the rectangle on <i>img</i> that
197  * the image will be drawn to.
198  *
199  *  img.set_fill(1, 2, 3, 4) #=> nil
200  */
201 static VALUE c_set_fill (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
202 {
203         GET_OBJ (self, RbEvasObject, e);
204
205         Check_Type (x, T_FIXNUM);
206         Check_Type (y, T_FIXNUM);
207         Check_Type (w, T_FIXNUM);
208         Check_Type (h, T_FIXNUM);
209
210         evas_object_image_fill_set (e->real, FIX2INT (x), FIX2INT (y),
211                                     FIX2INT (w), FIX2INT (h));
212
213         return Qnil;
214 }
215
216 static VALUE c_get_border (VALUE self)
217 {
218         int x = 0, y = 0, w = 0, h = 0;
219
220         GET_OBJ (self, RbEvasObject, e);
221
222         evas_object_image_border_get (e->real, &x, &y, &w, &h);
223
224         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
225                             INT2FIX (w), INT2FIX (h));
226 }
227
228 static VALUE c_set_border (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
229 {
230         GET_OBJ (self, RbEvasObject, e);
231
232         Check_Type (x, T_FIXNUM);
233         Check_Type (y, T_FIXNUM);
234         Check_Type (w, T_FIXNUM);
235         Check_Type (h, T_FIXNUM);
236
237         evas_object_image_border_set (e->real, FIX2INT (x), FIX2INT (y),
238                                       FIX2INT (w), FIX2INT (h));
239
240         return Qnil;
241 }
242
243 /*
244  * call-seq:
245  *  img.reload => nil
246  *
247  * Reloads <i>img</i>.
248  */
249 static VALUE c_reload (VALUE self)
250 {
251         GET_OBJ (self, RbEvasObject, e);
252
253         evas_object_image_reload (e->real);
254
255         return Qnil;
256 }
257
258 static VALUE c_data_get (int argc, VALUE *argv, VALUE self)
259 {
260         VALUE read_write = Qfalse;
261         void *data;
262         int w = 0, h = 0;
263
264         GET_OBJ (self, RbEvasObject, e);
265
266         rb_scan_args (argc, argv, "01", &read_write);
267
268         if (NIL_P (read_write))
269                 read_write = Qfalse;
270
271         data = evas_object_image_data_get (e->real, read_write == Qtrue);
272         evas_object_image_size_get (e->real, &w, &h);
273
274         return rb_str_new (data, h * w * 4);
275 }
276
277 static VALUE c_data_set (VALUE self, VALUE data)
278 {
279         GET_OBJ (self, RbEvasObject, e);
280
281         evas_object_image_data_set (e->real, StringValuePtr (data));
282
283         return Qnil;
284 }
285
286 static VALUE c_data_update_add (VALUE self, VALUE x, VALUE y,
287                                 VALUE w, VALUE h)
288 {
289         GET_OBJ (self, RbEvasObject, e);
290
291         evas_object_image_data_update_add (e->real,
292                                            FIX2INT (x), FIX2INT (y),
293                                            FIX2INT (w), FIX2INT (h));
294
295         return Qnil;
296 }
297
298 void Init_Image (void)
299 {
300         VALUE c = rb_define_class_under (mEvas, "Image", cEvasObject);
301
302         rb_define_method (c, "initialize", c_init, 1);
303         rb_define_method (c, "get_file", c_get_file, -1);
304         rb_define_method (c, "set_file", c_set_file, -1);
305         rb_define_method (c, "has_alpha?", c_has_alpha_get, 0);
306         rb_define_method (c, "has_alpha=", c_has_alpha_set, 1);
307         rb_define_method (c, "get_size", c_get_size, 0);
308         rb_define_method (c, "set_size", c_set_size, 2);
309         rb_define_method (c, "get_fill", c_get_fill, 0);
310         rb_define_method (c, "set_fill", c_set_fill, 4);
311         rb_define_method (c, "get_border", c_get_border, 0);
312         rb_define_method (c, "set_border", c_set_border, 4);
313         rb_define_method (c, "reload", c_reload, 0);
314         rb_define_method (c, "data", c_data_get, -1);
315         rb_define_method (c, "data=", c_data_set, 1);
316         rb_define_method (c, "data_update_add", c_data_update_add, 4);
317 }