|
1 | 1 | #include <stdbool.h> |
2 | 2 | #include <texture.h> |
3 | 3 | #include <SketchUpAPI/sketchup.h> |
| 4 | +#include <not_implemented.h> |
4 | 5 | #include <utils.h> |
5 | 6 |
|
| 7 | +static VALUE Sketchup_Texture_average_color(VALUE self) |
| 8 | +{ |
| 9 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 10 | + VALUE c = rb_obj_alloc(rb_path2class(SKETCHUP_COLOR)); |
| 11 | + SUColor* color = DATA_PTR(c); |
| 12 | + enum SUResult result = SUTextureGetAverageColor(texture, color); |
| 13 | + if (result != SU_ERROR_NONE) |
| 14 | + return Qnil; |
| 15 | + return c; |
| 16 | +} |
| 17 | + |
| 18 | +static VALUE Sketchup_Texture_filename(VALUE self) |
| 19 | +{ |
| 20 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 21 | + VALUE output; |
| 22 | + GETSTRING(SUTextureGetFileName, texture, output); |
| 23 | + return output; |
| 24 | +} |
| 25 | + |
| 26 | +static VALUE Sketchup_Texture_image_height(VALUE self) |
| 27 | +{ |
| 28 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 29 | + size_t width = 0; |
| 30 | + size_t height = 0; |
| 31 | + double s_scale = 0.0; |
| 32 | + double t_scale = 0.0; |
| 33 | + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); |
| 34 | + return ULL2NUM(height); |
| 35 | +} |
| 36 | + |
| 37 | +static VALUE Sketchup_Texture_height(VALUE self) |
| 38 | +{ |
| 39 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 40 | + size_t width = 0; |
| 41 | + size_t height = 0; |
| 42 | + double s_scale = 0.0; |
| 43 | + double t_scale = 0.0; |
| 44 | + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); |
| 45 | + return DBL2NUM(1.0 / t_scale); |
| 46 | +} |
| 47 | + |
| 48 | +static VALUE Sketchup_Texture_width(VALUE self) |
| 49 | +{ |
| 50 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 51 | + size_t width = 0; |
| 52 | + size_t height = 0; |
| 53 | + double s_scale = 0.0; |
| 54 | + double t_scale = 0.0; |
| 55 | + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); |
| 56 | + return DBL2NUM(1.0 / s_scale); |
| 57 | +} |
| 58 | + |
| 59 | +static VALUE Sketchup_Texture_image_width(VALUE self) |
| 60 | +{ |
| 61 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 62 | + size_t width = 0; |
| 63 | + size_t height = 0; |
| 64 | + double s_scale = 0.0; |
| 65 | + double t_scale = 0.0; |
| 66 | + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); |
| 67 | + return ULL2NUM(width); |
| 68 | +} |
| 69 | + |
| 70 | +static VALUE Sketchup_Texture_image_rep(VALUE self) |
| 71 | +{ |
| 72 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 73 | + SUImageRepRef image = SU_INVALID; |
| 74 | + SUTextureGetImageRep(texture, &image); |
| 75 | + return Data_Wrap_Struct(rb_path2class(SKETCHUP_IMAGEREP), 0, 0, image.ptr); |
| 76 | +} |
| 77 | + |
| 78 | +static VALUE Sketchup_Texture_Get_valid(VALUE self) |
| 79 | +{ |
| 80 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 81 | + return SUIsValid(texture) ? Qtrue : Qfalse; |
| 82 | +} |
| 83 | + |
| 84 | +static VALUE Sketchup_Texture_write(VALUE self, VALUE path, VALUE colorize) |
| 85 | +{ |
| 86 | + SUTextureRef texture = {DATA_PTR(self)}; |
| 87 | + const char* path_ptr = StringValuePtr(path); |
| 88 | + enum SUResult result = SU_ERROR_UNSUPPORTED; |
| 89 | + if (RTEST(colorize)) |
| 90 | + result = SUTextureWriteToFile(texture, path_ptr); |
| 91 | + else |
| 92 | + result = SUTextureWriteOriginalToFile(texture, path_ptr); |
| 93 | + return result != SU_ERROR_NONE ? Qfalse : Qtrue; |
| 94 | +} |
| 95 | + |
6 | 96 | void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) |
7 | 97 | { |
8 | 98 | VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity); |
9 | 99 | rb_undef_alloc_func(Sketchup_Texture); |
| 100 | + rb_define_method(Sketchup_Texture, "average_color", Sketchup_Texture_average_color, 0); |
| 101 | + rb_define_method(Sketchup_Texture, "filename", Sketchup_Texture_filename, 0); |
| 102 | + rb_define_method(Sketchup_Texture, "image_height", Sketchup_Texture_image_height, 0); |
| 103 | + rb_define_method(Sketchup_Texture, "height", Sketchup_Texture_height, 0); |
| 104 | + rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0); |
| 105 | + rb_define_method(Sketchup_Texture, "width", Sketchup_Texture_width, 0); |
| 106 | + rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0); |
| 107 | + rb_define_method(Sketchup_Texture, "valid?", Sketchup_Texture_Get_valid, 0); |
| 108 | + rb_define_method(Sketchup_Texture, "write", Sketchup_Texture_write, 2); |
10 | 109 | } |
0 commit comments