#include "rays/ruby/image.h" #include #include #include #include "defs.h" using namespace Rucy; using Rays::coord; static Class cImage; namespace Rays { 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(self) #define CHECK RUCY_CHECK_OBJECT(self, Rays::Image, cImage) static RUBY_DEF_ALLOC(alloc, klass) { return new_type(klass); } RUBY_END static RUBY_DEFN(initialize) { 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[0].is_kind_of(Rays::bitmap_class())) { if (argc < 1 || 2 < argc) arg_count_error("Image#initialize", argc, 1, 2); const Rays::Bitmap* bitmap = to(argv[0]); if (!bitmap) argument_error(); bool alphaonly = (argc == 2) ? to(argv[1]) : 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)) { rays_error( "Image.load('%s', %s) failed.", path.c_str(), alphaonly ? "true" : "false"); } return value(img); } RUBY_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_method("width", width); cImage.define_method("height", height); cImage.define_method("color_space", color_space); cImage.define_method("bitmap", bitmap); cImage.define_method("texture", texture); cImage.define_function("load", load); }