#include "rays/ruby/image.h" #include #include #include #include "defs.h" using namespace Rucy; using Rays::coord; namespace Rays { static Class cImage; Class image_class () { return cImage; } }// Rays namespace Rucy { Value value (const Rays::Image& image) { return new_type( Rays::image_class(), new Rays::Image(image)); } }// Rucy #define this to(self) #define CHECK CHECK_OBJECT(self, Rays::Image, Rays::image_class()) static RUBY_DEF_ALLOC(alloc, klass) { return new_type(klass, new Rays::Image); } RUBY_END static RUBY_DEFN(initialize) { 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); if (argc == 0) return self; if (argv[1].is_kind_of(Rays::bitmap_class())) { if (argc != 1 && argc != 2) arg_count_error("Image#initialize", argc, 0, 1, 2, 3); const Rays::Bitmap& bitmap = *to(argv[1]); bool alphaonly = (argc == 2) ? to(argv[2]) : false; *this = Rays::Image(bitmap, alphaonly); } else { int width = to(argv[0]); int height = to(argv[1]); uint colorspace = (argc == 3) ? to(argv[2]) : (uint) Rays::RGBA; *this = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace); } return self; } RUBY_END static RUBY_DEF0(width) { CHECK; return value(this->width()); } RUBY_END static RUBY_DEF0(height) { CHECK; return value(this->height()); } RUBY_END static RUBY_DEF0(color_space) { CHECK; return value(this->color_space().type()); } RUBY_END static RUBY_DEF0(bitmap) { CHECK; return value(this->bitmap()); } RUBY_END static RUBY_DEF0(texture) { CHECK; return value(this->texture()); } RUBY_END static RUBY_DEFN(load) { if (argc != 1 && argc != 2) arg_count_error("Image.load", argc, 1, 2); Rays::String path = argv[0].c_str(); bool alphaonly = (argc == 2) ? to(argv[1]) : false; Rays::Image img; if (!Rays::load_image(&img, path.c_str(), alphaonly)) { error( "Image.load('%s', %s) failed.", path.c_str(), alphaonly ? "true" : "false"); } return value(img); } RUBY_END void Init_image () { Module m = define_module("Rays"); Class c = m.define_class("Image"); Rays::cImage = c; c.define_alloc_func(alloc); c.define_method("initialize", initialize); c.define_method("width", width); c.define_method("height", height); c.define_method("color_space", color_space); c.define_method("bitmap", bitmap); c.define_method("texture", texture); c.define_function("load", load); }