ext/rays/image.cpp in rays-0.1.6 vs ext/rays/image.cpp in rays-0.1.7

- old
+ new

@@ -1,88 +1,54 @@ #include "rays/ruby/image.h" #include <rucy.h> -#include <rays/ruby/bitmap.h> -#include <rays/ruby/texture.h> +#include "rays/ruby/color_space.h" +#include "rays/ruby/bitmap.h" +#include "rays/ruby/texture.h" +#include "rays/ruby/painter.h" #include "defs.h" using namespace Rucy; using Rays::coord; static Class cImage; +RUCY_DEFINE_VALUE_FROM_TO(Rays::Image, cImage) -namespace Rays -{ +#define THIS to<Rays::Image*>(self) +#define CHECK RUCY_CHECK_OBJECT(Rays::Image, cImage, self) - Class - image_class () - { - return cImage; - } - -}// Rays - - -namespace Rucy -{ - - - Value - value (const Rays::Image& obj) - { - 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 CHECK RUCY_CHECK_OBJECT(self, Rays::Image, cImage) - - static -RUBY_DEF_ALLOC(alloc, klass) +RUCY_DEF_ALLOC(alloc, klass) { return new_type<Rays::Image>(klass); } -RUBY_END +RUCY_END static -RUBY_DEFN(initialize) +RUCY_DEFN(initialize) { - RUCY_CHECK_OBJ(self, Rays::Image, cImage); + RUCY_CHECK_OBJ(Rays::Image, cImage, self); + check_arg_count(__FILE__, __LINE__, "Image#initialize", argc, 1, 2, 3); - if (argc < 1 || 3 < argc) - arg_count_error("Image#initialize", argc, 1, 2, 3); - if (argc == 0) return self; if (argv[0].is_kind_of(Rays::bitmap_class())) { - if (argc < 1 || 2 < argc) - arg_count_error("Image#initialize", argc, 1, 2); + check_arg_count(__FILE__, __LINE__, "Image#initialize", argc, 1, 2); const Rays::Bitmap* bitmap = to<Rays::Bitmap*>(argv[0]); - if (!bitmap) argument_error(); + if (!bitmap) argument_error(__FILE__, __LINE__); - bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false; - *THIS = Rays::Image(*bitmap, alphaonly); + bool alpha_only = (argc == 2) ? to<bool>(argv[1]) : false; + *THIS = Rays::Image(*bitmap, alpha_only); } else { int width = to<int>(argv[0]); int height = to<int>(argv[1]); @@ -90,89 +56,124 @@ *THIS = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace); } return self; } -RUBY_END +RUCY_END static -RUBY_DEF0(width) +RUCY_DEF1(initialize_copy, obj) { + RUCY_CHECK_OBJ(Rays::Image, cImage, self); + + *THIS = to<Rays::Image&>(obj).copy(); + return self; +} +RUCY_END + +static +RUCY_DEF0(painter) +{ CHECK; + return value(THIS->painter()); +} +RUCY_END +static +RUCY_DEF0(width) +{ + CHECK; return value(THIS->width()); } -RUBY_END +RUCY_END static -RUBY_DEF0(height) +RUCY_DEF0(height) { CHECK; - return value(THIS->height()); } -RUBY_END +RUCY_END static -RUBY_DEF0(color_space) +RUCY_DEF0(color_space) { CHECK; - - return value(THIS->color_space().type()); + return value(THIS->color_space()); } -RUBY_END +RUCY_END static -RUBY_DEF0(bitmap) +RUCY_DEF0(alpha_only) { CHECK; + return value(THIS->alpha_only()); +} +RUCY_END +static +RUCY_DEF0(bitmap) +{ + CHECK; return value(THIS->bitmap()); } -RUBY_END +RUCY_END static -RUBY_DEF0(texture) +RUCY_DEF0(texture) { CHECK; - return value(THIS->texture()); } -RUBY_END +RUCY_END - static -RUBY_DEFN(load) +RUCY_DEF1(save, path) { - if (argc != 1 && argc != 2) arg_count_error("Image.load", argc, 1, 2); + CHECK; + Rays::save_image(*THIS, path.c_str()); + return self; +} +RUCY_END - Rays::String path = argv[0].c_str(); - bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false; - Rays::Image img; - if (!Rays::load_image(&img, path.c_str(), alphaonly)) - { - rays_error( - "Image.load('%s', %s) failed.", - path.c_str(), alphaonly ? "true" : "false"); - } - - return value(img); +static +RUCY_DEF2(load, path, alpha_only) +{ + return value(Rays::load_image(path.c_str(), to<bool>(alpha_only))); } -RUBY_END +RUCY_END void Init_image () { Module mRays = define_module("Rays"); cImage = mRays.define_class("Image"); cImage.define_alloc_func(alloc); cImage.define_private_method("initialize", initialize); + cImage.define_private_method("initialize_copy", initialize_copy); + cImage.define_method("painter", painter); cImage.define_method("width", width); cImage.define_method("height", height); cImage.define_method("color_space", color_space); + cImage.define_method("alpha_only", alpha_only); cImage.define_method("bitmap", bitmap); cImage.define_method("texture", texture); - cImage.define_function("load", load); + cImage.define_method("save", save); + cImage.define_function("load_image", load); } + + +namespace Rays +{ + + + Class + image_class () + { + return cImage; + } + + +}// Rays