rays 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,318 @@
1
+ #include "rays/ruby/bitmap.h"
2
+
3
+
4
+ #include <rucy.h>
5
+ #include "defs.h"
6
+
7
+
8
+ using namespace Rucy;
9
+
10
+ using Rays::uchar;
11
+ using Rays::ushort;
12
+ using Rays::ulong;
13
+ using Rays::coord;
14
+
15
+
16
+ namespace Rays
17
+ {
18
+
19
+
20
+ static Class cBitmap;
21
+
22
+ Class
23
+ bitmap_class ()
24
+ {
25
+ return cBitmap;
26
+ }
27
+
28
+
29
+ }// Rays
30
+
31
+
32
+ namespace Rucy
33
+ {
34
+
35
+
36
+ Value
37
+ value (const Rays::Bitmap& bitmap)
38
+ {
39
+ return new_type<Rays::Bitmap>(
40
+ Rays::bitmap_class(), new Rays::Bitmap(bitmap));
41
+ }
42
+
43
+
44
+ }// Rucy
45
+
46
+
47
+ #define this to<Rays::Bitmap*>(self)
48
+
49
+ #define CHECK CHECK_OBJECT(self, Rays::Bitmap, Rays::bitmap_class())
50
+
51
+
52
+ static
53
+ VALUE alloc(VALUE klass)
54
+ {
55
+ return new_type<Rays::Bitmap>(klass, new Rays::Bitmap);
56
+ }
57
+
58
+ static
59
+ VALUE initialize(VALUE self)
60
+ {
61
+ CHECK_OBJ(self, Rays::Bitmap, Rays::bitmap_class());
62
+ if (argc != 0 && argc != 2 && argc != 3)
63
+ arg_count_error("Bitmap#initialize", argc, 0, 2, 3);
64
+
65
+ if (argc == 0) return self;
66
+
67
+ int width = to<int>(argv[0]);
68
+ int height = to<int>(argv[1]);
69
+ uint colorspace = (argc == 3) ? to<uint>(argv[2]) : (uint) Rays::RGBA;
70
+
71
+ *this = Rays::Bitmap(width, height, (Rays::ColorSpaceType) colorspace);
72
+ return self;
73
+ }
74
+
75
+ static
76
+ VALUE width(VALUE self)
77
+ {
78
+ CHECK;
79
+
80
+ return value(this->width());
81
+ }
82
+
83
+ static
84
+ VALUE height(VALUE self)
85
+ {
86
+ CHECK;
87
+
88
+ return value(this->height());
89
+ }
90
+
91
+ static
92
+ VALUE color_space(VALUE self)
93
+ {
94
+ CHECK;
95
+
96
+ return value(this->color_space().type());
97
+ }
98
+
99
+ static
100
+ VALUE at(VALUE self, VALUE x, VALUE y)
101
+ {
102
+ CHECK;
103
+
104
+ int xx = x.as_i(), yy = y.as_i();
105
+ void* pixel = this->at<void*>(xx, yy);
106
+ if (!pixel) error("Bitmap#at(%d, %d) failed.", xx, yy);
107
+
108
+ Value array(this->color_space().Bpp() / this->color_space().Bpc(), NULL);
109
+ if (this->color_space().is_float())
110
+ {
111
+ float* p = (float*) pixel;
112
+ switch (this->color_space().type())
113
+ {
114
+ case Rays::GRAY_float:
115
+ array.push(p[0]);
116
+ break;
117
+ case Rays::RGB_float:
118
+ array.push(p[0]).push(p[1]).push(p[2]);
119
+ break;
120
+ case Rays::RGBA_float:
121
+ array.push(p[0]).push(p[1]).push(p[2]).push(p[3]);
122
+ break;
123
+ case Rays::ARGB_float:
124
+ array.push(p[1]).push(p[2]).push(p[3]).push(p[0]);
125
+ break;
126
+ case Rays::BGR_float:
127
+ array.push(p[2]).push(p[1]).push(p[0]);
128
+ break;
129
+ case Rays::BGRA_float:
130
+ array.push(p[2]).push(p[1]).push(p[0]).push(p[3]);
131
+ break;
132
+ case Rays::ABGR_float:
133
+ array.push(p[3]).push(p[2]).push(p[1]).push(p[0]);
134
+ break;
135
+ default:
136
+ error("Bitmap#at: unknown color space");
137
+ }
138
+ }
139
+ else
140
+ {
141
+ uchar* p = (uchar*) pixel;
142
+ switch (this->color_space().type())
143
+ {
144
+ case Rays::GRAY_8:
145
+ array.push(*(uchar*) pixel);
146
+ break;
147
+ case Rays::GRAY_16:
148
+ array.push(*(ushort*) pixel);
149
+ break;
150
+ case Rays::GRAY_32:
151
+ array.push(*(ulong*) pixel);
152
+ break;
153
+ case Rays::RGB_888:
154
+ array.push(p[0]).push(p[1]).push(p[2]);
155
+ break;
156
+ case Rays::RGBA_8888:
157
+ array.push(p[0]).push(p[1]).push(p[2]).push(p[3]);
158
+ break;
159
+ case Rays::ARGB_8888:
160
+ array.push(p[1]).push(p[2]).push(p[3]).push(p[0]);
161
+ break;
162
+ case Rays::RGBX_8888:
163
+ array.push(p[0]).push(p[1]).push(p[2]);
164
+ break;
165
+ case Rays::XRGB_8888:
166
+ array.push(p[1]).push(p[2]).push(p[3]);
167
+ break;
168
+ case Rays::BGR_888:
169
+ array.push(p[2]).push(p[1]).push(p[0]);
170
+ break;
171
+ case Rays::BGRA_8888:
172
+ array.push(p[2]).push(p[1]).push(p[0]).push(p[3]);
173
+ break;
174
+ case Rays::ABGR_8888:
175
+ array.push(p[3]).push(p[2]).push(p[1]).push(p[0]);
176
+ break;
177
+ case Rays::BGRX_8888:
178
+ array.push(p[2]).push(p[1]).push(p[0]);
179
+ break;
180
+ case Rays::XBGR_8888:
181
+ array.push(p[3]).push(p[2]).push(p[1]);
182
+ break;
183
+ default:
184
+ error("Bitmap#at: unknown color space");
185
+ }
186
+ }
187
+
188
+ return array;
189
+ }
190
+
191
+ static
192
+ VALUE assign_at(VALUE self, VALUE x, VALUE y, VALUE color)
193
+ {
194
+ CHECK;
195
+
196
+ int xx = x.as_i(), yy = y.as_i();
197
+ void* pixel = this->at<void*>(xx, yy);
198
+ if (!pixel) error("Bitmap#assign_at(%d, %d) failed.", xx, yy);
199
+
200
+ Value array(this->color_space().Bpp() / this->color_space().Bpc(), NULL);
201
+ if (this->color_space().is_float())
202
+ {
203
+ float* p = (float*) pixel;
204
+ switch (this->color_space().type())
205
+ {
206
+ #define C(n) ((float) color[n].as_f())
207
+ case Rays::GRAY_float:
208
+ p[0] = C(0);
209
+ break;
210
+ case Rays::RGB_float:
211
+ p[0] = C(0); p[1] = C(1); p[2] = C(2);
212
+ break;
213
+ case Rays::RGBA_float:
214
+ p[0] = C(0); p[1] = C(1); p[2] = C(2); p[3] = C(3);
215
+ break;
216
+ case Rays::ARGB_float:
217
+ p[0] = C(1); p[1] = C(2); p[2] = C(3); p[3] = C(0);
218
+ break;
219
+ case Rays::BGR_float:
220
+ p[0] = C(2); p[1] = C(1); p[2] = C(0);
221
+ break;
222
+ case Rays::BGRA_float:
223
+ p[0] = C(2); p[1] = C(1); p[2] = C(0); p[3] = C(3);
224
+ break;
225
+ case Rays::ABGR_float:
226
+ p[0] = C(3); p[1] = C(2); p[2] = C(1); p[3] = C(0);
227
+ break;
228
+ #undef C
229
+ default:
230
+ error("Bitmap#at: unknown color space");
231
+ }
232
+ }
233
+ else
234
+ {
235
+ uchar* p = (uchar*) pixel;
236
+ switch (this->color_space().type())
237
+ {
238
+ #define C(n) ((uchar) color[n].as_i())
239
+ case Rays::GRAY_8:
240
+ *(uchar*) pixel = C(0);
241
+ break;
242
+ case Rays::GRAY_16:
243
+ *(ushort*) pixel = C(0);
244
+ break;
245
+ case Rays::GRAY_32:
246
+ *(ulong*) pixel = C(0);
247
+ break;
248
+ case Rays::RGB_888:
249
+ p[0] = C(0); p[1] = C(1); p[2] = C(2);
250
+ break;
251
+ case Rays::RGBA_8888:
252
+ p[0] = C(0); p[1] = C(1); p[2] = C(2); p[3] = C(3);
253
+ break;
254
+ case Rays::ARGB_8888:
255
+ p[0] = C(1); p[1] = C(2); p[2] = C(3); p[3] = C(0);
256
+ break;
257
+ case Rays::RGBX_8888:
258
+ p[0] = C(0); p[1] = C(1); p[2] = C(2);
259
+ break;
260
+ case Rays::XRGB_8888:
261
+ p[0] = C(1); p[1] = C(2); p[2] = C(3);
262
+ break;
263
+ case Rays::BGR_888:
264
+ p[0] = C(2); p[1] = C(1); p[2] = C(0);
265
+ break;
266
+ case Rays::BGRA_8888:
267
+ p[0] = C(2); p[1] = C(1); p[2] = C(0); p[3] = C(3);
268
+ break;
269
+ case Rays::ABGR_8888:
270
+ p[0] = C(3); p[1] = C(2); p[2] = C(1); p[3] = C(0);
271
+ break;
272
+ case Rays::BGRX_8888:
273
+ p[0] = C(2); p[1] = C(1); p[2] = C(0);
274
+ break;
275
+ case Rays::XBGR_8888:
276
+ p[0] = C(3); p[1] = C(2); p[2] = C(1);
277
+ break;
278
+ #undef C
279
+ default:
280
+ error("Bitmap#at: unknown color space");
281
+ }
282
+ }
283
+
284
+ return color;
285
+ }
286
+
287
+
288
+ static
289
+ VALUE load(VALUE self, VALUE path)
290
+ {
291
+ Rays::Bitmap bmp;
292
+ if (!Rays::load_bitmap(&bmp, path.c_str()))
293
+ error("Bitmap.load('%s') failed.", path.c_str());
294
+
295
+ return value(bmp);
296
+ }
297
+
298
+
299
+ void
300
+ Init_bitmap ()
301
+ {
302
+ Module m = rb_define_module("Rays");
303
+
304
+ m.define_const("RGB", Rays::RGB);
305
+ m.define_const("RGBA", Rays::RGBA);
306
+
307
+ Class c = rb_define_class_under(m, "Bitmap", rb_cObject);
308
+ Rays::cBitmap = c;
309
+
310
+ rb_define_alloc_func(c, alloc);
311
+ rb_define_method(c, "initialize", RUBY_METHOD_FUNC(initialize), -1);
312
+ rb_define_method(c, "width", RUBY_METHOD_FUNC(width), 0);
313
+ rb_define_method(c, "height", RUBY_METHOD_FUNC(height), 0);
314
+ rb_define_method(c, "color_space", RUBY_METHOD_FUNC(color_space), 0);
315
+ c.define_method("[]", at);
316
+ c.define_method("[]=", assign_at);
317
+ rb_define_function(c, "load", RUBY_METHOD_FUNC(load), 1);
318
+ }
@@ -0,0 +1,124 @@
1
+ #include "rays/ruby/font.h"
2
+
3
+
4
+ #include <rucy.h>
5
+ #include "defs.h"
6
+
7
+
8
+ using namespace Rucy;
9
+
10
+ using Rays::coord;
11
+
12
+
13
+ namespace Rays
14
+ {
15
+
16
+
17
+ static Class cFont;
18
+
19
+ Class
20
+ font_class ()
21
+ {
22
+ return cFont;
23
+ }
24
+
25
+
26
+ }// Rays
27
+
28
+
29
+ namespace Rucy
30
+ {
31
+
32
+
33
+ Value
34
+ value (const Rays::Font& font)
35
+ {
36
+ return new_type<Rays::Font>(
37
+ Rays::font_class(), new Rays::Font(font));
38
+ }
39
+
40
+
41
+ }// Rucy
42
+
43
+
44
+ #define this to<Rays::Font*>(self)
45
+
46
+ #define CHECK CHECK_OBJECT(self, Rays::Font, Rays::font_class())
47
+
48
+
49
+ static
50
+ VALUE alloc(VALUE klass)
51
+ {
52
+ return new_type<Rays::Font>(klass, new Rays::Font);
53
+ }
54
+
55
+ static
56
+ VALUE initialize(VALUE self)
57
+ {
58
+ CHECK_OBJ(self, Rays::Font, Rays::font_class());
59
+ if (argc < 0 || 2 < argc)
60
+ arg_count_error("Font#initialize", argc, 0, 1, 2);
61
+
62
+ const char* name = (argc >= 1) ? argv[0].c_str() : NULL;
63
+ float size = (argc >= 2) ? to<float>(argv[1]) : 0;
64
+ *this = Rays::Font(name, size);
65
+
66
+ return self;
67
+ }
68
+
69
+ static
70
+ VALUE name(VALUE self)
71
+ {
72
+ CHECK;
73
+
74
+ return value(this->name().c_str());
75
+ }
76
+
77
+ static
78
+ VALUE size(VALUE self)
79
+ {
80
+ CHECK;
81
+
82
+ return value(this->size());
83
+ }
84
+
85
+ static
86
+ VALUE width(VALUE self, VALUE str)
87
+ {
88
+ CHECK;
89
+
90
+ coord width = 0;
91
+ if (!this->get_extent(&width, NULL, str.c_str()))
92
+ error("Font#width(%s) failed.", str.inspect().c_str());
93
+
94
+ return value(width);
95
+ }
96
+
97
+ static
98
+ VALUE height(VALUE self)
99
+ {
100
+ CHECK;
101
+
102
+ coord height = 0;
103
+ if (!this->get_extent(NULL, &height, NULL))
104
+ error("Font#height() failed.");
105
+
106
+ return value(height);
107
+ }
108
+
109
+
110
+ void
111
+ Init_font ()
112
+ {
113
+ Module m = rb_define_module("Rays");
114
+
115
+ Class c = rb_define_class_under(m, "Font", rb_cObject);
116
+ Rays::cFont = c;
117
+
118
+ rb_define_alloc_func(c, alloc);
119
+ rb_define_method(c, "initialize", RUBY_METHOD_FUNC(initialize), -1);
120
+ rb_define_method(c, "name", RUBY_METHOD_FUNC(name), 0);
121
+ rb_define_method(c, "size", RUBY_METHOD_FUNC(size), 0);
122
+ rb_define_method(c, "width", RUBY_METHOD_FUNC(width), 1);
123
+ rb_define_method(c, "height", RUBY_METHOD_FUNC(height), 0);
124
+ }
@@ -0,0 +1,163 @@
1
+ #include "rays/ruby/image.h"
2
+
3
+
4
+ #include <rucy.h>
5
+ #include <rays/ruby/bitmap.h>
6
+ #include <rays/ruby/texture.h>
7
+ #include "defs.h"
8
+
9
+
10
+ using namespace Rucy;
11
+
12
+ using Rays::coord;
13
+
14
+
15
+ namespace Rays
16
+ {
17
+
18
+
19
+ static Class cImage;
20
+
21
+ Class
22
+ image_class ()
23
+ {
24
+ return cImage;
25
+ }
26
+
27
+
28
+ }// Rays
29
+
30
+
31
+ namespace Rucy
32
+ {
33
+
34
+
35
+ Value
36
+ value (const Rays::Image& image)
37
+ {
38
+ return new_type<Rays::Image>(
39
+ Rays::image_class(), new Rays::Image(image));
40
+ }
41
+
42
+
43
+ }// Rucy
44
+
45
+
46
+ #define this to<Rays::Image*>(self)
47
+
48
+ #define CHECK CHECK_OBJECT(self, Rays::Image, Rays::image_class())
49
+
50
+
51
+ static
52
+ VALUE alloc(VALUE klass)
53
+ {
54
+ return new_type<Rays::Image>(klass, new Rays::Image);
55
+ }
56
+
57
+ static
58
+ VALUE initialize(VALUE self)
59
+ {
60
+ CHECK_OBJ(self, Rays::Image, Rays::image_class());
61
+ if (argc != 0 && argc != 1 && argc != 2 && argc != 3)
62
+ arg_count_error("Image#initialize", argc, 0, 1, 2, 3);
63
+
64
+ if (argc == 0) return self;
65
+
66
+ if (argv[1].is_kind_of(Rays::bitmap_class()))
67
+ {
68
+ if (argc != 1 && argc != 2)
69
+ arg_count_error("Image#initialize", argc, 0, 1, 2, 3);
70
+
71
+ const Rays::Bitmap& bitmap = *to<Rays::Bitmap*>(argv[1]);
72
+ bool alphaonly = (argc == 2) ? to<bool>(argv[2]) : false;
73
+ *this = Rays::Image(bitmap, alphaonly);
74
+ }
75
+ else
76
+ {
77
+ int width = to<int>(argv[0]);
78
+ int height = to<int>(argv[1]);
79
+ uint colorspace = (argc == 3) ? to<uint>(argv[2]) : (uint) Rays::RGBA;
80
+ *this = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace);
81
+ }
82
+
83
+ return self;
84
+ }
85
+
86
+ static
87
+ VALUE width(VALUE self)
88
+ {
89
+ CHECK;
90
+
91
+ return value(this->width());
92
+ }
93
+
94
+ static
95
+ VALUE height(VALUE self)
96
+ {
97
+ CHECK;
98
+
99
+ return value(this->height());
100
+ }
101
+
102
+ static
103
+ VALUE color_space(VALUE self)
104
+ {
105
+ CHECK;
106
+
107
+ return value(this->color_space().type());
108
+ }
109
+
110
+ static
111
+ VALUE bitmap(VALUE self)
112
+ {
113
+ CHECK;
114
+
115
+ return value(this->bitmap());
116
+ }
117
+
118
+ static
119
+ VALUE texture(VALUE self)
120
+ {
121
+ CHECK;
122
+
123
+ return value(this->texture());
124
+ }
125
+
126
+
127
+ static
128
+ VALUE load(VALUE self)
129
+ {
130
+ if (argc != 1 && argc != 2) arg_count_error("Image.load", argc, 1, 2);
131
+
132
+ Rays::String path = argv[0].c_str();
133
+ bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false;
134
+
135
+ Rays::Image img;
136
+ if (!Rays::load_image(&img, path.c_str(), alphaonly))
137
+ {
138
+ error(
139
+ "Image.load('%s', %s) failed.",
140
+ path.c_str(), alphaonly ? "true" : "false");
141
+ }
142
+
143
+ return value(img);
144
+ }
145
+
146
+
147
+ void
148
+ Init_image ()
149
+ {
150
+ Module m = rb_define_module("Rays");
151
+
152
+ Class c = rb_define_class_under(m, "Image", rb_cObject);
153
+ Rays::cImage = c;
154
+
155
+ rb_define_alloc_func(c, alloc);
156
+ rb_define_method(c, "initialize", RUBY_METHOD_FUNC(initialize), -1);
157
+ rb_define_method(c, "width", RUBY_METHOD_FUNC(width), 0);
158
+ rb_define_method(c, "height", RUBY_METHOD_FUNC(height), 0);
159
+ rb_define_method(c, "color_space", RUBY_METHOD_FUNC(color_space), 0);
160
+ rb_define_method(c, "bitmap", RUBY_METHOD_FUNC(bitmap), 0);
161
+ rb_define_method(c, "texture", RUBY_METHOD_FUNC(texture), 0);
162
+ rb_define_function(c, "load", RUBY_METHOD_FUNC(load), -1);
163
+ }