.doc/ext/rays/image.cpp in rays-0.1.3 vs .doc/ext/rays/image.cpp in rays-0.1.4
- old
+ new
@@ -10,16 +10,17 @@
using namespace Rucy;
using Rays::coord;
+static Class cImage;
+
+
namespace Rays
{
- static Class cImage;
-
Class
image_class ()
{
return cImage;
}
@@ -31,98 +32,106 @@
namespace Rucy
{
Value
- value (const Rays::Image& image)
+ value (const Rays::Image& obj)
{
- return new_type<Rays::Image>(
- Rays::image_class(), new Rays::Image(image));
+ return new_type(cImage, new Rays::Image(obj));
}
+ Value
+ value (const Rays::Image* obj)
+ {
+ return obj ? value(*obj) : nil();
+ }
+
}// Rucy
-#define this to<Rays::Image*>(self)
+#define THIS to<Rays::Image*>(self)
-#define CHECK CHECK_OBJECT(self, Rays::Image, Rays::image_class())
+#define CHECK RUCY_CHECK_OBJECT(self, Rays::Image, cImage)
static
VALUE alloc(VALUE klass)
{
- return new_type<Rays::Image>(klass, new Rays::Image);
+ return new_type<Rays::Image>(klass);
}
static
VALUE initialize(VALUE self)
{
- CHECK_OBJ(self, Rays::Image, Rays::image_class());
- if (argc != 0 && argc != 1 && argc != 2 && argc != 3)
- arg_count_error("Image#initialize", argc, 0, 1, 2, 3);
+ RUCY_CHECK_OBJ(self, Rays::Image, cImage);
+ if (argc < 1 || 3 < argc)
+ arg_count_error("Image#initialize", argc, 1, 2, 3);
+
if (argc == 0) return self;
- if (argv[1].is_kind_of(Rays::bitmap_class()))
+ if (argv[0].is_kind_of(Rays::bitmap_class()))
{
- if (argc != 1 && argc != 2)
- arg_count_error("Image#initialize", argc, 0, 1, 2, 3);
+ if (argc < 1 || 2 < argc)
+ arg_count_error("Image#initialize", argc, 1, 2);
- const Rays::Bitmap& bitmap = *to<Rays::Bitmap*>(argv[1]);
- bool alphaonly = (argc == 2) ? to<bool>(argv[2]) : false;
- *this = Rays::Image(bitmap, alphaonly);
+ const Rays::Bitmap* bitmap = to<Rays::Bitmap*>(argv[0]);
+ if (!bitmap) argument_error();
+
+ bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false;
+ *THIS = Rays::Image(*bitmap, alphaonly);
}
else
{
int width = to<int>(argv[0]);
int height = to<int>(argv[1]);
uint colorspace = (argc == 3) ? to<uint>(argv[2]) : (uint) Rays::RGBA;
- *this = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace);
+ *THIS = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace);
}
return self;
}
static
VALUE width(VALUE self)
{
CHECK;
- return value(this->width());
+ return value(THIS->width());
}
static
VALUE height(VALUE self)
{
CHECK;
- return value(this->height());
+ return value(THIS->height());
}
static
VALUE color_space(VALUE self)
{
CHECK;
- return value(this->color_space().type());
+ return value(THIS->color_space().type());
}
static
VALUE bitmap(VALUE self)
{
CHECK;
- return value(this->bitmap());
+ return value(THIS->bitmap());
}
static
VALUE texture(VALUE self)
{
CHECK;
- return value(this->texture());
+ return value(THIS->texture());
}
static
VALUE load(VALUE self)
@@ -133,11 +142,11 @@
bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false;
Rays::Image img;
if (!Rays::load_image(&img, path.c_str(), alphaonly))
{
- error(
+ rays_error(
"Image.load('%s', %s) failed.",
path.c_str(), alphaonly ? "true" : "false");
}
return value(img);
@@ -145,19 +154,17 @@
void
Init_image ()
{
- Module m = rb_define_module("Rays");
+ Module mRays = rb_define_module("Rays");
- Class c = rb_define_class_under(m, "Image", rb_cObject);
- Rays::cImage = c;
-
- rb_define_alloc_func(c, alloc);
- rb_define_method(c, "initialize", RUBY_METHOD_FUNC(initialize), -1);
- rb_define_method(c, "width", RUBY_METHOD_FUNC(width), 0);
- rb_define_method(c, "height", RUBY_METHOD_FUNC(height), 0);
- rb_define_method(c, "color_space", RUBY_METHOD_FUNC(color_space), 0);
- rb_define_method(c, "bitmap", RUBY_METHOD_FUNC(bitmap), 0);
- rb_define_method(c, "texture", RUBY_METHOD_FUNC(texture), 0);
- rb_define_function(c, "load", RUBY_METHOD_FUNC(load), -1);
+ cImage = rb_define_class_under(mRays, "Image", rb_cObject);
+ rb_define_alloc_func(cImage, alloc);
+ rb_define_private_method(cImage, "initialize", RUBY_METHOD_FUNC(initialize), -1);
+ rb_define_method(cImage, "width", RUBY_METHOD_FUNC(width), 0);
+ rb_define_method(cImage, "height", RUBY_METHOD_FUNC(height), 0);
+ rb_define_method(cImage, "color_space", RUBY_METHOD_FUNC(color_space), 0);
+ rb_define_method(cImage, "bitmap", RUBY_METHOD_FUNC(bitmap), 0);
+ rb_define_method(cImage, "texture", RUBY_METHOD_FUNC(texture), 0);
+ rb_define_function(cImage, "load", RUBY_METHOD_FUNC(load), -1);
}